Description
The purpose of this challenge is to use basic classes. This challenge loosely simulates a vending machine.
Requirements
- Create a class called Machine
- In class Machine, add the following private member data
- double total
- double change
- string message
- string codes[3]
- string desc[3]
- double amounts[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.
- 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).
- Create a private function void set_inventory(). In this function, fill up the array as below
- codes[0] = “A1”; codes[1] = “A2”; codes[2] = “A3”;
- desc[0] = “Pepsi”; desc[1] = “Dr.Pepper”; desc[2] = “Red Bull”;
- amounts[0] = 1.00; amounts[1] = 2.50; amounts[2] = 1.25;
- 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.
- Create a simple constructor that sets total and change to zero and message to an empty string. Call set_inventory() from within the constructor.
- Create a public function reset(). In this function, if total equals zero, set message to an empty string and set change to zero
- Create a public function void entry(string input). In this function, you will evaluate the value of input in the manner below:
- if input is “1”, then increment total by 1. This “1” represents an input of one dollar. Set message = “Total ” + to_string(total).
- if input is “Q”, then increment total by 0.25. This “Q” represents an input of one quarter. Set message = “Total ” + to_string(total).
- if input is “D”, then increment total by 0.10. This “D” represents an input of one dime. Set message = “Total ” + to_string(total).
- if input is “N”, then increment total by 0.05. This “N” represents an input of one dime. Set message = “Total ” + to_string(total).
- 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.
- 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.
- If the function call returns -1, then set message to “Unknown code”.
- 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.
- 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.
- 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