/* classLab.cpp Name: CIS 150 Date: Demonstrates simple class/object programming principles. */ #include #include #include #include #include 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> 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; }