Simulate an ATM. Start the user\'s checking account balanceat $5000. Then, enter
ID: 3608302 • Letter: S
Question
Simulate an ATM. Start the user's checking account balanceat $5000. Then, entering a user-input loop, give the userfour choices:
The user is allowed to enter a single character or an entireword and the program continues as appropriate:
Input Error Checking: Always check for invalid user input (likean invalid character choice, a negative number or an excessivewithdrawal) before proceeding. If the user commits an errorof any kind (a bad command letter such as 'T' or an illegalwithdrawal or deposit amount), print an appropriate error messageand return to the top of the main loop which asks the user foranother command: W, D,B or Q. You can optionallyadd confirmation messages before taking action.
There is one exception to the error checking just described.When it is time for the user to enter a number, you can assume hedoes not type some non-numeric value. You don't have to testfor this kind of error.
By the way -- a zero balance is allowed. Do notflag this as an error. Consider this allowable behavior arequirement, like everything else stated in this spec.
Test Run Requirements: Run your program for several cycles(passes) of the loop. Demonstrate all four options at leasttwice each in a random order. Also demonstrate the capacityto get either single letters or entire words from the user, showingat least one user-input error (an illegal choice) and one badnumeric input (overdraft and/or negative value).
Explanation / Answer
//Here isAtmAccount.java //Hope this will helpyou.import java.util.*;
class Account { private double balance; Account(double money) { balance = money; } Account() { balance = 0.0; } public void setBalance(double Num) { balance = Num; } public double getBalance() { return balance; } public double Withdraw(double Num) { if(Num <=0 || Num > balance) return -1; System.out.println("Withdrawing of amount " + Num); balance= balance - Num; return Num; } public double getWithdraw() { return balance; } public double Deposit(double Num) { if(Num <=0) return -1; System.out.println("Depositing of amount " + Num); balance = balance + (double)Num; return Num; } public double getDeposit() { return balance; } public void zerobalance(double Num) { System.out.println("Closing the Account"); balance = 0.0; } }; class AtmAccount { public static void main(String args[]) { char ch; String str; float amt; boolean c=false; Scanner input = new Scanner(System.in); Account account1 = new Account(5000.0);
while(true) { if(!c) System.out.println(" W (Withdrawal) D (Deposit) B (ShowBalance) Q (Quit) "); str=input.nextLine(); if(str.length()==1) ch=str.charAt(0); else { c=true; continue; } c=false; try{
if(ch =='W' || ch =='w') { System.out.println("Enter amount :"); amt = input.nextFloat(); if(account1.Withdraw(amt)!=-1) System.out.println("Withdrawal success"); else System.out.println("Can't withdraw this amount "+ amt); } else if(ch =='D' || ch =='d') { System.out.println("Enter amount :"); amt = input.nextFloat(); if(account1.Deposit(amt)!=-1) System.out.println("Deposit Success"); else System.out.println("Deposit Failure"); } else if(ch =='B' || ch =='b') { System.out.println(" The balance is " +account1.getBalance()); } else if(ch =='Q' || ch =='q') break; }catch(Exception e) { System.out.println("Invalid intput "); continue; } }
} }
/* import java.util.*;
class Account { private double balance; Account(double money) { balance = money; } Account() { balance = 0.0; } public void setBalance(double Num) { balance = Num; } public double getBalance() { return balance; } public double Withdraw(double Num) { if(Num <=0 || Num > balance) return -1; System.out.println("Withdrawing of amount " + Num); balance= balance - Num; return Num; } public double getWithdraw() { return balance; } public double Deposit(double Num) { if(Num <=0) return -1; System.out.println("Depositing of amount " + Num); balance = balance + (double)Num; return Num; } public double getDeposit() { return balance; } public void zerobalance(double Num) { System.out.println("Closing the Account"); balance = 0.0; } }; class AtmAccount { public static void main(String args[]) { char ch; String str; float amt; boolean c=false; Scanner input = new Scanner(System.in); Account account1 = new Account(5000.0);
while(true) { if(!c) System.out.println(" W (Withdrawal) D (Deposit) B (ShowBalance) Q (Quit) "); str=input.nextLine(); if(str.length()==1) ch=str.charAt(0); else { c=true; continue; } c=false; try{
if(ch =='W' || ch =='w') { System.out.println("Enter amount :"); amt = input.nextFloat(); if(account1.Withdraw(amt)!=-1) System.out.println("Withdrawal success"); else System.out.println("Can't withdraw this amount "+ amt); } else if(ch =='D' || ch =='d') { System.out.println("Enter amount :"); amt = input.nextFloat(); if(account1.Deposit(amt)!=-1) System.out.println("Deposit Success"); else System.out.println("Deposit Failure"); } else if(ch =='B' || ch =='b') { System.out.println(" The balance is " +account1.getBalance()); } else if(ch =='Q' || ch =='q') break; }catch(Exception e) { System.out.println("Invalid intput "); continue; } }
} }
import java.util.*;
class Account { private double balance; Account(double money) { balance = money; } Account() { balance = 0.0; } public void setBalance(double Num) { balance = Num; } public double getBalance() { return balance; } public double Withdraw(double Num) { if(Num <=0 || Num > balance) return -1; System.out.println("Withdrawing of amount " + Num); balance= balance - Num; return Num; } public double getWithdraw() { return balance; } public double Deposit(double Num) { if(Num <=0) return -1; System.out.println("Depositing of amount " + Num); balance = balance + (double)Num; return Num; } public double getDeposit() { return balance; } public void zerobalance(double Num) { System.out.println("Closing the Account"); balance = 0.0; } }; class AtmAccount { public static void main(String args[]) { char ch; String str; float amt; boolean c=false; Scanner input = new Scanner(System.in); Account account1 = new Account(5000.0);
while(true) { if(!c) System.out.println(" W (Withdrawal) D (Deposit) B (ShowBalance) Q (Quit) "); str=input.nextLine(); if(str.length()==1) ch=str.charAt(0); else { c=true; continue; } c=false; try{
if(ch =='W' || ch =='w') { System.out.println("Enter amount :"); amt = input.nextFloat(); if(account1.Withdraw(amt)!=-1) System.out.println("Withdrawal success"); else System.out.println("Can't withdraw this amount "+ amt); } else if(ch =='D' || ch =='d') { System.out.println("Enter amount :"); amt = input.nextFloat(); if(account1.Deposit(amt)!=-1) System.out.println("Deposit Success"); else System.out.println("Deposit Failure"); } else if(ch =='B' || ch =='b') { System.out.println(" The balance is " +account1.getBalance()); } else if(ch =='Q' || ch =='q') break; }catch(Exception e) { System.out.println("Invalid intput "); continue; } }
} }
/* * Sammpleoutput W (Withdrawal) D (Deposit) B (Show Balance) Q (Quit)
w Enter amount : 1000 Withdrawing of amount1000.0 Withdrawal success
W (Withdrawal) D (Deposit) B (Show Balance) Q (Quit)
asdfasdf adsfadsf qb b The balance is4000.0
W (Withdrawal) D (Deposit) B (Show Balance) Q (Quit)
q
*/