- 用户注册
- 用户登录
- 查询账户信息
- 存款
- 取款
- 转账
- 修改密码
- 退出系统
- 注销当前账户
用户注册
// 注册
public static void register(ArrayList<Account> acc, Scanner sc) {
Account a = new Account();
System.out.println("请输入你的姓名");
String name = sc.nextLine();
a.setName(name);
while (true) {
System.out.println("请输入你的密码");
String password = sc.nextLine();
System.out.println("请再次确认密码");
String okPassword = sc.nextLine();
if (!password.equals(okPassword)) {
System.out.println("两次密码不一致请重新输入");
} else {
a.setPassword(password);
break;
}
}
while (true) {
System.out.println("请设置当日取现额度");
double limit = sc.nextDouble();
if (limit > 0) {
a.setLimited(limit);
break;
} else {
System.out.println("当日取现额度不能为负数");
}
}
String cardId = getCardId(acc, 8);
a.setId(cardId);
acc.add(a);
System.out.println(a.getName() + "贵宾,你的账户已经开卡成功,你的卡号是:" + a.getId());
}
用户登录
public static void login(ArrayList<Account> acc, Scanner sc) {
System.out.println("================ 登录界面 =================");
if (acc.size() == 0) {
System.out.println("还没有任何卡的信息存入,请先去开通");
return;
}
while (true) {
System.out.println("请输入卡号");
String id = sc.nextLine();
Account a = contains(acc, id);
if (a == null) {
System.out.println("不存在此卡号,请重新输入");
} else {
while (true) {
System.out.println("请输入密码");
String password = sc.nextLine();
if (!a.getPassword().equals(password)) {
System.out.println("密码有误,请重新输入");
} else {
System.out.println(a.getName() + "贵宾,欢迎你进入系统,你的卡号是:" + a.getId());
show(a, sc, acc);
return;
}
}
}
}
}
查询账户信息
// 查询信息
private static void select(Account acc) {
System.out.println("========== 欢迎进入用户详情界面 ==========");
System.out.println("账户信息如下:");
System.out.println("卡号:" + acc.getId());
System.out.println("姓名:" + acc.getName());
System.out.println("余额:" + acc.getMoney());
System.out.println("当次取现额度:" + acc.getLimited());
}
存款
// 存款
private static void add(ArrayList<Account> list, Account acc, Scanner sc) {
System.out.println("========== 欢迎进入用户存款界面 ==========");
while (true) {
System.out.println("请输入存储的金额");
double money = sc.nextDouble();
if (money <= 0) {
System.out.println("无法存入负金额");
} else {
acc.setMoney(acc.getMoney()+money);
int index = search(list, acc);
if (index != -1) {
list.set(index, acc);
System.out.println("你已经存款成功");
select(acc);
return;
}
}
}
}
取款
// 取款
private static void with(ArrayList<Account> list, Account acc, Scanner sc) {
System.out.println("========== 欢迎进入用户取款界面 ==========");
if (acc.getMoney() < 100) {
System.out.println("账户金额不足100元,请先去存款吧!");
return;
}
while (true) {
System.out.println("请输入取款的金额");
double money = sc.nextDouble();
if (money > acc.getMoney()) {
System.out.println("对不起,你的卡没有这么多钱,无法进行取款");
} else if (money > acc.getLimited()) {
System.out.println("你当前取款超过了当次限额");
} else {
System.out.println("取款成功");
int index = search(list, acc);
acc.setMoney(acc.getMoney() - money);
list.set(index, acc);
return;
}
}
}
转账
// 转账
private static void transfer(ArrayList<Account> list, Account acc, Scanner sc) {
System.out.println("========== 欢迎进入用户转账界面 ==========");
if (list.size() < 2) {
System.out.println("当前系统,账户不足2个,无法进行转账");
return;
} else if (acc.getMoney() < 100) {
System.out.println("账户金额不足,无法进行转账");
return;
}
while (true) {
System.out.println("请输入转账的卡号");
String id = sc.nextLine();
Account a = contains(list, id);
if (a == null) {
System.out.println("不存在此账号,请重新输入");
} else {
while (true) {
String temp = "*" + a.getName().substring(1);
System.out.println("你当前要为" + temp + "转账!");
System.out.println("请输入姓氏进行确认:");
String ch = sc.nextLine();
if(ch.equalsIgnoreCase(a.getName().charAt(0)+"")){
while (true) {
System.out.println("请输入转账的金额");
double money = sc.nextDouble();
if(money>acc.getMoney()){
System.out.println("账户没有这么多钱,无法进行转账");
}else{
acc.setMoney(acc.getMoney()-money);
int index1 = search(list, acc);
a.setMoney(a.getMoney()+money);
int index2 = search(list, a);
list.set(index1,acc);
list.set(index2,a);
System.out.println("转账成功");
return ;
}
}
}else{
System.out.println("输入错误,请重新输入");
}
}
}
}
}
修改密码
// 修改密码
private static void updatePassword(ArrayList<Account> list, Account acc, Scanner sc) {
System.out.println("========== 欢迎进入用户转账界面 ==========");
while (true) {
System.out.println("请输入当前账户密码");
String password = sc.nextLine();
if(!password.equals(acc.getPassword())){
System.out.println("当前账号密码不正确");
}else{
while (true) {
System.out.println("请输入新的密码");
String pwd = sc.nextLine();
System.out.println("请你确认下新的密码");
String okpwd = sc.nextLine();
if(!pwd.equals(okpwd)){
System.out.println("两次密码不一致,请重新输入");
}else{
int index = search(list, acc);
acc.setPassword(pwd);
list.set(index,acc);
System.out.println("密码修改成功,请重新登录");
return;
}
}
}
}
}
销毁
// 销毁
private static void destory(ArrayList<Account> list, Account acc) {
System.out.println("========== 欢迎进入用户账户销毁界面 ==========");
boolean flag = list.remove(acc);
if(flag){
System.out.println("销毁成功");
}else
System.out.println("销毁失败");
}
完整版代码
public class Account {
private String Id;
private String name;
private String password;
private double money;
private double limited;
public Account() {
}
public Account(String Id, String name, String password, double money, double limited) {
this.Id = Id;
this.name = name;
this.password = password;
this.money = money;
this.limited = limited;
}
public String getId() {
return Id;
}
public void setId(String Id) {
this.Id = Id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 getLimited() {
return limited;
}
public void setLimited(double limited) {
this.limited = limited;
}
public String toString() {
return "Account{Id = " + Id + ", name = " + name + ", password = " + password + ", money = " + money + ", limited = " + limited + "}";
}
}
public class ATMSystem {
public static void main(String[] args) {
ArrayList<Account> list = new ArrayList<>();
list.add(new Account("68539113", "z", "1", 200, 1));
list.add(new Account("68539114", "a", "1", 10, 1));
domain(list);
}
// 选择菜单
public static void domain(ArrayList<Account> acc) {
Scanner sc = new Scanner(System.in);
while (true) {
menu();
String type = sc.nextLine();
switch (type) {
case "1" -> {
// 登录
login(acc, sc);
}
case "2" -> {
// 注册
register(acc, sc);
}
default -> {
System.out.println("选择错误,请重新选择");
}
}
}
}
// 登录
public static void login(ArrayList<Account> acc, Scanner sc) {
System.out.println("================ 登录界面 =================");
if (acc.size() == 0) {
System.out.println("还没有任何卡的信息存入,请先去开通");
return;
}
while (true) {
System.out.println("请输入卡号");
String id = sc.nextLine();
Account a = contains(acc, id);
if (a == null) {
System.out.println("不存在此卡号,请重新输入");
} else {
while (true) {
System.out.println("请输入密码");
String password = sc.nextLine();
if (!a.getPassword().equals(password)) {
System.out.println("密码有误,请重新输入");
} else {
System.out.println(a.getName() + "贵宾,欢迎你进入系统,你的卡号是:" + a.getId());
show(a, sc, acc);
return;
}
}
}
}
}
// 系统页面
public static void show(Account acc, Scanner sc, ArrayList<Account> list) {
while (true) {
System.out.println("================= 欢迎进入用户操作界面 ===================");
systemMenu();
String type = sc.nextLine();
switch (type) {
case "1" -> {
// 查询
select(acc);
}
case "2" -> {
// 取款
add(list, acc, sc);
}
case "3" -> {
// 存款
with(list, acc, sc);
}
case "4" -> {
// 转账
transfer(list, acc, sc);
}
case "5" -> {
// 修改密码
updatePassword(list,acc,sc);
return;
}
case "6" -> {
// 退出
return;
}
case "7" -> {
// 销毁账户
destory(list,acc);
return;
}
default -> System.out.println("选择错误,请重新选择:>");
}
}
}
// 销毁
private static void destory(ArrayList<Account> list, Account acc) {
System.out.println("========== 欢迎进入用户账户销毁界面 ==========");
boolean flag = list.remove(acc);
if(flag){
System.out.println("销毁成功");
}else
System.out.println("销毁失败");
}
// 修改密码
private static void updatePassword(ArrayList<Account> list, Account acc, Scanner sc) {
System.out.println("========== 欢迎进入用户转账界面 ==========");
while (true) {
System.out.println("请输入当前账户密码");
String password = sc.nextLine();
if(!password.equals(acc.getPassword())){
System.out.println("当前账号密码不正确");
}else{
while (true) {
System.out.println("请输入新的密码");
String pwd = sc.nextLine();
System.out.println("请你确认下新的密码");
String okpwd = sc.nextLine();
if(!pwd.equals(okpwd)){
System.out.println("两次密码不一致,请重新输入");
}else{
int index = search(list, acc);
acc.setPassword(pwd);
list.set(index,acc);
System.out.println("密码修改成功,请重新登录");
return;
}
}
}
}
}
// 转账
private static void transfer(ArrayList<Account> list, Account acc, Scanner sc) {
System.out.println("========== 欢迎进入用户转账界面 ==========");
if (list.size() < 2) {
System.out.println("当前系统,账户不足2个,无法进行转账");
return;
} else if (acc.getMoney() < 100) {
System.out.println("账户金额不足,无法进行转账");
return;
}
while (true) {
System.out.println("请输入转账的卡号");
String id = sc.nextLine();
Account a = contains(list, id);
if (a == null) {
System.out.println("不存在此账号,请重新输入");
} else {
while (true) {
String temp = "*" + a.getName().substring(1);
System.out.println("你当前要为" + temp + "转账!");
System.out.println("请输入姓氏进行确认:");
String ch = sc.nextLine();
if(ch.equalsIgnoreCase(a.getName().charAt(0)+"")){
while (true) {
System.out.println("请输入转账的金额");
double money = sc.nextDouble();
if(money>acc.getMoney()){
System.out.println("账户没有这么多钱,无法进行转账");
}else{
acc.setMoney(acc.getMoney()-money);
int index1 = search(list, acc);
a.setMoney(a.getMoney()+money);
int index2 = search(list, a);
list.set(index1,acc);
list.set(index2,a);
System.out.println("转账成功");
return ;
}
}
}else{
System.out.println("输入错误,请重新输入");
}
}
}
}
}
// 取款
private static void with(ArrayList<Account> list, Account acc, Scanner sc) {
System.out.println("========== 欢迎进入用户取款界面 ==========");
if (acc.getMoney() < 100) {
System.out.println("账户金额不足100元,请先去存款吧!");
return;
}
while (true) {
System.out.println("请输入取款的金额");
double money = sc.nextDouble();
if (money > acc.getMoney()) {
System.out.println("对不起,你的卡没有这么多钱,无法进行取款");
} else if (money > acc.getLimited()) {
System.out.println("你当前取款超过了当次限额");
} else {
System.out.println("取款成功");
int index = search(list, acc);
acc.setMoney(acc.getMoney() - money);
list.set(index, acc);
return;
}
}
}
// 存款
private static void add(ArrayList<Account> list, Account acc, Scanner sc) {
System.out.println("========== 欢迎进入用户存款界面 ==========");
while (true) {
System.out.println("请输入存储的金额");
double money = sc.nextDouble();
if (money <= 0) {
System.out.println("无法存入负金额");
} else {
acc.setMoney(acc.getMoney()+money);
int index = search(list, acc);
if (index != -1) {
list.set(index, acc);
System.out.println("你已经存款成功");
select(acc);
return;
}
}
}
}
// 查询每张卡的位置
public static int search(ArrayList<Account> list, Account acc) {
for (int i = 0; i < list.size(); i++) {
Account account = list.get(i);
if (account.equals(acc)) {
return i;
}
}
return -1;
}
// 查询信息
private static void select(Account acc) {
System.out.println("========== 欢迎进入用户详情界面 ==========");
System.out.println("账户信息如下:");
System.out.println("卡号:" + acc.getId());
System.out.println("姓名:" + acc.getName());
System.out.println("余额:" + acc.getMoney());
System.out.println("当次取现额度:" + acc.getLimited());
}
// 系统菜单
public static void systemMenu() {
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 static void register(ArrayList<Account> acc, Scanner sc) {
Account a = new Account();
System.out.println("请输入你的姓名");
String name = sc.nextLine();
a.setName(name);
while (true) {
System.out.println("请输入你的密码");
String password = sc.nextLine();
System.out.println("请再次确认密码");
String okPassword = sc.nextLine();
if (!password.equals(okPassword)) {
System.out.println("两次密码不一致请重新输入");
} else {
a.setPassword(password);
break;
}
}
while (true) {
System.out.println("请设置当日取现额度");
double limit = sc.nextDouble();
if (limit > 0) {
a.setLimited(limit);
break;
} else {
System.out.println("当日取现额度不能为负数");
}
}
String cardId = getCardId(acc, 8);
a.setId(cardId);
acc.add(a);
System.out.println(a.getName() + "贵宾,你的账户已经开卡成功,你的卡号是:" + a.getId());
}
// 创建卡号
public static String getCardId(ArrayList<Account> acc, int n) {
Random r = new Random();
StringBuilder sb = new StringBuilder();
while (true) {
for (int i = 0; i < n; i++) {
sb.append((r.nextInt(9) + 1) + "");
}
if (contains(acc, sb.toString()) == null) {
return sb.toString();
}
}
}
// 判断卡号是否唯一
public static Account contains(ArrayList<Account> acc, String id) {
for (int i = 0; i < acc.size(); i++) {
Account account = acc.get(i);
if (id.equals(account.getId())) {
return account;
}
}
return null;
}
// 菜单
public static void menu() {
System.out.println("======= 欢迎进入ATM系统 ========================");
System.out.println("1. 登录账户");
System.out.println("2. 注册开户");
System.out.println("请选择:>");
}
}