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: 3599576 • 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

Explanation / Answer

Here is program in C++, for your all desired operations in the piggy bank.

//Start of program
#include <iostream>
#include <ctype.h>

using namespace std;

//Structure for storing coins
struct Coins
{
   int quaters;
   int dimes;
   int nickels;
   int pennies;
};

// Displays the Account balance
void ShowAccountbalance(Coins coinSource)
{
   cout<<" ===========Account Balance============== ";
   cout<<"Quaters: "<<coinSource.quaters;
   cout<<" Dimes: "<<coinSource.dimes;
   cout<<" Nickels: "<<coinSource.nickels;
   cout<<" Pennies: "<<coinSource.pennies;
   cout<<" ========================================= ";
}

// To Enter the coins
Coins EnterCoins()
{
       Coins amount;
       cout<<"Enter the Quaters : ";
       cin>>amount.quaters;
       cout<<"Enter the Dimes: ";
       cin>>amount.dimes;
       cout<<"Enter the Nickels: ";
       cin>>amount.nickels;
       cout<<"Enter the Pennies: ";
       cin>>amount.pennies;
       return amount;

}

//o determine if the user is attempting to withdraw more coins than the bank contain
bool CoinsAvailable(Coins amount, Coins CoinSource)
{
   if (CoinSource.quaters < amount.quaters)
       return false;
   else if (CoinSource.dimes < amount.dimes)
       return false;
   else if (CoinSource.nickels < amount.nickels)
       return false;
   else if (CoinSource.pennies < amount.pennies)
       return false;

   else return true;

}

//To deposit money to deposit the coins into piggyBank before returning to step 2.
Coins DepositCoins (Coins amount, Coins CoinSource)
{
   CoinSource.quaters += amount.quaters;
   CoinSource.dimes += amount.dimes;
   CoinSource.nickels += amount.nickels;
   CoinSource.pennies += amount.pennies;

   return CoinSource;
}

// to remove the appropriate amount of coins from piggyBank
Coins WithdrawCoins(Coins amount, Coins coinSource)
{
   coinSource.quaters -= amount.quaters;
   coinSource.dimes -= amount.dimes;
   coinSource.nickels -= amount.nickels;
   coinSource.pennies -= amount.pennies;

   return coinSource;
}

// It will displays the choices avialble to do with bank
char ShowMenu ()
{
   char ch;
  
   cout<<" ========== Menu For The Bank==========";
   cout<<"W - Withdraw coins from Bank ";
   cout<<"S - Show Account Balance in Bank ";
   cout<<"D - Deposit coins in Bank ";
   cout<<"Q - To Enter Exit from the bank ";
   cout<<"Enter your Choicefrom above ";
   cout<<" ==============End Of Menu Of Bank======= ";
   cin>>ch;

   ch = toupper(ch); // Converting the character to uppercase
   return ch; // returning value

}

//Driver FUnction
int main ()
{
   // constructor of structure and assigning values
   Coins piggyBank;
   piggyBank.quaters =5;
   piggyBank.dimes = 10;
   piggyBank.nickels = 15;
   piggyBank.pennies = 20;

   char input;
   input = ShowMenu();
   // Stores the choice in variable;
   while (1==1) {
        // Stores the choice in variable;
   // to check the input enetered by user
   switch (input) {
       case 'S' :
       ShowAccountbalance(piggyBank);
       input =ShowMenu();
       break;

       case 'D' :  
       piggyBank = DepositCoins (EnterCoins(),piggyBank);
       input = ShowMenu();
      
       break;

       case 'W' :
       Coins check;
       check = EnterCoins();
       input = ShowMenu();

       if (CoinsAvailable(check,piggyBank ))
           piggyBank = WithdrawCoins(check,piggyBank);
       else
           cout<<"You don not have enough balance to withdraw. ";
       input = ShowMenu();

       break;

       case 'Q' :
       cout<<"Exiting from the Bank: ";
       return 0;
       break;

       default :
       cout<<"You have enetered the wrong choice Please Enter again: ";
       input = ShowMenu();

   }
}

}
//End Of program