java ATM(游戏)

测试用例:

//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;
    }


}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值