暂时还没学到IO流,就写内存里了
//主要功能 注册,登录,以及模拟一些操作
//ATMLauncher.java
package ATMSystem;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATMLauncher {
public static void register(Scanner sc, ArrayList<Account> accounts) {
//创建账户类对象
Account account = new Account();
//设置姓名
System.out.println("请设置您的姓名:");
String username = sc.next();
//存入姓名
account.setUserName(username);
//设置密码并检查
String password;
String passwordAgain;
while (true) {
System.out.println("请设置您的密码:");
password = sc.next();
System.out.println("请再次输入您的密码:");
passwordAgain = sc.next();
if (password.equals(passwordAgain)) {
//存入密码
account.setPassword(password);
break;
} else {
System.out.println("两次密码输入不一致,请重新输入!");
}
}
//设置余额
double balance = 0;
account.setBalance(balance);
//设置限额
System.out.println("请设置您的单次取款限额:");
double limitTakeOut = sc.nextDouble();
account.setLimitTakeOut(limitTakeOut);
//生成一个随机8位ID
String IDcard = getRandomIDcard(accounts);
account.setIDcard(IDcard);
System.out.println(account.getUserName() + "先生/女士," + "恭喜您创建账户成功!您的卡号是:" + account.getIDcard());
System.out.println("请使用您的卡号登录系统!");
accounts.add(account);
}
private static Account getUserByIDcard(String IDcard, ArrayList<Account> accounts) {
for (Account temp : accounts) {
String tempIDcard = temp.getIDcard();
if (tempIDcard.equals(IDcard)) {
//用户已经存在!
return temp;
}
}
return null;
}
private static String getRandomIDcard(ArrayList<Account> accounts) {
Random r = new Random();
while (true) {
String IDcard = "";
for (int i = 0; i < 8; i++) {
IDcard += r.nextInt(10);
}
if (getUserByIDcard(IDcard, accounts) == null) {
return IDcard;//退出函数
}
}
}
public static void main(String[] args) {
//一个集合存储Account 对象
ArrayList<Account> accounts = new ArrayList<>();
//一个扫描器
Scanner sc = new Scanner(System.in);
//开户界面
while (true) {
System.out.println("==================ATM系统=====================");
System.out.println("1.登录系统");
System.out.println("2.注册账户");
System.out.println("请选择你的操作:");
int choice = sc.nextInt();
switch (choice) {
case 1://登录系统
login(sc, accounts);
break;
case 2://注册账户
register(sc, accounts);
break;
default:
System.out.println("输入有误,重新选择!");
break;
}
}
}
private static void login(Scanner sc, ArrayList<Account> accounts) {
if (accounts.size() > 0) {
while (true) {
System.out.println("请输入您的账号:");
String IDcard = sc.next();
System.out.println("请输入您的密码:");
String password = sc.next();
//账号是否存在
Account acc = getUserByIDcard(IDcard, accounts);
if (acc == null) {
System.out.println("账号不存在!");
} else if (acc.getPassword().equals(password)) {
System.out.println("登录成功!欢迎使用系统");
showMenu(sc, acc, accounts);
return;
} else {
System.out.println("密码错误!请重新输入!");
}
}
} else {
System.out.println("系统中没有账户,请先创建一个账户!");
}
}
private static void showMenu(Scanner sc, Account acc, 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("8.注销账户");
System.out.print("选择:");
int choice = sc.nextInt();
switch (choice) {
case 1://查询功能
showUserInfo(acc);
break;
case 2://存款
saveMoney(acc, sc);
break;
case 3://取款
getMoney(acc, sc);
break;
case 4://转账
transMoney(acc, sc, accounts);
break;
case 5://修改密码
updatePassword(sc, acc);
break;
case 6:
updateLimitTakeOut(sc, acc);
break;
case 7://退出
System.out.println("您的账户已经退出,谢谢使用!");
return;
case 8:
accounts.remove(acc);
System.out.println("您的账户已经完成销毁,不可以进行登录了!");
return;
default:
System.out.println("指令有误!");
}
}
}
private static void updateLimitTakeOut(Scanner sc, Account acc) {
System.out.println("=================更改取款限额====================");
System.out.println("将取款限额更改为:");
double limitTakeOut = sc.nextDouble();
acc.setLimitTakeOut(limitTakeOut);
System.out.println("更改成功!");
}
private static void updatePassword(Scanner sc, Account acc) {
System.out.println("=================更改密码====================");
System.out.print("输入当前密码:");
String password = sc.next();
if (acc.getPassword().equals(password)) {
System.out.print("输入要更改的密码:");
String p1 = sc.next();
System.out.print("再次输入要更改的密码:");
String p2 = sc.next();
if (p1.equals(p2)) {
acc.setPassword(p1);
System.out.println("更改成功!");
return;
} else return;
}
}
private static void transMoney(Account acc, Scanner sc, ArrayList<Account> accounts) {
double balance = acc.getBalance();
String othersID;
Account otherPeople;
if (balance != 0) {
if (accounts.size() >= 2) {
while (true) {
System.out.print("输入接收者的ATM卡号:");
othersID = sc.next();
otherPeople = getUserByIDcard(othersID, accounts);
if (otherPeople == null) {
System.out.println("该用户不存在!");
} else if (othersID.equals(acc.getIDcard())) {
System.out.println("不能给自己转账!");
} else {
System.out.println("您的余额为:" + acc.getBalance());
break;
}}
double transMoney = 0;
while (true) {
System.out.print("输入转账金额:");
transMoney = sc.nextDouble();
if (transMoney > acc.getBalance()) {
System.out.println("余额不足,无法转账!");
} else {
break;
}
}
String name = "*" + otherPeople.getUserName().substring(1);
System.out.println("即将转入金额" + transMoney + "至" + name + "的账户");
System.out.println("请输入其姓氏进行确认:");
String firstName = sc.next();
if (otherPeople.getUserName().startsWith(firstName)) {
System.out.println("转账成功!");
otherPeople.setBalance(otherPeople.getBalance()+transMoney);
acc.setBalance(acc.getBalance() - transMoney);
System.out.println("您当前余额:" + acc.getBalance());
return;
} else {
System.out.println("姓氏输入错误!程序结束!");
return;
}
} else {
System.out.println("ATM系统还没有其他用户!无法转账!");
return;
}
} else {
System.out.println("您的余额为0,请先存入余额再转账!");
return;
}
}
private static void getMoney(Account acc, Scanner sc) {
double balance = acc.getBalance();
if (balance != 0) {
System.out.println("您的当前余额:" + balance);
while (true) {
System.out.print("输入取出金额:");
double money = sc.nextDouble();
if (money > balance) {
System.out.println("您的余额不足!");
} else if (money > acc.getLimitTakeOut()) {
System.out.println("超过单次取款最大限额" + acc.getLimitTakeOut() + "请重试");
} else {
acc.setBalance(balance - money);
System.out.println("您成功取得现金:" + money + "元" + "账户剩余余额:" + acc.getBalance() + "元。");
return;
}
}
} else {
System.out.println("您的余额为0,请先存入余额!");
}
}
private static void saveMoney(Account acc, Scanner sc) {
double balance = acc.getBalance();
System.out.println("您的当前余额:" + balance);
System.out.print("输入存入金额:");
double money = sc.nextDouble();
acc.setBalance(balance + money);
System.out.println("存款成功!当前余额:" + acc.getBalance());
return;
}
private static void showUserInfo(Account acc) {
System.out.println("==============您的信息如下==================");
System.out.println("姓名:" + acc.getUserName());
System.out.println("账户ID:" + acc.getIDcard());
System.out.println("余额:" + acc.getBalance());
System.out.println("单次取款限额:" + acc.getLimitTakeOut());
return;
}
}
//Account.java
package ATMSystem;
public class Account {
private String IDcard;//卡号
private String password;//密码
private String userName;//用户姓名
private double balance;//余额
private double limitTakeOut;//取款限额
public String getIDcard() {
return IDcard;
}
public void setIDcard(String IDcard) {
this.IDcard = IDcard;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getLimitTakeOut() {
return limitTakeOut;
}
public void setLimitTakeOut(double limitTakeOut) {
this.limitTakeOut = limitTakeOut;
}
}