Account賬戶類:
package org.mobiletrain;
/**
* 銀行賬戶
* @author apple
*
*/
public class Account {
private String id; //賬號
private double balance; //余額
private String password;//密碼
/**
* 構造器
* @param id
*/
public Account(String id,String password,double balance){
this.id = id;
this.password = password;
this.balance = balance;
}
/**
* 驗證身份是否有效
* @param password 密碼
* @return 密碼匹配返回true埃仪,否則返回false
*/
public boolean isValid(String password){
return this.password.equals(password);
}
/**
* 存錢
* @param money 存款金額
*/
public void deposit(double money){
balance += money;
}
/**
* 取錢
* @param money 取款金額
* @return 取款成功返回true乙濒,否則返回false
*/
public boolean withdraw(double money){//取錢
if (money <= balance) {
balance -= money;
return true;
}
return false;
}
/**
* 獲取賬戶ID
* @return 賬戶ID
*/
public String getId() {
return id;
}
/**
* 獲取賬戶余額
* @return 賬戶余額
*/
public double getBalance() {
return balance;
}
/**
* 修改密碼
* @param password 密碼
*/
public void setPassword(String password) {
this.password = password;
}
}
ATM類:
package org.mobiletrain;
/**
* ATM機
* @author apple
*
*/
public class ATM {
private Account currentAccount = null;
//用一個數(shù)組模擬銀行的數(shù)據(jù)庫系統(tǒng) 預先保存若干個銀行賬戶
private Account[] accountsArray= {
new Account("11223344","123123",1200),
new Account("22334455","123456",3000),
new Account("33445566","666666",5000)
};
/**
* 讀卡
* @param account 銀行賬戶
* @return 讀卡成功返回true,否則返回false
*/
public boolean readCard(String id){
for (Account account : accountsArray) {
if (account.getId().equals(id)) {
currentAccount = account;
return true;
}
}
return false;
}
/**
* 驗證身份
* @param password 密碼
* @return 驗證通過返回true贵试,否則返回false
*/
public boolean verify(String password){
if (currentAccount != null) {
return currentAccount.isValid(password);
}
return false;
}
/**
* 查詢余額
* @return 當前余額
*/
public double showBalance(){
return currentAccount.getBalance();
}
/**
* 存錢
* @param money
*/
public void deposit(int money){
currentAccount.deposit(money);
}
/**
* 取錢
* @param money
* @return
*/
public boolean withdraw(int money){
return currentAccount.withdraw(money);
}
/**
* 修改密碼
* @param newPassword 新密碼
*/
public void changePassword(String newPassword){
currentAccount.setPassword(newPassword);
}
/**
* 退卡
*/
public void quitCard(){
currentAccount = null;
}
/**
* 轉賬
* @param otherId 轉入賬號
* @param money 轉賬金額
* @return 轉賬成功返回true琉兜,否則返回false
*/
public boolean transfer(String otherId,double money){
for (Account account : accountsArray) {
if (account.getId().equals(otherId)) {
Account otherAccount = account;
if (currentAccount.getBalance() >= money) {
currentAccount.withdraw(money);
otherAccount.deposit(money);
return true;
}
else {
return false;
}
}
}
return false;
}
}
ATM測試類:
package org.mobiletrain.test;
import java.util.Scanner;
import org.mobiletrain.ATM;
public class ATMtest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ATM atm = new ATM();
boolean isRunning = true;
while(isRunning){
System.out.println("歡迎使用XX銀行ATM機");
System.out.print("請插入銀行卡:");
String id = input.next();
atm.readCard(id);
if (atm.readCard(id)) {
int counter = 0;
boolean isCorrect = false;
do{
counter += 1;
System.out.print("請輸入密碼:");
String password = input.next();
isCorrect = atm.verify(password);
}while(counter < 3 && !isCorrect);
if (isCorrect) {
boolean flag = true;
while(flag){
System.out.println("1.查詢余額");
System.out.println("2.存款");
System.out.println("3.取款");
System.out.println("4.轉賬");
System.out.println("5.修改密碼");
System.out.println("6.退卡");
int choice;
do {
System.out.print("請選擇:");
choice = input.nextInt();
} while (choice < 1 || choice > 6);
switch (choice) {
case 1:
System.out.println("賬戶余額:" + atm.showBalance());
break;
case 2:{//一堆花括號就構成了一個作用域(scope)
//在某個作用域中定義的變量只在該作用域中生效
System.out.print("請放入鈔票:");
int money = input.nextInt();
atm.deposit(money);
System.out.println("存款成功凯正!");
break;
}
case 3:{
System.out.print("請輸入或選擇取款金額:");
int money = input.nextInt();
if (atm.withdraw(money)) {
System.out.println("請取走你的鈔票");
}
else {
System.out.println("余額不足!");
}
break;
}
case 4:{
System.out.print("請輸入轉入賬號:");
String otherId = input.next();
System.out.print("請輸入轉賬金額:");
double money = input.nextDouble();
if (atm.transfer(otherId, money)) {
System.out.println("轉賬成功");
}
else {
System.out.println("轉賬失敗豌蟋,請檢查轉入賬號和賬戶余額是否正確!");
}
break;
}
case 5:
System.out.print("請輸入新密碼:");
String newPwd = input.next();
System.out.print("請再輸入一次:");
String rePwd = input.next();
if (newPwd.equals(rePwd)) {
atm.changePassword(newPwd);
System.out.println("密碼已修改");
}
else {
System.out.println("兩次密碼輸入不一致.");
}
break;
case 6:
System.out.println("請取走你的卡片");
flag = false;
break;
}
}
}
else {
System.out.println("輸入密碼錯誤次數(shù)超過3次廊散,你的卡被回收");
}
}
}
input.close();
}
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者