基于JAVA的ATM模擬系統(tǒng)_第1頁
基于JAVA的ATM模擬系統(tǒng)_第2頁
基于JAVA的ATM模擬系統(tǒng)_第3頁
基于JAVA的ATM模擬系統(tǒng)_第4頁
基于JAVA的ATM模擬系統(tǒng)_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、使用JAVA設(shè)計(jì)一個(gè)ATM程序,實(shí)現(xiàn)自動(dòng)取款機(jī)存款、取款、查詢等功能。程序:(1) 【Account】package atm ;/* * 賬戶類:包含兩種賬戶類型->1.儲(chǔ)蓄賬戶 2.信用賬戶 */public abstract class Account /屬性protected long id;protected String password;protected String name;protected String IDcardid;protected int accountType;protected double balance;/構(gòu)造方法public Account()s

2、uper();public Account(long id, String password, String name, String IDcardid,int accoutType,double balance) super();this.id = id;this.password = password; = name;this.IDcardid = IDcardid;this.accountType = accountType;this.balance = balance;/getXxx,setXxx方法public long getId() return id;publ

3、ic 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) = name;public String getIDcardid() return IDcardid;public void setIDcardid(

4、String IDcardid) this.IDcardid = IDcardid;public int getAccountType() return accountType;public void setAccountType(int accountType) this.accountType = accountType;public double getBalance() return balance;public void setBalance(double balance) this.balance = balance;/* * 存款 */public void deposit(do

5、uble money)balance += money;/* * 取款(取款方式由賬戶類型決定,所以設(shè)為抽象方法,相應(yīng)的Account類應(yīng)設(shè)為抽象類) */public abstract void fetchMoney(double money);(2) 【SavingAccount】package atm;/* * 儲(chǔ)蓄賬戶類 */public class SavingAccount extends Account /構(gòu)造函數(shù)public SavingAccount() super();public SavingAccount(long id, String password, String

6、 name, String IDcardid,int accountType, double balance) super(id, password, name, IDcardid, accountType, balance);/對(duì)父類的fetchMoney()實(shí)現(xiàn)public void fetchMoney(double money)if(balance < money)System.out.println("對(duì)不起,賬戶余額不足!");elsebalance -= money;(3) 【CreditAccount】package atm;/* * 信用賬戶類,增加

7、一個(gè)信用額度ceiling屬性 */public class CreditAccount extends Accountprivate int ceiling;/構(gòu)造函數(shù)public CreditAccount()super();public CreditAccount(long id, String password, String name,String IDcardid,int accountType, double balance, int ceiling)super(id, password, name, IDcardid, accountType, balance);this.ce

8、iling = ceiling;/getXxx,setXxx方法public int getCeiling() return ceiling;public void setCeiling(int ceiling) this.ceiling = ceiling;/實(shí)現(xiàn)父類的fetchMoney()public void fetchMoney(double money)if(balance + ceiling) < money)System.out.println("對(duì)不起,已超出您的信用額度!");elsebalance -= money;(4)【Bank】packag

9、e atm;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Properties;public class Bank private Account accounts = new Account20;private int

