测试用例:
//Enter a id:
100
Enter a correct id:
4
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice:
1
The balance is 100.0
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice:
2
Enter an amount to withdraw:
3
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice:
1
The balance is 97.0
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice:
3
Enter an amount to deposit:
10
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice:
1
The balance is 107.0
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice:
4
Enter a id:
//
import java.util.Date; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); Account[] account = new Account[10]; int id = -1; boolean flag = true; for (int i = 0; i < account.length; i++) { account[i] = new Account(i,100); } do{ if(flag){ System.out.println("Enter a id: "); id = input.nextInt(); while(id < 0 || id >9 ) { System.out.println("Enter a correct id: "); id = input.nextInt(); } } System.out.println(); printMenu(); System.out.println("Enter a choice:"); int choice = input.nextInt(); switch (choice){ case 1: flag = false; System.out.println("The balance is "+account[id].balance); break; case 2: flag = false; System.out.println("Enter an amount to withdraw:"); double withDraw = input.nextDouble(); account[id].withDraw(withDraw); break; case 3: flag = false; System.out.println("Enter an amount to deposit:"); double deposit = input.nextDouble(); account[id].deposit(deposit); break; case 4:flag = true;break; default: System.out.println("error input!"); } }while(true); } public static void printMenu(){ System.out.println("Main menu"); System.out.println("1: check balance"); System.out.println("2: withdraw"); System.out.println("3: deposit"); System.out.println("4: exit"); } public static void choiceMenu(int choice){ } } // class Account { int id = 0; double balance = 0; double annualInterestRate = 0; Date dateCreated; public Date getDateCreated() { return dateCreated; } public Account(){ } public Account(int id,double balance){ this.id = id; this.balance = balance; } public int getId() { return id; } public void setId(int id) { this.id = id; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public double getAnnualInterestRate() { return annualInterestRate; } public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; } public double getMonthlyInterestRate(){ return annualInterestRate / 100 / 12; } public double getMonthlyInterest(){ return balance * getMonthlyInterestRate(); } public void withDraw(double a){ this.balance -= a; } public void deposit(double a){ this.balance += a; } }