Account用户类
package code;
public class Account {
private String userName; //用户名
private String id; //账号
private String passWord; //密码
private double monty; //余额
private double limitMonty; //限定取款金额
public Account() {
}
public Account(String userName, String id, String passWord, double monty, double limitMonty) {
this.userName = userName;
this.id = id;
this.passWord = passWord;
this.monty = monty;
this.limitMonty = limitMonty;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public double getMonty() {
return monty;
}
public void setMonty(double monty) {
this.monty = monty;
}
public double getLimitMonty() {
return limitMonty;
}
public void setLimitMonty(double limitMonty) {
this.limitMonty = limitMonty;
}
}
ATMSystem
package code;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class ATMSystem {
public static List<Account> allAccount = new ArrayList<>();
public static Scanner sc = new Scanner(System.in);
//测试用例
static {
Account u1 = new Account("王作迪", "11111", "123", 100, 1000);
allAccount.add(u1);
Account u2 = new Account("顾紫燕", "22222", "123", 100, 1000);
allAccount.add(u2);
}
public static void main(String[] args) {
menu(); //主菜单
}
//主菜单
private static void menu() {
OUT:
while (true) {
System.out.println("==========欢迎进入ATM系统==========");
System.out.println("1.登录");
System.out.println("2.注册");
System.out.println("3.退出");
System.out.println("请选择:");
int sel = sc.nextInt();
switch (sel) {
case 1:
login(); //登录
break;
case 2:
enroll(); //注册
break;
case 3:
break OUT;
default:
System.out.println("输入不合法!");
}
}
}
//登录
private static void login() {
System.out.println("=========登录页面=========");
OUT:
while (true) {
System.out.println("请输入账号:");
String id = sc.next();
if (!isExistById(id)) {
System.out.println("不存在该用户,请核对后输入~");
} else {
Account use = selectUseById(id); //根据账号查找用户对象
while (true) {
System.out.println("请输入密码:");
String passWord = sc.next();
if (passWord.equals(use.getPassWord())) {
while (true) {
String code = getCode(); //获取验证码
System.out.println(code);
System.out.println("请输入验证码:");
String useCode = sc.next();
if (code.equalsIgnoreCase(useCode)) {
break;
} else {
System.out.println("验证码错误");
}
}
System.out.println("登录成功~");
useMenu(use); //用户主菜单
break OUT;
} else {
System.out.println("密码有误!");
}
}
}
}
}
//获取验证码
private static String getCode() {
Random r = new Random();
String result = "";
for (int i = 0; i < 4; i++) {
int sel = r.nextInt(3);
switch (sel) {
case 0:
result += (char)(r.nextInt(10) + 48);
break;
case 1:
result += (char)(r.nextInt(26) + 65);
break;
case 2:
result += (char)(r.nextInt(26) + 97);
break;
}
}
return result;
}
//用户主菜单
private static void useMenu(Account use) {
OUT:
while (true) {
System.out.println("============欢迎进入主菜单============");
System.out.println("您好:" + use.getUserName());
System.out.println("账号:" + use.getId());
System.out.println("您当前余额为:" + use.getMonty());
System.out.println("当前取款限额为:" + use.getLimitMonty());
System.out.println("------------------------------------");
System.out.println("1.取款");
System.out.println("2.存款");
System.out.println("3.转账");
System.out.println("4.修改限额");
System.out.println("5.修改密码");
System.out.println("6.退出");
System.out.println("请选择:");
int sel = sc.nextInt();
switch (sel) {
case 1:
//取款
drawMoney(use);
break;
case 2:
//存款
saveMoney(use);
break;
case 3:
//转账
transferAccount(use);
break;
case 4:
//修改限额
alterLimitMoney(use);
break;
case 5:
//修改密码
alterPassWord(use);
break;
case 6:
break OUT;
}
}
}
//修改密码
private static void alterPassWord(Account use) {
OUT:
while (true) {
System.out.println("请输入原密码:");
String oldPassWord = sc.next();
if (!oldPassWord.equals(use.getPassWord())) {
System.out.println("密码错误");
} else {
while (true) {
System.out.println("请输入新密码:");
String newPassWord1 = sc.next();
System.out.println("请再次输入新密码:");
String newPassWord2 = sc.next();
if (!newPassWord1.equals(newPassWord2)) {
System.out.println("两次密码不一致~");
} else {
use.setPassWord(newPassWord1);
System.out.println("修改成功~");
System.out.println("当前密码:" + use.getPassWord());
break OUT;
}
}
}
}
}
//修改限额
private static void alterLimitMoney(Account use) {
System.out.println("请输入限额:");
double limitMoney = sc.nextDouble();
use.setLimitMonty(limitMoney);
System.out.println("修改成功!");
}
//转账
private static void transferAccount(Account use) {
System.out.println("请输入目标账号:");
String id = sc.next();
Account targetUse = selectUseById(id); //获取目标用户对象
if (targetUse == null) {
System.out.println("该用户不存在~~");
} else {
String targetUseName = "*" + targetUse.getUserName().substring(1); //验证用户名
System.out.println(targetUseName);
System.out.println("请输入:");
String str = sc.next();
if (str.equals(targetUse.getUserName().substring(0,1))) {
System.out.println("请输入转账金额:");
double money = sc.nextDouble();
if (money > use.getLimitMonty()) {
System.out.println("转账失败,超出限额~");
} else {
if (money > use.getMonty()) {
System.out.println("转账失败,余额不足~");
} else {
System.out.println("成功向账号为:" + targetUse.getId() + "的用户转入:" + money);
targetUse.setMonty(targetUse.getMonty() + money);
use.setMonty(use.getMonty() - money);
System.out.println("您当前余额为:" + use.getMonty());
}
}
} else {
System.out.println("输入有误!");
}
}
}
//存款
private static void saveMoney(Account use) {
System.out.println("请输入存款金额:");
double money = sc.nextDouble();
use.setMonty(use.getMonty() + money);
System.out.println("存款成功~");
System.out.println("当前余额为:" + use.getMonty());
}
//取款
private static void drawMoney(Account use) {
System.out.println("请输入取款金额:");
double money = sc.nextDouble();
if (money > use.getLimitMonty()) {
System.out.println("取款失败!取款金额超出限定金额!");
} else {
if (money > use.getMonty()) {
System.out.println("取款失败!余额不足~");
} else {
use.setMonty(use.getMonty() - money); //修改当前余额
System.out.println("取款成功~");
System.out.println("当前余额为:" + use.getMonty());
}
}
}
//根据账号查找用户对象
private static Account selectUseById(String id) {
for (Account account : allAccount) {
if (account.getId().equals(id)) {
return account;
}
}
return null;
}
//注册
private static void enroll() {
System.out.println("=========注册页面=========");
Account use = new Account();
System.out.println("请输入用户名:");
String useName = sc.next();
use.setUserName(useName);
while (true) {
System.out.println("请输入密码:");
String passWord1 = sc.next();
System.out.println("请再次输入密码:");
String passWord2 = sc.next();
if (!passWord1.equals(passWord2)) {
System.out.println("两次密码不一致!");
} else {
use.setPassWord(passWord1);
break;
}
}
System.out.println("请设置取款限额:");
double limitMoney = sc.nextDouble();
use.setLimitMonty(limitMoney);
Random r = new Random();
String id;
while (true) {
id = "";
for (int i = 0; i < 5; i++) {
id += r.nextInt(9) + 1;
}
if (!isExistById(id)) { //判断用户是否存在
use.setId(id);
break;
}
}
allAccount.add(use);
System.out.println("注册成功!");
System.out.println("您的账号为:" + use.getId());
}
//判断用户是否存在
private static boolean isExistById(String id) {
if (allAccount.size() == 0) {
return false;
}
for (Account account : allAccount) {
if (id.equals(account.getId())) {
return true;
}
}
return false;
}
}