模拟ATM机
程序员文章站
2022-05-28 09:41:00
...
import java.io.*;
//银行卡类
class BankCard
{
//属性和行为:
//属性 即本类有什么?
//卡号
private String id;
//密码
private String pwd;
//真实姓名
private String name;
//钱数
private float money;
BankCard(String id,String pwd,String name,float money)
{
this.id = id;
this.pwd = pwd;
this.name = name;
this.money = money;
}
//功能
//获取卡号
public String getId()
{
return id;
}
//获取密码
public String getPwd()
{
return pwd;
}
//设置密码
public void setPwd(String newPwd)
{
pwd = newPwd;
}
//获取真实姓名
public String getName()
{
return name;
}
//获取钱数
public float getMoney()
{
return money;
}
//设置钱数
public void setMoney(float myMoney)
{
money += myMoney;
}
public String toString()
{
return "帐号:"+id+" 户主:"+name+" 余额:"+money;
}
};
//ATM机类
class ATM
{
//数据库链接
static DataBase db = new DataBase();
//键盘读写流
public BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//属性:
//插卡口
private BankCard bc;
//功能
//插卡
public void insertCard(BankCard bc) throws Exception
{
this.bc = bc;
welcome();
}
//欢迎界面
public void welcome() throws Exception
{
String s = "******************************************";
System.out.println(s+"\n\t欢迎使用IBM-ATM模拟系统\n"+s);
login();
}
//登录界面
public void login() throws Exception
{
String pwd = null;
int count = 0;
do
{
//验证用户密码
System.out.println("请输入您的密码:");
pwd = br.readLine();
if(pwd.equals(bc.getPwd()))
{
//验证成功转至操作界面
oparator();
break;
}
else
{
if(count!=2)
{
System.out.println("您输入的密码不正确!请重新输入!");
}
count++;
}
}
while (count<3);
if(count==3)
{
lock();
}
else
{
oparator();
}
}
//操作界面
public void oparator() throws Exception
{
System.out.println("请选择操作类型:(其他键退出)\n"
+"1.>取钱\n"
+"2.>存钱\n"
+"3.>查询\n"
+"4.>修改密码\n"
+"5.>转账\n"
+"6.>退卡");
String select = br.readLine();
char type;
if(!select.equals(""))
{
type = select.charAt(0);
}
else
{
type = '\0';
}
switch(type)
{
case '1':
{
blance();
oparator();
break;
}
case '2':
{
setMoney();
oparator();
break;
}
case '3':
{
query();
oparator();
break;
}
case '4':
{
setPwd();
oparator();
break;
}
case '5':
{
turn();
oparator();
break;
}
case '6':
{
exit();
break;
}
default:
{
exit();
}
}
}
//取钱
public void blance() throws Exception
{
System.out.println("请输入取钱金额:");
int money = Integer.parseInt(br.readLine());
if(money<bc.getMoney())
{
bc.setMoney(-money);
System.out.println("请收好您的钱!");
query();
}
else
{
System.out.println("余额不足!");
}
}
//存钱
public void setMoney() throws Exception
{
System.out.println("请输入存钱金额:");
float money = Float.parseFloat(br.readLine());
if(money>0)
{
bc.setMoney(money);
System.out.println("您的钱已存入!");
query();
}
else
{
System.out.println("请正确输入您要存的钱数!");
}
}
//查询
public void query()
{
System.out.println("用户姓名:"+bc.getName());
System.out.println("余额:"+bc.getMoney());
}
//修改密码
public void setPwd() throws Exception
{
String pwd = null;
int count = 0;
do
{
System.out.println("请输入旧密码:");
pwd = br.readLine();
if(pwd.equals(bc.getPwd()))
{
do
{
System.out.println("请输入新密码:");
String pwd1 = br.readLine();
System.out.println("请再次输入新密码:");
String pwd2 = br.readLine();
if(pwd1.equals(pwd2))
{
bc.setPwd(pwd2);
System.out.println("密码修改成功!请使用新密码!");
oparator();
}
else
{
System.out.println("两次密码输入不一致,请重新输入!");
}
}
while (true);
}
else
{
System.out.println("密码验证出错!");
count++;
}
}
while (count<3);
lock();
}
//转账
public void turn() throws Exception
{
System.out.println("请输入转入帐号:");
String id = br.readLine();
if(!id.equals(bc.getId()))
{
BankCard turn = db.query(id);
if(turn!=null)
{
System.out.println("转入帐号:"+turn.getId());
System.out.println("户主:"+turn.getName());
System.out.println("确认转账?y-确定 其他键-返回操作界面");
String ok = br.readLine();
if(ok.equals("y"))
{
System.out.println("请输入转账金额:");
float money = Float.parseFloat(br.readLine());
if(money<bc.getMoney())
{
bc.setMoney(-money);
turn.setMoney(money);
System.out.println("转账成功!");
System.out.println(db);
}
else
{
System.out.println("余额不足!不能转账!");
}
}
}
else
{
System.out.println("转入帐号不存在!");
}
}
else
{
System.out.println("不能为自己转账!");
}
}
//退卡
public void exit()
{
System.out.println("谢谢使用!再会!");
System.exit(0);
}
//吞卡
public void lock()
{
System.out.println("您操作失误次数过多,卡片暂时由我们保管!请即时和我们联系!");
System.exit(1);
}
};
class ATMTest
{
public static void main(String[] args) throws Exception
{
BankCard bc1 = new BankCard("001","123456","李四",100000);
BankCard bc2 = new BankCard("002","123456","王五",100000);
BankCard bc3 = new BankCard("003","123456","麻子",100000);
BankCard bc4 = new BankCard("004","123456","李毅",100000);
BankCard bc5 = new BankCard("005","123456","王三",100000);
BankCard bc6 = new BankCard("006","123456","杨怡",100000);
BankCard bc7 = new BankCard("008","123456","刘华",100000);
BankCard bc8 = new BankCard("010","123456","何新",100000);
BankCard bc9 = new BankCard("013","123456","无名",100000);
BankCard bc0 = new BankCard("007","123456","邦德",100000);
DataBase db = new DataBase();
db.add(bc0);
db.add(bc1);
db.add(bc2);
db.add(bc3);
db.add(bc4);
db.add(bc5);
db.add(bc6);
db.add(bc7);
db.add(bc8);
db.add(bc9);
ATM atm = new ATM();
atm.insertCard(bc0);
}
};
class DataBase
{
private static BankCard[] cards = new BankCard[10];
static int count;
public void add(BankCard bc)
{
cards[count] = bc;
count++;
}
public BankCard query(String id)
{
BankCard bc = null;
for(int i=0;i<cards.length;i++)
{
if(id.equals(cards[i].getId()))
{
bc = cards[i];
break;
}
}
return bc;
}
public String toString()
{
String message = "";
for(int i=0;i<cards.length;i++)
{
message += cards[i]+"\n";
}
return message;
}
};