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
  • In C# Create an “EmployeeDemo” class. It includes three functions: 1. In the main function, the...

    In C# Create an “EmployeeDemo” class. It includes three functions: 1. In the main function, the program calls the readData() function to get the data from file and calls the objSort() function to sort the array of objects. Then, the program prompts user to enter the range of current salary that user want to see and display the employee’s information whose current salary is in the range in a descending order. 2. readData(): it reads the workers and managers information...

  • 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 class calc with two data members (num1 & num2). The class should have a...

    Create a class calc with two data members (num1 & num2). The class should have a constructor that receives arguments during object creation and sets the values of the members. The class should also have a function called divide() that divides the two numbers and returns the result. The function should throw an exception if the denominator is 0. Create two objects of the class in the main() function. Use class templates making the first object an int and the...

  • 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...

  • Write a program that uses a class or structure to store the following data about a...

    Write a program that uses a class or structure to store the following data about a customer account. Name Address City, State and Zip Telephone Number Account Balance Date of Last Payment The program should use an array or vector of 10 objects. It should let the users enter data, change the contents of any element and display all the data stored in the array or vector. The program should be modular and menu driven. Validation: When the data for...

  • 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...

  • PYTHON CODING HELP: BELOW ARE THE STEPS I NEED HELP WITH PLEASE. :) First create a...

    PYTHON CODING HELP: BELOW ARE THE STEPS I NEED HELP WITH PLEASE. :) First create a text file named "items.txt" that has the following data in this order: Potatoes Tomatoes Carrots ... and have it be in the same directory as the Python program you are coding for this assignment. IMPORTANT: Your program should work with any size file. It should work with 100 items listed or with no items in the file! Write a Python program that ... 1....

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