Introduction to Java Programming编程题10.7<ATM>

/*
Enter an id: 4
Invalid value.

Enter an id: 978

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: 33

Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1
The balance is: 67.0

Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 3
Enter an amount to deposit: 100

Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1
The balance is: 167.0

Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 4

Enter an id:
 */

// ATM.java

package Uber; 
import java.util.Scanner;

public class ATM {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Account account = new Account();
        int number = 0;

        while (true) {
            System.out.print("Enter an id: ");
            String id = input.next();
            while (account.checkId(id)) {
                showMenu();
                number = input.nextInt();
                if (number == 1)
                    System.out.println("The balance is: " + account.getBalance() + "\n");
                else if (number == 2) {
                    System.out.print("Enter an amount to withdraw: ");
                    if (!account.withdraw(input.nextInt()))
                        System.out.println("Don't have enough monney in your account.\n");
                    else
                        System.out.println();
                }
                else if (number == 3) {
                    System.out.print("Enter an amount to deposit: ");
                    account.deposit(input.nextInt());
                }
                else if (number == 4)
                    break;
            }
            if (number != 4)
                System.out.println("Invalid value.\n");
        }
    }

    public static void showMenu() {
        System.out.print("Main menu\n1: check balance\n2: withdraw\n3: deposit\n4: exit\nEnter a choice: ");
    }
}

// Account.java

package Uber;

public class Account {
    private String id;
    private double balance;
    private double withdraw;
    private double deposit;

    public Account() {
        id = "978332";
        balance = 100;
    }

    public boolean checkId(String id) {
        if (this.id.equals(id))
            return true;
        else
            return false;
    }

    public boolean withdraw(int amount) {
        if (balance < amount)
            return false;
        else {
            balance -= amount;
            return true;
        }
    }

    public double getBalance() {
        return balance;
    }

    public void deposit(int amount) {
        balance += amount;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值