Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab + Homework 09 - Piggy Bank ATM Although Mr. Pigglesworth Bank has been your

ID: 3603034 • Letter: L

Question

Lab + Homework 09 - Piggy Bank ATM Although Mr. Pigglesworth Bank has been your trusted piggy bank since age 5, you have had enough of the establishment's antiquated style of service. You have decided to bring Mr. Pigglesworth into the modern era by programming an ATM application for your piggy bank. To model your piggy bank, you will define a struct called Coins, which contains four integer members: quarters, dimes, nickels, and pennies. You will also define several functions that will help you work with Coins structures. When your program is launched, it does the following. 1) It creates a Coin instance called piggyBank that contains 5 quarters, 10 dimes, 15, nickels, and 20 pennies. 2) The program calls ShowMenu() to display a menu of 5 choices: ‘D’ for depositing coins in bank, ‘W’ for withdrawing coins from bank, ’S’ for showing the account balance, and ‘Q’ for quitting the program. The program then waits for the user to make a choice. 3) The program proceeds to carry out the action the user selected. a. If ‘D’ or ‘d’ was selected, EnterCoins() is called to ask the user to specify how much of each type of coin should be deposited. Assume that the user always has the amount of coins they wish to deposit. DepositCoins (Coins amount, Coins coinSource) is called to deposit the coins into piggyBank before returning to step 2. b. If ‘W’ or ‘w’ was selected, EnterCoins() is called to ask the user to specify how much of each type of coin should be withdrawn from piggyBank. The function CoinsAvailable(Coins amount, Coins coinSource) is called to determine if the user is attempting to withdraw more coins than the bank contains. If the user is attempting to take more coins than the bank contains, the program cancels the transaction and displays an error message; otherwise, WithdrawCoins(Coins amount, Coins coinSource) is called to remove the appropriate amount of coins from piggyBank. Either way, program returns to step 2. c. If ’S’ or ’s’ was selected, the user is shown the quantities for each kind of coin in piggyBank. Program returns to step 2. d. if ‘Q’ or ‘q’ was selected, the program quits. e. If none of the above actions were selected, an error message is displayed before returning to step 2. Requirements: 1) The program must define a struct called Coins, that contains four integer data members: quarters, dimes, nickels, and pennies. CPSC 120 Week 09 2) The program must define and call these functions: 3) Do not make adjustments to the above function headers. They must be used as is. There are no restrictions on other functions you choose to create. 4) Follow good code style guidelines. Hints: 1) The assignment operator = works with structs, but most other operators, like + and - do not. You will need to do all of the calculations on each of the Coins data members. 2) Take note that the functions that make changes to Coins structs also return a Coins type. Recall that structs are passed by value, so changes you make to the Coins parameters are not made to the outside Coins instances you passed in. You will need to rely on the returned Coins object to update the contents of piggyBank. 3) Write and test each function, one at a time. Testing as you go will give you confidence your functions are correct. Waiting until after you have written everything before testing your program for the first time will make it 1000% harder to track down the issues. Expected Output: ------ATM Options------ D - Deposit coins in bank Coins DepositCoins (Coins amount, Coins coinSource) Takes amount parameter’s data members (quarters, dimes, …) and adds them to the coinSource parameter’s corresponding data members. It returns a Coins instance with data members that are the sum of coinSource’s and amount’s data members. Coins WidthdrawCoins (Coins amount, Coins coinSource) Takes the amount parameter’s data members and subtracts them from the coinSource parameter’s corresponding data members. It returns a Coins instance that has data members that are the differences of both parameter’s data members. bool CoinsAvailable (Coins amount, Coins coinSource) Allows you to test if the piggy bank has enough coins to complete a withdrawal transaction. Returns true when none of the amount parameter’s data members are greater than the coinSource parameter’s corresponding data members. Otherwise, it returns false. Coins EnterCoins () Asks the users for the number of quarters, dimes, nickels, and pennies and returns those quantities as a Coins instance. void ShowAccountBalance (Coins coinSource) Displays the number of quarters, dimes, nickels, and pennies the coinSource parameter’s data members contain. See expected output for details. void ShowMenu () Displays a menu of options to the user. See expected output for details. CPSC 120 Week 09

