您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“Java如何實現銀行賬戶管理子系統”,內容詳細,步驟清晰,細節處理妥當,希望這篇“Java如何實現銀行賬戶管理子系統”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
所用到的知識點:面向對象基礎語法,封裝,方法覆蓋(重寫)、繼承、多態
話不多說,直接上代碼
Account.java
package com.task1; import java.util.Scanner; public class Account { //規定賬戶類型: //0 – 儲蓄賬戶 1 – 信用賬戶 2 – 可貸款儲蓄賬戶 3– 可貸款信用賬戶 private Long id ;//賬戶號碼 private String password;//賬戶密碼 private String name;//真實姓名 private String personId;//身份證號碼 private String email;//客戶的電子郵箱 private double balance;//賬戶余額 private int type;//賬戶類型 //無參的構造方法 public Account(){ } //有參的構造方法 public Account(long id, String password, String name, String personId, String email, double balance, int type) { super(); this.id = id; this.password = password; this.name = name; this.personId = personId; this.email = email; this.balance = balance; this.type = type; } //get、set方法 public Long getId() { return id; } public void setId(long id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPersonId() { return personId; } public void setPersonId(String personId) { this.personId = personId; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public int getType() { return type; } public void setType(int type) { this.type = type; } //存款 public Account deposit(double money ) { this.balance+= money; //余額增加 return this; } //取款 public Account withdraw(double money) { if(balance - money >= 0) { //判斷余額是否足夠 balance -= money; //余額減少 // System.out.println("存款成功"); } else { System.out.println("您的余額不足!"); } return this; } //重寫toString方法 @Override public String toString() { return "Account [id=" + id + ", password=" + password + ", name=" + name + ", personId=" + personId + ", email=" + email + ", balance=" + balance + ", type=" + type + "]"; } }
SavingAccount.java
package com.task1; public class SavingAccount extends Account { public SavingAccount() { super(); // TODO Auto-generated constructor stub } public SavingAccount(long id, String password, String name, String personId, String email, double balance, int type) { super(id, password, name, personId, email, balance, type); // TODO Auto-generated constructor stub } @Override public String toString() { return "SavingAccount [getId()=" + getId() + ", getPassword()=" + getPassword() + ", getName()=" + getName() + ", getPersonId()=" + getPersonId() + ", getEmail()=" + getEmail() + ", getBalance()=" + getBalance() + ", getType()=" + getType() + "]"; } }
CreditAccpunt.java
package com.task1; public class CreditAccount extends Account{ private double ceiling; public CreditAccount() { super(); } public CreditAccount(long id, String password, String name, String personId, String email, double balance, int type,double ceiling) { super(id, password, name, personId, email, balance, type); this.ceiling = ceiling; } public double getCeiling() { return ceiling; } public void setCeiling(double ceiling) { this.ceiling = ceiling; } @Override public String toString() { return "CreditAccount [getCeiling()=" + getCeiling() + ", getId()=" + getId() + ", getPassword()=" + getPassword() + ", getName()=" + getName() + ", getPersonId()=" + getPersonId() + ", getEmail()=" + getEmail() + ", getBalance()=" + getBalance() + ", getType()=" + getType() + "]"; } @Override public Account withdraw(double money) { if(getBalance() >= money) { setBalance(getBalance() - money); }else if(getBalance() + getCeiling() >= money) { setCeiling(getCeiling()-(money-getBalance())); setBalance(0); }else { System.out.println("對不起,您的余額不足"); } return this; } }
Bank.java
package com.task1; import java.util.Scanner; public class Bank { int index = 0; //當前用戶數量 Account [] accounts = new Account [100];//數據庫 Account account = null; /* * 用戶開戶(register) 參數列表: Long 賬號, String密碼, String確認密碼,String 姓名,String身份證號碼,String郵箱,int 賬戶類型; (Long id, String password, String repassword, String name, String personID, String email, int type) 返回類型:Account 規定賬戶類型:0 – 儲蓄賬戶 1 – 信用賬戶 2 – 可貸款儲蓄賬戶 3– 可貸款信用賬戶 */ //用戶開戶 public Account register(Long id, String password, String repassword, String name, String personId, String email, int type) { if(password.equals(repassword)) { // 判斷兩次輸入密碼是否一致 switch (type) { //根據用戶類型創建不同賬戶 case 0: account = new SavingAccount(id, repassword, name, personId, email, 0, type); //創建儲蓄賬戶 break; case 1: account = new CreditAccount(id, repassword, name, personId, email, 0, type,1000); //創建信用賬戶 } accounts[index++] = account; //賬戶信息存到數據庫,用戶數量+1 } // else { // System.out.println("兩次輸入的密碼不一致"); // } return account; } //用戶登錄 public Account login(Long id, String password) { int find = searchIdPassword(index, id, password); //當前用戶數組下標 if(find >= 0) { //判斷賬戶密碼是否正確 // System.out.println("登錄成功"); return accounts[find]; //返回當前對象 } // else { // System.out.println("用戶名密碼錯誤"); // } return null; //如果賬戶密碼錯誤返回空 } //用戶存款 public Account deposit(Long id, double money) { int find = searchId(index, id);當前用戶數組下標 if(find >= 0) { //判斷賬戶是否存在 accounts[find].deposit(money); //調用Account類的存款方法 // System.out.println("存款成功"); return accounts[find]; //返回當前對象 // accounts[find].setBalance(accounts[find].getBalance()+money); } // else { // System.out.println("用戶不存在"); // } return null; //如果賬戶不存在返回空 } //用戶取款 public Account withdraw(Long id, String password, double money) { int find = searchIdPassword(index, id, password);//當前用戶數組下標 if(find >= 0) { //判斷賬戶密碼是否正確 accounts[find].withdraw(money); //調用當前對象的取款方法 // System.out.println("取款成功"); return accounts[find]; //返回當前對象 } return null; } //設置透支額度 public Account updateCeiling(Long id, String password, double money) { int find = searchIdPassword(index, id, password);//獲取當前用戶數組下標 if((find >= 0) && (accounts[find].getType()) == 1){ //判斷賬戶號碼和密碼是否正確,賬戶類型是否為信用賬戶 ((CreditAccount)accounts[find]).setCeiling(((CreditAccount)accounts[find]).getCeiling() + money); //調用set方法設置透支額度 return accounts[find]; } return null; } // 轉賬功能 // 參數:from轉出賬戶,passwordFrom 轉出賬號的密碼,to轉入賬戶,money轉賬的金額 public boolean transfer(Long from, String passwordFrom, Long to, double money) { int find = searchIdPassword(index, from, passwordFrom); //轉賬賬戶數組下標 int find2 = searchId(index, to); //收款賬戶數組下標 if(find >= 0 && find2 >= 0 && accounts[find].getBalance() >= money) { //判斷轉賬賬戶密碼、收款賬戶是否匹配,判斷轉賬用戶余額是否足夠 accounts[find].withdraw(money);//轉賬用戶轉賬操作==取款 accounts[find2].deposit(money);//收款用戶 == 存款 return true; } return false; } //統計銀行所有賬戶余額總數 public double balanceSum() { double sum = 0; //初始化所有賬戶余額 for(int i = 0; i < index; i++) { //遍歷數組 sum += accounts[i].getBalance();//求和(賬戶余額) } return sum; } //統計所有信用賬戶透支額度總數 public double ceilingSum() { double sum = 0; //初始化所有透支額度和 for(int i = 0; i < index; i++) { //遍歷 if(accounts[i].getType() == 1) { //判斷賬戶類型是否為信用賬戶 sum += ((CreditAccount)accounts[i]).getCeiling(); //求和 } } return sum; } //搜索Id與密碼返回數組下標位置 public int searchIdPassword(int index, Long id, String password) { for(int i = 0; i < index; i++) { if(id.equals(accounts[i].getId()) && password.equals(accounts[i].getPassword())){ //比較賬戶和密碼是否匹配 return i ; //匹配則返回賬戶數組下標 } } return -1; //不匹配則返回-1 } //搜索Id public int searchId(int index, Long id) { for(int i = 0; i < index; i++) { if(id.equals(accounts[i].getId())){ //比較賬戶是否匹配 return i ; //匹配則返回賬戶數組下標 } } return -1; //不匹配則返回-1 } }
TestAccount.java
package com.task1; public class TestAccount { public static void main(String[] args) { Account a =new Account(123456L, "123456","張三", "421356", "tt@tt.com", 100.0, 0); System.out.println(a); } }
TestBank.java
package com.task1; import java.util.Scanner; public class TestBank { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Bank bank = new Bank(); String action =null; do { System.out.println("菜單:"); System.out.println("---[1.開戶]---"); System.out.println("---[2.登錄]---"); System.out.println("---[3.存款]---"); System.out.println("---[4.取款]---"); System.out.println("---[5.設置透支額度]---"); System.out.println("---[6.轉賬]---"); System.out.println("請選擇服務"); int choice =sc.nextInt(); switch(choice) { case 1: //開戶 System.out.println("請輸入賬戶號碼:"); Long id = sc.nextLong(); System.out.println("請輸入賬戶密碼:"); String password = sc.next(); System.out.println("請確認賬戶密碼:"); String repassword = sc.next(); System.out.println("請輸入真實姓名:"); String name = sc.next(); System.out.println("請輸入身份證號:"); String personId = sc.next(); System.out.println("請輸入電子郵箱:"); String email = sc.next(); System.out.println("請輸入賬戶類型:0 – 儲蓄賬戶 1 – 信用賬戶 2 – 可貸款儲蓄賬戶 3– 可貸款信用賬戶"); int type = sc.nextInt(); Account a1 = bank.register(id, password, repassword, name, personId, email, type); if(a1 != null) { System.out.println(a1); System.out.println("開戶成功"); }else { System.out.println("兩次輸入密碼不一致"); } break; case 2: //登錄 System.out.println("請輸入賬戶號碼:"); id = sc.nextLong(); System.out.println("請輸入賬戶密碼:"); password = sc.next(); Account a2 = bank.login(id, password); if(a2 != null) { System.out.println(a2); System.out.println("登錄成功"); }else { System.out.println("賬戶號碼密碼錯誤"); } break; case 3: //存款 System.out.println("請輸入賬戶號碼:"); id = sc.nextLong(); System.out.println("請輸入存款金額:"); double money = sc.nextDouble(); Account a3 = bank.deposit(id, money); if(a3 != null) { System.out.println(a3); System.out.println("存款成功"); }else { System.out.println("賬戶不存在"); } break; case 4: //取款 System.out.println("請輸入賬戶號碼:"); id = sc.nextLong(); System.out.println("請輸入賬戶密碼:"); password = sc.next(); System.out.println("請輸入取款金額:"); money = sc.nextDouble(); Account a4 = bank.withdraw(id, password, money); if(a4 != null ) { System.out.println(a4); System.out.println("取款成功"); }else { System.out.println("賬戶號碼密碼錯誤"); } break; case 5://設置透支額度 System.out.println("請輸入賬戶號碼:"); id = sc.nextLong(); System.out.println("請輸入賬戶密碼:"); password = sc.next(); System.out.println("請輸入透支金額:"); money = sc.nextDouble(); Account a5 = bank.updateCeiling(id, password, money); if(a5 != null ) { System.out.println(a5); System.out.println("設置透支額度成功"); }else { System.out.println("賬戶號碼密碼錯誤或賬戶類型錯誤"); } break; case 6:// 轉賬功能 System.out.println("請輸入轉賬賬戶號碼:"); Long from = sc.nextLong(); System.out.println("請輸入轉賬賬戶密碼:"); String passwordFrom = sc.next(); System.out.println("請輸入收款賬戶號碼:"); Long to = sc.nextLong(); System.out.println("請輸入轉賬金額:"); money = sc.nextDouble(); boolean flag = bank.transfer(from, passwordFrom, to, money); if(flag) { System.out.println("轉賬成功"); }else { System.out.println("轉賬失敗"); } break; default: System.out.println("服務選擇錯誤"); } System.out.println("是否繼續(y/n)?"); action = sc.next(); }while("y".equals(action)); double balanceSum = bank.balanceSum(); double ceilingSum = bank.ceilingSum(); System.out.println("銀行所有賬戶余額總數:"+balanceSum); System.out.println("所有信用賬戶透支額度總數"+ceilingSum); } }
讀到這里,這篇“Java如何實現銀行賬戶管理子系統”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。