您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關java怎么實現銀行管理系統,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
具體內容如下
Bank類
package First; import java.util.TreeSet; //銀行類 public class Bank { private String Bankname = "坑對對銀行"; //銀行名稱 TreeSet<User> holder = null; //銀行存儲的信息 private static Bank INSTANCE = new Bank(); private Bank() {} //私有化銀行類 public static Bank getInstance() { // TODO Auto-generated method stub return INSTANCE; } //存款功能 public void saveMoney(User user,double money) { //初始金額與存入金額相加 double sum = user.getSum()+money; user.setSum(sum); System.out.print(money+"存款成功!!!"+",當前余額是:"+user.getSum()); System.out.print("\n"); } //取款功能 public void drawMoney(User user,double money,String pw) { if(user.getPw().equals(pw)) { //先判斷取款密碼 if(user.getSum()>=money) { //判斷余額 user.setSum(user.getSum()-money); System.out.print("取款成功"); System.out.print("\n"); System.out.print("當前余額為:"+"\t"+user.getSum()); System.out.print("\n"); }else { System.out.print("抱歉余額不足!!! 當前余額:"+user.getSum()); System.out.print("\n"); } } } //查看余額以及信息 public void looksum(User user) { System.out.print("姓名:"+user.getName()+",賬號:"+user.getAccount()+",余額:"+user.getSum()); System.out.print("\n"); } //歡迎語句 public void welcom(User user) { if(user.getSexual().equals("男")) { System.out.print(user.getName()+" 先生!!"+" "+(this.Bankname)+" 歡迎您!!!"); System.out.print("\n"); }else if(user.getSexual().equals("女")){ System.out.print(user.getName()+" 女士!!"+" "+(this.Bankname)+" 歡迎您!!!"); System.out.print("\n"); } } //再見語句 public void GoodBye(User user) { if(user.getSexual().equals("男")) { System.out.print(user.getName()+" 先生!!"+" "+(this.Bankname)+" 祝您生活愉快!!!"); System.out.print("\n"); }else if(user.getSexual().equals("女")){ System.out.print(user.getName()+" 女士!!"+" "+(this.Bankname)+" 祝您生活愉快!!!"); System.out.print("\n"); } } }
Data:數據存儲類
package First; import java.util.TreeSet; //模擬一個數據庫 //銀行的數據庫 public class Data { TreeSet<User> holder = new TreeSet<User>(new MyComparator()); //用于存儲用戶信息的容器---自定義比較規則 File file = File.getFile(); //獲取一個文件類 private static Data data = new Data(); //單例模式--因為存儲數據的類唯一.所以不能隨意被實例化 private Data() {} //私有化構造方法 public static Data getData() { //提供返回該對象的靜態方法 return data; } //添加用戶 public void addUser(User user) { holder.add(user); System.out.print("注冊成功!!"); System.out.print("\n"); } //刪除用戶 public void removeUser(User user) { if(holder.contains(user)) { //如果容器中包含所user對象,則移除對象 holder.remove(user); }else { //如果不包含則打印語句 System.out.print("抱歉請檢查輸入的信息是否正確"); System.out.print("\n"); } } //獲取容器 public TreeSet<User> getHolder() { return holder; } //寫入容器 public void setHolder(TreeSet<User> holder) { this.holder = holder; } }
FIie:文件IO類
package First; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Iterator; import java.util.TreeSet; /* * 文件類--負責向內存中初始化信息以及程序結束后向內存中存入信息 * */ public class File { TreeSet<User> holder = Data.getData().getHolder(); //從Data數據庫中獲取容器 //文件類只供使用不能被任意實際化 public static File FILE; //私有化一個File類 private File() {}; public static File getFile() { //對外界提供獲取方法 return FILE; } //保存到文件里 public static void SaveToFile(TreeSet<User> list) { //將獲取到的容器存入到文件中----序列化 ObjectOutputStream oo = null; try { oo = new ObjectOutputStream(new FileOutputStream(".\\src\\Test\\text1.txt")); //獲取文件存儲地址 oo.writeObject(list); //將容器寫入 } catch (Exception ex) { ex.printStackTrace(); }finally { try { oo.close(); //將流關閉 }catch(IOException e) { e.printStackTrace(); } } } //從文件里讀取 public static TreeSet<User> ReadFromFile() { ObjectInputStream ois = null; TreeSet<User> list = null; try { ois = new ObjectInputStream(new FileInputStream(".\\src\\Test\\text1.txt"));//反序列化從文件中獲取容器加載到內存 list = Data.getData().getHolder(); //獲取容器 list = (TreeSet<User>) ois.readObject(); //從流中獲取對象 Iterator it = list.iterator(); //迭代器 System.out.print("當前用戶信息:"); System.out.print("\n"); System.out.print("\n"); while(it.hasNext()) { //迭代初始信息 User user = (User) it.next(); System.out.print("\t"+"姓名:"+user.getName()+"\t"+"賬號:"+user.getAccount()+"\t"+"密碼:"+user.getPassword()+"\t"+"余額:"+user.getSum()+"\t"); System.out.print("\n"); System.out.print("\n"); } } catch (Exception ex) { ex.printStackTrace(); }finally { try { ois.close(); //將流關閉 }catch(IOException e){ e.printStackTrace(); } } return list; } }
MyComparator:自定義比較器類
package First; import java.io.Serializable; import java.util.Comparator; //自定義比較器 //想要存入集合必須實現序列化和反序列化 public class MyComparator implements Comparator,Serializable{ /** * */ private static final long serialVersionUID = 1L; @Override public int compare(Object arg0, Object arg1) { User user1 = (User)arg0; //重寫比較方法 --- 強轉成User類型 User user2 = (User)arg1; return user1.getAccount().compareTo(user2.getAccount());//比較兩個銀行賬戶 } }
Operate:系統核心
package First; import java.util.Iterator; import java.util.Scanner; import java.util.TreeSet; //后臺操作系統 //后臺管理員 public class Operate { Scanner sc = new Scanner(System.in); //鍵盤錄入 TreeSet<User> holder = Data.getData().getHolder(); //從Data數據庫中獲取容器 Bank bank = Bank.getInstance(); //獲取一個銀行類 File file = File.getFile(); //獲取一個文件類 //登錄菜單 public void registerMenu(){ System.out.print("****************************"); System.out.print("\n"); System.out.print("\n"); System.out.print(" 歡迎來到坑多多銀行登錄系統!!!\n"); System.out.print("\n"); System.out.print("****************************"); System.out.print("\n"); System.out.print("\n"); holder = File.ReadFromFile(); //從文件中獲取容器 Data.getData().setHolder(holder); //把從文件中獲取容器存儲到數據庫中 int str=1; //從鍵盤獲取數字 while(str!=0) { System.out.print("****************************"); System.out.print("\n"); System.out.print(" 1: 登錄 "); System.out.print("\n"); System.out.print(" 2: 注冊 "); System.out.print("\n"); System.out.print(" 3: 忘記密碼"); System.out.print("\n"); System.out.print(" 4: 注銷用戶"); System.out.print("\n"); System.out.print(" 0: 退出"); System.out.print("\n"); System.out.print("****************************"); System.out.print("\n"); System.out.print("請輸入:"); str = sc.nextInt(); switch(str){ //根據用戶輸入的值進行選擇 case 1: System.out.print("請輸入您的賬號:"); String account = sc.next(); //從鍵盤中獲取錄入用戶名 System.out.print("請輸入您的密碼:"); String pw = sc.next(); //從鍵盤中錄入密碼 User user = logon(holder,account,pw); if(user!=null) { //判斷是否登錄成功 System.out.print("登錄成功!!!!歡迎使用"); System.out.print("\n"); System.out.print("正在跳轉到銀行操作界面........."); System.out.print("\n"); this.BankMenu(user); //如果登錄成功則將user對象傳入到銀行操作界面并跳轉 }else { System.out.print("登錄失敗!!!請從新輸入"); //如果登錄失敗打印此語句 System.out.print("\n"); } break; case 2: this.logon(); //調用注冊方法 break; case 3: this.modify(); //調用修改方法 break; case 4: holder = logout(holder); //調用刪除操作 break; case 0: File.SaveToFile(holder); //退出系統式將信息保存到文件中 System.out.print("再見!!!!"); System.out.print("\n"); default: System.out.print("輸入錯誤!!!!"); System.out.print("\n"); } } } //登錄方法 public User logon(TreeSet<User> holder,String account,String pw) { Iterator<User> it = holder.iterator(); //獲取迭代器 while(it.hasNext()) { User user = it.next(); if(user.getAccount().equals(account)&&user.getPassword().equals(pw)){ System.out.print("登錄成功!!"); System.out.print("\n"); return user; } } System.out.print("賬戶或密碼錯誤 登錄失敗!!"); System.out.print("\n"); return null; } //注冊方法體 public void logon(){ System.out.print("歡迎來到注冊界面!!!!"); System.out.print("\n"); String name; //用戶名 String account; //銀行賬號 String password; //密碼 String pw; //取款密碼 double sum; //賬戶金額 String sexual; //性別 User user = new User(); //先創建一個空參的user對象 boolean flag = true; //語句的開關 while(flag) { //將注冊的信息依次輸入 switch(1) { case 1: System.out.print("請輸入姓名:"); //依次從鍵盤中獲取對應的姓名----正則表達式 name = sc.next(); user.setName(name); //將名字寫入對象中 case 2: System.out.print("請輸入賬號(9位數字):"); account = sc.next(); //從鍵盤中獲取賬號 String reges ="[0-9]{9}"; //正則表達式規則對賬號進行限制 if(!account.matches(reges)) { //如果不符合要求則報錯 System.out.print("賬號不符合規范!!"); System.out.print("\n"); System.out.print("請重新注冊"); System.out.print("\n"); break; } user.setAccount(account); //將賬號寫入對象中 case 3: System.out.print("請輸入密碼(6位):"); password = sc.next(); reges ="[0-9]{6}"; //正則表達式規則 if(!password.matches(reges)){ //如果密碼不符合正則規則則報錯 System.out.print("密碼不符合規范!!"); System.out.print("\n"); System.out.print("請重新注冊"); System.out.print("\n"); break; } user.setPassword(password); //將密碼寫入對象中 case 4: System.out.print("請輸入取款密碼(6位):"); pw = sc.next(); reges ="[0-9]{6}"; //正則表達式規則 if(!pw.matches(reges)) { System.out.print("取款密碼不符合規范!!"); System.out.print("\n"); System.out.print("請重新注冊"); System.out.print("\n"); break; } user.setPw(pw); //將取款密碼寫入到對象中 case 5: System.out.print("請輸入金額:"); sum = sc.nextDouble(); user.setSum(sum); //將金額寫入到對象中 case 6: System.out.print("請輸入性別:"); sexual = sc.next(); user.setSexual(sexual); //將性別寫入到對象中 case 7: if(!holder.contains(user)){ Data.getData().addUser(user); //一切準確無誤后將user存入數據庫Data中 } else { System.out.print("用戶存在!!!"); System.out.print("\n"); //結束循環 } flag = false; break; } } } //忘記密碼操作 public void modify() { boolean flag = true; Iterator<User> it = holder.iterator(); //獲取迭代器 System.out.print("請輸入賬戶:"); String account = sc.next(); //從鍵盤中獲取賬戶 while(it.hasNext()) { User user = it.next(); if(user.getAccount().equals(account)){ //如果容器中有這個對象則對它進行修改 System.out.print("請輸入新賬戶的密碼:"); String pw = sc.next(); //從監盤上錄入新的密碼 String reges ="[0-9]{6}"; //正則表達式規則 if(!pw.matches(reges)) { //如果密碼不符合正則規則則報錯 System.out.print("密碼不符合規范!!"); System.out.print("\n"); }else { //正則表達式判斷是否符合條件 holder.remove(user); //先從原容器中刪除user user.setPassword(pw); //修改user的屬性 holder.add(user); //然后再向容器中添加 System.out.print("修改成功!!!"); System.out.print("\n"); flag = false; break; } } } if(flag) { System.out.print("沒有找到該用戶"); System.out.print("\n"); } } //注銷方法 public TreeSet<User> logout(TreeSet<User> list) { String username; //用戶名 String password; //密碼 System.out.print("請輸入用戶賬號:"); username = sc.next(); //從鍵盤錄入用戶名 System.out.print("請輸入密碼:"); password = sc.next(); //從鍵盤錄入密碼 Iterator it = list.iterator(); //獲取迭代器 while(it.hasNext()) { User user = (User)it.next(); //獲取對象 if(user.getAccount().equals(username)&&user.getPassword().equals(password)){ list.remove(user); //如果錄入的信息匹配則從容器中刪除user對象 System.out.print("注銷成功!!"); System.out.print("\n"); return list; } } System.out.print("賬號或者密碼錯誤"); //不匹配打印提示語句 System.out.print("\n"); return list; } //銀行操作界面 public void BankMenu(User user) { bank.welcom(user); //銀行調用歡迎語句 System.out.print("****************************"); System.out.print("\n"); System.out.print("\n"); System.out.print(" 歡迎來到坑多多銀行擦操作系統!!!\n"); System.out.print("\n"); int str = 1; while(str!=0) { System.out.print("****************************"); System.out.print("\n"); System.out.print(" 1: 取錢 "); System.out.print("\n"); System.out.print(" 2: 存錢 "); System.out.print("\n"); System.out.print(" 3: 查看余額"); System.out.print("\n"); System.out.print(" 0: 退出登錄"); System.out.print("\n"); System.out.print("****************************"); System.out.print("\n"); System.out.print("\n"); System.out.print("請輸入您的選項:"); //從鍵盤中讀取字符串 str = sc.nextInt(); switch(str){ //根據用戶輸入的值進行選擇 case 1: //取錢選項 System.out.print("請輸入取款密碼:"); String pw = sc.next(); //從鍵盤獲取輸入的密碼 if(user.getPw().equals(pw)) { //將輸入的取款密碼與用戶的密碼進行比對:如果相同則繼續執行 System.out.print("請輸入取款金額:"); double money = sc.nextDouble(); //從鍵盤獲取輸入的金額 if(money>=0) { //判斷金額是否有誤 bank.drawMoney(user, money, pw); //調用取款方法 }else { System.out.print("輸入金額錯誤!!!"); System.out.print("\n"); } }else { System.out.print("輸入密碼有誤!!!"); System.out.print("\n"); } break; case 2: System.out.print("請輸入取款金額:"); System.out.print("\n"); double money1 = sc.nextDouble(); //從鍵盤獲取輸入的金額 if(money1>=0) { //判斷金額是否有誤 bank.saveMoney(user, money1);; //調用存款方法 }else { System.out.print("輸入金額錯誤!!!"); System.out.print("\n"); } break; case 3: bank.looksum(user); //調用銀行---查詢賬戶操作 break; case 0: bank.GoodBye(user); //調用結束語句 break; default: System.out.print("輸入錯誤"); System.out.print("\n"); } } } }
User類:
package First; import java.io.Serializable; /* * 這是一個User類 * */ public class User implements Serializable{ /** * 實現接口,用于序列化和反序列化 */ private static final long serialVersionUID = 1L; private String name; //用戶名 private String account; //銀行賬號 private String password; //密碼 private String pw; //取款密碼 private double sum; //賬戶金額 private String sexual; //性別 //獲取用戶名 public String getName() { return name; } //寫入用戶名 public void setName(String name) { this.name = name; } //獲取賬號 public String getAccount() { return account; } //寫入賬號 public void setAccount(String account) { this.account = account; } //獲取密碼 public String getPassword() { return password; } //修改密碼 public void setPassword(String password) { this.password = password; } //查看余額 public double getSum() { return sum; } //修改余額 public void setSum(double sum) { this.sum = sum; } //寫入性別 public String getSexual() { return sexual; } //修改性別 public void setSexual(String sexual) { this.sexual = sexual; } //獲取取款密碼 public String getPw() { return pw; } //寫入取款密碼 public void setPw(String pw) { this.pw = pw; } //hashCode比較方法 @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((account == null) ? 0 : account.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + ((pw == null) ? 0 : pw.hashCode()); result = prime * result + ((sexual == null) ? 0 : sexual.hashCode()); long temp; temp = Double.doubleToLongBits(sum); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } //equals比較方法 /* * 知識點: * public boolean equals(Object obj)名稱不可改變 * */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (account == null) { if (other.account != null) return false; } else if (!account.equals(other.account)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; if (pw == null) { if (other.pw != null) return false; } else if (!pw.equals(other.pw)) return false; if (sexual == null) { if (other.sexual != null) return false; } else if (!sexual.equals(other.sexual)) return false; if (Double.doubleToLongBits(sum) != Double.doubleToLongBits(other.sum)) return false; return true; } //初始化用戶 User(String name,String account,String password,String pw,double sum,String sexual){ this.name = name; this.account = account; this.password = password; this.sum = sum; this.sexual = sexual; this.pw = pw; } //空參構造函數 User(){ } }
Main()類
package First; public class Test { //測試類----唯一的主函數 public static void main(String[] args) { Operate operate = new Operate(); operate.registerMenu(); //調用登錄方法 } }
關于“java怎么實現銀行管理系統”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。