tcd9 tcd9 - 7 days ago 4
Java Question

Java ATM Program

I'm doing an atm program and I'm having a hard time trying to figure out how to get it to actually deposit and withdraw. The balance starts off automatically as $0 but I can't get anything I type in to actually add on or subtract to it what am I doing wrong?

public class ATM {
static Scanner keyboard = new Scanner(System.in);
static String acctNum, pwd, result;
static double oldBalance, newBalance, deposit, withdraw;
static int choose;

public static void main(String[] args) {
for (int run = 0; run < 3; run++) {
System.out.println("Enter your account number");
acctNum = keyboard.nextLine();
System.out.println("Enter your account password");
pwd = keyboard.nextLine();

result = checkID(acctNum, pwd);
if (!result.equals("ERROR")) {
break;
} else if (run == 2) {// you cannot try to log in anymore than 3
// times
System.out.println("MAXIMUM TRIES EXCEEDED");
return;
}

}
menu();
}

public static String checkID(String acctNum, Object pwd) {
String result = "ERROR";
String a = "44567-5 mypassword 520.36";
String b = "1234567-6 anotherpassword 48.20";
String c = "4321-0 betterpassword 96.74";

if (acctNum.equals("44567-5") && pwd.equals("mypassword")) {
result = "520.36";
} else if (acctNum.equals("1234567-6") && pwd.equals("anotherpassword")) {
result = "48.20";
} else if (acctNum.equals("4321-0") && pwd.equals("betterpassword")) {
result = "96.74";
}
System.out.println(result);
return result;
}

public static int menu() {
System.out
.println("Choose one of the following: \n1.Display Balance\n2.Deposit\n3.Withdraw\n4.Log Out");
choose = keyboard.nextInt();

if (choose == 1) {// 1. Display Balance
displayBalance();
menu();
return 1;

}
if (choose == 2) {// 2. Deposit
deposit();
menu();
return 2;

}
if (choose == 3) {// 3. Withdraw
withdraw();
menu();
return 3;

}
if (choose == 4) {// 4. Log out
System.out.println("You are logged out.");
return 4;

}
if (choose <= 5) {// type in anything greater than 4 and you will get a
// system error
System.out.println("System Error");
menu();
return 5;
}
if (choose >= 1) {// type in anything less than 1 and you will get a
// system error
System.out.println("System Error");
menu();
return 6;
}
return choose;

}

public static double deposit() {
System.out.println("How much would you like to deposit?");
deposit = keyboard.nextInt();
System.out.println((deposit + oldBalance)==newBalance);// deposit money into balance
return 2;
}

public static double displayBalance() {
System.out.println("Total balance is: $" + oldBalance);
oldBalance = 0.00;
return 1;
}

public static double withdraw() {
System.out.println("How much would you like to withdraw?");
withdraw = keyboard.nextInt();
System.out.println(newBalance + withdraw);// withdraw money from balance
return 3;
}


}

Answer

A few observations:

  1. Your class should not declare oldBalance, deposit, and withdrawal as global variables. The withdrawal method should not have access to the deposit variable and vice versa. It is a good programming practice to limit the scope of your variables as much as you can. Because a "deposit" variable is not really required outside this method, there is absolutely no reason to make it a global variable. The same cannot be said about "current balance." This variable is needed by all methods of the class: deposit, withdrawal, displayBalance, etc. Therefore, there is a good reason why to make it global (Still, if you can solve the problem with less scope, you should do that).

  2. I do not see why deposit() and withdrawal() need to return anything.

The simplest implementation of deposit() and withdrawal() is to simply add the user input to the currentBalance and save it in currentBalance.

public void deposit()
{
   Scanner input = new Scanner(System.in);
   System.out.printn("Enter deposit amount: );
   double amount = input.nextDouble();
   System.out.println("Your deposit amount: " + amount);
   currentBalance += amount;
   System.out.println("Your new balance is: + currentBalance);
}

public void withdrawal()
{
   Scanner input = new Scanner(System.in);
   System.out.printn("Enter withdrawal amount: );
   double amount = input.nextDouble();
   System.out.println("Your withdrawal amount: " + amount);
   currentBalance -= amount;
   System.out.println("Your new balance is: + currentBalance);
}

You are never saving the current balance. The == operator is a comparison operator, not an assignment operator. When you do something like this:

System.out.println((deposit + oldBalance)==newBalance);

Or like this:

System.out.println(newBalance + withdraw);

All you are doing is to pass the values to the println() method and not updating the variable that is supposed to hold the balance. In fact, the first one should print false (unless both deposit and oldBalance are zero). If you look at my code example, currentBalance += amount; is shorthand for currentBalance = currentBalance + amount; which means "add this amount to currentBalance and store it in currentBalance. Because I am reusing currentBalance, I eliminated the need for an oldBalance variable.

Comments