溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用java實現(xiàn)一個簡單的ATM項目

發(fā)布時間:2020-10-28 14:34:37 來源:億速云 閱讀:243 作者:Leah 欄目:開發(fā)技術(shù)

使用java實現(xiàn)一個簡單的ATM項目?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

首先要了解的是,這個ATM項目本身是一個輕量級的項目,只為了完成一些ATM具備的一些方法,并非是真正完成一個ATM的全部功能和需求

那么在這個輕量級的ATM項目中,我將完成添加儲蓄賬號,添加信用賬戶,提款,取款等基本功能。

適合新手查看,需要掌握java的繼承,多態(tài),封裝等基本技術(shù)能力

那么,首先創(chuàng)建如下的對象類:Account(賬戶類),Bank(銀行類),CreditAccount(信用賬戶),SavingAccount(儲蓄賬戶類);

大家首先應(yīng)該搞清楚,這些類文件中之間的關(guān)系,每個類之間需要用到什么樣的方法;

那么我們先填寫Account類

package com.atm.entity;
 
/**
 * 銀行賬戶類
 */
public abstract class Account {
 /**
 * 賬戶帳號
 */
 private String accountId;
 /**
 * 賬戶姓名
 */
 private String accountName;
 /**
 * 賬戶密碼
 */
 private String accountPwd;
 /**
 * 賬戶余額
 */
 private double accountBalance;
 /**
 * 賬戶身份證號
 */
 private String accountPersonId;
 /**
 * 賬戶郵箱
 */
 private String accountEmail;
 /**
 * 賬戶聯(lián)系電話
 */
 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 返回賬戶余額
 */
 public double depoist(double money) {// money 形式參數(shù)
 if (money > 0)
 this.accountBalance += money;
 return this.accountBalance;
 }
 
 /**
 * 取款
 * 
 * @param money
 *   取款金額
 * @return 返回賬戶余額
 */
 public abstract double withdraw(double money);
 
 /**
 * 轉(zhuǎn)賬
 * 
 * @param anotherAccount
 *   轉(zhuǎn)賬的對方賬戶
 * @param money
 *   轉(zhuǎn)賬金額
 * @return 返回當(dāng)前賬戶的余額
 */
 public double tranferAccount(Account anotherAccount, double money) {// 形參
 anotherAccount.accountBalance += money;
 this.accountBalance -= money;
 
 return this.accountBalance;
 }
 
}

之后填寫信用賬戶類CreditAccount;我們應(yīng)該明白,他是繼承Account類的,但是,他又需要擁有自身獨立的屬性,我們可以添加一個最高透支額度的屬性
這樣來實現(xiàn)代碼

/**
 * 信用賬戶
 * 
 *
 */
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;
 }
 
 
 
}

同理  填寫儲蓄賬戶(SavingAccount)類文件

package com.atm.entity;
/**
 * 儲蓄賬戶
 * 
 *
 */
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("賬戶余額不足");
 return getAccountBalance();
 
 }
 
 
}

最重要的是填寫B(tài)ank類的內(nèi)容,在這個類中,我們要完成注冊,產(chǎn)生銀行賬戶,統(tǒng)計所有信用賬戶的最高透支額度的總和,統(tǒng)計所有賬戶的總余額, 查詢出所有信用賬戶中透支額度最高的賬戶, 查詢出所有儲蓄賬戶中余額最高的賬戶等功能

我們這樣填寫      

package com.atm.entity;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
 * 銀行類
 * 
 * @author qianghj
 * 
 *   銀行開戶 ----> 銀行賬戶 Account account = bank.開戶(...)
 *
 */
public class Bank {
 public Account[] accArray = new Account[2000];
 
 public int count = 0;// 表示銀行賬戶的個數(shù)
 
 /**
 * 銀行賬戶開戶
 * 
 * @param accName
 *   用戶名稱
 * @param accPwd
 *   用戶密碼
 * @param accPersonId
 *   用戶身份證
 * @param accTelno
 *   用戶手機號碼
 * @param accEmail
 *   用戶郵箱
 * @param accountType
 *   賬戶類型 0: 儲蓄賬戶 1 : 信用賬戶
 * @param maxOverdraw
 *   信用賬戶的最高透支額度
 * @return 返回有效的銀行賬戶
 */
 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)生銀行賬戶帳號
 * 
 * @return 返回下一個賬戶的帳號 1,2,3,,4
 */
 public String generateNextAccountId() {
 
 return "62223421" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
 
 }
 
 // 統(tǒng)計所有信用賬戶的最高透支額度的總和 (1050 ) 2000 , 1050
 public double statisticsCreditAccountMaxoverdrawSum() {
 double sum = 0;
 for (int i = 0; i < count; i++) {
 // 判斷賬戶 是不是 CreditAccount類型
 if (accArray[i] instanceof CreditAccount) {
 CreditAccount creditAcc = (CreditAccount) accArray[i];
 sum += creditAcc.getMaxOverdraw();
 }
 }
 
 return sum;
 }
 
 // 統(tǒng)計所有賬戶的總余額
 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;
 
 }
 
 // 查詢出所有信用賬戶中透支額度最高的賬戶
 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;
 
 }
 
 // 查詢出所有儲蓄賬戶中余額最高的賬戶
 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;
 
 }
 
}

最后測試類

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: 儲蓄賬戶 1 : 信用賬戶
 Account acc = bank.registAccount("tom" + i, "abc123", "2729382932", 183923302L, "tom" + i + "@163.com",
  i % 2, (i % 2 == 0) &#63; 0 : 3000);
 if (i % 2 != 0) {
 CreditAccount creditAcc = (CreditAccount) acc;
 System.out.println("所有信用賬戶的名字:" + creditAcc.getAccountName() + "和透支額度:" + creditAcc.getMaxOverdraw());
 }
 
 }
 
 // 1000個銀行賬戶開戶,500是信用賬戶,最高透支額度隨機數(shù)賦值,再測試
 // double sum = bank.統(tǒng)計所有信用賬戶的最高透支額度的總和 ();
 double sum = bank.statisticsCreditAccountMaxoverdrawSum();
 System.out.println("所有信用賬戶的最高透支額度的總和 :" + sum);
 double sum1 = bank.aggregateAamount();
 System.out.println("總余額為" + sum1);
 }
 
}

測試類的內(nèi)容不多寫,大家有興趣可以自行測試。這樣,我們就完成了一個比較簡單的ATM項目。希望對新學(xué)者有所幫助。

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI