Solution Review: Design an ATM
Have a look at the solution to the 'Design an ATM' challenge.
We'll cover the following...
Rubric criteria
Solution
Press + to interact
class ATMmachine{public static void main (String args[]){ATM obj = new ATM("0-2281291291-21", "29wjsw2", 249000);// Withdrawing the moneySystem.out.println( obj.withdrawal("29wjsw2", "34000"));}}class ATM // Header of the class{//Private fieldsprivate String accNo;private String accPassword;private double balance;// The parameterized constructorpublic ATM(String AccNo, String Password, double Balance){accNo = AccNo;accPassword = Password;balance = Balance;}// Write your function withdrawal herepublic String withdrawal(String password, String cash){if (accPassword.equals(password)) // First checking the passowrd{/* Step 1: If cash is null; no further checks and return error messageStep 2: If cash doesn't have all numbers; no further checks and return error messageStep 3: If cash is greater than balance; no further checks and return error messageStep 4: If cash is greater than 50,000; return error message*/if (cash != null && cash.matches("[-+]?\\d*\\.?\\d+") && Double.parseDouble(cash) <= this.balance && Double.parseDouble(cash) <= 50000){balance -= Double.parseDouble(cash); // Deducting balancereturn "Withdrawal is successful. Your current balance is $" + balance;}else // error message{return "Please enter the correct amount.";}}else // If password wasn't correct{return "Wrong Password!";}}}
Rubric-wise explanation
- At line 12, we declare the header of the
ATM
class. The class ispublic
.
Point 1: