游戏:ATM机——————刚学java的新人写代码

(游戏:ATM机)使用编程练习题 9.7 中创建的 Account 类来模拟一台 ATM 机。创建一个有 10 个账户的数组,其id为 0,1,…, 9,并初始化收支为 100美元。系统提示用户输人一个id。 如果输人的id不正确,就要求用户输人正确的 id。一旦接受一个id, 就显示如运行示例所示 的主菜单。可以选择1来査看当前的收支,选择 2 表示取钱,选择 3 表示存钱,选择 4 表示退 出主菜单。一旦退出,系统就会提示再次输入 id。所以,系统一旦启动就不会停止。

——————————————方法块:


import java.util.*;

/**
 * 
 */
public class Account {

    /**
     * Default constructor
     */
    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() {
        // TODO implement here
    }

    /**
     * @return
     */
    public double getMonthlyInterest() {
        // TODO implement here
        return balance*(annualInterestRate/100/12);
    }

    /**
     * @return
     */
    public void withdraw(double reduce) {
        // TODO implement here
        balance-=reduce;
    }

    /**
     * @return
     */
    public void deposit(double increase) {
        // TODO implement here
        balance+=increase;
    }

    public Date getDateCreated() {
        return dateCreated;
    }
}

————————————————主函数块:

import java.util.Scanner;

public class Exercise10_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;
        }
        Exercise10_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: ");
    }
}

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值