Java创建Accont类,模拟的ATM机

介绍

Account类,模拟一台ATM机。创建一个有10个账户的数组,其id为0,1,…,9,并初始化收支为100元人民币,系统提示用户输入一个id,如果输入的id不正确,提示用户输入正确的id,一旦接受一个id,就显示下图所示的主菜单,可以选择1来查看当前的收支,选择2表示取钱,选择3表示存钱,选择4表示查看操作信息 (每笔交易都是一个Transaction类的实例,新数据域transactions,它的类型是ArrayList,可以为账户存储交易,每笔交易都是一个Transaction类的实例)选择5退出。退出主菜单。

运行效果

在这里插入图片描述
在这里插入图片描述

1.Account类

package src;
import java.util.ArrayList;
import java.util.Date;

public class Account {
    private int id = 0;
    private String name;
    private double balance = 0.0;
    private double annualInterestRate = 0.0;
    private Date dateCreate;
	private ArrayList<Transation> transation;

//构造方法
    public Account(){
        this.dateCreate = new Date();
        this.transation = new ArrayList<Transation>();
    };
    public Account(int id,double balance){
        this.id = id;
        this.balance = balance;
        this.transation = new ArrayList<Transation>();
    }

    public Account(int id,double balance,String name){
        this.dateCreate = new Date();
        this.id = id;
        this.balance = balance;
        this.name = name;
        this.transation = new ArrayList<Transation>();
    }

//访问器和修改器

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

    public int getId(){
        return id;
    }

    public void setBalance(double balance){
        this.balance = balance;
    }

    public double getBalance(){
        return balance;
    }

    public void setAnnualInterestRate(double annualInterestRate){
        this.annualInterestRate = annualInterestRate;
    }
    
    public double getAnnualInterestRate(){
        return annualInterestRate;
    }

    public Date getDateCreated(){
        return dateCreate;
    }
    public void setName(String name){
		this.name = name;
	}
	public String getName()
	{
		return name;
	}
	
	public ArrayList<Transation> getTransation()
	{
		return transation;
	}
	
//月利息

    public double getMonthInterestRate(){
        return this.balance * this.annualInterestRate/12;
    }

//取款

    public void withDraw(double money){
        this.balance -= money; 
        Transation tempT = new Transation('W', money, this.getBalance(), " 取钱 ");
		transation.add(tempT);
    }

//存款

    public void deposite(double money){
        this.balance += money;
		Transation tempT = new Transation('D', money, this.getBalance(), " 存钱 ");
		transation.add(tempT);
    }
}

2.Transation类

package src;

import java.util.Date;
 
public class Transation
{
	
	private Date date;
	private char type;
	private double amount;
	private double balance;
	private String description;
	
	public Transation()
	{
		date = new Date();
		description = new String();
	}
	
	public Transation(char type, double amount, double balance, String description)
	{
		this.date = new Date();
		this.type = type;
		this.amount = amount;
		this.balance =  balance;
		this.description = description;
	}
	
	public String toString()
	{
		return "交易类型" + type + " : " + description + amount + " balance " + balance + " 交易时间 " + date.toString();
	}
}

2.Test类(主函数在Test)

package src;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Account[] account = new Account[10];  //新建10个账户
        for(int i = 0; i < 10; i ++ ){          
            account[i] = new Account(i,100);
        }
        System.out.print("Enter an id: ");
        int id = sc.nextInt();
        while(true){
            if(id >=0 && id < 10) break;
            else{
                System.out.print("Id error\nEnter an id: ");
                id = sc.nextInt();
            }
        }
        while(true){
            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: Transation");
            System.out.println("5: exit");
            System.out.print("Enter a choice: ");
            int choice = sc.nextInt();
            if(choice == 5) break;
            switch(choice){
                case 1:{
                    System.out.println("The balance is " + account[id].getBalance());
                }break;
                
                case 2:{
                    System.out.print("Enter an amount to withdraw: ");
                    double money = sc.nextInt();
                    if (money > account[id].getBalance()){
                        System.out.println("The amount is too large, ignored");
                    }
                    else account[id].withDraw(money);
                }break;
                case 3:{
                    System.out.print("Enter an amount to deposit: ");
                    double money = sc.nextDouble();
                    account[id].deposite(money);
                }break;
                case 4:{
                    System.out.println("交易记录:");
                    for(int j = 0; j < account[id].getTransation().size(); j++)
                    {
                        System.out.println(account[id].getTransation().get(j).toString());
                    }
                }
                default:break;
            }
        }
        sc.close();
    }
}
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 。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值