第一次写这么多行代码,三百多行,虽然很简单,但是不容易啊,一个晚上,一边写一遍测试。
package com.test.hello.atm;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATMSystem {
public static void main(String[] args) throws InterruptedException {
ArrayList<Account> accounts = new ArrayList<Account>();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("===============ATM系统=================");
System.out.println("登录账户请输入:1");
System.out.println("注册账户请输入:2");
System.out.println("退出系统请输入:0");
System.out.println("进入系统后台查看账户信息:777");
int command = sc.nextInt();
switch (command) {
case 1:
login(accounts, sc);
break;
case 2:
register(accounts, sc);
break;
case 0:
return;
case 777:
queryAll(accounts);
break;
default:
System.out.println("没有这个操作,请重新输入");
break;
}
}
}
/**
* 查询系统中所有账户的信息
* @param accounts
*/
private static void queryAll(ArrayList<Account> accounts) {
System.out.println("===============后台查询=================");
System.out.println("i卡号-----用户名------余额------限额");
for (int i = 0; i < accounts.size(); i++) {
Account acc = accounts.get(i);
System.out.println(acc.getCarId() + "\t\t" + acc.getUserName() + "\t\t" + acc.getMoney() + "\t\t" + acc.getQuoteMoney());
}
}
/**
* 个人界面,可以查询、存钱、转账、修改密码、注销此账户这些功能
* @param accounts
* @param sc
* @throws InterruptedException
*/
private static void login(ArrayList<Account> accounts, Scanner sc) throws InterruptedException {
System.out.println("===============登录账户=================");
if (accounts.size() == 0) {
System.out.println("系统中没有账户,请先开户再来登录");
return;
}
while (true) {
System.out.println("请输入id卡号");
String carId = sc.next();
Account acc = getAccountByCarId(carId, accounts);
if (acc != null) {
System.out.println("请输入登录密码");
String passwd = sc.next();
if (acc.getPasswd().equals(passwd)) {
System.out.println("登录成功!");
System.out.println("===========这是" + acc.getUserName() + "的主场===========");
showUserCommand(acc, sc, accounts);
System.out.println("退出成功!即将回到主界面");
Thread.sleep(2000);
break;
} else {
System.out.println("密码错误");
}
} else {
System.out.println("系统中没有此id卡,请重新输入。。。");
}
}
}
/**
* 个人操作界面
* @param acc
* @param sc
* @param accounts
*/
private static void showUserCommand(Account acc, Scanner sc, ArrayList<Account> accounts) {
while (true) {
System.out.println("请输入你需要的操作");
System.out.println("查询请输入:0");
System.out.println("存款请输入:1");
System.out.println("取款请输入:2");
System.out.println("转账请输入:3");
System.out.println("改密码请输入:4");
System.out.println("注销请输入:5");
System.out.println("退出请输入:6");
int command = sc.nextInt();
switch (command) {
case 0:
query(acc);
break;
case 1:
depositMoney(acc, sc);
break;
case 2:
drawMoney(acc, sc);
break;
case 3:
trainforMoney(sc, acc, accounts);
break;
case 4:
updatePasswd(acc,sc);
return; //修改密码成功后,结束此方法,回到主界面
case 5:
cancellation(acc, sc,accounts); //注销账户
return;
case 6:
return;
}
}
}
/**
* 注销账户
* @param acc
* @param sc
* @param accounts
*/
private static void cancellation(Account acc, Scanner sc, ArrayList<Account> accounts) {
System.out.println("你真的要注销此账户??? y/n");
String flag = sc.next();
if ("y".equals(flag)) {
accounts.remove(acc);
System.out.println("注销完成");
} else if ("n".equals(flag)) {
System.out.println("已经取消注销操作");
}
}
/**
* 修改账户的密码
* @param acc
* @param sc
*/
private static void updatePasswd(Account acc, Scanner sc) {
System.out.println("=================改密码界面==============:");
while (true) {
System.out.println("请输入新密码");
String newPasswd = sc.next();
System.out.println("请再一次输入新密码");
String againNewPasswd = sc.next();
if (newPasswd.equals(againNewPasswd)){
if (newPasswd.equals(acc.getPasswd())) {
System.out.println("新密码不能与旧密码一致");
} else {
acc.setPasswd(newPasswd);
System.out.println("修改密码成功");
return;
}
} else {
System.out.println("两次输入的密码不一致,重新输");
}
}
}
/**
* 转账功能
* @param sc
* @param acc
* @param accounts
*/
private static void trainforMoney(Scanner sc, Account acc, ArrayList<Account> accounts) {
System.out.println("=================转账界面==============:");
//判断系统中账户是否有2个或者更多
if (accounts.size() < 2) {
System.out.println("用户少于2个,不能实现转账,先开户吧");
return;
}
//账户满足条件,判断此用户是否有钱
if (acc.getMoney() == 0) {
System.out.println("没钱,怎么转账");
return;
}
while (true) {
//满足以上条件,开始转账
System.out.println("输入要转账的id卡号");
String carId = sc.next();
//不能给自己转账
if (acc.getCarId().equals(carId)) {
System.out.println("不能给自己转账");
continue;
}
//首先判断输入的这个卡号是否存在
Account account = getAccountByCarId(carId, accounts);
if (account == null) {
System.out.println("没有这个卡号,请重新输入");
} else {
String name = account.getUserName();
System.out.println("有这个卡号");
System.out.println("输入对方的用户名的姓氏" + "*" + name.substring(1));
String inputName = sc.next();
if (name.startsWith(inputName)) {
System.out.println("要转多少钱");
double money = sc.nextDouble();
if (money > acc.getQuoteMoney()) {
System.out.println("单词转账不能超过" + acc.getQuoteMoney());
} else {
acc.setMoney(acc.getMoney() - money);
account.setMoney(account.getMoney() + money);
System.out.println("转账成功");
break;
}
} else {
System.out.println("你可能转错人了");
}
}
}
}
/**
* 取钱
*
* @param acc
* @param sc
*/
private static void drawMoney(Account acc, Scanner sc) {
System.out.println("=================取钱界面==============:");
if (acc.getMoney() > 0) {
System.out.println("说吧取多少");
double money = sc.nextDouble();
if (money > acc.getQuoteMoney()) {
System.out.println("每次最多取" + acc.getQuoteMoney());
} else {
if (acc.getMoney() > money) {
acc.setMoney(acc.getMoney() - money);
System.out.println("取钱成功,取了" + money);
query(acc);
} else {
System.out.println("都没有这么多钱,还去取这么多钱,先进厂螺丝赚钱吧靓仔");
}
}
} else {
System.out.println("钱都没有,还取钱???!!");
return;
}
}
/**
* 存钱
* @param acc
* @param sc
*/
private static void depositMoney(Account acc, Scanner sc) {
System.out.println("=================存钱界面==============:");
System.out.println("存多少钱");
double money = sc.nextDouble();
acc.setMoney(acc.getMoney() + money);
System.out.println("存钱成功");
query(acc);
}
/**
* 查询账户信息
* @param acc
*/
private static void query(Account acc) {
System.out.println("=================查询界面==============:");
System.out.println("卡号:" + acc.getCarId());
System.out.println("用户名:" + acc.getUserName());
System.out.println("余额:" + acc.getMoney());
System.out.println("限额:" + acc.getQuoteMoney());
}
/**
* 注册账户
* @param accounts
* @param sc
* @throws InterruptedException
*/
private static void register(ArrayList<Account> accounts, Scanner sc) throws InterruptedException {
System.out.println("===============注册账户=================");
Account account = new Account();
System.out.println("请输入账户的name");
String userName = sc.next();
account.setUserName(userName);
while (true) {
System.out.println("请输入账户密码");
String passwd = sc.next();
System.out.println("再一次输入您的密码");
String againPasswd = sc.next();
if (passwd.equals(againPasswd)) {
account.setPasswd(passwd);
break;
} else {
System.out.println("您两次输入的密码不一样,请重新输入");
}
}
System.out.println("请设置你的取款额度");
double quoteMoney = sc.nextDouble();
account.setQuoteMoney(quoteMoney);
System.out.println("正在生成你的id卡号,请稍等。。。");
Thread.sleep(2000);
String carId = getRandomCarID(accounts);
account.setCarId(carId);
accounts.add(account);
System.out.println("开户成功了" + account.getUserName());
System.out.println("你的卡号为:" + account.getCarId());
}
/**
* 随机生成一个6位数的数字组成的数字,并判断是否与系统中已有的id一样,一样则重新生成
*
* @param accounts
* @return
*/
private static String getRandomCarID(ArrayList<Account> accounts) {
//随机生成6个数字
while (true) {
String carId = "";
Random random = new Random();
for (int i = 0; i < 6; i++) {
carId += random.nextInt(10);
}
//判断是否与已有id一样
Account account = getAccountByCarId(carId, accounts);
if (account == null) {
return carId;
}
}
}
/**
* 根据id返回账户
*
* @param carId
* @param accounts
* @return
*/
private static Account getAccountByCarId(String carId, ArrayList<Account> accounts) {
for (int i = 0; i < accounts.size(); i++) {
if (carId.equals(accounts.get(i).getCarId())) {
return accounts.get(i);
}
}
return null;
}
}