java实战训练
案例 ATM系统开发
需求
完成ATM机的基本功能
public class ATMSystem {
public static void main(String[] args) {
//1.准备系统需要的容器对象,用于存储账户对象
ArrayList<Account> accounts = new ArrayList<>();
//2.准备系统的首页:登陆 开户
showMen(accounts);
}
public static void showMen(ArrayList<Account> accounts) {
System.out.println("=====================欢迎进入首页======================");
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请您输入您想要进行的操作:");
System.out.println("1、登录");
System.out.println("2、开户");
System.out.println("您可以输入命令了:");
int command = sc.nextInt();
switch (command) {
case 1:
//登录
login(accounts, sc);
break;
case 2:
//开户
register(accounts, sc);
break;
default:
System.out.println("您当前输入的命令不被支持!");
}
}
}
/**
* 完成用户登录
*
* @param accounts
*/
private static void login(ArrayList<Account> accounts, Scanner sc) {
//必须系统中存在账户才能登陆
if (accounts.size() == 0) {
//没有任何账户
System.out.println("当前系统中无任何账户,您需要先注册!");
return;
}
//2.让用户键盘录入卡号,根据卡号查询账户对象。
String cardId = null;
while (true) {
System.out.println("请您输入登陆的卡号:");
cardId = sc.next();
//根据卡号查询账户对象
Account acc = getAccountByCardId(cardId, accounts);
//3.判断账户对象是否存在,存在说明账户没问题
if (acc != null) {
//4.让用户继续输入密码
while (true) {
System.out.println("请您输入登陆的密码:");
String passWord = sc.next();
if (acc.getPassWord().equals(passWord)) {
//密码正确,登录成功
//展示系统登陆后的操作界面
System.out.println("恭喜您," + acc.getUserName() + "先生/女士成功进入系统,您的卡号是:" + acc.getCardId());
//展示操作页面
showUserCommand(sc, acc, accounts);
return;//继续结束登录方法
} else {
System.out.println("您的密码有误,请确认!");
}
}
} else {
System.out.println("对不去,不存在该卡号的账户!");
}
}
}
private static void showUserCommand(Scanner sc, Account acc, ArrayList 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("请输入您的操作命令:");
int command = sc.nextInt();
switch (command){
case 1:
//查询账户
showAccount(acc);
break;
case 2:
//存款
depositMoney(acc, sc);
break;
case 3:
//取款
drawMoney(acc, sc);
break;
case 4:
//转账
transferMoney(accounts, acc, sc);
break;
case 5:
//修改密码
updatePassword(acc, sc);
return;
case 6:
//退出
System.out.println("欢迎下次光临!");
return;
case 7:
//注销账户
//当前集合中抹除账户对象即可
accounts.remove(acc);
System.out.println("销户成功!");
return;
default:
System.out.println("您的命令输入有误!");
}
}
}
/**
* 修改密码
* @param acc
*/
private static void updatePassword(Account acc, Scanner sc) {
System.out.println("==============修改密码=============");
while (true) {
System.out.println("请您输入正确的密码!");
String okPassword = sc.next();
if (acc.getPassWord().equals(okPassword)){
//可以输入新密码
while (true) {
System.out.println("请您输入新密码:");
String newPassword = sc.next();
System.out.println("请您确认新密码:");
String okNewPassword = sc.next();
if (newPassword.equals(okNewPassword)){
//修改账户密码是新密码
acc.setPassWord(newPassword);
return;//修改密码结束
}else{
System.out.println("您两次输入的密码不一致!");
}
}
}else{
System.out.println("当前输入密码不正确~~~");
}
}
}
/**
* 转账功能
* @param accounts
* @param acc
* @param sc
*/
private static void transferMoney(ArrayList accounts, Account acc, Scanner sc) {
//1.判断系统中是否有2个账户及以上
if (accounts.size() < 2){
System.out.println("对不起,系统中无其他账户,您不可以转账!");
return;
}else{
//2.判断账户对象是否有钱
if (acc.getMoney() == 0){
System.out.println("对不起,您自己都没钱,就别转了!");
return;
}else{
//3.开始转账逻辑
while (true) {
System.out.println("请您输入对方账户的卡号!");
String cardId = sc.next();
Account account = getAccountByCardId(cardId, accounts);
//判断这个账户是否存在,存在说明对方卡号输入正确
if (account != null){
//判断这个账户对象是否是当前登陆的账户自己
if (account.getCardId().equals(acc.getCardId())) {
//正在给自己转账
System.out.println("您不可以为自己转账!");
}else{
//确认姓氏
String name = "*" + account.getUserName().substring(1);
System.out.println("请您确认【" + name + "】的姓氏!");
String preName = sc.next();
//判断
if (account.getUserName().startsWith(preName)){
//真正开始转账了
while (true) {
System.out.println("请您输入转账金额:");
double money = sc.nextDouble();
//判断金额是否超过余额
if (money > acc.getMoney()){
System.out.println("对不起,您要转账的金额太多,您最多可以转账:" + acc.getMoney());
}else{
//真的可以转了
acc.setMoney(acc.getMoney() - money);
account.setMoney(account.getMoney() + money);
System.out.println("恭喜您,转账成功了,已经为" + account.getUserName() + "转账" + money + "元");
showAccount(acc);
return;
}
}
}else{
System.out.println("对不起,您认证的信息有误~~~");
}
}
}else{
System.out.println("您输入的转账卡号有问题!");
}
}
}
}
}
/**
* 取款
* @param acc
* @param sc
*/
private static void drawMoney(Account acc, Scanner sc) {
System.out.println("==============取款==============");
//1.判断账户是否足够100元
if(acc.getMoney() >= 100){
while (true) {
System.out.println("请你你输入取款的金额:");
double money = sc.nextDouble();
//2.判断金额有没有超过当次金额
if(money > acc.getQuotaMoney()){
System.out.println("您当次取款金额超过每次限额,不要取那么多,最多可以取:" + acc.getQuotaMoney());
}else{
//3.判断当前余额是否足够取钱
if (money > acc.getMoney()){
System.out.println("您的余额不足!");
}else{
acc.setMoney(acc.getMoney() - money);
System.out.println("恭喜您取款成功!当前账户还剩余:" + acc.getMoney());
showAccount(acc);
return;//取钱后结束
}
}
}
}else{
System.out.println("您的金额不超过一百元,就别取了!");
}
}
/**
* 存钱的
* @param acc
*/
private static void depositMoney(Account acc, Scanner sc) {
System.out.println("============存钱操作===========");
System.out.println("请您输入存款金额:");
double money = sc.nextDouble();
//直接把金额修改到账户对象的money属性中去
acc.setMoney(acc.getMoney() + money);
System.out.println("存款完成!");
showAccount(acc);
}
private static void showAccount(Account acc) {
System.out.println("=============当前账户详情==============");
System.out.println("卡号:" + acc.getCardId());
System.out.println("姓名:" + acc.getUserName());
System.out.println("余额:" + acc.getMoney());
System.out.println("当次限额" + acc.getQuotaMoney());
}
/**
* 用户开户功能
*
* @param accounts 账户的集合对象
*/
private static void register(ArrayList<Account> accounts, Scanner sc) {
//2.键盘录入 姓名 密码 确认密码
System.out.println("============用户开户功能=============");
System.out.println("请您输入开户名:");
String name = sc.next();
String password = "";
while (true) {
System.out.println("请您输入开户密码:");
password = sc.next();
System.out.println("请您输入确认密码:");
String okPassword = sc.next();
//判断两次密码输入是否一致
if (okPassword.equals(password)) {
break;
} else {
System.out.println("两次密码必须一致~~~");
}
}
System.out.println("请您输入当此限额:");
double quotaMoney = sc.nextDouble();
//3.生成一个账户的卡号,卡号是8位,而且不能与其他账户卡号重复。
String cardId = createCardId(accounts);
//4.创建一个账户对象封装账户信息
Account account = new Account(cardId, name, password, quotaMoney);
//5.账户对象添加到集合中去
accounts.add(account);
System.out.println("恭喜您开户成功,您的卡号是:" + account.getCardId() + "请您妥善保管!");
}
public static String createCardId(ArrayList<Account> accounts) {
while (true) {
//生成8位随机数字达标卡号
String cardId = "";
Random r = new Random();
for (int i = 0; i < 8; i++) {
cardId += r.nextInt(10);
}
//判断卡号是否重复
Account acc = getAccountByCardId(cardId, accounts);
if (acc == null) {
//说明当前卡号没有重复
return cardId;
}
}
}
public static Account getAccountByCardId(String cardId, ArrayList<Account> accounts) {
//根据卡号查询账户对象
for (int i = 0; i < accounts.size(); i++) {
Account acc = accounts.get(i);
if (acc.getCardId().equals(cardId)) {
return acc;
}
}
return null;//代表查无此物
}
}
public class Account {
private String cardId;
private String userName;
private String passWord;
private double money;
private double quotaMoney;
public Account() {
}
public Account(String cardId, String userName, String passWord, double quotaMoney) {
this.cardId = cardId;
this.userName = userName;
this.passWord = passWord;
this.quotaMoney = quotaMoney;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
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 getQuotaMoney() {
return quotaMoney;
}
public void setQuotaMoney(double quotaMoney) {
this.quotaMoney = quotaMoney;
}
}