Modify the supplied program (classLab.cpp) to make it work properly.
/*
classLab.cpp
Name:
CIS 150
Date:
Demonstrates simple class/object programming principles.
*/
#include <cctype>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
/*
** TODO **
Create a class named Product
It should have three private instance variables
- a string description
- an integer for quantity
- a double for price
It must have two constructors
- both constructors must use the mutator methods to set any values
- a default constructor that sets description to "", quantity and price to 0
- a constructor that accepts a string, int, and double and uses them to set the instance variables
Three mutator functions
- setDescription, with error checking so it uses "Undefined" if attempt to set to zero-length string
- setQuantity, with error checking so attempt to set negative sets it to 0
- setPrice, with error checking so attempt to set negative sets it to 0
Three accessor functions
- getDescription
- getQuantity
- getPrice
** END OF TODO **
*/
int getInt(string prompt);
int getInt(string prompt, int min, int max);
double getDouble(string prompt);
string getString(string prompt);
char getChar(string prompt);
char getChar(string prompt, string allowed);
void displayItem(const Product& prod);
int getItemToChange(int length);
int main(int argc, char* argv[]) {
const int SIZE = 5;
char choice = '?';
string menu = "\nd) Description change\nq) Quantity change\np) Price change\nv) View all items\nx) exit\nChoice: ";
Product items[SIZE];
int itemNumber = 0;
Product product1("Test item", 42, 52.4);
cout << "Test product object #1: ";
displayItem(product1);
Product product2("", -42, -52.4);
cout << "Test product object #2: ";
displayItem(product2);
do {
choice = toupper(getChar(menu, "dDqQpPvVxX"));
switch (choice) {
case 'D':
itemNumber = getItemToChange(SIZE) - 1;
cout << "Currently: ";
displayItem(items[itemNumber]);
items[itemNumber].setDescription(getString("Enter new description: "));
cout << "Now: ";
displayItem(items[itemNumber]);
break;
case 'Q':
itemNumber = getItemToChange(SIZE) - 1;
cout << "Currently: ";
displayItem(items[itemNumber]);
items[itemNumber].setQuantity(getInt("Enter new quantity: "));
cout << "Now: ";
displayItem(items[itemNumber]);
break;
case 'P':
itemNumber = getItemToChange(SIZE) - 1;
cout << "Currently: ";
displayItem(items[itemNumber]);
items[itemNumber].setPrice(getDouble("Enter new price: "));
cout << "Now: ";
displayItem(items[itemNumber]);
break;
case 'V':
cout << "List of items in inventory:\n";
for (itemNumber=0; itemNumber<SIZE; itemNumber++) displayItem(items[itemNumber]);
break;
case 'X':
break;
default:
cout << "Invalid choice\n";
}
} while (choice != 'X');
return 0;
}
int getInt(string prompt) {
int val;
cout << prompt;
while (! (cin >> val)) {
cin.clear();
cin.ignore(100, '\n');
cout << prompt;
}
cin.ignore();
return val;
}
int getInt(string prompt, int min, int max) {
int val = getInt(prompt);
while (val < min || val > max) {
if (val < min) cout << "Error: Value below minimum of " << min << '\n';
else cout << "Error: Value above maximum of " << max << '\n';
val = getInt(prompt);
}
return val;
}
double getDouble(string prompt) {
double val;
cout << prompt;
while (! (cin >> val)) {
cin.clear();
cin.ignore(100, '\n');
cout << prompt;
}
cin.ignore();
return val;
}
string getString(string prompt) {
string val;
cout << prompt;
getline(cin, val);
return val;
}
char getChar(string prompt) {
char val;
cout << prompt;
while (! (cin >> val)) {
cin.clear();
cin.ignore(100, '\n');
cout << prompt;
}
cin.ignore();
return val;
}
char getChar(string prompt, string allowed) {
char ch = getChar(prompt);
while (allowed.find(ch) == string::npos) {
cout << "Error: Invalid choice\n";
ch = getChar(prompt);
}
return ch;
}
void displayItem(const Product& prod) {
cout << prod.getDescription() << ": " << prod.getQuantity() << " @ "
<< fixed << showpoint << setprecision(2) << prod.getPrice() << '\n';
}
int getItemToChange(int length) {
stringstream prompt;
prompt << "Enter item number (1-" << length << "): ";
int num = getInt(prompt.str(), 1, length);
return num;
}
Test product object #1: Test item: 42 @ 52.40 Test product object #2: Undefined: 0 @ 0.00 d) Description change q) Quantity change p) Price change v) View all items x) exit Choice: d Enter item number (1-5): 6 Error: Value above maximum of 5 Enter item number (1-5): 0 Error: Value below minimum of 1 Enter item number (1-5): 4 Currently: Undefined: 0 @ 0.00 Enter new description: 64GB Flash drive Now: 64GB Flash drive: 0 @ 0.00 d) Description change q) Quantity change p) Price change v) View all items x) exit Choice: q Enter item number (1-5): 4 Currently: 64GB Flash drive: 0 @ 0.00 Enter new quantity: -20 Now: 64GB Flash drive: 0 @ 0.00 d) Description change q) Quantity change p) Price change v) View all items x) exit Choice: q Enter item number (1-5): 4 Currently: 64GB Flash drive: 0 @ 0.00 Enter new quantity: 13 Now: 64GB Flash drive: 13 @ 0.00 d) Description change q) Quantity change p) Price change v) View all items x) exit Choice: p Enter item number (1-5): 4 Currently: 64GB Flash drive: 13 @ 0.00 Enter new price: -12.32 Now: 64GB Flash drive: 13 @ 0.00 d) Description change q) Quantity change p) Price change v) View all items x) exit Choice: p Enter item number (1-5): 4 Currently: 64GB Flash drive: 13 @ 0.00 Enter new price: 9.90 Now: 64GB Flash drive: 13 @ 9.90 d) Description change q) Quantity change p) Price change v) View all items x) exit Choice: v List of items in inventory: Undefined: 0 @ 0.00 Undefined: 0 @ 0.00 Undefined: 0 @ 0.00 64GB Flash drive: 13 @ 9.90 Undefined: 0 @ 0.00 d) Description change q) Quantity change p) Price change v) View all items x) exit Choice: x