10、 number;/賬戶數(shù)目private int id = 1001;/確定銀行賬號(hào)從1001開始生成,即第一個(gè)賬戶的賬號(hào)是1001/構(gòu)造函數(shù)public Bank() accounts=new Account20;/以后不足時(shí)擴(kuò)容。number = 0;BufferedReader bufReader = null;Properties props=System.getProperties();String path=props.getProperty("user.dir");try bufReader=new BufferedReader(new FileReader(

11、new File(path,"account.txt");String s = bufReader.readLine();while(s != null)String str = s.split(",");if(str4.equals("0")Account savingAcc = new SavingAccount(Long.parseLong(str0),str1.toString(), str2.toString(),str3.toString(),Integer.parseInt(str4),Double.parseDoubl

12、e(str5);accountsnumber = savingAcc;elseAccount creditAcc = new CreditAccount(Long.parseLong(str0),str1.toString(), str2.toString(),str3.toString(),Integer.parseInt(str4),Double.parseDouble(str5),5000);accountsnumber = creditAcc;number +;id+;s = bufReader.readLine(); catch (NumberFormatException e) e

13、.printStackTrace(); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();finallytry if(bufReader != null)bufReader.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();/getXxx,setXxxpublic Account getAccounts() return accounts;

14、public void setAccounts(Account accounts) this.accounts = accounts;public int getNumber() return number;public void setNumber(int number) this.number = number;public int getId() return id;public void setId(int id) this.id = id;/* * 開戶 */public Account openAccount(String passwd1, String passwd2, Stri

15、ng name, String IDcardid, int type)/創(chuàng)建一個(gè)新賬戶Account account = null;/判斷兩次密碼是否一致if(passwd1.equals(passwd2)/若一致,再判斷賬戶類型(根據(jù)type的值)if(type = 1)/可令開始余額為10,信用額度為5000account = new CreditAccount(id, passwd1, name, IDcardid, type, 10, 5000);elseaccount = new SavingAccount(id, passwd1, name, IDcardid, type, 10)

16、;/將賬戶存入賬戶數(shù)組accounts中/判斷是否超出存儲(chǔ)空間if(number >= accounts.length)/擴(kuò)容Account newAccounts = new Accountaccounts.length*2;/copy原來的相關(guān)數(shù)據(jù)System.arraycopy(accounts, 0, newAccounts, 0, accounts.length);/將newAccounts賦給accountsaccounts = newAccounts;accountsnumber = account;elseaccountsnumber = account;System.o

17、ut.println("開戶成功!賬戶信息見下");System.out.println("您的卡號(hào)為:"+id+"n"+"您的密碼為:"+passwd1+"n"+"您的戶名為:"+name+"n"+"您的身份證號(hào)為:"+IDcardid+"n"+"您的賬戶類型為:"+type+"n");account.accountType = type;number+;id+;return

18、 account;/此時(shí)開戶成功elseSystem.out.println("對(duì)不起!您兩次密碼輸入不匹配,開戶失??!");return null;/此時(shí)開戶失敗/* * 保存數(shù)據(jù) */public void saveAccountDate()BufferedWriter bufWriter=null;try Properties props=System.getProperties();String path=props.getProperty("user.dir");bufWriter=new BufferedWriter(new FileWrit

19、er(new File(path,"account.txt");for(int i = 0;i < accounts.length;i+)/若存在賬戶if(accountsi != null)/寫入賬戶信息到account.txtbufWriter.write(accountsi.id+",");bufWriter.write(accountsi.getPassword()+",");bufWriter.write(accountsi.getName()+",");bufWriter.write(accoun

20、tsi.getIDcardid()+",");bufWriter.write(accountsi.getAccountType()+",");bufWriter.write(Double.toString(accountsi.getBalance();bufWriter.newLine();elsebreak;bufWriter.flush();/清空緩存中的內(nèi)容 catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();finallytry if(bufWrit

21、er!=null)bufWriter.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();/* * 登錄驗(yàn)證 */public Account verifyAccount(long id, String password)Account account = null;for(int i = 0;i < accounts.length;i+)/若存在賬戶if(accountsi != null)/驗(yàn)證id號(hào)和passwordif(id = accountsi.getId()

22、&& password.equals(accountsi.getPassword()account = accountsi;break;elsebreak;return account;/* * 轉(zhuǎn)賬驗(yàn)證(方法的重載) */public Account verifyAccount(long id)Account account = null;for(int i = 0;i < accounts.length;i+)/若存在賬戶if(accountsi != null)/驗(yàn)證id號(hào)和passwordif(id = accountsi.getId()account = acc

23、ountsi;break;elsebreak;return account;/* * 轉(zhuǎn)賬 */public void transferAccount(Account account1, Account account2, double money)account1.fetchMoney(money);account2.deposit(money);/* * 存款 */public void deposit(Account account, double money)account.deposit(money);/* * 取款 */public void fetchMoney(Account

24、account, double money)account.fetchMoney(money);(5)【ATM】package atm;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.util.Properties;import java.util.Scanner;/* * ATM類,提供用戶界面操作 */public class ATM /屬性privat

25、e Bank bank;/構(gòu)造函數(shù)public ATM() bank = new Bank();/main方法public static void main(String args)ATM atm = new ATM(); /實(shí)例化ATMBank bank = atm.bank;/標(biāo)號(hào),判斷是否退出一級(jí)菜單boolean firstFlag = true;while(firstFlag)/一級(jí)菜單System.out.println("*歡迎使用XXX銀行模擬ATM系統(tǒng),請(qǐng)按如下步驟操作*");System.out.println(" *1.用已有賬戶登錄&quo

26、t;);System.out.println(" *2.沒有賬戶,開戶");System.out.println(" *3.退出");Scanner scanner = new Scanner(System.in);System.out.print("請(qǐng)選擇:");try int choice1 = scanner.nextInt();switch(choice1)case 1:scanner = new Scanner(System.in);System.out.print("請(qǐng)輸入銀行卡號(hào):");long id

27、 = scanner.nextInt();scanner = new Scanner(System.in);System.out.print("請(qǐng)輸入銀行密碼:");String password = scanner.next();Account account = bank.verifyAccount(id, password);if(account != null)/標(biāo)號(hào),判斷是否退出二級(jí)菜單boolean secondFlag = true;while(secondFlag)/二級(jí)菜單System.out.println("*歡迎使用XXX銀行模擬ATM系統(tǒng)

28、,請(qǐng)按如下步驟操作*");System.out.println(" *1.查詢賬戶余額");System.out.println(" *2.存款");System.out.println(" *3.取款");System.out.println(" *4.轉(zhuǎn)賬");System.out.println(" *5.退卡");scanner = new Scanner(System.in);System.out.print("請(qǐng)選擇:");try int choice2

29、 = scanner.nextInt();switch(choice2)case 1:System.out.println("您賬戶的當(dāng)前余額為:"+account.getBalance();break;case 2:scanner = new Scanner(System.in);System.out.print("請(qǐng)輸入您的存款金額:");double money1 = scanner.nextDouble();bank.deposit(account, money1);break;case 3:scanner = new Scanner(Syste

30、m.in);System.out.print("請(qǐng)輸入您的取款金額:");double money2 = scanner.nextDouble();bank.fetchMoney(account, money2);break;case 4:scanner = new Scanner(System.in);System.out.print("請(qǐng)輸入您要轉(zhuǎn)入賬戶的卡號(hào):");long id2 = scanner.nextLong();Account account2 = bank.verifyAccount(id2);if(account2 != null)

31、scanner = new Scanner(System.in);System.out.print("請(qǐng)輸入您要轉(zhuǎn)入賬戶的金額:");double money = scanner.nextLong();if(money <= account.balance)bank.transferAccount(account, account2, money);System.out.println("轉(zhuǎn)賬成功!");elseSystem.out.println("抱歉,您賬戶沒有足夠的金額!請(qǐng)查看后重新選擇輸入!");elseSystem.out.println("抱歉,沒有找到您要轉(zhuǎn)入的賬戶信息!請(qǐng)核對(duì)后重新選擇輸入!");break;case 5:seco

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論