Java用Account类,来模拟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();
}
}