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

Week 10 CPSC 120 Week 10 Adding Class to Your Programs Lab+ Homework 10 - Piggy

ID: 3606496 • Letter: W

Question

Week 10 CPSC 120 Week 10 Adding Class to Your Programs Lab+ Homework 10 - Piggy Bank ATM 2 Although Mr. Pigglesworth Bank has been your trusted piggy bank since age 5, you have had enough Pigglesworth into the moderm era by programming an ATM application for your piggy bank. To model your piggy bank, you will define a class called Coins, which contains four integer members: quarters, dimes, nickels, and pennies. You will also define several member functions that will help you work with your Coins instances of the establishment's antiquated style of service. You have decided to bring Mr. 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. Hint: Use a constructor 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. 2) The program proceeds to carry out the action the user selected. a· 3) r was selected, Coins member tunction EnterCoins0 is called on an Coins lf D'or instance called amount 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 Coins member function DepositCoins (Coins amount) is then called on piggyBank to deposit amount's quantity of coins before returning to step 2. b. f W or w' was selected, Coins member function EnterCoins) is called on a Coins instance called amount to ask the user to specify how much of each type of coin should be withdrawn from piggyBank. The Coins member function CoinsAvailable(Coins amount) is called on piggyBank 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 Coins member function WithdrawCoins(Coins amount) is called on piggyBank to remove the appropriate amount of coins from piggyBank. Either way, program returns to step 2 If 'S'or 's' was selected, the user is shown the quantities for each kind of coin in piggyBank. Program returns to step 2 c. 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 class called Coins that contains four private integer data members: quarters, dimes, nickels, and pennies

Explanation / Answer

#include <iostream>

using namespace std;

class Coins {

private:

int quarters, dimes, nickels, pennies;

public:

Coins(){

quarters = 0;

dimes = 0;

nickels = 0;

pennies = 0;

}

Coins(int newQuarters, int newDimes, int newNickels, int newPennies) {

quarters = newQuarters;

dimes = newDimes;

nickels = newNickels;

pennies = newPennies;

}

int getQuarters() {

return quarters;

}

void setQuarters(int quarters) {

this->quarters = quarters;

}

int getDimes() {

return dimes;

}

void setDimes(int dimes) {

this->dimes = dimes;

}

int getNickels() {

return nickels;

}

void setNickels(int nickels) {

this->nickels = nickels;

}

int getPennies() {

return pennies;

}

void setPennies(int pennies) {

this->pennies = pennies;

}

void DepositCoins(Coins amount){
EnterCoins();

this->quarters+= amount.quarters;

this->dimes+= amount.dimes;

this->nickels+= amount.nickels;

this->pennies+= amount.pennies;

}

void WithdrawCoins(Coins amount){

if(CoinsAvailable(amount) == true){

this->quarters-= amount.quarters;

this->dimes-= amount.dimes;

this->nickels-= amount.nickels;

this->pennies-= amount.pennies;

}

else{

cout<<"There are not enough coins to complete the transaction.";

}

}

bool CoinsAvailable(Coins amount){

if(amount.quarters<this->quarters

&& amount.dimes<this->dimes

&& amount.nickels<this->nickels

&& amount.pennies<this->pennies){

return true;

}

else{

return false;

}

}

void EnterCoins(){

cout<<"Enter number of dimes: "<<endl;

this->dimes;

cout<<"Enter number of quarters: "<<endl;

this->quarters;

cout<<"Enter number of nickels: "<<endl;

this->nickels;

cout<<"Enter number of pennies: "<<endl;

this->pennies;

}

void showMenu(Coins);

void ShowAccountBalance(){
  
cout<<"======Account========"<<endl;

cout<<"Quarters: "<<this->quarters<<endl;

cout<<"Dimes: "<<this->dimes<<endl;

cout<<"Nickels: "<<this->nickels<<endl;

cout<<"Pennies: "<<this->pennies<<endl;

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

}

};

void Coins::showMenu(Coins piggyBank){

char choice;

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<<"------------------------"<<endl;

cout<<"Enter your selection : "<<endl;

cin>>choice;

switch(choice){

case 'd':

case 'D': DepositCoins(piggyBank);

break;

case 'w':

case 'W': WithdrawCoins(piggyBank);

break;

case 's':

case 'S': ShowAccountBalance();

break;

case 'q':

case 'Q': exit(0);

break;

default: cout<<"Please make appropriate Choice."<<endl;

break;

}

}

int main(){

Coins piggyBank(5,10,15,20);

piggyBank.showMenu(piggyBank);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote