部分运行画面如图
话不多说!上码:
AccountInfo
类
package account;
/**
* author:Zohing 2023/07/02
*/
public class AccountInfo {
private String userName;
private String password;
private Integer cardId;
//银行存款
private Integer deposit;
//构造方法
public AccountInfo(String userName,String password,
Integer cardId,Integer deposit) {
this.userName = userName;
this.password = password;
this.cardId = cardId;
this.deposit = deposit;
}
public AccountInfo() {
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getCardId() {
return cardId;
}
public void setCardId(Integer cardId) {
this.cardId = cardId;
}
public Integer getDeposit() {
return deposit;
}
public void setDeposit(Integer deposit) {
this.deposit = deposit;
}
}
Business
类
package service;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
import account.AccountInfo;
/**
* author:Zohing 2023/07/02
*/
public class Business {
static List<AccountInfo> accountList = new ArrayList<AccountInfo>();
// 注册用的当前账户数据
static AccountInfo accountInfo = new AccountInfo();
// 当前账户数据
static AccountInfo accountCurrentInfo = new AccountInfo();
static boolean loginSuccess = false;
static int index;
static boolean out = false;
static boolean toTransferAccountExist = false;
static boolean duplicateAccountNameCheck = false;
Scanner sc = new Scanner(System.in);
public void menu() {
System.out.println("***********************************");
System.out.println("**欢迎使用自助银行业务系统!请选择您的操作 **");
System.out.println("**注册 ---> 键入数字1 **");
System.out.println("**登录 ---> 键入数字2 **");
System.out.println("***********************************");
}
public void detail() {
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("**退出系统 ---> 键入数字7 **");
System.out.println("***********************************");
}
public void error1() {
System.out.println("***********************************");
System.out.println("**输入有误,请重新操作! **");
System.out.println("***********************************");
detail();
try {
int businessNo = sc.nextInt();
service(businessNo, accountCurrentInfo.getUserName());
} catch (Exception e) {
error2();
}
}
public void error2() {
System.out.println("***********************************");
System.out.println("**输入有误,请务必输入整数! **");
System.out.println("***********************************");
}
public void showDeposit() {
System.out.println("您当前余额为:" + accountCurrentInfo.getDeposit() + "元");
}
public void depositMoney(String accountName) {
try {
System.out.println("请输入存款金额");
int moneyForDeposit = sc.nextInt();
accountList.forEach(e -> {
if (e.getUserName().equals(accountName)) {
accountCurrentInfo.setDeposit(e.getDeposit() + moneyForDeposit);
// e.setDeposit(e.getDeposit() + moneyForDeposit);
System.out.println("您当前余额为:" + e.getDeposit() + "元");
}
});
} catch (Exception e) {
error2();
}
}
public void withdrawMoney() {
try {
System.out.println("请输入取款金额");
int moneyForWithdraw = sc.nextInt();
if (accountCurrentInfo.getDeposit() == 0) {
System.out.println("账户余额为0!稍后为您跳转上一级");
} else if (moneyForWithdraw > accountCurrentInfo.getDeposit()) {
System.out.println("账户余额不足!请更改取款金额");
withdrawMoney();
} else {
accountCurrentInfo.setDeposit(accountCurrentInfo.getDeposit() - moneyForWithdraw);
// accountList.get(index).setDeposit(accountList.get(index).getDeposit()
// - moneyForWithdraw);
System.out.println("您已取款" + moneyForWithdraw + "元");
System.out.println("当前存款为" + accountCurrentInfo.getDeposit() + "元");
}
} catch (Exception e) {
error2();
}
}
public void transferAccounts(String toUserName) {
try {
accountList.forEach(e -> {
if (Objects.equals(e.getUserName(), toUserName)) {
toTransferAccountExist = true;
System.out.println("请输入您将转入的金额");
int transferMoney = sc.nextInt();
if (transferMoney <= accountCurrentInfo.getDeposit()) {
System.out.println("《测试用,实际不可见!对方存款为"
+e.getDeposit()+ "元》");
accountCurrentInfo.setDeposit(accountCurrentInfo.getDeposit() - transferMoney);
e.setDeposit(e.getDeposit() + transferMoney);
System.out.println("转账成功!您当前余额为" + accountCurrentInfo.getDeposit() + "元," + "转账金额"
+ transferMoney + "元已成功转入对方账户<" + toUserName + ">中");
System.out.println("《测试用,实际不可见!对方存款为"
+e.getDeposit()+ "元》");
} else {
System.out.println("账户余额不足!请更改转账金额");
transferAccounts(toUserName);
}
}
});
if (!toTransferAccountExist) {
System.out.println("您转账的对象不存在!请重新输入的用户名");
String toUserNameAgain = sc.next();
transferAccounts(toUserNameAgain);
}
} catch (Exception e) {
error2();
}
}
public void modifyAccount() {
System.out.println("***********************************");
System.out.println("**更改账户名称 ---> 键入数字1 **");
System.out.println("**更改密码 ---> 键入数字2 **");
System.out.println("***********************************");
try {
int modifyType = sc.nextInt();
if (modifyType == 1) {
System.out.println("请输入您想变更后的账户名称");
String changeName = sc.next();
if (!duplicateAccountNameCheck(changeName)) {
accountCurrentInfo.setUserName(changeName);
// accountList.get(index).setUserName(changeName);
System.out.println("账户名称已变更为<" + changeName + ">");
}
} else if (modifyType == 2) {
System.out.println("请输入您想变更后的账户密码");
String changePsw = sc.next();
accountCurrentInfo.setPassword(changePsw);
// accountList.get(index).setPassword(changePsw);
System.out.println("账户名密码已变更为<" + changePsw + ">");
} else {
System.out.println("业务序号输入不正确,请重新输入");
modifyAccount();
}
} catch (Exception e) {
error1();
}
}
// 账户名不能重复,是唯一值
public boolean duplicateAccountNameCheck(String name) {
accountList.forEach(e->{
if(Objects.equals(name, e.getUserName())) {
duplicateAccountNameCheck = true;
}
});
return duplicateAccountNameCheck;
}
public void logOff() {
System.out.println("您将注销账户!请输入YES再次确认,返回输入NO");
try {
String logOff = sc.next();
if (logOff.equals("YES")) {
accountList.remove(index);
System.out.println("当前账户已注销!即将返回登陆页面");
OperationProcess();
} else if (logOff.equals("NO")) {
System.out.println("账户注销已取消!即将返回上一页");
} else {
System.out.println("输入有误!请重新输入");
logOff();
}
} catch (Exception e) {
error1();
}
}
public void out() {
sc.close();
}
// 注册账号
public void register() {
try {
System.out.println("请输入您的用户名");
String accountName = sc.next();
accountInfo.setUserName(accountName);
System.out.println("请输入您的密码");
String accountPassword = sc.next();
accountInfo.setPassword(accountPassword);
// 随机生成卡号
accountInfo.setCardId((int) (Math.random() * 999999999));
accountInfo.setDeposit(0);
if(!duplicateAccountNameCheck(accountName)) {
// 注册信息录入系统
accountList.add(accountInfo);
System.out.println("恭喜您账户创建成功!请重新登录");
login();
}else {
System.out.println("用户名已存在!请重新输入");
register();
}
} catch (Exception e) {
error1();
}
}
// 登录
public void login() {
try {
System.out.println("请输入您的用户名");
String accountName = sc.next();
System.out.println("请输入您的密码");
String accountPassword = sc.next();
if (loginCheck(accountName, accountPassword)) {
System.out.println("欢迎登录自助银行业务系统!");
detail();
int businessNo = sc.nextInt();
service(businessNo, accountName);
if (!out) {
detail();
}
} else {
System.out.println("您输入的用户名或者密码不正确!请重新输入");
login();
}
} catch (Exception e) {
error1();
}
}
public void service(int businessNo, String accountName) {
try {
switch (businessNo) {
case 1:
showDeposit();
break;
case 2:
depositMoney(accountName);
break;
case 3:
withdrawMoney();
break;
case 4:
System.out.println("请输入您将转入的用户名");
String toUserName = sc.next();
transferAccounts(toUserName);
break;
case 5:
modifyAccount();
break;
case 6:
logOff();
break;
case 7:
out();
out = true;
break;
default:
System.out.println("您输入的业务序号不正确!请重新输入");
int businessNoAgain = sc.nextInt();
service(businessNoAgain, accountName);
break;
}
if (!out) {
detail();
int businessNoAgain = sc.nextInt();
service(businessNoAgain, accountName);
}
} catch (Exception e) {
error2();
}
}
// 登录check
public boolean loginCheck(String accountName, String accountPassword) {
accountList.forEach(e -> {
if (Objects.equals(accountName, e.getUserName()) && Objects.equals(accountPassword, e.getPassword())) {
loginSuccess = true;
accountCurrentInfo = e;
// 获取当前账户在虚拟数据库中的list下标,此举为了避免后续多处业务操作的for循环取数据
index = accountList.indexOf(accountCurrentInfo);
}
});
return loginSuccess;
}
public void OperationProcess() {
try {
menu();
int loginNum = sc.nextInt();
if (loginNum == 1) {
register();
} else {
login();
}
} catch (Exception e) {
error2();
}
// 关闭键入操作
sc.close();
}
public static void main(String[] args) {
Business business = new Business();
// 虚拟数据库
accountList.add(new AccountInfo("zohing1", "123456", 147258369, 1500));
accountList.add(new AccountInfo("zohing2", "123789", 963852741, 9500));
accountList.add(new AccountInfo("1", "1", 999999999, 1000));
accountList.add(new AccountInfo("张三", "1", 899999998, 100));
business.OperationProcess();
}
}