大纲
ATM 机系统
1.注册(账户(系统随机生成 15 位) - 密码(6位) - 余额)
2.登录
4.修改密码
3.查询余额
4.存钱 -(显示操作完成后的余额)
5.取钱 - (显示操作完成后的余额)
6.退出
7.支持通过银行卡转账功能(未完成)
8.数据持久化(未完成)
public class User {
private String userId;
private String userPassword;
private double money;
public User() {
super();
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.function.DoublePredicate;
import java.util.regex.Pattern;
public class ATMSystem {
private ArrayList<User> users;
private Scanner scanner;
private int loginIndex;
public ATMSystem() {
this.users = new ArrayList<>();
this.scanner = new Scanner(System.in);
stratCaiDan();
}
private void stratCaiDan() {
System.out.println("【1】注册 【2】登录 【3】退出");
System.out.print(">");
String result = scanner.nextLine();
switch (result) {
case "1":// 注册
register();
stratCaiDan();
break;
case "2":// 登入
login();
break;
case "3":// 退出
break;
}
}
private void bankCaiDan() {
System.out.println("【1】查询余额 【2】 存钱 【3】取钱 【4】修改密码 【5】转账 【6】退出");
System.out.print(">");
String result = scanner.nextLine();
switch (result) {
case "1"://查询余额
showMoney();
bankCaiDan();
break;
case "2"://存钱
saveMoney();
bankCaiDan();
break;
case "3"://取钱
drawMoney();
bankCaiDan();
break;
case "4"://修改密码
updatePassword();
bankCaiDan();
break;
case "5"://转账
break;
case "6"://退出
break;
}
}
public void showMoney() {
System.out.println("余额:"+users.get(loginIndex).getMoney());
}
public void saveMoney() {
System.out.print("请输入存款金额:");
String money = scanner.nextLine();
users.get(loginIndex).setMoney(users.get(loginIndex).getMoney()+Double.parseDouble(money));
System.out.println("存款成功");
showMoney();
}
public void drawMoney() {
System.out.print("请输入取款金额:");
String money = scanner.nextLine();
if(users.get(loginIndex).getMoney()>=Double.parseDouble(money)) {
users.get(loginIndex).setMoney(users.get(loginIndex).getMoney()- Double.parseDouble(money));
}else {
System.out.println("余额不足!");
return;
}
System.out.println("取款成功");
showMoney();
}
public void updatePassword() {
System.out.print("请输入原密码:");
String password = scanner.nextLine();
if(!users.get(loginIndex).getUserPassword().equals(password)) {
System.out.println("密码错误!");
return;
}
while (true) {
System.out.print("请输入新密码:");
String newPassword = scanner.nextLine();
if (!Pattern.matches("[0-9]{6}", newPassword)) {
System.out.println("密码输入有误,请重新输入");
continue;
}
System.out.print("请确认新密码:");
String rePassword = scanner.nextLine();
if (!rePassword.equals(newPassword)) {
System.out.println("您两次输入的密码不一样,请重新输入");
continue;
}
users.get(loginIndex).setUserPassword(newPassword);
System.out.println("修改密码成功");
break;
}
}
private void login() {
System.out.print("请输入您的卡号:");
String id = scanner.nextLine();
int index = getUserIndexById(id);
if (index == -1) {
System.out.println("您输入的账号不存在,请检查后再试");
}
System.out.print("请输入密码:");
String password = scanner.nextLine();
if(users.get(index).getUserPassword().equals(password)) {
System.out.println("登入成功");
this.loginIndex = index;
bankCaiDan();
}else {
System.out.println("密码错误,请检查后再试");
login();
return;
}
}
private int getUserIndexById(String id) {
for (int index = 0; index < users.size(); index++) {
if (users.get(index).getUserId().equals(id)) {
return index;
}
}
return -1;
}
private void register() {
User user = new User();
Random ran = new Random();
for (int i = 0; i < 15; i++) {
if (i == 0) {
user.setUserId(ran.nextInt(9) + 1 + "");
} else {
user.setUserId(user.getUserId() + ran.nextInt(10));
}
}
System.out.println("账号 :" + user.getUserId() + "请妥善保管");
while (true) {
System.out.print("请输入密码:");
String password = scanner.nextLine();
if (!Pattern.matches("[0-9]{6}", password)) {
System.out.println("密码输入有误,请重新输入");
continue;
}
System.out.print("请确认密码:");
String rePassword = scanner.nextLine();
if (!rePassword.equals(password)) {
System.out.println("您两次输入的密码不一样,请重新输入");
continue;
}
user.setUserPassword(password);
users.add(user);
System.out.println("注册成功。账户为:" + user.getUserId());
break;
}
}
}
public class Test {
public static void main(String[] args) {
new ATMSystem();
}
}