Created
April 9, 2013 14:47
-
-
Save KingSirLee/5346285 to your computer and use it in GitHub Desktop.
简单的ATM取款机项目开发。。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Account { | |
private String username; | |
private String password; | |
private double money; | |
private boolean enable; | |
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 boolean isEnable() { | |
return enable; | |
} | |
public void setEnable(boolean enable) { | |
this.enable = enable; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ATM类 | |
* @author tom | |
* | |
*/ | |
public class Atm { | |
private java.util.Scanner input = new java.util.Scanner(System.in); | |
private Bank bank = new Bank(); | |
/** | |
* ATM启动方法 | |
*/ | |
public void start() { | |
System.out.println("-----------------------"); | |
System.out.println("1. 开户"); | |
System.out.println("2. 插卡"); | |
System.out.println("3. 系统管理"); | |
System.out.println("4. 退出"); | |
System.out.println("-----------------------"); | |
System.out.println("请选择菜单项:"); | |
String index = input.next(); | |
if("1".equals(index)) { | |
createAccount(); | |
} else if("2".equals(index)) { | |
login(); | |
} else if("3".equals(index)) { | |
adminLogin(); | |
} else if("4".equals(index)) { | |
System.exit(0); | |
} else { | |
System.out.println("输入错误"); | |
start(); | |
} | |
} | |
/** | |
* 管理员登录 | |
*/ | |
private void adminLogin() { | |
System.out.println("请输入管理员密码:"); | |
String password = input.next(); | |
if("000000".equals(password)) { | |
showAdminMenu(); | |
} else { | |
System.out.println("密码错误"); | |
start(); | |
} | |
} | |
/** | |
* 显示管理员登录后的菜单 | |
*/ | |
private void showAdminMenu() { | |
System.out.println("------------------------------"); | |
System.out.println("1. 显示所有账户"); | |
System.out.println("2. 冻结账户"); | |
System.out.println("3. 恢复账户"); | |
System.out.println("4. 退出"); | |
System.out.println("------------------------------"); | |
System.out.println("请选择菜单项:"); | |
String index = input.next(); | |
if("1".equals(index)) { | |
showAllAccount(); | |
} else if("2".equals(index)) { | |
disableAccount(); | |
} else if("3".equals(index)) { | |
enableAccount(); | |
} else if("4".equals(index)) { | |
start(); | |
} else { | |
System.out.println("输入错误"); | |
showAdminMenu(); | |
} | |
} | |
/** | |
* 恢复账户 | |
*/ | |
private void enableAccount() { | |
Account[] accounts = bank.getAccounts(); | |
System.out.println("账户名\t\t\t是否可用"); | |
System.out.println("-----------------------------"); | |
for (int i = 0; i < accounts.length; i++) { | |
if(accounts[i] != null && !accounts[i].isEnable()) { | |
System.out.println(accounts[i].getUsername() + "\t\t\t" + (accounts[i].isEnable() ? "√" : "X")); | |
} | |
} | |
System.out.println("请输入被恢复的账户名称:"); | |
String name = input.next(); | |
Account a = bank.validateByName(name); | |
if(a == null) { | |
System.out.println("该账户不存在"); | |
} else { | |
a.setEnable(true); | |
System.out.println("恢复成功"); | |
} | |
showAdminMenu(); | |
} | |
/** | |
* 冻结账号 | |
*/ | |
private void disableAccount() { | |
Account[] accounts = bank.getAccounts(); | |
System.out.println("账户名\t\t\t是否可用"); | |
System.out.println("-----------------------------"); | |
for (int i = 0; i < accounts.length; i++) { | |
if(accounts[i] != null && accounts[i].isEnable()) { | |
System.out.println(accounts[i].getUsername() + "\t\t\t" + (accounts[i].isEnable() ? "√" : "X")); | |
} | |
} | |
System.out.println("请输入被冻结的账户名称:"); | |
String name = input.next(); | |
Account a = bank.validateByName(name); | |
if(a == null) { | |
System.out.println("该账户不存在"); | |
} else { | |
a.setEnable(false); | |
System.out.println("冻结成功"); | |
} | |
showAdminMenu(); | |
} | |
/** | |
* 显示所有账户 | |
*/ | |
private void showAllAccount() { | |
Account[] accounts = bank.getAccounts(); | |
System.out.println("账户名\t\t\t是否可用"); | |
System.out.println("-----------------------------"); | |
for (int i = 0; i < accounts.length; i++) { | |
if(accounts[i] != null) { | |
System.out.println(accounts[i].getUsername() + "\t\t\t" + (accounts[i].isEnable() ? "√" : "X")); | |
} | |
} | |
showAdminMenu(); | |
} | |
/** | |
* 账号登录 | |
*/ | |
private void login() { | |
System.out.println("请输入账号:"); | |
String username = input.next(); | |
System.out.println("请输入密码:"); | |
String password = input.next(); | |
//根据用户名或密码来验证是否正确 | |
Account account = bank.validateByName(username); | |
if(account != null && account.getPassword().equals(password)) { | |
if(account.isEnable()) { | |
System.out.println("欢迎你," + account.getUsername()); | |
showAccountMenu(account); | |
} else { | |
System.out.println("该账号被冻结,请联系所在银行"); | |
} | |
} else { | |
System.out.println("用户名或密码错误"); | |
} | |
start(); | |
} | |
private void showAccountMenu(Account a) { | |
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("---------------------------"); | |
System.out.println("请选择菜单项:"); | |
String index = input.next(); | |
if("1".equals(index)) { | |
showAccountMoney(a); | |
} else if("2".equals(index)) { | |
saveMoney(a); | |
} else if("3".equals(index)) { | |
getMoney(a); | |
} else if("4".equals(index)) { | |
tranMoney(a); | |
} else if("5".equals(index)) { | |
editPassword(a); | |
} else if("6".equals(index)) { | |
start(); | |
} else { | |
System.out.println("输入错误"); | |
showAccountMenu(a); | |
} | |
} | |
private void tranMoney(Account a) { | |
System.out.println("请输入转入方账号:"); | |
String inName = input.next(); | |
Account inAccount = bank.validateByName(inName); | |
if(inAccount == null) { | |
System.out.println("转入方账号错误"); | |
} else { | |
if(inAccount.isEnable()) { | |
boolean flag = false; | |
do{ | |
System.out.println("请输入转账金额:"); | |
double money = input.nextDouble(); | |
if(money <= 0) { | |
System.out.println("金额输入错误"); | |
flag = true; | |
} else { | |
if(a.getMoney() < money) { | |
System.out.println("余额不足"); | |
} else { | |
a.setMoney(a.getMoney() - money); | |
inAccount.setMoney(inAccount.getMoney() + money); | |
flag = false; | |
System.out.println("转账成功"); | |
} | |
} | |
}while(flag); | |
} else { | |
System.out.println("转入方账号不可用"); | |
} | |
} | |
showAccountMenu(a); | |
} | |
/** | |
* 修改密码 | |
* @param a | |
*/ | |
private void editPassword(Account a) { | |
System.out.println("请输入原始密码:"); | |
String oldPassword = input.next(); | |
if(a.getPassword().equals(oldPassword)) { | |
boolean flag = false; | |
do{ | |
System.out.println("请输入新密码:"); | |
String password = input.next(); | |
System.out.println("请确认新密码:"); | |
String repassword = input.next(); | |
if(password.equals(repassword)) { | |
a.setPassword(password); | |
System.out.println("密码修改成功"); | |
flag = false; | |
} else { | |
System.out.println("两次密码不一致"); | |
flag = true; | |
} | |
}while(flag); | |
} else { | |
System.out.println("原始密码错误"); | |
} | |
showAccountMenu(a); | |
} | |
/** | |
* 取款 | |
* @param a | |
*/ | |
private void getMoney(Account a) { | |
System.out.println("请输入取款金额:"); | |
double money = input.nextDouble(); | |
if(money <= 0) { | |
System.out.println("金额输入错误"); | |
saveMoney(a); | |
} else { | |
if(a.getMoney() < money) { | |
System.out.println("余额不足"); | |
} else { | |
a.setMoney(a.getMoney() - money); | |
System.out.println("取款成功"); | |
} | |
showAccountMenu(a); | |
} | |
} | |
/** | |
* 存款 | |
* @param a | |
*/ | |
private void saveMoney(Account a) { | |
System.out.println("请输入存款金额:"); | |
double money = input.nextDouble(); | |
if(money <= 0) { | |
System.out.println("金额输入错误"); | |
saveMoney(a); | |
} else { | |
a.setMoney(a.getMoney() + money); | |
System.out.println("存款成功"); | |
showAccountMenu(a); | |
} | |
} | |
/** | |
* 显示账户余额 | |
* @param a | |
*/ | |
private void showAccountMoney(Account a) { | |
System.out.println("余额:¥" + a.getMoney()); | |
showAccountMenu(a); | |
} | |
/** | |
* 开户 | |
*/ | |
private void createAccount() { | |
System.out.println("请输入账号:"); | |
String name = input.next(); | |
//验证账号是否可用 | |
Account result = bank.validateByName(name); | |
if(result == null) { | |
//该账号可用 | |
boolean flag = false; | |
do{ | |
System.out.println("请输入密码:"); | |
String password = input.next(); | |
System.out.println("请确认密码:"); | |
String repassword = input.next(); | |
if(password.equals(repassword)) { | |
Account account = new Account(); | |
account.setUsername(name); | |
account.setPassword(password); | |
account.setEnable(true); | |
boolean isSave = bank.save(account); | |
if(isSave) { | |
System.out.println("开户成功!"); | |
} else { | |
System.out.println("服务器忙,请稍后..."); | |
} | |
start(); | |
} else { | |
System.out.println("两次密码不一致"); | |
flag = true; | |
} | |
}while(flag); | |
} else { | |
//该账号已被占用 | |
System.out.println("该账号已被占用"); | |
createAccount(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Bank { | |
private Account[] accounts = new Account[100]; | |
public Account[] getAccounts() { | |
return this.accounts; | |
} | |
/** | |
* 根据账号来验证该账号是否可用 | |
* @param name 被验证的账号 | |
* @return 如果可用返回true 否则false | |
*/ | |
public Account validateByName(String name) { | |
for (int i = 0; i < accounts.length; i++) { | |
if(accounts[i] != null && accounts[i].getUsername().equals(name)) { | |
return accounts[i]; | |
} | |
} | |
return null; | |
} | |
/** | |
* 将开户的account对象存入银行 | |
* @param account | |
* @return | |
*/ | |
public boolean save(Account account) { | |
boolean flag = false; | |
for (int i = 0; i < accounts.length; i++) { | |
if(accounts[i] == null) { | |
accounts[i] = account; | |
flag = true; | |
break; | |
} | |
} | |
return flag; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test { | |
public static void main(String[] args) { | |
Atm atm = new Atm(); | |
atm.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment