1 .定义一个账户类Account,至少需要包含(卡号、姓名、性别、密码、余额、每次取现额度)
2.一个账户类Account,定义系统关心的账户信息。
package com.zhang.atm;
public class Account {
private String cardId;
private String userName;
private char gender;
private String passWord;
private double balance;//余额
private double quota;//当前取款额度
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getUserName() {
return userName + (gender == '男' ? "先生" : "女士");
}
public void setUserName(String userName) {
this.userName = userName;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
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 getQuota() {
return quota;
}
public void setQuota(double quota) {
this.quota = quota;
}
}
定义一个ATM类,用来代表ATM系统,负责提供所有的业务需求,比如:展示ATM的系统欢迎页、查询账户、存款、取款、转账、修改密码、退出、注销账户。
在这里插入代码片
package com.zhang.atm;
import java.util.ArrayList;
import java.util.Scanner;
public class Atm {
ArrayList<Account> accounts = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
private Account loginAccount;
// 定义一个方法,启动系统
public void start() {
while (true) {
System.out.println("===欢迎进入ATM系统===");
System.out.println("1.用户登录");
System.out.println("2.用户开户");
System.out.println("请选择你的操作的命令");
int command = scanner.nextInt();
switch (command) {
case 1:
// 用户登录
login();
break;
case 2:
register();
break;
default:
System.out.println("你输入的指令有误,请重新输入");
}
}
}
/**
* 用户登录功能的实现
*/
private void login() {
System.out.println("==用户登录页面==");
// 1.判断系统中是否有账户对象
if (accounts.size() == 0) {
System.out.println("当前系统中没有账户可以登录,请先注册一个再回来登录吧");
return;
}
// 2.系统中有账户,让用户输入卡号
while (true) {
System.out.println("请您输入登录卡号:");
String cardId = scanner.next();
// 3.判断卡号是否存在
Account account = getAccountByCardId(cardId);
if (account == null) {
// 没有找到账户,说明卡号不存在
System.out.println("您输入的卡号不存在,请重新输入");
} else {
// 卡号存在
while (true) {
System.out.println("请您输入密码:");
String password = scanner.next();
// 判断密码是否正确
if (password.equals(account.getPassWord())) {
// 密码正确
System.out.println("恭喜您," + account.getUserName() + "登录成功,您的卡号是:" + account.getCardId());
loginAccount = account;
showUserCommands();
// System.out.println("进入操作页面");
return;
} else {
System.out.println("您输入的密码有误,请重新输入");
}
}
}
}
}
/**
* 用户操作页
*/
private void showUserCommands() {
while (true) {
System.out.println("==" + loginAccount.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 = scanner.nextInt();
switch (command) {
case 1:
//查询账户
showUserCommands();
break;
case 2:
//存款
deposit();
break;
case 3:
//取款
withdraw();
break;
case 4:
//转账
transfer();
break;
case 5:
// 修改密码
updatePassword();
break;
case 6:
// 退出
System.out.println("退出成功,欢迎下次使用===");
return;
case 7:
// 注销账户
boolean flag = deleteAccount();
if (flag) {
// 销户成功
return;
} else {
// 销户不成功
break;
}
default:
System.out.println("你输入的密码命令有误请重新输入");
}
}
}
/**
* 用户修改密码功能的实现
*/
private void updatePassword() {
while (true) {
System.out.println("==修改密码页面==");
System.out.println("请输入当前密码:");
String input = scanner.next();
if (input.equals(loginAccount.getPassWord())) {
// 密码正确
while (true) {
System.out.println("请输入新密码:");
String newPassword = scanner.next();
System.out.println("请再次输入新密码:");
String reNewPassword = scanner.next();
if (newPassword.equals(reNewPassword)) {
// 可以修改了
loginAccount.setPassWord(newPassword);
return;
} else {
System.out.println("您两次输入的密码不一致,请重新输入");
}
}
} else {
System.out.println("您输入的密码有误,请重新输入");
}
}
}
/**
* 用户转账功能的实现
*/
private void transfer() {
// 1.判断当前系统中是否有2个以上的账户
if (accounts.size() < 2) {
System.out.println("当前系统中没有其他账户可以转账,请先注册一个账户");
return;
}
// 2.输入转账金额
System.out.println("请输入你的转账金额");
double money = scanner.nextDouble();
if (loginAccount.getBalance() < money) {
System.out.println("你自己都没钱,还是别转了");
return;
}
while (true) {
System.out.println("请输入你的账户卡号");
String cardId = scanner.next();
// 4.判断目标卡号是否存在
Account targetAccount = getAccountByCardId(cardId);
if (targetAccount == null) {
System.out.println("请输入的卡号不存在,请重新输入");
} else {
String targeUsername = targetAccount.getUserName();
String tip = "*" + targeUsername.substring(1);
System.out.println("请输入" + tip + "的姓氏");
String surname = scanner.next();
// 判断用户输入的姓氏是否和真实的一样
if (surname.equals(targeUsername.substring(0, 1))) {
// 可以转账了
loginAccount.setBalance(loginAccount.getBalance() - money);
targetAccount.setBalance(targetAccount.getBalance() - money);
System.out.println("恭喜你,转账" + money + "成功");
showUserCommands();
return;
} else {
System.out.println("你输入的姓氏有误,不能转账");
}
}
}
}
/**
* 用户取款功能的实现
*/
private void withdraw() {
System.out.println("===用户取款页面===");
if (loginAccount.getBalance() < 100) {
System.out.println("你的账户余额不足100");
return;
}
while (true) {
// 说明账户余额大于100元
System.out.println("请输入你的取款金额");
double money = scanner.nextInt();
// 判断取款金额是否超过当次限额
if (money > loginAccount.getQuota()) {
System.out.println("你一次不能去这么多钱," + loginAccount.getQuota());
continue;
}
// 判断取款金额是否超过账户余额
if (money > loginAccount.getBalance()) {
System.out.println("对不起你没有这么多钱你可以多去" + loginAccount.getBalance());
continue;
}
// 可以取款了,更新账户余额
loginAccount.setBalance(loginAccount.getBalance());
System.out.println("恭喜你,取款成功,当前账余额为" + loginAccount.getBalance());
return;
}
}
/**
* 用户存款功能的实现
*/
private void deposit() {
System.out.println("===用户存款页面===");
System.out.println("请输入存款金额");
double money = scanner.nextDouble();
// 更新账户余额
loginAccount.setBalance(loginAccount.getBalance() + money);
System.out.println("恭喜你,存钱成功,你的存款后的账户信息");
showUserCommands();
}
/**
* 查询账户功能
*/
private void showAccountInfo() {
System.out.println("==查询账户信息页面==");
System.out.println("户主:" + loginAccount.getUserName());
System.out.println("性别:" + loginAccount.getGender());
System.out.println("账户余额:" + loginAccount.getBalance());
System.out.println("当次取款限额:" + loginAccount.getQuota());
}
/**
* 用户注册功能的实现
*/
private void register() {
Account account = new Account();
while (true) {
System.out.println("===系统开户操作");
System.out.println("请输入你的用户名");
Scanner scanner = new Scanner(System.in);
String username = scanner.next();
account.setUserName(username);
System.out.println("请输入你的性别男/女");
char gender = scanner.next().charAt(0);
if (gender == '男' || gender == '女') {
account.setGender(gender);
break;
} else {
System.out.println("你输入的性别有误,请从新输入");
}
}
while (true) {
System.out.println("请输入你的账户密码");
// Scanner scanner = new Scanner(System.in);
String password = scanner.next();
// Account account1=new Account();
System.out.println("请再次验证你输入的密码");
String rePassword = scanner.next();
if (password.equals(rePassword)) {
account.setPassWord(password);
break;
} else {
System.out.println("你两次输入的密码不一致");
}
}
System.out.println("请输入你的每次取款额度");
//Scanner scanner = new Scanner(System.in);
double quota = scanner.nextDouble();
account.setQuota(quota);
while (true) {
String cardId = generatecCardId();
Account acc = getAccountByCardId(cardId);
if (acc == null) {
account.setCardId(cardId);
break;
}
}
System.out.println("恭喜你" + account.getUserName() + "开户成功,你的卡号是" + account.getCardId());
accounts.add(account);
}
/**
* 根据卡号查找账户对象
*
* @param cardId 卡号
* @return 找到的账户对象,找不到返回null
*/
public Account getAccountByCardId(String cardId) {
for (int i = 0; i < accounts.size(); i++) {
if (accounts.get(i).getCardId().equals(cardId)) {// if (accounts.get(i).equals(cardId)) {
//找到了
return accounts.get(i);
}
}
return null;
}
/**
* 生成一个8位随机卡号
*/
public String generatecCardId() {
String cardId = "";
Random random = new Random();
for (int i = 0; i < 8; i++) {
cardId += random.nextInt(10);
}
return cardId;
}
/**
* 用户销户功能的实现
* @return 是否销户成功
*/
private boolean deleteAccount(){
while (true){
System.out.println("===用户销户页面");
System.out.println("你真的要销户吗?y/n");
String input=scanner.next();
switch (input){
case "y":
if (loginAccount.getBalance()>0){
System.out.println("你账户还有钱那,先取完在销户把");
return false;
}
else {
accounts.remove(loginAccount);
System.out.println("销户成功");
return true;
}
case "n":
System.out.println("好的保留你的账户");
return false;
default:
System.out.println("你输入的指令有误,请重新输入y/n");
}
}
}
}
定义了一个Test类,负责测试系统:创建ATM对象代表ATM系统并启动。
package com.zhang.atm;
public class Test01 {
public static void main(String[] args) {
Atm atm=new Atm();
atm.start();
}
}
这里是注意事项:
以上需要创建3个类Account、Atm、Test类 在同一个包下(package)com.xxx.xxx;