Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The to-do parts I need help with... /* classLab.cpp Name: CIS 150 Date: Demonstrates simple class/object programming principles. */ #include #include #include #include #include using

The to-do parts I need help with...

/* 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 = " d) Description change q) Quantity change p) Price change v) View all items x) exit Choice: "; 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: "; for (itemNumber=0; itemNumber> val)) { cin.clear(); cin.ignore(100, ' '); 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 << ' '; else cout << "Error: Value above maximum of " << max << ' '; val = getInt(prompt); } return val; } double getDouble(string prompt) { double val; cout << prompt; while (! (cin >> val)) { cin.clear(); cin.ignore(100, ' '); 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, ' '); 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 "; ch = getChar(prompt); } return ch; } void displayItem(const Product& prod) { cout << prod.getDescription() << ": " << prod.getQuantity() << " @ " << fixed << showpoint << setprecision(2) << prod.getPrice() << ' '; } int getItemToChange(int length) { stringstream prompt; prompt << "Enter item number (1-" << length << "): "; int num = getInt(prompt.str(), 1, length); return num; } 

Example run of program

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 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions

Question

How do writers decide what to cover in a trip report?

Answered: 1 week ago

Question

What is the difference between Needs and GAP Analyses?

Answered: 1 week ago

Question

What are ERP suites? Are HCMSs part of ERPs?

Answered: 1 week ago