Explore the English language on a new scale using AI-powered English language navigator.
Practice. Menu-driven "bank account" applicationIn current practice lesson we are going to develop a menu-driven application to manage simple bank account. It supports following operations:
Application is driven by a text menu. SkeletonFirst of all, let's create an application to run infinitely and ask user for a choice, until quit option is chosen: import java.util.Scanner;
public class BankAccount { public static void main(String[] args) { Scanner in = new Scanner(System.in); int userChoice; boolean quit = false; do { System.out.print("Your choice, 0 to quit: "); userChoice = in.nextInt(); if (userChoice == 0) quit = true; } while (!quit); } } Your choice, 0 to quit: 2 Your choice, 0 to quit: 6 Your choice, 0 to quit: 0 Bye! Draw your attention to boolean variable quit, which is responsible for correct loop interruption. What a reason to declare additional variable, if one can check exit condition right in while statement? It's the matter of good programming style. Later you'll see why. MenuNow let's add a menu and empty menu handlers using switch statement: import java.util.Scanner;
public class BankAccount { public static void main(String[] args) { Scanner in = new Scanner(System.in); int userChoice; boolean quit = false; do { System.out.println("1. Deposit money"); System.out.println("2. Withdraw money"); System.out.println("3. Check balance"); System.out.print("Your choice, 0 to quit: "); userChoice = in.nextInt(); switch (userChoice) { case 1: // deposit money break; case 2: // withdraw money break; case 3: // check balance break; case 0: quit = true; break; default: System.out.println("Wrong choice."); break; } System.out.println(); } while (!quit); System.out.println("Bye!"); } } 1. Deposit money 2. Withdraw money 3. Check balance Your choice, 0 to quit: 1
1. Deposit money 2. Withdraw money 3. Check balance Your choice, 0 to quit: 4 Wrong choice.
1. Deposit money 2. Withdraw money 3. Check balance Your choice, 0 to quit: 0
Bye! Bank account's functionalityIt's time to add principal functionality to the program:
import java.util.Scanner;
public class BankAccount { public static void main(String[] args) { Scanner in = new Scanner(System.in); int userChoice; boolean quit = false; float balance = 0f; do { System.out.println("1. Deposit money"); System.out.println("2. Withdraw money"); System.out.println("3. Check balance"); System.out.print("Your choice, 0 to quit: "); userChoice = in.nextInt(); switch (userChoice) { case 1: float amount; System.out.print("Amount to deposit: "); amount = in.nextFloat(); balance += amount; break; case 2: System.out.print("Amount to withdraw: "); amount = in.nextFloat(); balance -= amount; break; case 3: System.out.println("Your balance: $" + balance); break; case 0: quit = true; break; default: System.out.println("Wrong choice."); break; } System.out.println(); } while (!quit); System.out.println("Bye!"); } } 1. Deposit money 2. Withdraw money 3. Check balance Your choice, 0 to quit: 1 Amount to deposit: 100
1. Deposit money 2. Withdraw money 3. Check balance Your choice, 0 to quit: 2 Amount to withdraw: 50
1. Deposit money 2. Withdraw money 3. Check balance Your choice, 0 to quit: 3 Your balance: $50.0
1. Deposit money 2. Withdraw money 3. Check balance Your choice, 0 to quit: 0
Bye! Safety checksAt the moment program allows to withdraw more than actual balance, deposit and withdraw negative amounts of money. Let's fix it, using if statement and conditions combinations. import java.util.Scanner;
public class BankAccount { public static void main(String[] args) { Scanner in = new Scanner(System.in); int userChoice; boolean quit = false; float balance = 0f; do { System.out.println("1. Deposit money"); System.out.println("2. Withdraw money"); System.out.println("3. Check balance"); System.out.print("Your choice, 0 to quit: "); userChoice = in.nextInt(); switch (userChoice) { case 1: float amount; System.out.print("Amount to deposit: "); amount = in.nextFloat(); if (amount <= 0) System.out.println("Can't deposit nonpositive amount."); else { balance += amount; System.out.println("$" + amount + " has been deposited."); } break; case 2: System.out.print("Amount to withdraw: "); amount = in.nextFloat(); if (amount <= 0 || amount > balance) System.out.println("Withdrawal can't be completed."); else { balance -= amount; System.out.println("$" + amount + " has been withdrawn."); } break; case 3: System.out.println("Your balance: $" + balance); break; case 0: quit = true; break; default: System.out.println("Wrong choice."); break; } System.out.println(); } while (!quit); System.out.println("Bye!"); } } 1. Deposit money 2. Withdraw money 3. Check balance Your choice, 0 to quit: 1 Amount to deposit: -45 Can't deposit nonpositive amount.
1. Deposit money 2. Withdraw money 3. Check balance Your choice, 0 to quit: 2 Amount to withdraw: 45 Withdrawal can't be completed.
1. Deposit money 2. Withdraw money 3. Check balance Your choice, 0 to quit: 2 Amount to withdraw: -45 Withdrawal can't be completed.
1. Deposit money 2. Withdraw money 3. Check balance Your choice, 0 to quit: 0
Bye!
Final sourceProgram is complete. Below you can find final source code. import java.util.Scanner;
public class BankAccount { public static void main(String[] args) { Scanner in = new Scanner(System.in); int userChoice; boolean quit = false; float balance = 0f; do { System.out.println("1. Deposit money"); System.out.println("2. Withdraw money"); System.out.println("3. Check balance"); System.out.print("Your choice, 0 to quit: "); userChoice = in.nextInt(); switch (userChoice) { case 1: float amount; System.out.print("Amount to deposit: "); amount = in.nextFloat(); if (amount <= 0) System.out.println("Can't deposit nonpositive amount."); else { balance += amount; System.out.println("$" + amount + " has been deposited."); } break; case 2: System.out.print("Amount to withdraw: "); amount = in.nextFloat(); if (amount <= 0 || amount > balance) System.out.println("Withdrawal can't be completed."); else { balance -= amount; System.out.println("$" + amount + " has been withdrawn."); } break; case 3: System.out.println("Your balance: $" + balance); break; case 0: quit = true; break; default: System.out.println("Wrong choice."); break; } System.out.println(); } while (!quit); System.out.println("Bye!"); } } |