In: Computer Science
C++
Using what you know about inputs and outputs, code a simple ATM interaction. Your ATM will start with only $500 to give out, and YOUR balance will be 1000. The amounts the ATM can dispense are $40, $80, $200 and $400. After you've received your disbursement, the console will show the amount you have left in your account, as well as how much the ATM has to dispense (we live in a small, fictional town - no one will use this information for nefarious purposes).
You may choose to use characters to show the interface, or just list the options for the user at the console.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Choose One
| $40 |$80
| $200 |$400
EXIT
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Save the following code in file atm.cpp .compile and run it.
#include <iostream>
using namespace std;
int main(){
int choice = 0;
int balance = 1000;
int withdrawal = 0;
int max_withdraw = 500;
int step_withdraw = 0;
cout<<"\nInitial Balance = "<<balance;
while(balance > 0 && withdrawal <= max_withdraw)
{
//The choice Menu to ATM user
cout<<"\n>>>>>>>>>>>>>>>>>>>>>>>>>"
<< endl;
cout<< "Choose One" << endl;
cout<<"1. $40 "<< endl;
cout<<"2. $80 "<< endl;
cout<<"3. $200 "<< endl;
cout<<"4. $400 "<< endl;
cout<<"5. EXIT "<< endl;
cout<<">>>>>>>>>>>>>>>>>>>>>>>>>"
<< endl;
cout<<"\nEnter your choice( 1 - 5): ";
cin>>choice;
// Take action as per choice...
switch(choice){
case 1:
step_withdraw = 40;
break;
case 2:
step_withdraw = 80;
break;
case 3:
step_withdraw = 200;
break;
case 4:
step_withdraw = 400;
break;
case 5: exit(0);
default: cout<<"\nInvalid choice";
break;
}
// Maintain ATM balance
if(withdrawal + step_withdraw > max_withdraw){
cout<<"\nYou can't withdraw more than " << max_withdraw
<< endl;
exit(0);
}else{
balance = balance - step_withdraw;
withdrawal = withdrawal + step_withdraw;
cout <<"\nYour Balance = " << balance;
cout <<"\nYour total withdrawal = " <<
withdrawal;
}
}
return 0;
}
Here is the program output:
Initial Balance = 1000
>>>>>>>>>>>>>>>>>>>>>>>>>
Choose One
1. $40
2. $80
3. $200
4. $400
5. EXIT
>>>>>>>>>>>>>>>>>>>>>>>>>
Enter your choice( 1 - 5): 4
Your Balance = 600
Your total withdrawal = 400
>>>>>>>>>>>>>>>>>>>>>>>>>
Choose One
1. $40
2. $80
3. $200
4. $400
5. EXIT
>>>>>>>>>>>>>>>>>>>>>>>>>
Enter your choice( 1 - 5): 2
Your Balance = 520
Your total withdrawal = 480
>>>>>>>>>>>>>>>>>>>>>>>>>
Choose One
1. $40
2. $80
3. $200
4. $400
5. EXIT
>>>>>>>>>>>>>>>>>>>>>>>>>
Enter your choice( 1 - 5): 1
You can't withdraw more than 500