JAVA Program The goal of this program is to create an ATM interface. Create a ne
ID: 3713219 • Letter: J
Question
JAVA Program
The goal of this program is to create an ATM interface.
Create a new class with a global variable of type double named balance.
Implement four ATM menu options:
1.Deposit - Increases the balance accordingly
2.Withdraw - Decreases the balance accordingly.
3.Show balance - Prints the balance to the user
4.Exit - Ends the ATM program
The functionality of all of these options, except for Exit, should be contained within a method.
The ATM should first welcome the user and display the input options. In order to specify an option, use Scanner to allow the user to enter the numbers 1, 2, 3, or 4, corresponding to the options Deposit, Withdraw, Show balance, and Exit, respectively.
The ATM should continue to prompt the user for an option (in the main) until the user decides to Exit (4) the ATM. If the user chooses 1 or 2, the ATM should prompt the user to input an amount to deposit or withdraw. If the user attempts to withdraw more money than they have, do not withdraw the money and instead print an error message.
Explanation / Answer
Bank.java
public interface Bank{
public void deposit(double amount);
public void withdraw(double amount);
public double showBalance();
}
MyBank.java
public class MyBank implements Bank{
private double balance;
@Override
public void deposit(double amount) {
balance += amount;
}
@Override
public void withdraw(double amount) {
balance -= amount;
}
@Override
public double showBalance() {
return balance;
}
}
Driver.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) throws IOException{
Scanner in = new Scanner(System.in);
MyBank mb = new MyBank();
while(true) {
System.out.println("Enter your choice: ");
System.out.println("1.Deposit 2.Withdraw 3.Display 4.Exit");
int choice = in.nextInt();
if(choice == 1) {
System.out.println("Enter amount to deposit");
double amount = in.nextDouble();
mb.deposit(amount);
}
else if(choice == 2) {
System.out.println("Enter amount to withdraw");
double amount = in.nextDouble();
mb.withdraw(amount);
}
else if(choice == 3)
System.out.println("Balance in the account: " + mb.showBalance());
else if(choice == 4)
break;
else
System.out.println("Invalid Choice");
}
}
}
**Comment for any further queries.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.