項目要求:
1、用戶需要從控制檯輸入賬號密碼,賬號或者密碼不正確報異常
2、每日取款的金額有限制(100,30000),否則報異常
3、每次取款都要有記錄,並在下一次取款時顯示出來
思路:
1、先在“銀行類”裏生成一些用戶(跳過了註冊環節)
2、可使用List集合存儲取款日誌
3、可使用Map集合將“用戶名”和對應的用戶信息建立“鍵值關係”
4、使用while循環控制流程
項目實現代碼:
管理類(含main方法):
import java.util.Scanner;
public class Manager {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//創建BankServer()類對象
BankServer server = new BankServer();
//創建User類對象,先令其爲空
User user = null;
//調用creatAccount()方法生成四個用戶
server.creatAccount();
//while循環控制流程
while(true){
System.out.println("請輸入您的賬號:");
String id = input.nextLine();
System.out.println("請輸入您的密碼:");
String password = input.nextLine();
try {
user=server.chick(id, password);//調用chick()方法,讓user有意義
System.out.println("===========歡迎進入銀行取款系統===========");
System.out.println("您的賬戶餘額爲:"+user.getMoney());
while(true){
System.out.println("請輸入您的取款金額(必須大於100,小於30000):");
double money = input.nextDouble();
server.getMoney(user, money);
input.nextLine();
System.out.println("請問您需要繼續取款嗎? Y or N");
String selec = input.nextLine();
if(selec.equalsIgnoreCase("N")){
System.out.println("歡迎使用!");
break;
}
}
} catch (Exception e) {
System.out.println(">>> 賬號/密碼錯誤!請重新登錄輸入 <<<");
}
}
}
}
銀行服務類(BankServer類):
import java.util.HashMap;
import java.util.Map;
/**
* 銀行服務類,實現用戶的創建
* @author
*
*/
public class BankServer {
//聲明四個常量表示賬戶用戶名
final String AC1 = "1111-111111-1111AA";
final String AC2 = "2222-222222-2222BB";
final String AC3 = "3333-333333-3333CC";
final String AC4 = "4444-444444-4444DD";
//聲明四個常量,用於存儲密碼,每個賬戶有一個專屬的密碼
final String PASSWORD1 = "111111";
final String PASSWORD2 = "222222";
final String PASSWORD3 = "333333";
final String PASSWORD4 = "444444";
//聲明一個靜態常量規定初始金額爲100000元
public static final Double BALANCE = 100000.00;
//創建一個map集合用於存儲用戶賬號和對應的賬戶信息
Map<String, User> map = new HashMap<>();
/**
* 生成用戶的方法,將用戶存入map集合中
*/
public void creatAccount(){
map.put(AC1, new User(AC1,PASSWORD1,BALANCE));
map.put(AC2, new User(AC2,PASSWORD2,BALANCE));
map.put(AC3, new User(AC3,PASSWORD3,BALANCE));
map.put(AC4, new User(AC4,PASSWORD4,BALANCE));
}
/**
* 比較用戶從控制檯輸入的數據是否和銀行預先存儲的賬戶一樣
* @param id
* @param password
* @return
* @throws Exception
*/
public User chick(String id,String password) throws Exception{
User Account = null; //此時的Account對象爲null
//密碼已經被放進map集合中,不好取出,所以需要在User類中定義一個checkPassword方法,其返回的是布爾類型的值(詳情見User類)
if(map.containsKey(id) ){
Account=map.get(id); //如果賬戶名對上了,則調用map集合的get()方法給Account對象賦值
if(Account.checkPassword(password)){
System.out.println("登錄成功!"); //如果密碼也對上了,提示“登錄成功”,否則拋出異常
}else{
throw new Exception("密碼錯誤");
}
}else{
throw new Exception("賬號/密碼錯誤!");
}
return Account; //返回一個Account對象
}
/**
* 這是一個取錢並將記錄存入list集合的方法
* @param loginUA
* @param money
*/
public void getMoney(User user, double money) {
try {
//用戶餘額已經被放進map集合中,同理,在User類中定義一個getMoney()方法,其返回的是布爾類型的值
if(user.getMoney(money)){
System.out.println("取款成功,用戶當前餘額:"+user.getBalance());
System.out.println("==============================");
//輸出日誌
for (String str : user.getList()) {
System.out.println(str);
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
用戶類(User類):
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class User {
private double balance;//用戶賬戶金額
private String id;//用戶賬號名
private String passward;//用戶密碼
private List<String> list=new ArrayList<>() ;//用於存儲用戶取款記錄的日誌集合
//無參構造方法
public User() {
super();
}
//有參構造方法
public User(String id, String passward,double money) {
super();
this.balance = money;
this.id = id;
this.passward = passward;
}
//各個屬性的set和get方法
public double getMoney() {
return balance;
}
public void setMoney(int money) { this.balance = money; }
public String getAccount() { return id; } public void setAccount(String account) { this.id = account; } public String getPassward() { return passward; } public void setPassward(String passward) { this.passward = passward; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } /** * 因爲賬戶的密碼被裝在map集合中,不方便直接訪問,所以在User類中定義一個檢查密碼是否正確的方法 * 其返回一個布爾類型的值 * @param password * @return */ public boolean checkPassword(String password){ if(password.equals(this.passward)){ return true; }else{ return false; } } /** * 與上面的方法同理,判斷取款金額是否合理的方法,返回一個布爾類型的值 * @param money * @return * @throws Exception */ public boolean getMoney(double money) throws Exception { if (money < 100 || money > 5000) { //規定每次取款金額的範圍,超過這個範圍則拋出異常 throw new Exception("取款金額不合理!取款金額必須大於100,小於5000"); } if (this.balance < money) {//這個判斷條件被許多人忽略,當賬戶內餘額不足時,拋出異常 throw new Exception("您的餘額不足!"); } //從帳號餘額中扣除相應金額 this.balance -= money; //將取款記錄到日誌中 String logStr = User.currentTime()+"\t取款 " + money +"元, 餘額:"+this.balance; this.list.add(logStr); //將記錄添加進list集合中 return true; //返回true } /** * 獲取當前時間的方法 * @return */ public static String currentTime(){ long ct = System.currentTimeMillis(); Date d = new Date(ct); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:ss:mm"); return sdf.format(d); } }