文章目录
- 前言
- 1.创立Account类
- 2.创立集合存储用户对象地址
- 3.欢迎页面(首页)
- 4.开户功能
- 4.1.为账户随机一个8位且与其他账户的卡号不重复的号码
- 5.登录功能
- 5.0.遍历查找该cardId是否存在于集合的对象里
- 5.1.进入登陆后的系统界面
- 5.1.1.银行用户详情界面
- 5.1.2.存钱界面
- 5.1.3.取款界面
- 5.1.4.转账界面
- 5.1.5.密码修改页面
- 5.1.6.销户操作
- 最后(ATMSystem.java资源)
- 总结
前言
在学到了ATM系统,忍不住自己重新实现了一次,搞定之后真的蛮熟悉面向对象编程了的,嘿嘿。
1.创立Account类
使用标准Java bean创建类(私有成员变量、无参构造方法、get and set方法)无参构造方法
默认存在,我也没写有参数构造方法
,所以就不用特意去写一遍无参构造方法
了~
Account.java
package com.itheima.atm;
public class Account {
private String cardId;
private String userName;
private String password;
private double money;//余额
private double quotoMoney;//取现额度
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 getQuotoMoney() {
return quotoMoney;
}
public void setQuotoMoney(double quotoMoney) {
this.quotoMoney = quotoMoney;
}
}
2.创立集合存储用户对象地址
ArrayList<Account> accounts = new ArrayList<>();
3.欢迎页面(首页)
ATMSystem.java 的 main 方法
public static void main(String[] args) {
// 1.创立Account类
// 2.创立集合存储用户对象地址
ArrayList<Account> accounts = new ArrayList<>();
// 3.欢迎页面(首页)
while (true) {
System.out.println("================黑马ATM系统==================");
System.out.println("1、开户功能");
System.out.println("2、登陆账户");
Scanner sc = new Scanner(System.in);
System.out.print("请输入命令1、2选择对应的操作:");
int command = sc.nextInt();
switch (command) {
case 1:
//开户功能
createAccount(accounts);
break;
case 2:
//登陆账户
login(accounts);
break;
default:
System.out.println("对不起,您输入的选择有误!");
break;
}
}
}
4.开户功能
public static void createAccount(ArrayList<Account> accounts) {
System.out.println("================欢迎进入黑马银行用户办卡界面==================");
//创建一个用户对象,用于后期封装账户信息
Account account = new Account();
//录入当前这个账户的信息,注入到账户对象去
Scanner sc = new Scanner(System.in);
System.out.print("请您输入您的姓名:");
account.setUserName(sc.next());
//循环执行直到两次密码相同
while (true) {
System.out.print("请您输入账户密码:");
String psw1 = sc.next();
System.out.print("请您输入确认密码:");
String psw2 = sc.next();
if (psw1.equals(psw2)) {
//密码认证通过,可以诸注入对象
account.setPassword(psw1);
break;//密码录入成功,没必要继续了
} else System.out.println("对不起,您输入的两次密码不一致!,请重新输入~~");
}
//循环执行直到限额输入正确
while (true) {
System.out.print("请设置账户当日取现额度:");
double quotoMoney = sc.nextDouble();
if (quotoMoney >= 0) {
account.setQuotoMoney(quotoMoney);
break;
} else System.out.println("您输入的账户限额小于0,有误!请重新输入~");
}
//为账户随机一个8位且与其他账户的卡号不重复的号码
String cardId = cardIdRandom(accounts, 8);
account.setCardId(cardId);
//往账户集合中存进此账户的地址
if (accounts.add(account))
System.out.println(account.getUserName() + "先生/女士,您的账户已经开卡成功,您的卡号是:" + account.getCardId());
}
4.1.为账户随机一个8位且与其他账户的卡号不重复的号码
public static String cardIdRandom(ArrayList<Account> accounts, int num) {
Random r = new Random();
while (true) {
//1.生成8位卡号
String cardId = "";
for (int i = 0; i < num; i++) {
cardId += r.nextInt(10);//注意添加边界值! 0-9
}
// 2.判断与其他账户卡号是否相冲突
Account acc = getAccountById(accounts, cardId);
if (acc == null) return cardId;
}
}
5.登录功能
public static void login(ArrayList<Account> accounts) {
System.out.println("================欢迎进入黑马银行用户登陆界面==================");
//输入当前这个账户的信息,并查询是否存在
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("请输入您的卡号:");
String cardId = sc.next();
//查询该卡号是否存在
//存在返回持有该卡号的account,不存在返回null
Account account = getAccountById(accounts, cardId);
//存在,继续输入密码
if (account != null) {
while (true) {
System.out.print("请输入您的密码:");
String psw = sc.next();
if (psw.equals(account.getPassword())) {
System.out.println(account.getUserName() + "先生/女士,欢迎您进入系统,您的卡号:" + account.getCardId());
//进入用户登陆后的界面
System.out.println("================欢迎进入黑马银行ATM系统==================");
showUserCommand(accounts, account, sc);
return;
} else {
System.out.println("密码有误!请重新输入!");
}
}
} else {
System.out.println("对不起,系统中不存在该账户卡号,请重新输入~~");
}
}
}
5.0.遍历查找该cardId是否存在于集合的对象里
public static Account getAccountById(ArrayList<Account> accounts, String cardId) {
//遍历查找该cardId是否存在于集合的对象里
for (int i = 0; i < accounts.size(); i++) {
Account account = accounts.get(i);
if (account.getCardId().equals(cardId)) {
//账户已存在
return account;
}
}
return null;
}
5.1.进入登陆后的系统界面
public static void showUserCommand(ArrayList<Account> accounts, Account account, Scanner sc) {
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.print("您可以继续选择功能进行操作了:");
int command = sc.nextInt();
switch (command) {
case 1:
//查询本卡信息
System.out.println("================欢迎进入黑马银行用户详情界面==================");
queryAccount(account);
break;
case 2:
//存款
System.out.println("================欢迎进入黑马银行存钱界面==================");
doposit(account, sc);
break;
case 3:
//取款
System.out.println("================欢迎进入黑马银行取款界面==================");
drawMoney(account, sc);
break;
case 4:
//转账
System.out.println("================欢迎进入黑马银行转账界面==================");
transferAccount(accounts, account, sc);
break;
case 5:
//修改密码
System.out.println("================欢迎进入黑马银行用户密码修改页面==================");
changePassword(account, sc);
break;
case 6:
//退出
return;
case 7:
//注销当前账户==在账户集合中删除该账户并退出咯
if (deleteAccount(accounts, account, sc)) {//删除成功返回true
return;
} else
break;
default:
System.out.println("您的选择有误!请重新选择!");
break;
}
}
}
5.1.1.银行用户详情界面
public static void queryAccount(Account account) {
System.out.println("您的账户信息如下:");
System.out.println("卡号:" + account.getCardId());
System.out.println("姓名:" + account.getUserName());
System.out.println("余额:" + account.getMoney());
System.out.println("当次取现额度:" + account.getQuotoMoney());
return;
}
5.1.2.存钱界面
public static void doposit(Account account, Scanner sc) {
while (true) {
System.out.print("请输入存款金额:");
double money = sc.nextDouble();
if (money > 0) {
account.setMoney(money + account.getMoney());
System.out.println("存款成功!您卡号为" + account.getCardId() + "的银行卡当前余额:" + account.getMoney());
return;
} else {
System.out.println("您不能存" + money + "元哦~~,重新选择功能操作把!");
}
}
}
5.1.3.取款界面
public static void drawMoney(Account account, Scanner sc) {
//余额小于100无法取款,直接返回
if (account.getMoney() < 100) {
System.out.println("对不起,您的余额小于100元,不能取款~\n您卡号为" + account.getCardId() + "的银行卡当前余额:" + account.getMoney());
return;
}
while (true) {
//使取款金额大于0
System.out.print("请输入取款金额:");
double money = sc.nextDouble();
if (money < 0) {
System.out.println("对不起,您不能取负数款哦~~");
} else if (money == 0) {
System.out.println("对不起,您不能取0元哦~~");
} else {
//取款金额在限额内 且 取款金额小于等于账户余额
if (money <= account.getQuotoMoney() && money <= account.getMoney()) {
System.out.println("取款" + money + "¥成功!");
//取款,修改账户信息
account.setMoney(account.getMoney() - money);
//显示账户信息
queryAccount(account);
return;
}
//取款金额超出该卡单次取款额度
else if (money > account.getQuotoMoney()) {
System.out.println("对不起,取款金额超出取款额度,每次最多可取:" + account.getQuotoMoney() + "¥");
} else {
System.out.println("余额不足!您当前余额为:" + account.getMoney() + "¥");
}
}
}
}
5.1.4.转账界面
public static void transferAccount(ArrayList<Account> accounts, Account account, Scanner sc) {
//遍历账户集合,如果没有两个账户及以上不允许转账
if (accounts.size() < 2) {
System.out.println("当前系统,账户不足2个,不能转账");
return;
}
//当前余额为0
if (account.getMoney() == 0) {
System.out.println("当前余额为0,无法转账!");
return;
}
//2个账户以上可以转账
System.out.print("请您输入转账的账户卡号:");
String cardId = sc.next();
Account acc = getAccountById(accounts, cardId);//存在返回持有该卡号的account,不存在返回null
if (acc == null) System.out.println("不存在该账户,请重新确认!");
else {
//将姓存储进lastName中
String lastName = acc.getUserName().substring(0, 1);
//截取从第2位开始后续字符串
String secretName = "*" + acc.getUserName().substring(1);
while (true) {
//让用户确认姓氏
System.out.println("您当前要为" + secretName + "转账!");
System.out.print("请您输入姓氏确认:");
//确认成功
if (lastName.equals(sc.next())) {
System.out.println("姓氏确认成功!");
while (true) {
System.out.print("请输入转账金额:");
double money = sc.nextDouble();
//转账金额(0~上限&&余额)
if (money > 0 && money <= account.getQuotoMoney() && money <= account.getMoney()) {
//当前用户扣钱
account.setMoney(account.getMoney() - money);
//目标用户收钱
acc.setMoney(acc.getMoney() + money);
System.out.println("转账成功,您的余额为:" + account.getMoney());
return;
} else if (money > account.getQuotoMoney()) {
System.out.println("对不起,转账金额(" + money + ")超出每次操作额度(" + account.getQuotoMoney() + ")");
} else if (money > account.getMoney()) {
System.out.println("对不起,您当前余额不足,余额(" + account.getMoney() + ")");
} else System.out.println("对不起,您输入的金额不正确,请重新输入~~");
}
} else System.out.println("对不起,姓氏确认失败!请重新输入~");
}
}
}
5.1.5.密码修改页面
public static void changePassword(Account account, Scanner sc) {
while (true) {
System.out.print("请您输入当前密码:");
String psw = sc.next();
//比对成功
if (account.getPassword().equals(psw)) {
while (true) {
System.out.print("请输入新密码:");
String newPsw1 = sc.next();
//新密码和旧密码不同
if (!psw.equals(newPsw1)) {
System.out.print("请确认新密码:");
String newPsw2 = sc.next();
//两次输入一致
if (newPsw1.equals(newPsw2)) {
//更新密码
account.setPassword(newPsw1);
System.out.println("密码修改成功!");
return;
} else {
System.out.println("您输入的2次密码不一致!请重新输入");
}
} else System.out.println("新旧密码一致!请重新输入~");
}
} else System.out.println("密码与当前账号密码不一致!请重新输入");
}
}
5.1.6.销户操作
public static boolean deleteAccount(ArrayList<Account> accounts, Account account, Scanner sc) {
System.out.println("------------------------------------");
System.out.print("您真的要销户吗?(y/n):");
while (true) {
String rs = sc.next();
switch (rs) {
case "y":
//确定
if (accounts.remove(account)) {
System.out.println("销户成功!");
//销户成功返回主菜单
return true;
}
break;
case "n":
//否定
System.out.println("好的,当前账户继续为您保留~");
return false;
default:
System.out.print("输入有误,请重新输入您的选择(y/n):");
break;
}
}
}
最后(ATMSystem.java资源)
如果不知道怎么将各个功能集合起来,那我把完整代码放在下面把~
一共两个文件!Account.java在上面已经完整给出了(黄色荧光笔标记)
ATMSystem.java
package com.itheima.atm;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATMSystem {
public static void main(String[] args) {
// 1.创立Account类
// 2.创立集合存储用户对象地址
ArrayList<Account> accounts = new ArrayList<>();
// 3.欢迎页面(首页)
while (true) {
System.out.println("================黑马ATM系统==================");
System.out.println("1、开户功能");
System.out.println("2、登陆账户");
Scanner sc = new Scanner(System.in);
System.out.print("请输入命令1、2选择对应的操作:");
int command = sc.nextInt();
switch (command) {
case 1:
//开户功能
createAccount(accounts);
break;
case 2:
//登陆账户
login(accounts);
break;
default:
System.out.println("对不起,您输入的选择有误!");
break;
}
}
}
public static void login(ArrayList<Account> accounts) {
System.out.println("================欢迎进入黑马银行用户登陆界面==================");
//输入当前这个账户的信息,并查询是否存在
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("请输入您的卡号:");
String cardId = sc.next();
//查询该卡号是否存在
//存在返回持有该卡号的account,不存在返回null
Account account = getAccountById(accounts, cardId);
//存在,继续输入密码
if (account != null) {
while (true) {
System.out.print("请输入您的密码:");
String psw = sc.next();
if (psw.equals(account.getPassword())) {
System.out.println(account.getUserName() + "先生/女士,欢迎您进入系统,您的卡号:" + account.getCardId());
//进入用户登陆后的界面
System.out.println("================欢迎进入黑马银行ATM系统==================");
showUserCommand(accounts, account, sc);
return;
} else {
System.out.println("密码有误!请重新输入!");
}
}
} else {
System.out.println("对不起,系统中不存在该账户卡号,请重新输入~~");
}
}
}
/**
* 进入登陆后的系统界面
*
* @param accounts
* @param account
*/
public static void showUserCommand(ArrayList<Account> accounts, Account account, Scanner sc) {
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.print("您可以继续选择功能进行操作了:");
int command = sc.nextInt();
switch (command) {
case 1:
//查询本卡信息
System.out.println("================欢迎进入黑马银行用户详情界面==================");
queryAccount(account);
break;
case 2:
//存款
System.out.println("================欢迎进入黑马银行存钱界面==================");
doposit(account, sc);
break;
case 3:
//取款
System.out.println("================欢迎进入黑马银行取款界面==================");
drawMoney(account, sc);
break;
case 4:
//转账
System.out.println("================欢迎进入黑马银行转账界面==================");
transferAccount(accounts, account, sc);
break;
case 5:
//修改密码
System.out.println("================欢迎进入黑马银行用户密码修改页面==================");
changePassword(account, sc);
break;
case 6:
//退出
return;
case 7:
//注销当前账户==在账户集合中删除该账户并退出咯
if (deleteAccount(accounts, account, sc)) {//删除成功返回true
return;
} else
break;
default:
System.out.println("您的选择有误!请重新选择!");
break;
}
}
}
/**
* 销户
* 删除成功返回true
* @param accounts
* @param account
* @param sc
* @return
*/
public static boolean deleteAccount(ArrayList<Account> accounts, Account account, Scanner sc) {
System.out.println("------------------------------------");
System.out.print("您真的要销户吗?(y/n):");
while (true) {
String rs = sc.next();
switch (rs) {
case "y":
//确定
if (accounts.remove(account)) {
System.out.println("销户成功!");
//销户成功返回主菜单
return true;
}
break;
case "n":
//否定
System.out.println("好的,当前账户继续为您保留~");
return false;
default:
System.out.print("输入有误,请重新输入您的选择(y/n):");
break;
}
}
}
/**
* 修改密码
*
* @param account
* @param sc
*/
public static void changePassword(Account account, Scanner sc) {
while (true) {
System.out.print("请您输入当前密码:");
String psw = sc.next();
//比对成功
if (account.getPassword().equals(psw)) {
while (true) {
System.out.print("请输入新密码:");
String newPsw1 = sc.next();
//新密码和旧密码不同
if (!psw.equals(newPsw1)) {
System.out.print("请确认新密码:");
String newPsw2 = sc.next();
//两次输入一致
if (newPsw1.equals(newPsw2)) {
//更新密码
account.setPassword(newPsw1);
System.out.println("密码修改成功!");
return;
} else {
System.out.println("您输入的2次密码不一致!请重新输入");
}
} else System.out.println("新旧密码一致!请重新输入~");
}
} else System.out.println("密码与当前账号密码不一致!请重新输入");
}
}
/**
* 转账
*
* @param accounts
* @param account
* @param sc
*/
public static void transferAccount(ArrayList<Account> accounts, Account account, Scanner sc) {
//遍历账户集合,如果没有两个账户及以上不允许转账
if (accounts.size() < 2) {
System.out.println("当前系统,账户不足2个,不能转账");
return;
}
//当前余额为0
if (account.getMoney() == 0) {
System.out.println("当前余额为0,无法转账!");
return;
}
//2个账户以上可以转账
System.out.print("请您输入转账的账户卡号:");
String cardId = sc.next();
Account acc = getAccountById(accounts, cardId);//存在返回持有该卡号的account,不存在返回null
if (acc == null) System.out.println("不存在该账户,请重新确认!");
else {
//将姓存储进lastName中
String lastName = acc.getUserName().substring(0, 1);
//截取从第2位开始后续字符串
String secretName = "*" + acc.getUserName().substring(1);
while (true) {
//让用户确认姓氏
System.out.println("您当前要为" + secretName + "转账!");
System.out.print("请您输入姓氏确认:");
//确认成功
if (lastName.equals(sc.next())) {
System.out.println("姓氏确认成功!");
while (true) {
System.out.print("请输入转账金额:");
double money = sc.nextDouble();
//转账金额(0~上限&&余额)
if (money > 0 && money <= account.getQuotoMoney() && money <= account.getMoney()) {
//当前用户扣钱
account.setMoney(account.getMoney() - money);
//目标用户收钱
acc.setMoney(acc.getMoney() + money);
System.out.println("转账成功,您的余额为:" + account.getMoney());
return;
} else if (money > account.getQuotoMoney()) {
System.out.println("对不起,转账金额(" + money + ")超出每次操作额度(" + account.getQuotoMoney() + ")");
} else if (money > account.getMoney()) {
System.out.println("对不起,您当前余额不足,余额(" + account.getMoney() + ")");
} else System.out.println("对不起,您输入的金额不正确,请重新输入~~");
}
} else System.out.println("对不起,姓氏确认失败!请重新输入~");
}
}
}
/**
* 取款
*
* @param account 当前登陆用户账户对象
* @param sc 扫描器
*/
public static void drawMoney(Account account, Scanner sc) {
//余额小于100无法取款,直接返回
if (account.getMoney() < 100) {
System.out.println("对不起,您的余额小于100元,不能取款~\n您卡号为" + account.getCardId() + "的银行卡当前余额:" + account.getMoney());
return;
}
while (true) {
//使取款金额大于0
System.out.print("请输入取款金额:");
double money = sc.nextDouble();
if (money < 0) {
System.out.println("对不起,您不能取负数款哦~~");
} else if (money == 0) {
System.out.println("对不起,您不能取0元哦~~");
} else {
//取款金额在限额内 且 取款金额小于等于账户余额
if (money <= account.getQuotoMoney() && money <= account.getMoney()) {
System.out.println("取款" + money + "¥成功!");
//取款,修改账户信息
account.setMoney(account.getMoney() - money);
//显示账户信息
queryAccount(account);
return;
}
//取款金额超出该卡单次取款额度
else if (money > account.getQuotoMoney()) {
System.out.println("对不起,取款金额超出取款额度,每次最多可取:" + account.getQuotoMoney() + "¥");
} else {
System.out.println("余额不足!您当前余额为:" + account.getMoney() + "¥");
}
}
}
}
/**
* 存款
*
* @param account 当前登陆用户账户对象
* @param sc 扫描器
*/
public static void doposit(Account account, Scanner sc) {
while (true) {
System.out.print("请输入存款金额:");
double money = sc.nextDouble();
if (money > 0) {
account.setMoney(money + account.getMoney());
System.out.println("存款成功!您卡号为" + account.getCardId() + "的银行卡当前余额:" + account.getMoney());
return;
} else {
System.out.println("您不能存" + money + "元哦~~,重新选择功能操作把!");
}
}
}
/**
* 显示账户信息
*
* @param account
*/
public static void queryAccount(Account account) {
System.out.println("您的账户信息如下:");
System.out.println("卡号:" + account.getCardId());
System.out.println("姓名:" + account.getUserName());
System.out.println("余额:" + account.getMoney());
System.out.println("当次取现额度:" + account.getQuotoMoney());
return;
}
/**
* 开户功能
*
* @param accounts
*/
public static void createAccount(ArrayList<Account> accounts) {
System.out.println("================欢迎进入黑马银行用户办卡界面==================");
//创建一个用户对象,用于后期封装账户信息
Account account = new Account();
//录入当前这个账户的信息,注入到账户对象去
Scanner sc = new Scanner(System.in);
System.out.print("请您输入您的姓名:");
account.setUserName(sc.next());
//循环执行直到两次密码相同
while (true) {
System.out.print("请您输入账户密码:");
String psw1 = sc.next();
System.out.print("请您输入确认密码:");
String psw2 = sc.next();
if (psw1.equals(psw2)) {
//密码认证通过,可以诸注入对象
account.setPassword(psw1);
break;//密码录入成功,没必要继续了
} else System.out.println("对不起,您输入的两次密码不一致!,请重新输入~~");
}
//循环执行直到限额输入正确
while (true) {
System.out.print("请设置账户当日取现额度:");
double quotoMoney = sc.nextDouble();
if (quotoMoney >= 0) {
account.setQuotoMoney(quotoMoney);
break;
} else System.out.println("您输入的账户限额小于0,有误!请重新输入~");
}
//为账户随机一个8位且与其他账户的卡号不重复的号码
String cardId = cardIdRandom(accounts, 8);
account.setCardId(cardId);
//往账户集合中存进此账户的地址
if (accounts.add(account))
System.out.println(account.getUserName() + "先生/女士,您的账户已经开卡成功,您的卡号是:" + account.getCardId());
}
/**
* 为账户随机一个8位且与其他账户的卡号不重复的号码
*
* @return
*/
public static String cardIdRandom(ArrayList<Account> accounts, int num) {
Random r = new Random();
while (true) {
//1.生成8位卡号
String cardId = "";
for (int i = 0; i < num; i++) {
cardId += r.nextInt(10);//注意添加边界值! 0-9
}
// 2.判断与其他账户卡号是否相冲突
Account acc = getAccountById(accounts, cardId);
if (acc == null) return cardId;
}
}
/**
* 遍历查找该cardId是否存在于集合的对象里
*
* @param accounts
* @param cardId
* @return 存在返回持有该卡号的account,不存在返回null
*/
public static Account getAccountById(ArrayList<Account> accounts, String cardId) {
//遍历查找该cardId是否存在于集合的对象里
for (int i = 0; i < accounts.size(); i++) {
Account account = accounts.get(i);
if (account.getCardId().equals(cardId)) {
//账户已存在
return account;
}
}
return null;
}
}
总结
以上就是今天的ATM系统小代码,做这个项目让我熟悉了常用的·API,和稳固了OOP思想(面向对象编程)。
加油,准备找实习!!!