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