Challenge 38

Description

The purpose of this challenge is to use basic classes. This challenge loosely simulates a vending machine.

Requirements

  1. Create a class called Machine
  2. In class Machine, add the following private member data
    1. double total
    2. double change
    3. string message
    4. string codes[3]
    5. string desc[3]
    6. double amounts[3]
  3. Create a public function string get_message()Do NOT cout in this function. This is a simple getter function that returns the value of message 
  4. Create a public getter function for change. However, even though this is a getter function for change (a double), return a string value. If change is zero, return a blank string. If change is non-zero, return to_string(change). 
  5. Create a private function void set_inventory(). In this function, fill up the array as below
    1. codes[0] = “A1”; codes[1] = “A2”; codes[2] = “A3”;
    2. desc[0] = “Pepsi”; desc[1] = “Dr.Pepper”; desc[2] = “Red Bull”;
    3. amounts[0] = 1.00; amounts[1] = 2.50; amounts[2] = 1.25;
  6. Create a private function int find_item(string code). This function will search the codes array for code. If is found, identify the index position where it is found. If it is not found, return -1.
  7. Create a simple constructor that sets total and change to zero and message to an empty string. Call set_inventory() from within the constructor.
  8. Create a public function reset(). In this function, if total equals zero, set message to an empty string and set change to zero
  9. Create a public function void entry(string input). In this function, you will evaluate the value of input in the manner below:
    1. if input is “1”, then increment total by 1. This “1” represents an input of one dollar. Set message = “Total ” + to_string(total).  
    2. if input is “Q”, then increment total by 0.25. This “Q” represents an input of one quarter. Set message = “Total ” + to_string(total). 
    3. if input is “D”, then increment total by 0.10. This “D” represents an input of one dime. Set message = “Total ” + to_string(total). 
    4. if input is “N”, then increment total by 0.05. This “N” represents an input of one dime. Set message = “Total ” + to_string(total). 
    5. For all the steps above, the to_string() function will convert total to its string representation; this value will likely be displayed with several decimal places — this is ok, no need to format into 2 decimal places.
    6. if input is anything else, assume it is an item code. Then, call find_item() passing in input and assigning the returned value to an integer index. 
      1. If the function call returns -1, then set message to “Unknown code”.
      2. If the function call returns a number that is not -1, then this number represents the index into the arrays where the item was found.
        1. If total is greater than or equal to amounts[index], then set change to total – amounts[index]. Also, set message to desc[index]. Set total to zero.
        2. If total is less than amounts[index], set message to “Enter more money”

Sample Interaction / Output

: 1
Total: 1.00

: M1 
Unknown Code

: A2 
Enter more money

: 1 
Total: 2.00

: Q 
Total: 2.25

: D 
Total: 2.35

: N 
Total: 2.40

: Q 
Total: 2.65

: A2 
Dr. Pepper
0.15

Run it again (or keep going after a completed purchase)

: 1
Total: 1.00

: D 
Total: 1.10

: D 
Total: 1.20

: N 
Total: 1.25

: A3 
Red Bull

Sample main()

int main()
{
  Machine vending;
  string input;

  while (true)
  {
    vending.reset();

    cout << ": ";
    cin >> input;

    vending.entry(input);

    cout << vending.get_message() << endl;
    cout << vending.get_change() << endl;
  }

  return 0;
}

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0038

Print Requirements