W - Withdraw coins from bank

S - Show account balance

Q - Quit -----------------------

Enter your selection: s

========Account========

Quarters: 5 Dimes: 10 Nickels: 15 Pennies: 20

=======================

------ATM Options------

D - Deposit coins in bank

W - Withdraw coins from bank

S - Show account balance

Q - Quit -----------------------

Enter your selection: d Enter number of quarters: 2

Enter number of dimes: 4 Enter number of nickels: 6 Enter number of pennies: 8

------ATM Options------

D - Deposit coins in bank

W - Withdraw coins from bank

S - Show account balance

Q - Quit -----------------------

Enter your selection: s

========Account========

Quarters: 7 Dimes: 14 Nickels: 21 Pennies: 28

=======================

------ATM Options------

D - Deposit coins in bank W - Withdraw coins from bank

S - Show account balance

Q - Quit -----------------------

Enter your selection: w

Enter number of quarters: 8 Enter number of dimes: 0 Enter number of nickels: 0

Please use: #include <iostream> ; using namespace std; ; use endl ; don't use some advanced coede. This is a beginner class.

Explanation / Answer

Please give your feedback and let me know if you have any doubts.Thanks.

here is the complete solution in C++.

#include <iostream>

#include <fstream>

#include <string>

#include <cstdlib>

#include <sstream>

using namespace std;

struct Coins {

int quarters;

int dimes;

int nickels;

int pennies;

};

void ShowMenu()

{

cout << "------ATM Options------" << endl;

cout << "D - Deposit coins in bank "<< endl;

cout << "W - Withdraw coins from bank" << endl;

cout << "S - Show account balance " << endl;

cout << "Q - Quit -----------------------" << endl;

cout << "Enter your selection : ";

}

Coins EnterCoins(){

Coins coin;

int quarters;

int dimes;

int nickels;

int pennies;

cout << "Enter number of quarters: ";

cin>>quarters;

cout << "Enter number of dimes:" ;

cin>>dimes;

cout << "Enter number of nickels: " ;

cin>>nickels;

cout << "Enter number of pennies:" ;

cin>>pennies;

coin.quarters=quarters;

coin.dimes=dimes;

coin.nickels=nickels;

coin.pennies=pennies;

return coin;

}

void DepositCoins (Coins amount, Coins *coinSource) {

coinSource->quarters+=amount.quarters;

coinSource->dimes+=amount.dimes;

coinSource->nickels+=amount.nickels;

coinSource->pennies+=amount.pennies;

}

bool CoinsAvailable(Coins amount, Coins *coinSource){

bool flag=true;

if(amount.quarters>coinSource->quarters || amount.dimes>coinSource->dimes

|| amount.nickels>coinSource->nickels || amount.pennies>coinSource->pennies){

flag=false;

}

return flag;

}

void WithdrawCoins(Coins amount, Coins *coinSource){

coinSource->quarters-=amount.quarters;

coinSource->dimes-=amount.dimes;

coinSource->nickels-=amount.nickels;

coinSource->pennies-=amount.pennies;

}

void ShowAccountBalance (Coins coinSource){

cout<<"========Account========"<<endl;

cout<<" Quarters: "<<coinSource.quarters

<<" Dimes: "<<coinSource.dimes

<<" Nickels: "<<coinSource.nickels

<<" Pennies: "<<coinSource.pennies<<endl;

}

int main(){

Coins piggyBank ;

piggyBank.quarters=5;

piggyBank.dimes=10;

piggyBank.nickels=15;

piggyBank.pennies=20;

char choice;

Coins amount ;

do

{

ShowMenu();

cin >> choice;

bool flag;

if(choice>97){

choice=choice-32;

}

switch (choice)

{

case 'D':

amount=EnterCoins();

DepositCoins(amount,&piggyBank);

break;

case 'W':

amount=EnterCoins();

flag=CoinsAvailable(amount,&piggyBank);

if(!flag){

cout<<"Entered coins amount is not available "<<endl;

}else{

WithdrawCoins(amount,&piggyBank);  

}

break;

case 'S':

ShowAccountBalance(piggyBank);

break;

case 'Q':

break;

default:

cout << "Invalid input" << endl;

}

}while (choice != 'Q' );

return 0;

}