In: Computer Science
Need Java Code and UML Design for the following program:
Use the Account class created above to simulate an ATM machine. Create five accounts in an array with id 0, 1, ..., 4, 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 (see below). 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, the ATM machine will not stop.
Sample run of the ATM machine is as follows: Enter an id to start transactions: 4
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1 The balance is 100.0
Main menu
1: check balance 2: withdraw
3: deposit
4: exit
Enter a choice: 2
Enter an amount to withdraw: 3
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1 The balance is 97.0
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 3
Enter an amount to deposit: 10
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1 The balance is 107.0
Main menu
1: check balance 2: withdraw
3: deposit
4: exit
Enter a choice: 4
Enter an id to start transactions:
Given below is the code for the question. Please do rate the answe if it helped. Thank you.
Account.java
---
public class Account {
private int id;
private double balance;
public Account() {
id = 0;
balance = 0;
}
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public void setId(int id) {
this.id = id;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void withdraw(double amt) {
if(amt <= balance)
balance =
balance - amt;
}
public void deposit(double amt) {
if(amt > 0)
balance =
balance + amt;
}
}
ATM.java
---
import java.util.Scanner;
public class ATM {
public static void main(String[] args) {
Account[] acc = new
Account[5];
for(int i = 0; i < 5; i++)
{
acc[i] = new
Account();
acc[i].setId(i);
acc[i].setBalance(100);
}
int id;
double amount;
Scanner input = new
Scanner(System.in);
int choice;
while(true) {
do {
System.out.print("Enter an id (0-4) to start
transactions: ");
id = input.nextInt();
}while(id < 0
|| id > 4);
do {
System.out.println("Main menu");
System.out.println("1: check balance");
System.out.println("2: withdraw");
System.out.println("3: deposit");
System.out.println("4: exit");
System.out.print("Enter a choice: ");
choice = input.nextInt();
switch(choice) {
case 1:
System.out.println("The balance is " + acc[id].getBalance());
break;
case 2:
System.out.print("Enter an amount to withdraw: ");
amount =
input.nextDouble();
acc[id].withdraw(amount);
break;
case 3:
System.out.print("Enter an amount to deposit: ");
amount =
input.nextDouble();
acc[id].deposit(amount);
break;
case 4:
break;
default:
System.out.println("Invalid menu choice!");
}
}while(choice !=
4);
}
}
}
output
----
Enter an id (0-4) to start transactions: 4
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1
The balance is 100.0
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 2
Enter an amount to withdraw: 3
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1
The balance is 97.0
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 3
Enter an amount to deposit: 10
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1
The balance is 107.0
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 4
Enter an id (0-4) to start transactions: