JAVA版ATM(自动取款机)的实现

import java.util.Scanner;

class Account {
	private int[] id = new int[10];
	private static double balance = 100;

	Account(double balance) {
		balance = 100;
	}

	public void getbalance() {
		System.out.println(balance);
	}

	public void withdraw(double withdraw) {
		System.out.println("your balance is " + (balance - withdraw));
	}

	public void deposit(double deposit) {
		System.out.println("your balance is " + (balance + deposit));
	}

	public void menu() {
		System.out.print("main menu\n" + "1: check balance\n" + ""
				+ "2: withdraw\n" + "3: deposit\n" + "4: exit");
	}

	public void setId(int[] id) {
		this.id = id;
	}

	public int[] getId() {
		return id;
	}
}

public class ATM_machine {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		do {
			System.out.println("input your ID !");
			int id = input.nextInt();
			double money = 0;
			Account account = new Account(id);
			System.out.println("choice !");
			account.menu();
			int choice = input.nextInt();
			if (choice == 2 || choice == 3) {
				System.out.println("input the number of money !");
				money = input.nextDouble();
			}
			switch (choice) {
			case 1:
				account.getbalance();
				break;
			case 2:
				account.withdraw(money);
				break;
			case 3:
				account.deposit(money);
				break;
			case 4:
				account.menu();
				break;
			}
		} while (true);
	}
}


运行结果:

input your ID !
4
choice !
main menu
1: check balance
2: withdraw
3: deposit
4: exit

3
input the number of money !
10
your balance is 110.0
input your ID !

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值