package com.wang.Atm;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATM {
public static void main(String[] args) {
//定义账户类
//定义一个集合容器,负责存储账户对象,进行相关的操作
ArrayList<Account> accounts = new ArrayList<>();
Scanner sc = new Scanner(System.in);
while (true) {
menu();
System.out.println("请选择操作");
int comand = sc.nextInt();
command(comand, accounts, sc);
}
}
//展示系统的页面
public static void menu() {
System.out.println("========ATM系统========");
System.out.println("1、账户登录");
System.out.println("2、注册开户");
}
public static void command(int c, ArrayList<Account> accounts, Scanner sc) {
switch (c) {
case 1:
//登录
ogin(accounts, sc);
break;
case 2:
//开户alt+enter
register(accounts, sc);
break;
default:
System.out.println("选择输入错误,请重新输入");
break;
}
}
/**
* 用户登录功能
*
* @param accounts 接受账户集合
*/
private static void ogin(ArrayList<Account> accounts, Scanner sc) {
System.out.println("======ATM系统登录界面======");
//判断是否有账户存在
if (accounts.size() == 0) {
System.out.println("没有账户,请开户");
return; //卫语言风格,解决方法的执行
}
while (true) {
System.out.println("请输入卡号");
String carId = sc.next();
//判断卡号是否存在,根据卡号咋子集合中查询
Account account = getAccountByCarId(carId, accounts);
if (account != null) {
while (true) {
System.out.println("请输入密码");
String passwordok = sc.next();
//判断密码是否正确
if (account.getPassword().equals(passwordok)) {
//登录成功
System.out.println(account.getName() + "登录成功,你的卡号是" + account.getId());
//登录后的操作页
showUserCommand(account, sc, accounts);
return;//退出登录
} else {
System.out.println("密码有误");
}
}
} else {
System.out.println("不存在此账户,请重新输入");
}
}
}
/**
* 登录成功后页面展示
*/
private static void showUserCommand(Account account, Scanner sc, ArrayList<Account> accounts) {
while (true) {
System.out.println("======欢迎进入ATM系统用户操作界面=======");
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:
//查询账户
//当前登录的用户信息
shouAccount(account);
break;
case 2:
//存款
depositMoney(account, sc);
break;
case 3:
//取款
drawMoney(account, sc);
break;
case 4:
//转账
transferMonry(account, sc, accounts);
break;
case 5:
//修改密码
updateOassworld(sc, account);
return;
case 6:
//退出
System.out.println("退出成功,欢迎下次登录");
return;//退出当前方法
case 7:
//注销账户
if(deleteAccount(account, sc, accounts)){
return;
}else{
break;
}
default:
System.out.println("没有选项,重新输入");
}
}
}
/**
* 销户
*
* @param account
* @param sc
* @param accounts
*/
private static boolean deleteAccount(Account account, Scanner sc, ArrayList<Account> accounts) {
System.out.println("=======用户销户操作======");
System.out.println("是否要删除账户y/n");
String rs = sc.next();
switch (rs) {
case "y":
//从当前账户集合删除账户对象
if (account.getUnfilled() > 0) {
System.out.println("账户还有余额,不能销户");
} else {
accounts.remove(account);
System.out.println("删除成功");
return true;
}
break;
default:
System.out.println("账户保留");
}
return false;
}
/**
* 修改密码
*
* @param sc
* @param account
*/
private static void updateOassworld(Scanner sc, Account account) {
System.out.println("=======用户修改密码操作======");
while (true) {
System.out.println("请输入密码");
String password = sc.next();
if (account.getPassword().equals(password)) {
//密码认证通过,可以注入给账户
System.out.println("请输入新密码");
String passwordok = sc.next();
System.out.println("请再次输入密码");
String passwordok1 = sc.next();
if (passwordok.equals(passwordok1)) {
account.setPassword(passwordok);
System.out.println("密码修改成功");
return;
} else {
System.out.println("两次输入的密码不一致,请重新输入");
}
} else {
System.out.println("密码不一致,请重新输入");
}
}
}
/**
* 转账
*
* @param account 自己的账户
* @param sc
* @param accounts 所有账户
*/
private static void transferMonry(Account account, Scanner sc, ArrayList<Account> accounts) {
System.out.println("=======用户转账操作======");
//判断是否有多个账户
if (accounts.size() >= 2) {
//判断余额是否足够
if (account.getUnfilled() > 0) {
while (true) {
System.out.println("请输入转账账户:");
String carId = sc.next();
//不能是自己的卡
if (carId.equals(account.getId())) {
System.out.println("不能给自己转账");
continue;//结束当次执行
}
//判断转账账户是否存在
Account account1 = getAccountByCarId(carId, accounts);
if (account1 == null) {
System.out.println("无此账户,请选择");
System.out.println("1.继续");
System.out.println("2.退出");
int command = sc.nextInt();
switch (command) {
case 1:
transferMonry(account, sc, accounts);
break;
case 2:
return;
}
} else {
while (true) {
//判断姓氏认证
String userName = account1.getName();
String tip = "*" + userName.substring(1);
System.out.println("请输入" + tip + "姓氏");
String preName = sc.next();
//是否正确
if (userName.startsWith(preName)) {
while (true) {
System.out.println("请输入转账金额");
int money = sc.nextInt();
while (true) {
System.out.println("请输入密码");
String passward = sc.next();
if (passward.equals(account.getPassword())) {
if (account.getUnfilled() > money) {
account.setUnfilled(account.getUnfilled() - money);
account1.setUnfilled(account1.getUnfilled() + money);
System.out.println("转账成功,你的账号信息如下");
shouAccount(account);
return;
} else {
System.out.println("余额不足,你最多转账" + account.getUnfilled());
break;
}
} else {
System.out.println("密码错误,重新输入");
}
}
}
} else {
System.out.println("输入信息有误");
}
}
}
}
} else {
System.out.println("余额不足,无法转账");
return;
}
} else {
System.out.println("不足2个账户,无法转账");
return;
}
}
/**
* 取款
*
* @param account
* @param sc
*/
private static void drawMoney(Account account, Scanner sc) {
System.out.println("======取款界面=======");
if (account.getUnfilled() == 0) {
System.out.println("当前余额为0,无法取钱");
return;
} else {
while (true) {
System.out.println("输入取款金额:");
double money = sc.nextDouble();
//判断金额是否满足要求
if (money > account.getLimit()) {
System.out.println("超出限额,无法取款,每次最多可以取" + account.getLimit());
} else {
//判断是否超出总余额
if (money > account.getUnfilled()) {
System.out.println("余额不足,当前余额为:" + account.getUnfilled());
} else {
//更新余额
account.setUnfilled(account.getUnfilled() - money);
System.out.println("取款成功,取出" + money);
shouAccount(account);
return; //退出取钱
}
}
}
}
}
/**
* 存款
*
* @param sc
*/
private static void depositMoney(Account account, Scanner sc) {
System.out.println("======用户存钱界面======");
System.out.println("请输入存款金额");
double money = sc.nextDouble();
//更新账户余额
account.setUnfilled(account.getUnfilled() + money);
System.out.println("存钱成功,当前账户信息如下");
shouAccount(account);
}
/**
* 展示账户信息
*/
private static void shouAccount(Account account) {
System.out.println("======账户信息======");
System.out.println("卡号:" + account.getId());
System.out.println("户主:" + account.getName());
System.out.println("余额:" + account.getUnfilled());
System.out.println("限额:" + account.getLimit());
}
/**
* 用户开户功能
*
* @param accounts 接收到账户集合
* @param sc
*/
private static void register(ArrayList accounts, Scanner sc) {
System.out.println("======系统开户操作======");
//创建一个账户对象,用于封装账户信息
Account account = new Account();
//录入信息
System.out.println("请输入账户名");
String name = sc.next();
account.setName(name);
while (true) {
System.out.println("请输入密码");
String password = sc.next();
account.setPassword(password);
System.out.println("请再次输入密码确认");
String passwordok = sc.next();
account.setPassword(passwordok);
if (passwordok.equals(password)) {
//密码认证通过,可以注入给账户
account.setPassword(passwordok);
break;//成功退出循坏
} else {
System.out.println("两次输入的密码不一致,请重新输入");
}
}
while (true) {
System.out.println("请输入账户当次限额");
double quotaMoney = sc.nextDouble();
if (quotaMoney > 0) {
account.setLimit(quotaMoney);
break;
} else {
System.out.println("请重新输入限额");
}
}
//随机一个独立的卡号(8位)
String carID = getRandomCarId(accounts);
account.setId(carID);
//把账户对象添加到账户集合中
accounts.add(account);
System.out.println(name + "开户成功,卡号是" + carID);
}
/**
* 为账户生成8位账户
*
* @return
*/
private static String getRandomCarId(ArrayList<Account> accounts) {
//生成8位数字
Random r = new Random();
while (true) {
String carId = "";
for (int i = 0; i < 8; i++) {
carId += r.nextInt(10);
}
//判断是否重复
//根据卡号查询账户
Account account = getAccountByCarId(carId, accounts);
if (account == null) {
//是个新卡号
return carId;
}
}
}
/**
* 根据卡号查出账户
*
* @param carId 卡号
* @param accounts 全部账户的集合
* @return 账户对象|null
*/
private static Account getAccountByCarId(String carId, ArrayList<Account> accounts) {
for (int i = 0; i < accounts.size(); i++) {
Account account = accounts.get(i);
if (account.getId().equals(carId)) {
return account;
}
}
return null;//没有账户
}
}
ATM系统
最新推荐文章于 2024-08-29 18:44:48 发布