ATM2(添加新功能)

public class ATMMachine {
//所有用户信息
private UserInfo[] allUsers;
//当前操作用户信息
private UserInfo user;
// 现金
private int cash;
// 现金容量上限
public static final int MAX_CASH = 200000;
// 存取上限
public static final int GET_SET_MAX = 2000;

public ATMMachine() {
// 预载入用户信息---文件读操作
this.allUsers = FileOperation.readFile();
// this.allUsers = new UserInfo[5];
// for (int i = 0; i < allUsers.length; i++) {
// this.allUsers[i] = new UserInfo("zhang" + i, "123456" + i,
// 10000 + i * 1000);
// }
this.cash = 100000; //本机现有金额
}

// 运行
public void run() {
this.welcome();
boolean flag = this.login();
if (flag) {
System.out.print("正在登陆,请稍候");
for(int i = 0;i<6;i++){
System.out.print(".");
try {
Thread.sleep(600);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
System.out.println("\n"+"登陆成功");
System.out.println("用户" + this.user.getUsername() + "于" +
this.getDate()+ "登录成功,欢迎您!");
while (true) {
int choice = this.choiceMenu();
switch (choice) {
case 1:
this.query();
break;
case 2:
this.storeMoney();
break;
case 3:
this.getMoney();
break;
case 4:
this.changePwd();
break;
case 5:
this.exit();
break;
default:
System.out.println("对不起,没有该选项!");
break;
}
}
} else {
System.out.println("您的账户被冻结!请到柜台处理!");
}
}

// 显示欢迎
private void welcome() {
System.out.println("******************************");
System.out.println("*********欢**迎**登**录*********");
System.out.println("******************************");
System.out.println("*********爱存不存银行ATM*********");
System.out.println("******************************");
System.out.println("*****************version2.0***");
}

// 登录
private boolean login() {
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("请输入用户名:");
String inputName = scan.next();
System.out.println("请输入密码:");
String inputPwd = scan.next();
for (UserInfo tmp : allUsers) {
if (tmp.getUsername().equals(inputName)
&& tmp.getPassword().equals(inputPwd)) {
this.user = tmp;
return true;
}
}
System.out.println("输入有误!您还有" + (2 - i) + "次机会");
}
return false;
}

// 选择菜单
private int choiceMenu() {
int choice = 0;
Scanner scan = new Scanner(System.in);
System.out.println("请选择您要执行的操作:");
System.out.println("1、查询;2、存款;3、取款;4、修改密码;5、退出");

String choiceStr = scan.next();
if(choiceStr.matches("[1-5]")){
choice = Integer.parseInt(choiceStr);
}

return choice;
}

// 查询余额
private void query() {
//直接显示user对象中的account属性值
System.out.println("您当前的余额是:" + user.getAccount() + "元!");
}

// 存钱
private void storeMoney() {
//1、接收用户输入
//2、判断--不能为0,不能为负,不能不是100的倍数,不能超过存取上限,不能超过现金容量--给出提示
//3、判断通过--加现金,加余额
System.out.println("请输入您要存储的金额:");
String inputMoney = new Scanner(System.in).next();

if(inputMoney.matches("(20|1[0-9]|[1-9])00")){
int money = Integer.parseInt(inputMoney);
if(money + this.cash > this.MAX_CASH){
System.out.println("对不起,本机容量不足!");
}else{
this.cash += money;
this.user.setAccount(this.user.getAccount() + money);
//文件写操作
FileOperation.writeFile(this.allUsers);
System.out.println("您于" + this.getDate() + "成功存款" + money + "元!");
}
}else{
System.out.println("请输入有效金额!本机只提供2000以内的百元服务!");
}
}

// 取款
private void getMoney() {
System.out.println("请输入您要取出的金额:");
String inputMoney = new Scanner(System.in).next();
if(inputMoney.matches("(20|1[0-9]|[1-9])00")){
int money = Integer.parseInt(inputMoney);
if(money >= this.cash){
System.out.println("对不起,本机现金不足!");
}else if(money > this.user.getAccount() ){
System.out.println("对不起,您的账户上余额不足!");
}else{
this.user.setAccount(this.user.getAccount() - money);
this.cash -= money;
//文件写操作
FileOperation.writeFile(this.allUsers);
System.out.println("您于" + this.getDate() + "成功取款" + money + "元!");
}
}else{
System.out.println("请输入有效金额!本机只提供2000以内的百元服务!");
}
}

// 修改密码
private void changePwd() {
Scanner scan = new Scanner(System.in);
System.out.println("请输入老密码:");
String oldPwd = scan.next();
System.out.println("请输入新密码:");
String newPwd = scan.next();
System.out.println("再次输入新密码:");
String rePwd = scan.next();

if(!oldPwd.equals(this.user.getPassword())){
System.out.println("老密码输入错误!");
}else if(!newPwd.equals(rePwd)){
System.out.println("两次密码不一致!");
}else{
this.user.setPassword(newPwd);
//文件写操作
FileOperation.writeFile(this.allUsers);
System.out.println("您于" + this.getDate() + "成功修改密码!");
}


}

// 退出
private void exit() {
System.out.println("谢谢您的使用!请收好您的卡!");
System.exit(0);
}

//格式化日期
private String getDate(){
String date = "";
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 E HH:mm:ss");
date = sdf.format(now);
return date;
}

}

 添加的新功能有:正则表达式处理输入问题,只要不符合正则表达式的规则就会给出相应提示(try   catch模块的使用,解决输入的问题,已经被正则表达式所代替)。

添加一个信息的读写,每次取钱存钱修改过后,会修改这个文件中的内容,使得下次登录操作时,信息保持之前的状态。

显示时间功能,程序睡眠功能。

转载于:https://www.cnblogs.com/Blueses/p/5562140.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值