Java 类Account模拟ATM机

Account.java

package account;
import java.util.Date;

public class Account 
{
	//dataField
	private int id = 0;
	private double balance;
	private double annualInterestRate;
	Date dateCreated;
	//constructor
	Account()
	{
		dateCreated = new Date();
	}
	Account(int id, double balance)
	{
		this.id = id;
		this.balance = balance;
		dateCreated = new Date();
	}
	//accessor
	int getId()
	{
		return id;
	}
	double getBalance()
	{
		return balance;
	}
	double getARate()
	{
		return annualInterestRate;
	}
	Date getCreatedDate()
	{
		return dateCreated;
	}
	//mutator
	void setId(int id)
	{
		this.id = id;
	}
	void setBalance(double ba)
	{
		this.balance = ba;
	}
	void setARate(double rate)
	{
		this.annualInterestRate = rate;
	}
	//method
	double getMonthlyInterestRate()
	{
		return this.annualInterestRate/12;
	}
	void withDraw(double money)
	{
		if(money > balance)
			System.out.println("Your balance is not enough!");
		else
			this.balance -= money; 
	}
	void deposite(double money)
	{
		this.balance += money;
	}
	public static void main(String[] args)
	{
		Account a = new Account(1122, 20000);
		a.setARate(4.5/100);
		a.withDraw(2500);
		a.deposite(3000);
		
		System.out.println("余额 " + a.getBalance());
		System.out.println("月利息 " + a.getMonthlyInterestRate());
		System.out.println("开户日期 " + a.getCreatedDate());
	}
}

ATM.java

package account;
import java.util.Scanner;

public class ATM 
{
	public static void main(String args[])
	{
		//construct 10 initial account
		Account[] atm = new Account[10];
		for(int i = 0; i< atm.length ; i++)
		{
			atm[i] = new Account((i+100),100);
		}
		
		//ATM机启动
		int id = 0;
		int choice = 0;
		Scanner input = new Scanner(System.in);
		while(true)
		{
			//输入账户
			while(!correct(id))
			{
				System.out.print("Plese enter a right ID:");
				id = input.nextInt();
				System.out.println();
			}
			//System.out.println("yes!");
			System.out.println("------------------------------");
			System.out.println("Main menu\r\n1:check balance");
			System.out.println("2:withdraw");
			System.out.println("3:deposite");
			System.out.println("4:exit");
			System.out.println("------------------------------");
			choice = input.nextInt();
			if(choice == 1)
			{
				System.out.println("Balance:" + atm[id - 100].getBalance());
			}
			else if(choice == 2)
			{
				System.out.println("Enter the money you want to withdraw");
				atm[id - 100].withDraw(input.nextDouble());
			}
			else if(choice == 3)
			{
				System.out.println("Enter the money you want to deposite");
				atm[id - 100].deposite(input.nextDouble());
			}
			else if(choice ==4)
			{
				id = 0;//id错误,重新输入id
			}
		}
	}
	
