The purpose of this project is to design a simulation of an automatic teller mac
ID: 3839164 • Letter: T
Question
Explanation / Answer
import java.util.*;
public class Bank {
public static Scanner kbd = new Scanner(System.in);
// The checkID method determines if AccountNum is a valid account number
// and password is the correct password for the account. If the account information
// is valid, the method returns the current account balance, as a string.
// If the account information is invalid, the method returns the string "error".
public static String checkID(String AccountNum, String password)
{
String result = "error";
// Strings a, b, and c contain the valid account numbers and passwords along with balance.
String a = "4456765 Pavan@1 520.36";
String b = "432180 king4ever 96.74";
String c = "123567890 gud14U 2006.74";
//differentiates between accountnum password and balance
if (AccountNum.equals(a.substring(0, a.indexOf(" "))) &&
password.equals(a.substring(a.indexOf(" ")+1,a.lastIndexOf(" "))))
return result = a.substring(a.lastIndexOf(" ") + 1);
if (AccountNum.equals(b.substring(0, b.indexOf(" "))) &&
password.equals(b.substring(b.indexOf(" ")+1,b.lastIndexOf(" "))))
return result = b.substring(b.lastIndexOf(" ") + 1);
if (AccountNum.equals(c.substring(0, c.indexOf(" "))) &&
password.equals(c.substring(c.indexOf(" ") + 1,c.lastIndexOf(" "))))
return result = c.substring(c.lastIndexOf(" ") + 1);
return result;
}
//Logic to implement Menu
public static int menu()
{
int menuChoice;
do
{
System.out.print(" Please Choose From the Following Options:"
+ " 1. Display Balance 2. Deposit"
+ " 3. Withdraw 4. Log Out ");
menuChoice = kbd.nextInt();
if (menuChoice < 1 || menuChoice > 4){
System.out.println("error");
}
}while (menuChoice < 1 || menuChoice > 4);
return menuChoice;
}
//method for displaying balance
public static void displayBalance(double x)
{
System.out.printf(" Your Current Balance is $%.2f ", x);
}
//method to give deposit amount and calculate new balance
public static double deposit(double x, double y)
{
double depositAmt = y, currentBalance = x;
double newBalance = depositAmt + currentBalance;
System.out.printf("Your New Balance is $%.2f ", newBalance);
return newBalance;
}
//method to give withdrawal amount and calculate new balance
public static double withdraw(double x, double y)
{
double withdrawAmt = y, currentBalance = x, newBalance;
newBalance = currentBalance - withdrawAmt;
System.out.printf("Your New Balance is %.2f ",newBalance);
return newBalance;
}
public static void main(String[] args) {
String AccountNum, password, originalBalance = "error";
int count = 0, menuOption = 0;
double depositAmt = 0, withdrawAmt = 0, currentBalance=0;
//loop that will count the number of login attempts
//you make and will exit program if it is more than 3.
//as long as originalBalance equals an error.
do{
System.out.println("Please Enter Your Account Number: ");
AccountNum = kbd.next();
System.out.println("Enter Your Password: ");
password = kbd.next();
originalBalance = checkID(AccountNum, password);
count++;
if (count >= 3 && originalBalance.equals("error")){
System.out.print("Maximum Login Attempts Reached.");
System.exit(0);
}
if (!(originalBalance.equals("error"))){
System.out.println(" Your New Balance is: $ "+ originalBalance);
}
else
System.out.println(originalBalance);
}while(originalBalance.equals("error"));
currentBalance=Double.parseDouble(originalBalance);
//this loop will keep track of the options that the user inputs in for the menu.
//and will give the option of deposit, withdraw, or logout.
while (menuOption != 4)
{
menuOption=menu();
switch (menuOption)
{
case 1:
displayBalance(currentBalance);
break;
case 2:
System.out.print(" Enter Amount You Wish to Deposit: $ ");
depositAmt = kbd.nextDouble();
currentBalance=deposit(depositAmt, currentBalance);
break;
case 3:
System.out.print(" Enter Amount You Wish to Withdrawl: $ ");
withdrawAmt = kbd.nextDouble();
while(withdrawAmt>currentBalance){
System.out.print("ERROR: INSUFFICIENT FUNDS!! "
+ "PLEASE ENTER A DIFFERENT AMOUNT: $");
withdrawAmt = kbd.nextDouble();
}
currentBalance = withdraw(currentBalance, withdrawAmt);
break;
case 4:
System.out.print(" Thank For Using My ATM. Have a Nice Day. Good-Bye!");
System.exit(0);
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.