Java實現ATM機操作系統
本文實例為大傢分享瞭Java實現ATM機操作系統的具體代碼,供大傢參考,具體內容如下
用IO流操作txt文件作為數據庫模擬實現一個ATM業務操作系統—->網上銀行,實現登錄,查詢餘額,存款,取款,轉賬,開戶,銷戶等業務功能
看代碼
1.用戶類—–>User:
package atm; import java.io.Serializable; public class User implements Serializable{ //建議除瞭私有屬性 無參數有參數構造方法 屬性對應的get、set方法 //建議類實現一個序列化接口 添加一個序列化版本號 private static final long serialVersionUID = 1L; //隻是為瞭記錄數據庫中的一行信息 賬號 密碼 餘額 private String aName; private String aPassword; private Float aSalary; public User() {} public User(String aName,String aPassword,Float aSalary) { this.aName = aName; this.aPassword = aPassword; this.aSalary = aSalary; } public String getaName() { return this.aName; } public void setaName(String newName) { this.aName = newName; } public String getaPassword() { return this.aPassword; } public void setaPassword(String newPassword) { this.aPassword = newPassword; } public Float getSalary() { return this.aSalary; } public void setSalary(Float newSalary) { this.aSalary = newSalary; } }
2.操作IO的類——>FileLoadAndCommit:
package atm; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; //操作文件 public class FileLoadAndCommit { //文件路徑 private String pathName = null; public FileLoadAndCommit(String pathName) { this.pathName = pathName; } //讀取文件裝入集合 public HashMap<String,User> loadFile(){ //臨時的存儲空間,將文件中讀取的數據存儲以便於修改 HashMap<String,User> userBox = new HashMap<String,User>(); //IO流讀取 FileReader fr = null; BufferedReader br = null; try { fr = new FileReader(new File(pathName)); br = new BufferedReader(fr); String code = br.readLine();//讀取一行 //循環遍歷所有行 按"-"拆分一行的數據,作為User存入集合 while(code != null) { String[] value = code.split("-"); User user = new User(value[0],value[1],Float.parseFloat(value[2])); userBox.put(user.getaName(),user); code = br.readLine(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { //如果流管道存在,才關閉 if(fr != null) { fr.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(br != null) { br.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return userBox; } //將集合更新入文件 public void commit(HashMap<String,User> userBox) { //IO流寫入 FileWriter fw = null; BufferedWriter bw = null; try { fw = new FileWriter(new File(pathName)); bw = new BufferedWriter(fw); //迭代集合內所有賬戶名 按賬戶名找到對應的User對象 Iterator<String> names = userBox.keySet().iterator(); while(names.hasNext()) { String name = names.next(); User user = userBox.get(name); //用StringBuilder拼接字符串 StringBuilder sBuilder = new StringBuilder(); sBuilder.append(user.getaName()); sBuilder.append("-"); sBuilder.append(user.getaPassword()); sBuilder.append("-"); sBuilder.append(user.getSalary()); bw.write(sBuilder.toString());//將拼好的一行數據寫入文件 bw.flush();//刷新 bw.newLine();//換行 } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { //必須先關閉BufferedWriter再關閉FileWrite,否賊會拋異常java.io.IOException: Stream closed try { if(bw != null) { bw.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(fw != null) { fw.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
3.負責讀寫的類—–>AtmDao:
package atm; import java.util.HashMap; //持久化,負責讀寫數據,緩存 public class AtmDao { private FileLoadAndCommit flac = new FileLoadAndCommit("D:\\test\\ATM\\testATM.txt"); HashMap<String,User> userBox = flac.loadFile(); //獲取一個User對象 public User selectOne(String aName) { return userBox.get(aName); } //更新文件數據 public void update(User user) { userBox.put(user.getaName(),user); flac.commit(userBox); } //刪除文件數據 public void delete(User user) { userBox.remove(user.getaName(),user); flac.commit(userBox); } }
4.負責業務邏輯的類——>ATMService:
package atm; import java.util.HashMap; //負責業務邏輯 判斷 比較 計算 public class ATMService { //登錄 private AtmDao dao = new AtmDao(); public String login(String aName,String aPassword) { User user = dao.selectOne(aName); if(user!=null) { if(aPassword.equals(user.getaPassword())) { return "登錄成功"; } } return "用戶名或密碼錯誤"; } //查詢餘額 public Float inquiry(String aName) { User user = dao.selectOne(aName); return user.getSalary(); } //存款 public void addMoney(String aName,Float addMoney) { User user = dao.selectOne(aName);//獲取該用戶對象 user.setSalary(user.getSalary()+addMoney);//用User的set方法修改用戶金額 dao.update(user);//更新文件數據 System.out.println("存款成功\n"); } //取款 public void getMoney(String aName,Float getMoney) { User user = dao.selectOne(aName);//獲取該用戶對象 if(getMoney <= user.getSalary()) {//如果要取出的錢大於餘額就不能取瞭 user.setSalary(user.getSalary()-getMoney); dao.update(user); System.out.println("取款成功\n"); }else { System.out.println("對不起,您的餘額不足\n"); } } //轉賬 public void transferMoney(String outName,String inName,Float transferMoney) { User outUser = dao.selectOne(outName);//轉出用戶 User inUser = dao.selectOne(inName);//轉入用戶 if(inUser!=null) {//轉入用戶存在 if(outUser!=inUser) {//轉出和轉入用戶不能是同一個用戶 if(transferMoney <= outUser.getSalary()) {//轉出金額大於餘額就不能轉瞭 outUser.setSalary(outUser.getSalary()-transferMoney);//修改轉出用戶餘額 inUser.setSalary(inUser.getSalary()+transferMoney);//修改轉入用戶餘額 //更新文件數據 dao.update(outUser); dao.update(inUser); System.out.println("轉賬成功\n"); }else { System.out.println("對不起"+outName+",您的餘額不足\n"); } }else { System.out.println("對不起,不能給自己轉賬,您可以試試使用存款業務\n"); } }else { System.out.println("對不起,您輸入的用戶不存在\n"); } } //開戶 public User creatUser(String aName,String aPassword,Float aSalary) { User user = new User(aName,aPassword,aSalary); dao.update(user);//更新文件數據 System.out.println("用戶"+aName+",創建成功"); return user; } //銷戶 public void deleteUser(String aName) { User user = dao.selectOne(aName);//獲取名為aName的用戶 if(user != null) {//判斷該用戶是否在文件數據內存在 dao.delete(user); System.out.println("用戶"+aName+",刪除成功"); }else { System.out.println("要銷毀的賬戶不存在"); } } }
5.測試類—–>TestMain:
package atm; import java.util.Scanner; public class TestMain { public static void main(String[] args) { ATMService atmService = new ATMService();//獲取執行業務的方法 Scanner input = new Scanner(System.in); System.out.println("歡迎進入ATM機系統"); System.out.println("請選擇要操作的業務:\n1.用戶登錄\n2.開戶\n3.按任意鍵退出"); String choice = input.next(); if(choice.equals("1")) { System.out.println("請輸入賬戶名"); String name = input.next(); System.out.println("請輸入密碼"); String password = input.next(); String afterLogin = atmService.login(name,password);//判斷輸入的賬戶名密碼是否正確 if(afterLogin.equals("登錄成功")) {//if正確則登錄成功 System.out.println("登錄成功!\n"); //使用while循環反復進行switch執行操作業務 while(true) { System.out.println("請選擇服務項目:"); System.out.println("1.查詢\n2.存款\n3.取款\n4.轉賬\n5.銷戶\n(按q退出系統)"); String option = input.next(); switch (option) { case "1": //查詢 Float money = atmService.inquiry(name); System.out.println("尊敬的客戶,您的餘額為"+money+"元\n"); break; case "2": //存款 System.out.println("請輸入存款金額"); Float addMoney = input.nextFloat(); atmService.addMoney(name,addMoney); break; case "3": //取款 System.out.println("請輸入取款金額"); Float getMoney = input.nextFloat(); atmService.getMoney(name,getMoney); break; case "4": //轉賬 System.out.println("請輸入轉賬用戶ID:"); String id = input.next(); System.out.println("請輸入轉賬金額:"); Float transferMoney = input.nextFloat(); atmService.transferMoney(name,id,transferMoney); break; case"5": //銷戶 System.out.println("您確定要銷毀當前賬戶嗎?賬戶內所有餘額都會消失\nYes/No\n"); String decision = input.next(); if(decision.equalsIgnoreCase("yes")) { atmService.deleteUser(name);//刪除當前user數據 }else if(decision.equalsIgnoreCase("no")){ break; } break; case "q": System.out.println("已退出ATM機系統,感謝您的使用!!!"); System.exit(0);//退出程序 break; default: System.out.println("請輸入正確的指令\n"); break; } } }else { System.out.println(afterLogin); } }else if(choice.equals("2")){ //開戶 System.out.println("請設置您的用戶名"); String newUserName = input.next(); System.out.println("請設置您的密碼"); String newPassword = input.next(); User newUser = atmService.creatUser(newUserName, newPassword, 0.0F); System.out.println("\n初始餘額為"+newUser.getSalary()+"元"); }else { System.out.println("已退出ATM機系統,感謝您的使用!!!"); } } }
部分運行結果:
1.賬戶登錄
2.查詢
3.存款
4.退出
作為數據庫的txt文件:
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。