	public static boolean correct(int a)
	{
		return (a >= 100) && (a <= 109);
	}
}

 

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 账户(满分50分) 版本1:满分10 分 设计Account1 ,包含: ■ 一个名为 id 的int 型的私有数据域(默认值为 0),长度为 6 位。 ■ 一个名为 balance的double 型的私有数据域(默认值为 0)。 ■ 一个名为 annualInterestRate 的double 型的私有数据域存储当前利率(默认值为 0)。 假设所有的账户都有相同的利率。 ■ 一个名为 dateCreated 的Date 型的私有数据域存储账户的开户日期。 ■ 一个能创建默认账户的无参构造方法。 ■ 一个能创建带特定 id 和初始余额的构造方法,初始余额不能为负数。 ■ id 、balance和annualInterestRate 的访问器和修改器。 ■ dateCreated 的访问器。 ■ 一个名为 getMonthlyInterestRate 的方法返回月利率。 ■ 一个名为 withDraw 的方法从账户提取特定金额。 ■ 一个名为 deposit 的方法向账户存人特定金额。 ■ double 型的数据域保留 2 位小数。 ■ 成员方法和数据域应进行基本的合理性检查。 设计测试 ATMMachine1: ■ 创建一个有 100 个账户的数组,其 id 为0,1,2,...99, 并初始化收支为 1000 美元。 ■ 主菜单如下(可参考教材中文版 P296或英文版 P367): Main menu 1: check balance 2: withdraw 3: deposit 版本2:满分20 分 扩展Account1 Account2 : ■ Account2 继承 Account1 。 ■ 为Account2 新增一个名为 password 的String 型的私有数据域存储账号密码。 password 只能为字母或数字,长度不能小于 6 且不能大于 10。密码显示时为*******。 ■ 为Account2 新增一个名为 name的String 型的私有数据域存储客户名字。 ■ 为Account2 新增一个名为 transactions 的ArrayList 型的新数据域,其为客户存 储交易记录。这要求新建一个名为 Transaction 的的定义请参照教材中文版 P327或英 文版P404。每笔交易都是 Transaction 的一个实例。 ■ 新增一个带初始余额的构造方法,其 id 随产生,但不能与当前系统的 id 重复。 若初始余额的参数为负数,则抛出一个自定义异常并在当前构造方法中进行处理。 ■ 重写方法 withDraw ,要求支取的金额为 100 的整数倍,并且当日支取金额不能超过 5000,支取金额不允许透支。每进行一次操作应向 transactions 数组线性表添加一笔交易。 ■ 重写方法 deposit ,要求每进行一次操作应向 transactions 数组线性表添加一笔交易。 ■ 新增一个方法 changePassword ,只有旧密码正确,新密码符合要求,且两次输入相 同的情况下才可以成功修改密码 设计测试 ATMMachine2,其主菜单如下(可参考教材中文版 P296或英文版 P367): Main menu 0:create a account 1: check balance 2: withdraw 3: deposit 4:details of the transaction 5: change password 6:exit ■ 若用户选择新建一个账号,则应提示用户输入账号 password 、balance 和 annualInterestRate ,其中 id 随产生。新产生的账户应序列化到名为 accounts.dat 的文件中。 所有账户只能通过这种方式产生。 ■ 所有用户操作结果应同步到 accounts.dat 文件中相应账户中。 ■ 所有用户操作应有友好、简介的提示语。 版本3:满分20 分 请参照银行的 AT M界面,在 Account2 的基础上开发一个 GUI 界面的AT M系统。 要求界面应模拟小键盘,并且账户信息读、写于文件 accounts.dat 。
好的,那我来回答你的问题。 首先,我们需要定义ATM的功能,比如取款、查询余额、转账等。然后,我们可以设计一个ATM,在该中实现这些功能。 以下是ATM的一个简单实现: ```java import java.util.Scanner; public class ATM { private int balance; // 余额 private int password; // 密码 public ATM(int balance, int password) { this.balance = balance; this.password = password; } /** * 取款 */ public void withdraw() { Scanner scanner = new Scanner(System.in); System.out.println("请输入密码:"); int pwd = scanner.nextInt(); if (pwd != password) { System.out.println("密码错误!"); return; } System.out.println("请输入取款金额:"); int amount = scanner.nextInt(); if (amount <= 0) { System.out.println("取款金额必须大于0!"); return; } if (amount > balance) { System.out.println("余额不足!"); return; } balance -= amount; System.out.println("取款成功,余额为:" + balance); } /** * 查询余额 */ public void checkBalance() { System.out.println("当前余额为:" + balance); } /** * 转账 */ public void transfer() { Scanner scanner = new Scanner(System.in); System.out.println("请输入密码:"); int pwd = scanner.nextInt(); if (pwd != password) { System.out.println("密码错误!"); return; } System.out.println("请输入转账金额:"); int amount = scanner.nextInt(); if (amount <= 0) { System.out.println("转账金额必须大于0!"); return; } if (amount > balance) { System.out.println("余额不足!"); return; } System.out.println("请输入对方账户:"); String account = scanner.next(); System.out.println("转账成功,余额为:" + (balance - amount)); } public static void main(String[] args) { ATM atm = new ATM(1000, 123456); Scanner scanner = new Scanner(System.in); while (true) { System.out.println("请选择功能:1.取款 2.查询余额 3.转账 4.退出"); int choice = scanner.nextInt(); switch (choice) { case 1: atm.withdraw(); break; case 2: atm.checkBalance(); break; case 3: atm.transfer(); break; case 4: System.exit(0); break; default: System.out.println("输入有误,请重新输入!"); break; } } } } ``` 在上面的代码中,我们定义了ATM,该有三个功能:取款、查询余额、转账。我们在main方法中实现了ATM的交互界面,用户可以选择不同的功能来操作ATM。 当用户选择取款功能时,ATM会要求用户输入密码和取款金额,然后检查密码是否正确、取款金额是否大于0和余额是否足够,如果都符合要求,则进行取款操作,并更新余额。 当用户选择查询余额功能时,ATM会直接输出当前余额。 当用户选择转账功能时,ATM会要求用户输入密码、转账金额和对方账户,然后检查密码是否正确、转账金额是否大于0和余额是否足够,如果都符合要求,则进行转账操作,并更新余额。 以上就是一个简单的ATM模拟实现,你可以根据需要进行修改和完善。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值