java實(shí)現(xiàn)簡(jiǎn)單的ATM項(xiàng)目
本文實(shí)例為大家分享了java實(shí)現(xiàn)簡(jiǎn)單的ATM項(xiàng)目的具體代碼,供大家參考,具體內(nèi)容如下
首先要了解的是,這個(gè)ATM項(xiàng)目本身是一個(gè)輕量級(jí)的項(xiàng)目,只為了完成一些ATM具備的一些方法,并非是真正完成一個(gè)ATM的全部功能和需求
那么在這個(gè)輕量級(jí)的ATM項(xiàng)目中,我將完成添加儲(chǔ)蓄賬號(hào),添加信用賬戶(hù),提款,取款等基本功能。
適合新手查看,需要掌握java的繼承,多態(tài),封裝等基本技術(shù)能力
那么,首先創(chuàng)建如下的對(duì)象類(lèi):Account(賬戶(hù)類(lèi)),Bank(銀行類(lèi)),CreditAccount(信用賬戶(hù)),SavingAccount(儲(chǔ)蓄賬戶(hù)類(lèi));
大家首先應(yīng)該搞清楚,這些類(lèi)文件中之間的關(guān)系,每個(gè)類(lèi)之間需要用到什么樣的方法;
那么我們先填寫(xiě)Account類(lèi)
package com.atm.entity; /** * 銀行賬戶(hù)類(lèi) */public abstract class Account { /** * 賬戶(hù)帳號(hào) */ private String accountId; /** * 賬戶(hù)姓名 */ private String accountName; /** * 賬戶(hù)密碼 */ private String accountPwd; /** * 賬戶(hù)余額 */ private double accountBalance; /** * 賬戶(hù)身份證號(hào) */ private String accountPersonId; /** * 賬戶(hù)郵箱 */ private String accountEmail; /** * 賬戶(hù)聯(lián)系電話(huà) */ private long accountTelno; public Account() { } public Account(String accountName, String accountPwd, String accountPersonId, long accountTelno, String accountEmail) { this.accountName = accountName; this.accountPwd = accountPwd; this.accountPersonId = accountPersonId; this.accountTelno = accountTelno; this.accountEmail = accountEmail; } public String getAccountId() { return accountId; } public void setAccountId(String accountId) { this.accountId = accountId; } public String getAccountName() { return accountName; } public void setAccountName(String accountName) { this.accountName = accountName; } public String getAccountPwd() { return accountPwd; } public void setAccountPwd(String accountPwd) { this.accountPwd = accountPwd; } public double getAccountBalance() { return accountBalance; } public void setAccountBalance(double accountBalance) { this.accountBalance = accountBalance; } public String getAccountPersonId() { return accountPersonId; } public void setAccountPersonId(String accountPersonId) { this.accountPersonId = accountPersonId; } public String getAccountEmail() { return accountEmail; } public void setAccountEmail(String accountEmail) { this.accountEmail = accountEmail; } public long getAccountTelno() { return accountTelno; } public void setAccountTelno(long accountTelno) { this.accountTelno = accountTelno; } /** * 存款 * * @param money * 存款金額 * @return 返回賬戶(hù)余額 */ public double depoist(double money) {// money 形式參數(shù) if (money > 0) this.accountBalance += money; return this.accountBalance; } /** * 取款 * * @param money * 取款金額 * @return 返回賬戶(hù)余額 */ public abstract double withdraw(double money); /** * 轉(zhuǎn)賬 * * @param anotherAccount * 轉(zhuǎn)賬的對(duì)方賬戶(hù) * @param money * 轉(zhuǎn)賬金額 * @return 返回當(dāng)前賬戶(hù)的余額 */ public double tranferAccount(Account anotherAccount, double money) {// 形參 anotherAccount.accountBalance += money; this.accountBalance -= money; return this.accountBalance; } }
之后填寫(xiě)信用賬戶(hù)類(lèi)CreditAccount;我們應(yīng)該明白,他是繼承Account類(lèi)的,但是,他又需要擁有自身獨(dú)立的屬性,我們可以添加一個(gè)最高透支額度的屬性這樣來(lái)實(shí)現(xiàn)代碼
/** * 信用賬戶(hù) * * */public class CreditAccount extends Account { //成員變量 private double maxOverdraw;//最高透支額度 //構(gòu)造函數(shù) public CreditAccount(String accountName,String accountPwd,String accountPersonId,long accountTelno,String accountEmail,double maxOverdraw){ super( accountName, accountPwd, accountPersonId, accountTelno, accountEmail); this.maxOverdraw = maxOverdraw; } //set,get public void setMaxOverdraw(double maxOverdraw ){ this.maxOverdraw = maxOverdraw; } public double getMaxOverdraw(){ return this.maxOverdraw; } @Override public double withdraw(double money) { // TODO Auto-generated method stub return 0; } }
同理 填寫(xiě)儲(chǔ)蓄賬戶(hù)(SavingAccount)類(lèi)文件
package com.atm.entity;/** * 儲(chǔ)蓄賬戶(hù) * * */public class SavingAccount extends Account { public SavingAccount(String accountName,String accountPwd,String accountPersonId,long accountTelno,String accountEmail){ super( accountName, accountPwd, accountPersonId, accountTelno, accountEmail); } @Override public double withdraw(double money) { // TODO Auto-generated method stub if(money <= getAccountBalance()){ } else System.out.println('賬戶(hù)余額不足'); return getAccountBalance(); } }
最重要的是填寫(xiě)B(tài)ank類(lèi)的內(nèi)容,在這個(gè)類(lèi)中,我們要完成注冊(cè),產(chǎn)生銀行賬戶(hù),統(tǒng)計(jì)所有信用賬戶(hù)的最高透支額度的總和,統(tǒng)計(jì)所有賬戶(hù)的總余額, 查詢(xún)出所有信用賬戶(hù)中透支額度最高的賬戶(hù), 查詢(xún)出所有儲(chǔ)蓄賬戶(hù)中余額最高的賬戶(hù)等功能
我們這樣填寫(xiě)
package com.atm.entity; import java.text.SimpleDateFormat;import java.util.Date; /** * 銀行類(lèi) * * @author qianghj * * 銀行開(kāi)戶(hù) ----> 銀行賬戶(hù) Account account = bank.開(kāi)戶(hù)(...) * */public class Bank { public Account[] accArray = new Account[2000]; public int count = 0;// 表示銀行賬戶(hù)的個(gè)數(shù) /** * 銀行賬戶(hù)開(kāi)戶(hù) * * @param accName * 用戶(hù)名稱(chēng) * @param accPwd * 用戶(hù)密碼 * @param accPersonId * 用戶(hù)身份證 * @param accTelno * 用戶(hù)手機(jī)號(hào)碼 * @param accEmail * 用戶(hù)郵箱 * @param accountType * 賬戶(hù)類(lèi)型 0: 儲(chǔ)蓄賬戶(hù) 1 : 信用賬戶(hù) * @param maxOverdraw * 信用賬戶(hù)的最高透支額度 * @return 返回有效的銀行賬戶(hù) */ public Account registAccount(String accName, String accPwd, String accPersonId, long accTelno, String accEmail, int accountType, double maxOverdraw) { Account account = null; if (accountType == 0) account = new SavingAccount(accName, accPwd, accPersonId, accTelno, accEmail); else account = new CreditAccount(accName, accPwd, accPersonId, accTelno, accEmail, maxOverdraw); account.setAccountId(generateNextAccountId()); accArray[count++] = account; return account; } /** * 產(chǎn)生銀行賬戶(hù)帳號(hào) * * @return 返回下一個(gè)賬戶(hù)的帳號(hào) 1,2,3,,4 */ public String generateNextAccountId() { return '62223421' + new SimpleDateFormat('yyyyMMddHHmmssSSS').format(new Date()); } // 統(tǒng)計(jì)所有信用賬戶(hù)的最高透支額度的總和 (1050 ) 2000 , 1050 public double statisticsCreditAccountMaxoverdrawSum() { double sum = 0; for (int i = 0; i < count; i++) { // 判斷賬戶(hù) 是不是 CreditAccount類(lèi)型 if (accArray[i] instanceof CreditAccount) { CreditAccount creditAcc = (CreditAccount) accArray[i]; sum += creditAcc.getMaxOverdraw(); } } return sum; } // 統(tǒng)計(jì)所有賬戶(hù)的總余額 public double aggregateAamount() { double sum = 0; for (int i = 0; i < count; i++) { if (accArray[i] instanceof SavingAccount) { SavingAccount savingAccount = (SavingAccount) accArray[i]; sum += savingAccount.getAccountBalance(); } } return sum; } // 查詢(xún)出所有信用賬戶(hù)中透支額度最高的賬戶(hù) public double maxLimit() { double tem = 0; for (int i = 0; i < count; i++) { if (accArray[i] instanceof CreditAccount) { CreditAccount creditAccount = (CreditAccount) accArray[i]; if (creditAccount.getMaxOverdraw() > tem) { tem = creditAccount.getMaxOverdraw(); } } } return tem; } // 查詢(xún)出所有儲(chǔ)蓄賬戶(hù)中余額最高的賬戶(hù) public double maxBalance() { double tem = 0; for (int i = 0; i < count; i++) { if (accArray[i] instanceof SavingAccount) { SavingAccount savingAccount = (SavingAccount) accArray[i]; if (savingAccount.getAccountBalance() > tem) { tem = savingAccount.getAccountBalance(); } } } return tem; } }
最后測(cè)試類(lèi)
package test; import org.junit.Test; import com.atm.entity.Account;import com.atm.entity.Bank;import com.atm.entity.CreditAccount; public class TestAccount { @Test public void testRegist() { Bank bank = new Bank(); for (int i = 0; i < 1000; i++) { // 0: 儲(chǔ)蓄賬戶(hù) 1 : 信用賬戶(hù) Account acc = bank.registAccount('tom' + i, 'abc123', '2729382932', 183923302L, 'tom' + i + '@163.com', i % 2, (i % 2 == 0) ? 0 : 3000); if (i % 2 != 0) { CreditAccount creditAcc = (CreditAccount) acc; System.out.println('所有信用賬戶(hù)的名字:' + creditAcc.getAccountName() + '和透支額度:' + creditAcc.getMaxOverdraw()); } } // 1000個(gè)銀行賬戶(hù)開(kāi)戶(hù),500是信用賬戶(hù),最高透支額度隨機(jī)數(shù)賦值,再測(cè)試 // double sum = bank.統(tǒng)計(jì)所有信用賬戶(hù)的最高透支額度的總和 (); double sum = bank.statisticsCreditAccountMaxoverdrawSum(); System.out.println('所有信用賬戶(hù)的最高透支額度的總和 :' + sum); double sum1 = bank.aggregateAamount(); System.out.println('總余額為' + sum1); } }
測(cè)試類(lèi)的內(nèi)容不多寫(xiě),大家有興趣可以自行測(cè)試。這樣,我們就完成了一個(gè)比較簡(jiǎn)單的ATM項(xiàng)目。希望對(duì)新學(xué)者有所幫助。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. moment轉(zhuǎn)化時(shí)間戳出現(xiàn)Invalid Date的問(wèn)題及解決2. python爬蟲(chóng)實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊3. Ajax返回值類(lèi)型與用法實(shí)例分析4. 如何在jsp界面中插入圖片5. .NET6打包部署到Windows Service的全過(guò)程6. UDDI FAQs7. 解決ajax請(qǐng)求后臺(tái),有時(shí)收不到返回值的問(wèn)題8. 開(kāi)發(fā)效率翻倍的Web API使用技巧9. asp批量添加修改刪除操作示例代碼10. HTML 絕對(duì)路徑與相對(duì)路徑概念詳細(xì)
