Question

Create an Inventory class with data members for stock number, quantity, and price, and overloaded data...

Create an Inventory class with data members for stock number, quantity, and price, and overloaded data entry and output operators. The data entry operator function should throw:

- An error message, if the stock number is negative or higher than 999

- The quantity, if it is less than 0

- The price, if it is over $100.00

Then perform the following tasks:

a. Write a main() function that instantiates an array of five Inventory objects, and accepts data for each. Display an appropriate error message for each exception situation. When an exception is detected, replace all data fields with zeroes. At the end of the program, display data fields for all five objects. Save the file as Inventory.cpp.

b. Write a main() function that instantiates an array of five Inventory objects and accepts data for each. If an exception is thrown because of an invalid stock number, force the user to reenter the stock number for the Inventoryobject. If the quantity is invalid, do nothing. If the price is in error, then set the price to 99.99. (Add a setPrice() function to the Inventory class to accomplish this.) At the end of the program, display data fields for all five objects. Save the file as Inventory2.cpp.

c. Write a main()function that instantiates an array of five Inventory objects and accepts data for each. If an exception is thrown because of an invalid stock number, force the stock number to 999; otherwise, do nothing. (Add any needed functions to the Inventory class.) At the end of the program, display data fields for all five objects. Save the file as Inventory3.cpp.

d. Write a main()function that instantiates an array of five Inventory objects and accepts data for each. If any exception is thrown, stop accepting data. Display as many objects as were entered correctly. Save the file as Inventory4.cpp.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ineventory.cpp

#include<bits/stdc++.h>
using namespace std;

class Inventory{
public:
int stockNumber;
int quantity;
float price;
};

int main()
{
Inventory arr[5] = {0};
bool flag = false;
for(int i = 0;i < 5;i++){
cin >> arr[i].stockNumber;
if (arr[i].stockNumber < 0 || arr[i].stockNumber > 999){
cout<< "Error: Stock Number must be in range (0,999)"<<endl;
arr[i].stockNumber = 0;
flag = true;
break;
}
cin >> arr[i].quantity;
if (arr[i].quantity < 0){
cout<< "Error: Quantity must be positive"<<endl;
arr[i].quantity = 0;
flag = true;
break;
}
cin >> arr[i].price;
if (arr[i].price > 100 || arr[i].price < 0){
cout<<"Error: Price must be in range (0,100)"<<endl;
arr[i].price = 0;
flag = true;
break;
}
}
for(int i = 0;i < 5;i++){
if (flag){
arr[i].stockNumber = arr[i].quantity = arr[i].price = 0;
}
}
cout<<"All inventory data:\n";
for(int i = 0;i < 5;i++){
cout << arr[i].stockNumber<<" "<<arr[i].quantity<<" "<<arr[i].price<<endl;
}
return 0;
}

//Inventory2.cpp

#include<bits/stdc++.h>
using namespace std;

class Inventory{
public:
int stockNumber;
int quantity;
float price;
void setPrice(float val){
price = val;
}

};

int main()
{
Inventory arr[5] = {0};

for(int i = 0;i < 5;i++){
here:
cin >> arr[i].stockNumber;
if (arr[i].stockNumber < 0 || arr[i].stockNumber > 999){
cout<< "Info:Please re-enter stock number in range (0,999)"<<endl;
arr[i].stockNumber = 0;
goto here;
}
cin >> arr[i].quantity;
if (arr[i].quantity < 0){
arr[i].quantity = 0;
}
cin >> arr[i].price;
if (arr[i].price > 100 || arr[i].price < 0){
arr[i].setPrice(99.9);
}
}
cout<<"All inventory data:\n";
for(int i = 0;i < 5;i++){
cout << arr[i].stockNumber<<" "<<arr[i].quantity<<" "<<arr[i].price<<endl;
}
return 0;
}

//Inventory3.cpp

#include<bits/stdc++.h>
using namespace std;

class Inventory{
public:
int stockNumber;
int quantity;
float price;
void setPrice(float val){
price = val;
}
void setStocknumber(int num){
stockNumber = num;
}

};

int main()
{
Inventory arr[5] = {0};

for(int i = 0;i < 5;i++){
cin >> arr[i].stockNumber;
if (arr[i].stockNumber < 0 || arr[i].stockNumber > 999){
arr[i].setStocknumber(999);
}
cin >> arr[i].quantity;
if (arr[i].quantity < 0){
arr[i].quantity = 0;
}
cin >> arr[i].price;
if (arr[i].price > 100 || arr[i].price < 0){
arr[i].setPrice(99.9);
}
}
cout<<"All inventory data:\n";
for(int i = 0;i < 5;i++){
cout << arr[i].stockNumber<<" "<<arr[i].quantity<<" "<<arr[i].price<<endl;
}
return 0;
}

