Omkar - 9 months ago 37
Java Question

# Block of code getting ignored in Do Statement

I am making a bank machine project where you can deposit, withdraw and log off the bank. I am using a do statement for the menu so that the user can return to it any time. For the options I am using if statements but depending on whatever option I put first it will ignore the other ones. For example I put the log out option right after the menu and the withdraw option right after that but when I try to withdraw it terminates the program. When I flip the options around it will do whatever is right after the menu and ignore the rest.

    do {
System.out.println("|----------------------------------------|");
System.out.println("|        Welcome Mr. " + name + "        |"); // Fix
System.out.println("|    Thank you for choosing Maze Bank    |");
System.out.println("      Your current balance is $" + bal + " "); // Fix System.out.println("| Press [4] to deposit money |"); System.out.println("| Press [3] to withdraw money |"); System.out.println("| Press [2] to log off |"); System.out.println("|----------------------------------------|"); option = input.nextInt(); if (option == 2) { System.out.println("Logging out..."); TimeUnit.SECONDS.sleep(2); // Exit message System.out.println("Thank you for choosing Maze Bank!"); System.out.println("We hope to see you again!"); System.exit(0); if (option == 3 && bal <= 0) { System.out.println("You don't have any money to withdraw"); } System.out.println("Press [1] to return to the home screen"); option = input.nextInt(); if (option == 3 && bal > 0) { System.out.println("Loading..."); TimeUnit.SECONDS.sleep(1); System.out.println(".........."); TimeUnit.SECONDS.sleep(1); System.out.println("How much money would you like to withdraw?"); int withdraw = input.nextInt(); if (withdraw > bal) { System.out.println("Error, you cannot withdraw more than$" + bal);
withdraw = input.nextInt();
if (withdraw > bal) {
System.out.println("Error, you cannot withdraw more than $" + bal); } withdraw = input.nextInt(); if (withdraw > bal) { System.out.println("Error, you cannot withdraw more than$" + bal);
}
withdraw = input.nextInt();
}
}
}
} while (option == 1);


I think you should use the autoformat-function of your dev-tool. You are missing some codeparts and a flowchart would help you to understand, what should happen.....

Here is a simple example of your "bank machine", but I think you should read more about pseudocode and structural analysis.

package stackoverflow;

import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

public class testWhile {

public static void main (String [] args) {
String name = "";
Integer bal = 0;
Integer option = 0;
Scanner input = new Scanner(System.in);
try {
do {
System.out.println("|----------------------------------------|");
System.out.println("|        Welcome Mr. " + name + "        |"); // Fix
System.out.println("|    Thank you for choosing Maze Bank    |");
System.out.println("      Your current balance is $" + bal + " "); // Fix System.out.println("| Press [4] to deposit money |"); System.out.println("| Press [3] to withdraw money |"); System.out.println("| Press [2] to log off |"); System.out.println("|----------------------------------------|"); option = input.nextInt(); if (option == 2) { System.out.println("Logging out..."); TimeUnit.SECONDS.sleep(2); // Exit message System.out.println("Thank you for choosing Maze Bank!"); System.out.println("We hope to see you again!"); System.exit(0); } if (option == 3 && bal <= 0) { System.out.println("You don't have any money to withdraw"); System.out.println("Press [1] to return to the home screen"); option = input.nextInt(); } if (option == 3 && bal > 0) { System.out.println("Loading..."); TimeUnit.SECONDS.sleep(1); System.out.println(".........."); TimeUnit.SECONDS.sleep(1); System.out.println("How much money would you like to withdraw?"); Integer withdraw = input.nextInt(); while (withdraw > bal) { System.out.println("Error, you cannot withdraw more than$" + bal);
withdraw = input.nextInt();
}
System.out.println("You withdrawed \$" + withdraw.toString() + ". Thanks.");
}
if (option == 4) {
System.out.println("How much money would you like to deposit?");
int deposit = input.nextInt();
bal += deposit;
}
} while (true);
} catch (InterruptedException ex) {
Logger.getLogger(testWhile.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
}

}
}