Java Use the Account class created in Programming Exercise 9
Java
Use the Account class created in Programming Exercise 9.7 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. Enter an id: Main menu check balance withdraw deposit exit Enter a choice: The balance is 100.0 Main menu check balance withdraw deposit exit Enter a choice: Enter an amount to withdraw: Main menu check balance withdraw deposit exit Enter a choice: The balance is 97.0 Main menu check balance withdraw deposit exit Enter a choice: Enter an amount to deposit: 10 Main menu check balance withdraw deposit exit Enter a choice: The balance is 107.0 Main menu check balance withdraw deposit exit Enter a choice: Enter an id:Solution
import java.util.*;
public class ATM {
public static Scanner kbd = new Scanner(System.in);
public static String checkdetails(String acctNum, String pwd)
{
String result = \"invlaid\";
String a = \"...................\";
if (acctNum.equals(a.substring(0, a.indexOf(\" \"))) &&
pwd.equals(a.substring(a.indexOf(\" \")+1,a.lastIndexOf(\" \"))))
return result = a.substring(a.lastIndexOf(\" \") + 1);
return result;
}
public static int display()
{
int display_options;
do
{
System.out.print(\"\"\ 1. Display Balance \ 2. Deposit\"
+ \"\ 3. Withdraw\ 4. Log Out\ \ \");
menuChoice = kbd.nextInt();
if (display_options < 1 || display_options> 4){
System.out.println(\"invalid\");
}
}while (display_options < 1 || display_options > 4);
return display_options;
}
public static void displayBalance(double x)
{
System.out.printf(\"\ Your Current Balance is $%.2f\ \", x);
}
public static double deposit(double x, double y)
{
double depositAmt = y, currentBal = x;
double newBalance = depositAmt + currentBal;
System.out.printf(\"Your New Balance is $%.2f\ \", newBalance);
return newBalance;
}
public static double withdraw(double x, double y)
{
double withdrawAmt = y, currentBal = x, newBalance;
newBalance = currentBal - withdrawAmt;
System.out.printf(\"Your New Balance is %.2f\ \",newBalance);
return newBalance;
}
public static void main(String[] args) {
String accNum, pass, origBal = \"error\";
int count = 0, menuOption = 0;
double depositAmt = 0, withdrawAmt = 0, currentBal=0;
boolean foundNonDigit;
do{
foundNonDigit = false;
System.out.println(\" Enter Your Account Number: \");
accNum = kbd.next();
System.out.println(\"Enter Your Password: \");
pass = kbd.next();
origBal = checkID(accNum, pass);
count++;
if (count >= 3 && origBal.equals(\"error\")){
System.out.print(\"Maximum Login Attempts Reached.\");
System.exit(0);
}
if (!(origBal.equals(\"error\"))){
System.out.println(\"\ Your New Balance is: $ \"+ origBal);
}
else
System.out.println(origBal);
}while(origBal.equals(\"error\"));
currentBal=Double.parseDouble(origBal);
while (display_options != 4)
{
display_options=menu();
switch (display_options)
{
case 1:
displayBalance(currentBal);
break;
case 2:
System.out.print(\"\ Enter Amount to Deposit: \");
depositAmt = kbd.nextDouble();
currentBal=deposit(depositAmt, currentBal);
break;
case 3:
System.out.print(\"\ Enter Amount to Withdrawl: \");
withdrawAmt = kbd.nextDouble();
while(withdrawAmt>currentBal){
System.out.print(\"ERROR: INSUFFICIENT FUNDS!! \"
+ \"PLEASE ENTER A DIFFERENT AMOUNT: \");
withdrawAmt = kbd.nextDouble();
}
currentBal = withdraw(currentBal, withdrawAmt);
break;
case 4:
System.out.print(\"\ Thank For Using My ATM\");
System.exit(0);
break;
}
}
}
}


