?? banktransaction.java
字號:
package banking;
import java.util.*;
/**
* 銀行業務處理類
* 處理功能:1.創建帳戶;2.注銷帳號;3.存款;4.取款;5.轉帳;6.打印所有帳號信息
* 進行各項處理失敗時,將錯誤信息存入message
* @author rainliu
*/
public class BankTransaction {
/** 所有帳戶信息的列表 */
private static List accountList = new ArrayList();
/** 操作發生錯誤時的提示信息 */
private String message = "";
public BankTransaction() {}
/** 信息載入 */
static {
FileRW reader = new FileRW();
//FileAccesser reader = new FileAccesser();
//FileProperties reader = new FileProperties();
//IDataFile reader = new FileRW();
//ADataFile reader = new FileRW();
accountList = reader.readAccountInfo();
if (accountList==null) {
accountList = new ArrayList();
}
}
/**
* 創建一個新的帳號
* @param newAccount 新的帳戶ID
* @param password 輸入的密碼
* @return true 創建成功
*/
public boolean createAccount(String newAccount,String password) {
//檢查要創建的帳號是否已經存在
for (int i=0;i<accountList.size();i++) {
AccountInfo ai = (AccountInfo)accountList.get(i);
//目前帳戶信息為空
if (ai==null) break;
String aid = ai.accountId;
if (newAccount.equals(aid)) {
setMessage("該帳號已經存在!");
return false;
}
}
//創建一個新的帳號
AccountInfo acc = new AccountInfo();
acc.accountId = newAccount;
acc.password = password;
accountList.add(acc);
//將最新信息更新到文件
saveData();
return true;
}
/**
* 創建一個新的帳號
* @param accountId 待刪除的帳戶ID
* @param password 待刪除帳戶的密碼
* @return true 刪除成功
*/
public boolean deleteAccount(String accountId,String password) {
//身份驗證
if (!checkAccount(accountId, password)) {
return false;
}
for (int i=0;i<accountList.size();i++) {
AccountInfo ai = (AccountInfo)accountList.get(i);
if (accountId.equals(ai.accountId)) {
accountList.remove(i);
}
}
//將最新信息更新到文件
saveData();
return true;
}
/**
* 存款操作的處理
* @param accountid 要存款的帳戶ID
* @param password 密碼
* @param money 操作的金額數
* @return true 操作成功
*/
public boolean deposit(String accountId, String password, double money) {
//身份驗證
if (!checkAccount(accountId, password)) {
return false;
}
//金額操作
for (int i = 0; i < accountList.size(); i++) {
//待修改的帳戶
AccountInfo ai = (AccountInfo) accountList.get(i);
String aa = ai.accountId;
//檢索該帳戶
if (ai.accountId.equals(accountId)
&& ai.password.equals(password)) {
//增加金額
ai.balance += money;
//將最新信息更新到文件
saveData();
return true;
}
}
return false;
}
/**
* 取款操作的處理
* @param accountid 要取款的帳戶ID
* @param password 密碼
* @param money 操作的金額數
* @return true 操作成功
*/
public boolean withdraw(String accountId, String password, double money) {
//身份驗證
if (!checkAccount(accountId, password)) {
return false;
}
//金額操作
for (int i = 0; i < accountList.size(); i++) {
//待修改的帳戶
AccountInfo ai = (AccountInfo) accountList.get(i);
String aa = ai.accountId;
//檢索該帳戶
if (ai.accountId.equals(accountId)
&& ai.password.equals(password)) {
if (ai.balance < money) {
setMessage("對不起,您的余額不足!");
return false;
} else {
ai.balance -= money;
//將最新信息更新到文件
saveData();
return true;
}
}
}
return false;
}
/**
* 轉帳操作的處理
* @param fromId 要轉出的帳戶ID
* @param fromPass 要轉出的帳戶密碼
* @param toId 要轉入的帳戶ID
* @param toPass 要轉入的帳戶密碼
* @param money 操作的金額數
* @return true 操作成功
*/
public boolean transfer(String fromId,String fromPass,String toId,String toPass,double money) {
//身份驗證
if (!checkAccount(fromId,fromPass) || !checkAccount(toId,toPass)) {
return false;
}
//先從帳戶fromId取款
if (withdraw(fromId,fromPass,money)) {
//在存入帳戶toId
deposit(toId,toPass,money);
//將最新信息更新到文件
saveData();
return true;
}
return false;
}
/**
* 執行操作前,檢驗帳戶的信息是否合法
* @param accountId 帳戶ID
* @param password 密碼
* @return true 若用戶名和密碼均存在且對應正確
*/
private boolean checkAccount(String accountId,String password) {
//檢查要創建的帳號是否已經存在并合法
for (int i=0;i<accountList.size();i++) {
AccountInfo ai = (AccountInfo)accountList.get(i);
String aa = ai.accountId;
if (accountId.equals(aa) && ai.password.equals(password)) {
return true;
}
}
setMessage("帳戶不存在或密碼有誤!");
return false;
}
/**
* 將最新帳戶信息保存到數據文件中
*/
private void saveData() {
//將最新信息更新到文件
FileRW writer = new FileRW();
//FileAccesser writer = new FileAccesser();
//FileProperties writer = new FileProperties();
//IDataFile writer = new FileRW();
//ADataFile writer = new FileRW();
writer.writeAccountInfo(accountList);
}
/**
* 打印出所有的帳號和余額
*/
public void printAllAcount() {
System.out.println("--------------------------------");
System.out.println("accountId\t\tbalance");
System.out.println("--------------------------------");
for (int i=0;i<accountList.size();i++) {
AccountInfo ai = (AccountInfo)accountList.get(i);
System.out.println(ai.accountId + "\t\t" + ai.balance);
}
System.out.println("--------------------------------");
}
public void setMessage(String msg) {
this.message = msg;
}
public String getMessage() {
return message;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -