Java基於對象流實現銀行系統
Java基於對象流實現銀行系統的具體代碼,供大傢參考,具體內容如下
系統特點:
- 數據持久化到文件中,系統啟動後,加載文件中數據到集合中,相當於做瞭一個緩存。
- 文件讀寫使用的是對象流(ObjectInputStream和ObjectOutputStream),用戶是看不懂那些文件的,一定程度上保證瞭密碼和餘額的安全性。
- 采用瞭MVC分層設計思想,雖然沒有C控制層和V視圖層,但是有完備的M模型層,數據處理Service,數據讀寫Dao,數據存儲POJO(每一層隻負責處理自己的事情,層次內部的改動不會影響其他層次)。
- 雖然有些細節問題由於時間關系沒有處理好,比如沒有處理輸入類型不匹配的異常,但是絕大多數功能都合理得實現瞭。
全部代碼
pojo:
public class User implements Serializable { private static final long serialVersionUID = -7019656536540026883L; private String username;//用戶名 private String password;//密碼 private Double balance;//餘額 public User(String username, String password, Double balance) { this.username = username; this.password = password; this.balance = balance; } public User() { } 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 getBalance() { return balance; } public void setBalance(Double balance) { this.balance = balance; } public String toString(){ StringBuilder builder = new StringBuilder("{"); builder.append(this.username).append(",").append(this.password).append(",").append(this.balance).append("}"); return builder.toString(); }
dao層:
public class ATMDao { private String userFilePath = "src\\userMsg.txt";//相對路徑 private HashMap<String, User> userMsg; { userMsg = this.loadFile();//讀取文件信息 } public HashMap<String, User> getUserMsg() { return userMsg; } //查詢用戶 public User selectUser(String username){ return userMsg.get(username); } //更新用戶信息 public void updateUser(User user){ this.userMsg.replace(user.getUsername(),user); this.updateFile(userMsg); } //增加用戶 public void addUser(User user){ this.userMsg.put(user.getUsername(),user); this.updateFile(userMsg); } //刪除用戶 public void deleteUser(User user){ this.userMsg.remove(user.getUsername()); this.updateFile(userMsg); } //隻在初始化時時調用一次 /* public void initFile(){ User user1 = new User("張三","123",800.0); User user2 = new User("李四","666",70.0); User user3 = new User("王五","888",555.0); userMsg.put(user1.getUsername(),user1); userMsg.put(user2.getUsername(),user2); userMsg.put(user3.getUsername(),user3); this.updateFile(userMsg); }*/ private HashMap<String, User> loadFile(){ File file = new File(userFilePath); FileInputStream fis = null; ObjectInputStream ois = null; HashMap<String, User> box = null; try { fis = new FileInputStream(file); ois = new ObjectInputStream(fis); try { box = (HashMap<String, User>)ois.readObject(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); }finally { try { if(fis!=null) fis.close(); } catch (IOException e) { e.printStackTrace(); } try { if(ois!=null) ois.close(); } catch (IOException e) { e.printStackTrace(); } } return box; } private void updateFile(HashMap<String,User> userMsg){ File file = new File(userFilePath); FileOutputStream fos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream(file); oos = new ObjectOutputStream(fos); oos.writeObject(userMsg); oos.flush(); } catch (IOException e) { e.printStackTrace(); }finally { try { if(fos!=null) fos.close(); } catch (IOException e) { e.printStackTrace(); } try { if(oos!=null) oos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
service層:
public class ATMService { ATMDao dao = new ATMDao(); public boolean login(String username,String password){ User user = dao.selectUser(username); if (user!=null && user.getPassword().equals(password)) return true; else return false; } //查詢餘額 public Double queryBalance(String name){ User user = dao.selectUser(name); return user.getBalance(); } //存款 public void deposit(String name,Double account){ User user = dao.selectUser(name); user.setBalance(user.getBalance()+account); dao.updateUser(user); } //取款 public void withdrawal(String name,Double account){ User user = dao.selectUser(name); if (user.getBalance()>=account) { user.setBalance(user.getBalance() - account); dao.updateUser(user); }else { System.err.println("對不起,"+user.getUsername()+",您的餘額不足"); } } //轉賬 public void transfer(String oldName,String newName,Double account){ User oldUser = dao.selectUser(oldName); User newUser = dao.selectUser(newName); if (newUser!=null){ if (oldUser.getBalance()>=account){ oldUser.setBalance(oldUser.getBalance()-account); newUser.setBalance(newUser.getBalance()+account); dao.updateUser(oldUser); dao.updateUser(newUser); }else { System.err.println("對不起,"+oldUser.getUsername()+",您的餘額不足"); } }else { System.err.println("對不起,您輸入的賬號不存在"); } } //銷戶 public void closeAccount(String name){ User u = dao.selectUser(name); dao.deleteUser(u); } //註冊 public void register(String name,String password){ User user = dao.selectUser(name); if (user!=null){ System.err.println("對不起,您輸入的賬號已存在"); }else { user = new User(name,password,0.0); dao.addUser(user); } } }
測試:
public class Test { public static void main(String[] args) { ATMService service = new ATMService(); Scanner input = new Scanner(System.in); System.out.println("歡迎您使用銀行自助服務系統\n請輸入您的賬號:"); String username = input.nextLine(); System.out.println("請輸入您的密碼:"); String password = input.nextLine(); if (service.login(username,password)){ System.out.println("登錄成功!"); while(true) { System.out.println("請選擇您的操作:\n0.註冊\n1.查詢餘額\n2.存款\n3.取款\n4.轉賬\n5.銷戶\n6.退出"); String option = input.nextLine(); switch (option) { case "0": System.out.println("請輸入您的用戶名:"); String newname = input.nextLine(); System.out.println("請輸入您的密碼:"); String newPass = input.nextLine(); service.register(newname, newPass); System.out.println("恭喜您,註冊成功!,您的用戶名為" + newname); break; case "1": Double balance = service.queryBalance(username); System.out.println("您的餘額為" + balance); break; case "2": System.out.println("請輸入存款金額:"); double amount = input.nextDouble(); input.nextLine(); service.deposit(username, amount); System.out.println("恭喜您,存款成功"); break; case "3": System.out.println("請輸入取款金額:"); double d = input.nextDouble(); input.nextLine(); service.withdrawal(username, d); System.out.println("恭喜您,取款成功"); break; case "4": System.out.println("請輸入對方賬戶:"); String anothername = input.nextLine(); System.out.println("請輸入轉賬金額:"); double t = input.nextDouble(); input.nextLine(); service.transfer(username, anothername, t); System.out.println("恭喜您,轉賬成功"); break; case "5": service.closeAccount(username); System.out.println("銷戶成功"); break; case "6": System.exit(0); } } }else { System.err.println("用戶名或密碼錯誤"); } //dao.initFile(); /*HashMap<String, User> userMsg = dao.getUserMsg(); Iterator<String> iterator = userMsg.keySet().iterator(); while (iterator.hasNext()){ String username = iterator.next(); User user = userMsg.get(username); System.out.println(user); }*/ } }
部分測試截圖
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。