效果:
目录结构
Account 账户类
package com.mytest;
public class Account {
private String cardId;
private String userName;
private char sex;
private String password;
private double balance;
private double limit; //限额
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getUserName() {
return userName + (sex == '男' ? "先生" : "女士");
}
public void setUserName(String userName) {
this.userName = userName;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getLimit() {
return limit;
}
public void setLimit(double limit) {
this.limit = limit;
}
}
Atm类
package com.mytest;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Atm {
private ArrayList<Account> accounts = new ArrayList<Account>();
private Scanner sc = new Scanner(System.in);
private Account loginAcc;//记住当前登录的账户
/**
* 启动ATM系统 展示欢迎界面
*/
public void start() {
while (true) {
System.out.println("===欢迎您进入ATM系统===");
System.out.println("1. 用户登录");
System.out.println("2. 用户开户");
System.out.println("请选择:");
int command = sc.nextInt();
switch (command) {
case 1:
// 用户登录
login();
break;
case 2:
// 用户开户
createAccount();
break;
default:
System.out.println("输入错误,请重新选择");
break;
}
}
}
/**
* 完成用户登录操作
*/
private void login() {
System.out.println("==系统登录操作==");
if (accounts.size() == 0) {
System.out.println("对不起,ATM系统目前没有用户,请先进行开户操作");
return;
}
while (true) {
System.out.println("请输入您的卡号:");
String cardId = sc.next();
Account acc = getAccountByCardId(cardId);
if (acc == null) {
System.out.println("对不起,您输入的卡号不存在");
} else {
while (true) {
System.out.println("请输入您的密码:");
String password = sc.next();
if (acc.getPassword().equals(password)) {
System.out.println("欢迎您," + acc.getUserName() + "您的卡号是:" + acc.getCardId());
loginAcc = acc;
showUserCommond();
return;
} else {
System.out.println("对不起,您输入的密码错误");
}
}
}
}
}
/**
* 展示登录后的操作
*/
private void showUserCommond() {
while (true) {
System.out.println(loginAcc.getUserName() + ",您可以选择如下的功能进行账户的处理==");
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:
// 查询账户信息
showLoginAccount();
break;
case 2:
// 存款
depositMoney();
break;
case 3:
// 取款
withdrawMoney();
break;
case 4:
// 转账
transferMoney();
break;
case 5:
// 密码修改
updatePassword();
return;
case 6:
// 退出
System.out.println(loginAcc.getUserName() + ",您退出系统成功");
return; //跳出并结束当前方法
case 7:
// 注销当前账户
if (deleteAccount()) {
return;
}
break;
default:
System.out.println("您当前选择的操作不正确,请重新选择");
}
}
}
/**
* 密码修改
* */
private void updatePassword() {
System.out.println("==密码修改操作==");
while (true) {
System.out.println("请输入您当前密码:");
String password = sc.next();
if (!loginAcc.getPassword().equals(password)) {
System.out.println("对不起,您输入的密码错误");
} else {
while (true) {
System.out.println("请输入您的新密码:");
String newPassword = sc.next();
System.out.println("请再次输入您的新密码:");
String okNewPassword = sc.next();
if (!newPassword.equals(okNewPassword)) {
System.out.println("两次输入的新密码不一致,请重新输入");
}else{
loginAcc.setPassword(newPassword);
System.out.println("恭喜您,密码修改成功");
return;
}
}
}
}
}
/**
* 销户
* */
private boolean deleteAccount() {
System.out.println("==注销账户操作==");
System.out.println("您确定要注销您的账户吗?y/n");
String yn = sc.next();
if (yn.equals("y")) {
if (loginAcc.getBalance() > 0) {
System.out.println("对不起,您账户中有余额,不允许销户");
return false;
} else {
accounts.remove(loginAcc);
System.out.println("恭喜您,注销成功");
return true;
}
}
System.out.println("好的,您的账户保留");
return false;
}
/**
* 转账
*/
private void transferMoney() {
System.out.println("==转账操作==");
// 判断系统中是否存在其他账户
if (accounts.size() < 2) {
System.out.println("当前系统中只有你一个账户,无法为其他账户转账--");
return;
}
if (loginAcc.getBalance() == 0) {
System.out.println("您自己都没钱就别转了");
return;
}
while (true) {
System.out.println("请输入您要转账到的卡号:");
String cardId = sc.next();
Account acc = getAccountByCardId(cardId);
if (acc == null) {
System.out.println("对不起,您输入的卡号不存在");
}else if(acc.getCardId().equals(loginAcc.getCardId())){
System.out.println("对不起,自己不能给自己转账");
} else {
String name = "*" + acc.getUserName().substring(1);
System.out.println("请输入【" + name + "】的姓氏:");
String preName = sc.next();
if (acc.getUserName().startsWith(preName)) {
System.out.println("请输入您要转账的金额:");
double money = sc.nextDouble();
if (money > loginAcc.getBalance()) {
System.out.println("对不起,您的余额不足,最多可以转账"+loginAcc.getBalance());
} else {
// 转账成功
loginAcc.setBalance(loginAcc.getBalance() - money);
acc.setBalance(acc.getBalance() + money);
System.out.println("恭喜您,转账成功!您的余额剩余:" + loginAcc.getBalance());
return;
}
} else {
System.out.println("对不起,您认证的姓氏有问题--");
}
}
}
}
/**
* 取款
*/
private void withdrawMoney() {
System.out.println("==取钱操作==");
if (loginAcc.getBalance() < 100) {
System.out.println("对不起,您的余额不足,请充值后进行取款");
return;
}
while (true) {
System.out.println("请输入您要取出的金额:");
double money = sc.nextDouble();
if (loginAcc.getBalance() >= money) {
if (money > loginAcc.getLimit()) {
System.out.println("对不起,每次取现额度为:" + loginAcc.getLimit());
} else {
loginAcc.setBalance(loginAcc.getBalance() - money);
System.out.println("恭喜您,取款" + money + "成功!您的余额剩余:" + loginAcc.getBalance());
break;
}
} else {
System.out.println("余额不足,您的账户余额是:" + loginAcc.getBalance() + "请充值后进行取款");
}
}
}
/**
* 存钱
*/
private void depositMoney() {
System.out.println("==存钱操作==");
System.out.println("请输入您要存入的金额:");
double money = sc.nextDouble();
loginAcc.setBalance(loginAcc.getBalance() + money);
System.out.println("恭喜您,存钱成功!您的余额是:" + loginAcc.getBalance());
}
/**
* 展示当前登陆的账户信息
*/
private void showLoginAccount() {
System.out.println("==您的账户信息如下==");
System.out.println("卡号:" + loginAcc.getCardId());
System.out.println("户主:" + loginAcc.getUserName());
System.out.println("性别:" + loginAcc.getSex());
System.out.println("余额:" + loginAcc.getBalance());
System.out.println("每次取现额度:" + loginAcc.getLimit());
}
/**
* 完成用户开户操作
*/
private void createAccount() {
System.out.println("==系统开户操作==");
// 1、创建账户对象。用于封装用户的开户信息
Account acc = new Account();
// 2、获取用户输入的开户信息,赋值给账户对象
System.out.println("请输入您的账户名称:");
String name = sc.next();
acc.setUserName(name);
while (true) {
System.out.println("请输入您的性别:");
char sex = sc.next().charAt(0);
if (sex == '男' || sex == '女') {
acc.setSex(sex);
break;
} else {
System.out.println("您的输入有误,只能是 男 或者 女");
}
}
while (true) {
System.out.println("请输入您的账户密码:");
String password = sc.next();
System.out.println("请输入您的确认密码:");
String okPassword = sc.next();
if (password.equals(okPassword)) {
acc.setPassword(password);
break;
} else {
System.out.println("您两次输入的密码不一致,请重新输入");
}
}
System.out.println("请输入您的取现额度:");
double limit = sc.nextDouble();
acc.setLimit(limit);
// 为账户生成卡号
String newCardId = createCardId();
acc.setCardId(newCardId);
// 将账户对象添加到ATM系统中
accounts.add(acc);
System.out.println("恭喜您,开户成功" + acc.getUserName() + ",您的卡号是:" + acc.getCardId());
}
/**
* 返回一个8位数字的卡号并且不能与 已有卡号重复
*/
private String createCardId() {
while (true) {
// 1、随机生成一个8位数字的卡号
String cardId = "";
Random r = new Random();
for (int i = 0; i < 8; i++) {
int data = r.nextInt(10);//0-9
cardId += data;
}
// 2、判断该卡号是否已经存在,如果存在则重新生成
Account acc = getAccountByCardId(cardId);
if (acc == null) {
return cardId;
}
}
}
/**
* 根据卡号查询账户对象返回 accounts = [ca,c2,c3]
*/
private Account getAccountByCardId(String cardId) {
//遍历全部对象,判断卡号是否相同
for (int i = 0; i < accounts.size(); i++) {
Account acc = accounts.get(i);
if (acc.getCardId().equals(cardId)) {
return acc;
}
}
return null;
}
}
Test类
package com.mytest;
public class Test {
public static void main(String[] args) {
Atm atm = new Atm();
atm.start();
}
}