不看后悔系列
模拟银行开户、存款、取款、转账、修改密码、注销账户等功能,其中还有生成随机验证码等小功能,蕴含很多逻辑,非常适合初学JAVA的选手,为后面的面向对象进阶学习打下基础!
只有两个JAVA文件,源码如下:
(具体运行效果比较长就不放图片了,只需你复制粘贴运行一下就可啦)
/**
系统的账户类,代表账户的信息
*/
public class Account {
private String cardId; // 卡号
private String userName; // 用户名称
private String passWord; // 密码
private double money; // 账户余额
private double quotaMoney; // 每次取现额度限度。
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
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 double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public double getQuotaMoney() {
return quotaMoney;
}
public void setQuotaMoney(double quotaMoney) {
this.quotaMoney = quotaMoney;
}
}
/**
* @description:ATM系统模拟
* @version: 1.0
*/
public class ATMSystemExercise {
public static void main(String[] args) {
//准备一个账户集合 存储账户对象
ArrayList<Account> accounts = new ArrayList<>();
//进入系统首页
showMain(accounts);
}
//系统首页
public static void showMain(ArrayList<Account> accounts) {
while (true) {
System.out.println("==================欢迎您进入到ATM模拟系统===============");
System.out.println("1、登录账户");
System.out.println("2、注册账户");
System.out.println("请您输入1、2选择操作:");
Scanner sc = new Scanner(System.in);
int command = sc.nextInt();
switch (command) {
case 1:
//登录
login(accounts, sc);
break;
case 2:
//开户
register(accounts, sc);
break;
default:
System.out.println("输入命令有误!");
}
}
}
/**
* 用户登录
*
* @param accounts 账户的集合对象
* @param sc 扫描器对象
*/
private static void login(ArrayList<Account> accounts, Scanner sc) {
if (accounts.size() > 0) {
System.out.println("==================欢迎您进入到登录页面==================");
while (true) {
System.out.println("请您输入登录的卡号:");
String cardId = sc.next();
Account acc = getAccountByCardId(accounts, cardId);
if (acc == null) {
System.out.println("您输入的卡号不存在,请重新输入");
} else {
while (true) {
System.out.println("请您输入登录密码:");
String password = sc.next();
if (password.equals(acc.getPassWord())) {
System.out.println("欢迎" + acc.getUserName() + "先生/女士进入系统,您可开始办理你的业务了!");
//showCommand()进入操作页面
showCommand(acc, sc, accounts);
return;
} else {
System.out.println("您的密码不正确!");
}
}
}
}
} else {
System.out.println("当前系统无任何账户,请先注册再登录!");
}
}
/**
* 账户登陆成功的操作页面
*
* @param acc
* @param sc
*/
private static void showCommand(Account acc, Scanner sc, ArrayList<Account> accounts) {
while (true) {
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("请您输入操作命令:");
int command = sc.nextInt();
switch (command) {
case 1:
//查询
showUserMessage(acc);
break;
case 2:
//存款
depositMoney(acc, sc);
break;
case 3:
//取款
drawMoney(acc, sc);
break;
case 4:
//转账
transferMoney(accounts, acc, sc);
break;
case 5:
//修改密码
updatePassword(acc, sc);
break;
case 6:
//退出账户
System.out.println("已安全退出,欢迎下次光临!");
return;
case 7:
//注销账户
logoutAccount(accounts, acc, sc);
return;
default:
System.out.println("请输入正确的数字指令1-7");
break;
}
}
}
/**
* 转账
*
* @param accounts
* @param acc
* @param sc
*/
private static void transferMoney(ArrayList<Account> accounts, Account acc, Scanner sc) {
System.out.println("==================欢迎您进入转账操作页面==================");
System.out.println("请您输入收款人账号");
while (true) {
String cardId = sc.next();
if ("-1".equals(cardId)) {
return;
}
Account otherAcc = getAccountByCardId(accounts, cardId);
if (otherAcc != null) {
String name = otherAcc.getUserName();
System.out.println("请输入对方姓氏进行认证:【*" + name.substring(1) + "】");
String surname = sc.next();
if (name.startsWith(surname)) {
System.out.println("身份验证成功,请您输入转账金额:");
while (true) {
double money = sc.nextDouble();
if (money <= acc.getMoney()) {
acc.setMoney(acc.getMoney() - money);
otherAcc.setMoney(otherAcc.getMoney() + money);
System.out.println("转账成功,卡内余额:" + acc.getMoney() + "元");
return;
} else {
System.out.println("账户余额不足,你只有" + acc.getMoney() + "元,是否要重新输入金额? Y/N");
String choose = sc.next();
if ("Y".equalsIgnoreCase(choose)) {
System.out.println("请您输入转账金额:");
} else if ("N".equalsIgnoreCase(choose)) {
return;
}
}
}
} else {
System.out.println("身份认证失败,返回操作页面");
return;
}
} else {
System.out.println("账户不存在,请重新输入:(可输入 -1 返回操作界面)");
}
}
}
/**
* 销户功能
*
* @param accounts
* @param acc
* @param sc
*/
private static void logoutAccount(ArrayList<Account> accounts, Account acc, Scanner sc) {
int count = 0;
String verificationCode = createVerificationCode();
System.out.println("请您输入验证码“" + verificationCode + "“");
while (true) {
String code = sc.next();
if (code.equalsIgnoreCase(verificationCode)) {
accounts.remove(acc);
System.out.println("注销账户成功");
return;
} else {
if (count < 2) {
count++;
System.out.println("验证码输入失败,请重新输入:");
} else {
System.out.println("您已错误三次,怀疑是非法操作,将自动退出系统");
return;
}
}
}
}
/**
* 更改密码
*
* @param acc
* @param sc
*/
private static void updatePassword(Account acc, Scanner sc) {
System.out.println("==================欢迎进入更改密码页面======================");
System.out.println("请您输入当前密码进行认证");
while (true) {
String password = sc.next();
if (password.equals(acc.getPassWord())) {
while (true) {
System.out.println("请输入新密码:");
String newPassword = sc.next();
System.out.println("请您再次输入新密码:");
String okPassword = sc.next();
if (okPassword.equals(newPassword)) {
System.out.println("密码修改成功");
return;
} else {
System.out.println("两次密码不一致");
}
}
} else {
System.out.println("密码输入错误,请重新输入");
}
}
}
/**
* 生成6位的验证码用来销户前的人机验证
*
* @return
*/
private static String createVerificationCode() {
Random r = new Random();
String s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
String code = "";
for (int i = 0; i < 6; i++) {
code += s.charAt(r.nextInt(s.length()));
}
return code;
}
/**
* 取款
*
* @param acc
* @param sc
*/
private static void drawMoney(Account acc, Scanner sc) {
System.out.println("==================欢迎您进入账户取款页面======================");
if (acc.getMoney() > 0) {
while (true) {
System.out.println("请您输入取款金额:");
double money = sc.nextDouble();
if (money > acc.getMoney()) {
System.out.println("余额不足,您卡上余额只有" + acc.getMoney() + "元");
} else {
if (money > acc.getQuotaMoney()) {
System.out.println("取款数额超过您的单次取款额度" + acc.getQuotaMoney() + "元");
} else {
acc.setMoney(acc.getMoney() - money);
System.out.println("取款完成,卡内余额:" + acc.getMoney() + "元");
break;
}
}
}
} else {
System.out.println("卡内无余额可取");
}
}
/**
* 存款
*
* @param acc
* @param sc
*/
private static void depositMoney(Account acc, Scanner sc) {
System.out.println("==================欢迎您进入账户存款页面======================");
System.out.println("请您输入存款金额:");
double money = sc.nextDouble();
acc.setMoney(money + acc.getMoney());
System.out.println(money + "元存款成功");
showUserMessage(acc);
}
/**
* 展示用户信息页面
*
* @param acc
*/
private static void showUserMessage(Account acc) {
System.out.println("==================您当前账户详情信息如下==================");
System.out.println("卡号:" + acc.getCardId());
System.out.println("户主:" + acc.getUserName());
System.out.println("余额:" + acc.getMoney());
System.out.println("当次取现额度:" + acc.getQuotaMoney());
}
/**
* 用户开户功能
*
* @param accounts 账户的集合对象
* @param sc 扫描器对象
*/
private static void register(ArrayList<Account> accounts, Scanner sc) {
Account acc = new Account();
System.out.println("==================欢迎您进入到开户页面======================");
System.out.println("请您输入账户名称");
String username = sc.next();
acc.setUserName(username);
while (true) {
System.out.println("请您输入账户密码:");
String passWord = sc.next();
System.out.println("请您输入确认密码:");
String okPassWord = sc.next();
if (okPassWord.equals(passWord)) {
// 密码无问题
acc.setPassWord(okPassWord);
break;
} else {
System.out.println("两次输入的密码不一致!");
}
}
String cardId = createCardId(accounts);
acc.setCardId(cardId);
System.out.println("请您设置当次取现额度:");
double quotaMoney = sc.nextDouble();
acc.setQuotaMoney(quotaMoney);
accounts.add(acc);
System.out.println(acc.getUserName() + ",恭喜您开户完成,您的卡号是:" + acc.getCardId());
}
/**
* 生成8位的随机数字不重复卡号
*
* @param accounts
* @return
*/
private static String createCardId(ArrayList<Account> accounts) {
Random r = new Random();
String cardId = "";
while (true) {
cardId = "";
for (int i = 0; i < 8; i++) {
cardId += r.nextInt(10);
}
Account acc = getAccountByCardId(accounts, cardId);
if (acc == null) {
return cardId;
}
}
}
/**
* 通过卡号找用户
*
* @param accounts
* @param cardId
* @return
*/
private static Account getAccountByCardId(ArrayList<Account> accounts, String cardId) {
for (int i = 0; i < accounts.size(); i++) {
Account acc = accounts.get(i);
if (cardId.equals(acc.getCardId())) {
return acc;
}
}
return null;
}
}