package chapter10;
import java.util.Date;
import java.util.Scanner;
public class Code_07 {
public staticvoidmain(String[] args){
Account[] accounts = new Account[10];for(int i =0; i <10; i++)
accounts[i]= new Account(1,100);
System.out.print("Enter an id:");
Scanner input = new Scanner(System.in);int id = input.nextInt();while(id <0|| id >9){
System.out.print("The if is nonExistent,please input again:");
id = input.nextInt();}mainMenu();int choice = input.nextInt();
boolean judge = choice ==1|| choice ==2|| choice ==3;while(judge){switch(choice){case1:
System.out.println("The balance is "+accounts[id].getBalance());break;case2:
System.out.print("Enter an amount to withdraw: ");double withdraw = input.nextDouble();
accounts[id].withdraw(withdraw);break;case3:
System.out.print("Enter an amount to deposit:");double deposit=input.nextDouble();
accounts[id].deposit(deposit);break;}mainMenu();
choice=input.nextInt();
judge = choice ==1|| choice ==2|| choice ==3;}
Code_07.main(args);}
public staticvoidmainMenu(){
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 ");
System.out.print("Enter a choice: ");}}
class Account {
public Account(){
dateCreated = new Date();}
public Account(int id,double balance){
this.id = id;
this.balance = balance;
dateCreated = new Date();}
private int id;
private double balance;
private double annualInterestRate;
public intgetId(){return id;}
public voidsetId(int id){
this.id = id;}
public doublegetBalance(){return balance;}
public voidsetBalance(double balance){
this.balance = balance;}
public doublegetAnnualInterestRate(){return annualInterestRate;}
public voidsetAnnualInterestRate(double annualInterestRate){
this.annualInterestRate = annualInterestRate;}
private Date dateCreated=new Date();
public voidAccount(){}
public doublegetMonthlyInterest(){return balance*(annualInterestRate/100/12);}
public voidwithdraw(double reduce){
balance-=reduce;}
public voiddeposit(double increase){
balance+=increase;}
public Date getDateCreated(){return dateCreated;}}
结果显示:
Enter an 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 an id: