Use the Account class created to simulate an ATM machine. Create ten accounts in
ID: 3682061 • Letter: U
Question
Use the Account class created to simulate an ATM machine. Create ten accounts in an array with id 0, 1, . . . , 9, and initial balance $100. The system prompts the user to enter an id. If the id is entered incorrectly, ask the user to enter a correct id. Once an id is accepted, the main menu is displayed as shown in the sample run. You can enter a choice 1 for viewing the current balance, 2 for withdrawing money, 3 for depositing money, and 4 for exiting the main menu. Once you exit, the system will prompt for an id again. Thus, once the system starts, it will not stop.
Sample run:
Please enter an Id from (0-9):
5
Main menu:
1: check balance
2: withdraw
3: deposit
4: exit
Please enter a choice:1
The balance is:100.0
Main menu:
1: check balance
2: withdraw
3: deposit
4: exit
Please enter a choice:2
Enter an amount to withdraw: 20
The new balance is:80.0
Main menu:
1: check balance
2: withdraw
3: deposit
4: exit
Please enter a choice:3
Enter an amount to deposit: 50
The new balance is:130.0
Main menu:
1: check balance
2: withdraw
3: deposit
4: exit
Please enter a choice:6
Illegal choice input. Try again.
Main menu:
1: check balance
2: withdraw
3: deposit
4: exit
Please enter a choice:4
Please enter an Id from (0-9):
------------------------------------------------
Explanation / Answer
java program:
import java.io.*;
import java.util.Scanner;
public class ATM {
public static void main(String []args)throws IOException{
int id,selection,amount;
Account []a=new Account[10];
Scanner scan=new Scanner(System.in);
for(int i=0;i<10;i++)
a[i]=new Account(i);
while(true){
System.out.print("Please enter an Id from (0-9):");
id=scan.nextInt();
System.out.println();
selection=0;
while(selection!=4){
System.out.println("Main menu: " +
"1: check balance " +
"2: withdraw " +
"3: deposit " +
"4: exit");
selection=scan.nextInt();
switch(selection){
case 1:a[id].checkBalance();break;
case 2: System.out.print("Please enter a Amount to withdraw:");
amount=scan.nextInt();
System.out.println();
a[id].withdraw(amount);
break;
case 3:System.out.print("Please enter a Amount to deposite:");
amount=scan.nextInt();
System.out.println();
a[id].deposite(amount);
break;
case 4:break;
default:System.out.println("Wrong OPTION");
}
}
}
}
}
class Account{
private int id;
private double balance;
public Account(int id){
this.id=id;
this.balance=100;
}
public void checkBalance(){
System.out.println("Balance in your account:"+balance);
}
public void withdraw(double amount){
if(amount>balance)
System.out.println("there is no much amount in your account");
else
balance-=amount;
System.out.println("Balance in your account:"+balance+" After withdraw:"+amount);
}
public void deposite(double amount){
if(amount>0){
balance+=amount;
System.out.println("Balance in your account:"+balance+" After deposite:"+amount);
}
else
System.out.println("Amount should be positive");
}
}
output:
run:
Please enter an Id from (0-9):1
Main menu:
1: check balance
2: withdraw
3: deposit
4: exit
1
Balance in your account:100.0
Main menu:
1: check balance
2: withdraw
3: deposit
4: exit
2
Please enter a Amount to withdraw:36
Balance in your account:64.0 After withdraw:36.0
Main menu:
1: check balance
2: withdraw
3: deposit
4: exit
3
Please enter a Amount to deposite:21
Balance in your account:85.0 After deposite:21.0
Main menu:
1: check balance
2: withdraw
3: deposit
4: exit
4
Please enter an Id from (0-9):6
Main menu:
1: check balance
2: withdraw
3: deposit
4: exit
1
Balance in your account:100.0
Main menu:
1: check balance
2: withdraw
3: deposit
4: exit
4
Please enter an Id from (0-9):
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.