Computer Science, asked by nutansgharate, 6 months ago

write bank menudriven java program

Answers

Answered by kejal1
1

Answer:

Practice. Menu-driven "bank account" application

In current practice lesson we are going to develop a menu-driven application to manage simple bank account. It supports following operations:

deposit money;

withdraw money;

check balance.

Application is driven by a text menu.

Skeleton

First 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.

Menu

Now 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!

Similar questions