Challenge 20

Description

The purpose of this challenge is to define a basic class with private and public data members and functions. This challenge simulates a shopping cart.

 Requirements

  1. Define a class called Cart as below
    class Cart 
    {
      private: 
        int count;
        double total;
        string descriptions[25];
        double amounts[25];
        int qty[25];
      public:
        
    };
  2. The arrays descriptions, amounts and qty act as parallel arrays.
  3. Write a default constructor that will initialize count and total to zero.
  4. Write a public function void add_item(string description, double amount, int quantity). A shopping cart item is represented by a description, and amount and a quantity. Use this function to add an item into the descriptions, amount and qty arrays. Make sure to increment count with each addition. Make sure to not allow adding more than 25 items (this is the maximum size of the array). Do not cout/cin for input in this function.
  5. Write a private member function void calc_total(). This function will go through the parallel arrays calculating the total amount of items added so far. Make sure to use the count variable to know how many items in the arrays actually contain valid items. Write this function outside of the class – do not write this class inline; use the scope resolution operator
  6. Every time add_item() is called, make sure to update total using the calc_total() function. 
  7. Write a public member function double get_total(). This will return the current total of the shopping cart.
  8. Write a public member function double get_count(). This will return the number of items in the shopping cart.
  9. Write a public member function string delete_last(). This function deletes the last item added to the parallel arrays. When the item is deleted, it should no longer be included when calculating the total. This function returns the description of the item just deleted. Call calc_total() after the delete to update the current total. There is a very simple way to do this — don’t overthink the solution. 
  10. None of the functions in the class should cout or cin.
  11. In main, create a menu that will ask the user to add an item to the cart, show the current total or delete the last item in the cart.

Sample main()

int main()
{
  Cart cart;
  char input;

  do
  {
    cout << "a - Add an item" << endl;
    cout << "d - Delete last item" << endl;
    cout << "t - Show total" << endl;
    cout << "Command: " << endl;
    cin >> input;

    switch (input)
    {
      case 'a': // ask user for item details
                // add to cart
      case 'd': // delete the last item
      case 't': // show total
    }

    // other code here

  } while (input != 'q');

  return 0;
}

Sample Interaction

a - Add an item
d - Delete last item
t - Show total
Command: a

Item description? DVD Player
Amount? 39.99
Quantity? 1

1 item(s) in cart

a - Add an item
d - Delete last item
t - Show total
Command: a

Item description? Power Drill
Amount? 119.99
Quantity? 1

2 item(s) in cart

a - Add an item
d - Delete last item
t - Show total
Command: a

Item description? iPad 
Amount? 439.99
Quantity? 1

3 item(s) in cart

a - Add an item
d - Delete last item
t - Show total
Command: d

iPad removed from cart

2 item(s) in cart

a - Add an item
d - Delete last item
t - Show total
Command: t

Cart Total: $159.98

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0020

Print Requirements