1.Account类:
package com.china.atm;
public class Account {
private long CardNumber;
private String UserName;
private double balance; //余额
private double CashWithdrawalLimit; //取现额度
private String PassWord;
public Account() {
}
public Account(long cardNumber, String userName, double balance, double cashWithdrawalLimit, String passWord) {
CardNumber = cardNumber;
UserName = userName;
this.balance = balance;
CashWithdrawalLimit = cashWithdrawalLimit;
PassWord = passWord;
}
public long getCardNumber() {
return CardNumber;
}
public void setCardNumber(long cardNumber) {
CardNumber = cardNumber;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getCashWithdrawalLimit() {
return CashWithdrawalLimit;
}
public void setCashWithdrawalLimit(double cashWithdrawalLimit) {
CashWithdrawalLimit = cashWithdrawalLimit;
}
public String getPassWord() {
return PassWord;
}
public void setPassWord(String passWord) {
PassWord = passWord;
}
}
2.AccountDemo类:
package com.china.atm;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class AccountDemo {
public static void main(String[] args) {
ArrayList<Account> accounts = new ArrayList<>();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("=====欢迎您进入银行ATM系统=====\n1.登录账户\n2.注册账户\n3.退出系统\n请输入命令1 2 选择对应的操作:\t");
int operation = sc.nextInt();
switch (operation) {
case 1:
signIn(accounts, sc); //登录
break;
case 2:
register(accounts, sc); //注册
break;
case 3:
System.out.println("bye");
System.exit(0); //退出虚拟机
}
}
}
//登录
public static void signIn(ArrayList<Account> accounts, Scanner sc) {
System.out.print("=====银行ATM系统登录页面=====");
while (true) {
System.out.print("请您输入您的卡号: ");
long CardNumber = sc.nextLong();
Account acc = checkNumber(accounts, CardNumber);
if (acc != null) {
System.out.print("请输入您的密码: ");
String pwd = sc.next();
if (pwd.equals(acc.getPassWord())) {
System.out.println(acc.getUserName() + "贵宾,欢迎您进入系统,您的卡号: " + CardNumber);
userActionPage(acc, sc, accounts);
return;
} else {
System.out.println("您输入的密码有误,请确认!");
}
} else {
System.out.println("账号不存在!");
}
}
}
//用户操作页面
public static void userActionPage(Account a, Scanner sc, ArrayList<Account> accounts) {
end:
while (true) {
System.out.print("=====欢迎您进入银行用户操作界面=====\n" +
"1 查询\n" +
"2 存款\n" +
"3 取款\n" +
"4 转账\n" +
"5 修改密码\n" +
"6 退出\n" +
"7 注销当前账户\n" +
"您可以选择您的操作了: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("查询");
query(a);
break;
case 2:
System.out.println("存款");
deposit(a, sc);
break;
case 3:
System.out.println("取款");
withdrawal(a, sc);
break;
case 4:
transfer(a, sc, accounts);
break;
case 5:
System.out.println("修改密码");
changePassword(a, sc, accounts);
break end;
case 6:
System.out.println("退出");
break end;
case 7:
System.out.println("注销当前账户");
accounts.remove(a);
System.out.println("注销成功!");
break end;
}
}
}
//转账
public static void transfer(Account a, Scanner sc, ArrayList<Account> accounts) {
System.out.println("=====欢迎进入银行用户转账页面=====");
while (true) {
if (accounts.size() < 2) {
System.out.println("当前系统,账户不足两个,不能转账!");
return;
}
if (a.getBalance() <= 0) {
System.out.println("您自己都没钱,就别转了!");
return;
}
System.out.println("请您输入转账的账号卡号: ");
long CardNumber = sc.nextLong();
Account acc = checkNumber(accounts, CardNumber);
if (acc != null) {
if (a.getCardNumber() != CardNumber) {
String getMoneyName = "*" + acc.getUserName().substring(1);
System.out.println("您当前要向" + getMoneyName + "转账");
System.out.print("请输入姓氏确认: ");
String okName = sc.next();
if (okName.equals(acc.getUserName())) {
while (true) {
System.out.println("请输入转账金额: ");
Double money = sc.nextDouble();
if (money > a.getCashWithdrawalLimit()) {
System.out.println("超出每日限额!");
break;
}
if (money > a.getBalance()) {
System.out.println("你哪有那么多给人家!");
} else {
a.setBalance(a.getBalance() - money);
acc.setBalance(acc.getBalance() + money);
System.out.println("你成功向" + getMoneyName + "转了" + money + "元");
query(a);
return;
}
}
} else {
System.out.println("姓氏");
}
} else {
System.out.println("不能向自己转账!");
}
} else {
System.out.println("您输入的账号不存在");
}
}
}
//用户查询
public static void query(Account a) {
System.out.println("=====欢迎进入银行用户详情页面=====\n您的账号信息如下: ");
System.out.println("卡号: " + a.getCardNumber());
System.out.println("姓名: " + a.getUserName());
System.out.println("余额: " + a.getBalance());
System.out.println("当次取现额度: " + a.getCashWithdrawalLimit());
}
//用户存款
public static void deposit(Account a, Scanner sc) {
System.out.println("=====欢迎进入银行用户存款页面=====");
System.out.println("请输入存款金额: ");
int amountDd = sc.nextInt(); 输入存款金额
a.setBalance(a.getBalance() + amountDd);
System.out.println("存储成功" +
"您当前余额为: " + a.getBalance());
}
//用户取款
public static void withdrawal(Account a, Scanner sc) {
System.out.println("=====欢迎进入银行用户取款页面=====");
if (a.getBalance() < 100) {
System.out.println("账户余额不足100元,先去存钱吧!");
return;
}
while (true) {
System.out.println("请输入取款金额");
int wdAmount = sc.nextInt(); //输入取款金额
if (wdAmount > a.getCashWithdrawalLimit()) { //判断是否超当日限额,是则提示并回到死循环
System.out.println("您当前取款金额超过了当次限额!");
} else {
if (wdAmount > a.getBalance()) { //判断是否超余额,是则提示并回到死循环
System.out.println("您的账户余额不足");
} else {
a.setBalance(a.getBalance() - wdAmount); //没有超过余额,提示并跳出死循环
System.out.println("您已取款成功!~~~\n" + "账户余额: " + a.getBalance());
break;
}
}
}
}
//开户功能
public static void register(ArrayList<Account> accounts, Scanner sc) {
System.out.println("=====欢迎您进入银行用户办卡界面=====" + "\n请输入您的姓名:");
String name = sc.next();
String pwd;
while (true) {
System.out.println("请输入您的密码:");
pwd = sc.next();
System.out.println("请再次确认密码:");
String ConfirmPwd = sc.next();
if (pwd.equals(ConfirmPwd)) break;
System.out.println("密码不一致!");
}
System.out.println("请设置当日取现额度:");
Double CashWithdrawalLimit = sc.nextDouble();
long CardNumber = GenerateCardNumber(accounts); //拿取随机生成的卡号
Account a = new Account(CardNumber, name, 0.0, CashWithdrawalLimit, pwd);
accounts.add(a);
System.out.println(name + "贵宾,您的账户已经开卡成功,您的卡号是:" + CardNumber);
}
//生成随机卡号
public static long GenerateCardNumber(ArrayList<Account> accounts) {
Random r = new Random();
long CardNumber = 0;
while (true) {
//随机生成一个8位数
CardNumber = 0;
for (int i = 0; i < 8; i++) {
int num = r.nextInt(9) + 1;
CardNumber += num * Math.pow(10, i);
/*pow函数表示求次数方幂
CardNumber += num * Math.pow(10, i)表示累积如:
1*1+2*10+3*100........
*/
}
System.out.println(CardNumber);
//循环所有账户 判断产生的卡号是否已存在
boolean flag = true;
for (int i = 0; i < accounts.size(); i++) {
if (accounts.get(i).getCardNumber() == CardNumber) {
flag = false;
break;
}
}
if (flag) { //如果flag还为true说明没重复,终止死循环
break;
}
}
return CardNumber;
}
//修改密码
public static void changePassword(Account a, Scanner sc, ArrayList<Account> accounts) {
System.out.println("=====欢迎您进入银行密码修改界面=====");
int index = accounts.lastIndexOf(a);
while (true) {
System.out.println("请您输入当前账户的密码: ");
String pwd = sc.next();
if (a.getPassWord().equals(pwd)) {
while (true) {
System.out.println("请您输入新的密码: ");
String newPwd = sc.next();
System.out.println("请您确认下新密码: ");
String pwdConfirmation = sc.next();
if (newPwd.equals(pwdConfirmation)) {
a.setPassWord(newPwd);
accounts.set(index, a);
System.out.println("密码修改成功,请您重新登录!");
return;
}
System.out.println("密码确认有误,请重新输入!");
}
}
System.out.println("密码错误!");
}
}
//根据卡号返回Account对象
public static Account checkNumber(ArrayList<Account> accounts, long CarNumber) {
for (int i = 0; i < accounts.size(); i++) {
Account acc = accounts.get(i);
if (acc.getCardNumber() == CarNumber) {
return acc;
}
}
return null;//遍历完后都匹配不到,说明没有该卡号对应的账户.
}
}