第十章第七题(游戏:ATM机)(Game: ATM)

第十章第七题(游戏:ATM机)(Game: ATM)

  • **10.7(游戏:ATM机)
    **10.7(Game: ATM)
  • 参考代码:
package chapter10;

import java.util.Date;
import java.util.Scanner;

public class Code_07 {
    public static void main(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) {
                case 1:
                    System.out.println("The balance is "+accounts[id].getBalance());
                    break;
                case 2:
                    System.out.print("Enter an amount to withdraw: ");
                    double withdraw = input.nextDouble();
                    accounts[id].withdraw(withdraw);
                    break;
                case 3:
                    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 static void mainMenu(){
        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 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;
    }

    private Date dateCreated=new Date();

    public void Account() {
    }

    public double getMonthlyInterest() {
        return balance*(annualInterestRate/100/12);
    }

    public void withdraw(double reduce) {
        balance-=reduce;
    }

    public void deposit(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:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值