// Inventory4.cpp

#include<bits/stdc++.h>
using namespace std;

class Inventory{
public:
int stockNumber;
int quantity;
float price;
};

int main()
{
Inventory arr[5] = {0};
int corrected = 0;
for(int i = 0;i < 5;i++){
cin >> arr[i].stockNumber;
if (arr[i].stockNumber < 0 || arr[i].stockNumber > 999){
cout<< "Error: Stock Number must be in range (0,999)"<<endl;
arr[i].stockNumber = 0;
break;
}
cin >> arr[i].quantity;
if (arr[i].quantity < 0){
cout<< "Error: Quantity must be positive"<<endl;
arr[i].quantity = 0;
break;
}
cin >> arr[i].price;
if (arr[i].price > 100 || arr[i].price < 0){
cout<<"Error: Price must be in range (0,100)"<<endl;
arr[i].price = 0;
break;
}
corrected = i;
}
cout<<"All inventory data:\n";
for(int i = 0;i < corrected;i++){
cout << arr[i].stockNumber<<" "<<arr[i].quantity<<" "<<arr[i].price<<endl;
}
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Create an Inventory class with data members for stock number, quantity, and price, and overloaded data...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Define the class HotelRoom. The class has the following private data members: the room number...

    C++ Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [3pts]. The constructors and the set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception...

  • Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program...

    Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program named GetIDAndAge that continually prompts the user for an ID number and an age until a terminal 0 is entered for both. If the ID and age are both valid, display the message ID and Age OK. Throw a DataEntryException if the ID is not in the range of valid ID numbers (0 through 999), or if the age is not in the range...

  • Purpose: Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment...

    Purpose: Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment also aims at creating a C++ project to handle multiple files (one header file and two .cpp files) at the same time. Remember to follow documentation and variable name guidelines. Create a C++ project to implement a simplified banking system. Your bank is small, so it can have a maximum of 100 accounts. Use an array of pointers to objects for this. However, your...

  • Using: C# Inventory Management System Objective: Work with multiple objects and reading data files. Description: A...

    Using: C# Inventory Management System Objective: Work with multiple objects and reading data files. Description: A wholesale distributor has six warehouses (Atlanta, Baltimore, Chicago, Denver, Ely and Fargo) and sells five different items (identified by part number: 102, 215, 410, 525 and 711). Each warehouse may stock any or all of the five items. The company buys and sells these items constantly. Company transaction records contain a transaction code (‘P’ for a purchase or ‘S’ for a sale) followed by...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

  • Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read...

    Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read and process a file containing customer purchase data for books. The books available for purchase will be read from a separate data file. Process the customer sales and produce a report of the sales and the remaining book inventory. You are to read a data file (customerList.txt, provided) containing customer book purchasing data. Create a structure to contain the information. The structure will contain...

  • 1. Create a class named Pizza with data fields dor desceiption ( such as sasuage and...

    1. Create a class named Pizza with data fields dor desceiption ( such as sasuage and onion) and price. include a constructor fhay requires arguments for borh fields ans a method to diplay the data. Create a subclass names DelicedPizza that inherits from Pizza bit adds a delivery fee and a delicery address. The description, price, and delicery address are required as arguments to the constructor. The delivery fee is $3 if the pizza ordered cost more that $15; otherwise...

  • Part I: Problem: Code the following in Java: Candy Class -price -number of pounds sold Create...

    Part I: Problem: Code the following in Java: Candy Class -price -number of pounds sold Create the following methods: -Two constructors – default and second - accessor and mutator methods for each of the fields. - check for invalid price. Negative price is invalid (use if then else and display error message) - a method named userInput to get user input for all the fields. - check for invalid price. Negative price is invalid (use a loop to prompt the...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Create a c++ code and answer number 1 Homework Ch. 13 - Employee Class 30 points...

    Create a c++ code and answer number 1 Homework Ch. 13 - Employee Class 30 points Design a class named Employee that has the following attributes: * Name ID Number- (Employee's] Identification Number " Department-the name of the department where the employee works " Position-the employee's job title The class should have the following constructors: A constructor that accepts values for all the member data as arguments A constructor that accepts the following values as arguments and assigns them to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT