版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、使用JAVA設計一個ATM程序,實現(xiàn)自動取款機存款、取款、查詢等功能。程序:(1) 【Account】package atm ;/* * 賬戶類:包含兩種賬戶類型->1.儲蓄賬戶 2.信用賬戶 */public abstract class Account /屬性protected long id;protected String password;protected String name;protected String IDcardid;protected int accountType;protected double balance;/構造方法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;/* * 取款(取款方式由賬戶類型決定,所以設為抽象方法,相應的Account類應設為抽象類) */public abstract void fetchMoney(double money);(2) 【SavingAccount】package atm;/* * 儲蓄賬戶類 */public class SavingAccount extends Account /構造函數(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);/對父類的fetchMoney()實現(xiàn)public void fetchMoney(double money)if(balance < money)System.out.println("對不起,賬戶余額不足!");elsebalance -= money;(3) 【CreditAccount】package atm;/* * 信用賬戶類,增加
7、一個信用額度ceiling屬性 */public class CreditAccount extends Accountprivate int ceiling;/構造函數(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;/實現(xiàn)父類的fetchMoney()public void fetchMoney(double money)if(balance + ceiling) < money)System.out.println("對不起,已超出您的信用額度!");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;/確定銀行賬號從1001開始生成,即第一個賬戶的賬號是1001/構造函數(shù)public Bank() accounts=new Account20;/以后不足時擴容。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)建一個新賬戶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中/判斷是否超出存儲空間if(number >= accounts.length)/擴容Account newAccounts = new Accountaccounts.length*2;/copy原來的相關數(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("您的卡號為:"+id+"n"+"您的密碼為:"+passwd1+"n"+"您的戶名為:"+name+"n"+"您的身份證號為:"+IDcardid+"n"+"您的賬戶類型為:"+type+"n");account.accountType = type;number+;id+;return
18、 account;/此時開戶成功elseSystem.out.println("對不起!您兩次密碼輸入不匹配,開戶失?。?quot;);return null;/此時開戶失敗/* * 保存數(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();/清空緩存中的內容 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();/* * 登錄驗證 */public Account verifyAccount(long id, String password)Account account = null;for(int i = 0;i < accounts.length;i+)/若存在賬戶if(accountsi != null)/驗證id號和passwordif(id = accountsi.getId()
22、&& password.equals(accountsi.getPassword()account = accountsi;break;elsebreak;return account;/* * 轉賬驗證(方法的重載) */public Account verifyAccount(long id)Account account = null;for(int i = 0;i < accounts.length;i+)/若存在賬戶if(accountsi != null)/驗證id號和passwordif(id = accountsi.getId()account = acc
23、ountsi;break;elsebreak;return account;/* * 轉賬 */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;/構造函數(shù)public ATM() bank = new Bank();/main方法public static void main(String args)ATM atm = new ATM(); /實例化ATMBank bank = atm.bank;/標號,判斷是否退出一級菜單boolean firstFlag = true;while(firstFlag)/一級菜單System.out.println("*歡迎使用XXX銀行模擬ATM系統(tǒ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("請選擇:");try int choice1 = scanner.nextInt();switch(choice1)case 1:scanner = new Scanner(System.in);System.out.print("請輸入銀行卡號:");long id
27、 = scanner.nextInt();scanner = new Scanner(System.in);System.out.print("請輸入銀行密碼:");String password = scanner.next();Account account = bank.verifyAccount(id, password);if(account != null)/標號,判斷是否退出二級菜單boolean secondFlag = true;while(secondFlag)/二級菜單System.out.println("*歡迎使用XXX銀行模擬ATM系統(tǒng)
28、,請按如下步驟操作*");System.out.println(" *1.查詢賬戶余額");System.out.println(" *2.存款");System.out.println(" *3.取款");System.out.println(" *4.轉賬");System.out.println(" *5.退卡");scanner = new Scanner(System.in);System.out.print("請選擇:");try int choice2
29、 = scanner.nextInt();switch(choice2)case 1:System.out.println("您賬戶的當前余額為:"+account.getBalance();break;case 2:scanner = new Scanner(System.in);System.out.print("請輸入您的存款金額:");double money1 = scanner.nextDouble();bank.deposit(account, money1);break;case 3:scanner = new Scanner(Syste
30、m.in);System.out.print("請輸入您的取款金額:");double money2 = scanner.nextDouble();bank.fetchMoney(account, money2);break;case 4:scanner = new Scanner(System.in);System.out.print("請輸入您要轉入賬戶的卡號:");long id2 = scanner.nextLong();Account account2 = bank.verifyAccount(id2);if(account2 != null)
31、scanner = new Scanner(System.in);System.out.print("請輸入您要轉入賬戶的金額:");double money = scanner.nextLong();if(money <= account.balance)bank.transferAccount(account, account2, money);System.out.println("轉賬成功!");elseSystem.out.println("抱歉,您賬戶沒有足夠的金額!請查看后重新選擇輸入!");elseSystem.out.println("抱歉,沒有找到您要轉入的賬戶信息!請核對后重新選擇輸入!");break;case 5:seco
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 【正版授權】 ISO 5273:2025 EN Passenger car tyres - Preparation method for an artificially worn state for wet grip testing
- 二零二五版昆明公租房電子合同租賃合同爭議解決途徑與流程2篇
- 二零二五版教育培訓項目合同范本共二十項條款3篇
- 2025版工業(yè)園區(qū)害蟲防治與安全防護服務協(xié)議3篇
- 2025版信用社小微企業(yè)貸款業(yè)務合作協(xié)議3篇
- 酒店管理公司2025年度戰(zhàn)略合作協(xié)議2篇
- 2025版臨時工技能培訓免責合同4篇
- 2025年度建筑裝修工程合同標的質量驗收:1、客戶居住環(huán)境4篇
- 2025水面承包權經(jīng)營與管理合同3篇
- 上海市房屋預售合同6篇
- 物業(yè)民法典知識培訓課件
- 2023年初中畢業(yè)生信息技術中考知識點詳解
- 2024-2025學年八年級數(shù)學人教版上冊寒假作業(yè)(綜合復習能力提升篇)(含答案)
- 《萬方數(shù)據(jù)資源介紹》課件
- 第一章-地震工程學概論
- 《中國糖尿病防治指南(2024版)》更新要點解讀
- 浙江省金華市金東區(qū)2022-2024年中考二模英語試題匯編:任務型閱讀
- 青島版(五四制)四年級數(shù)學下冊全冊課件
- 大健康行業(yè)研究課件
- 租賃汽車可行性報告
- 計算機輔助設計AutoCAD繪圖-課程教案
評論
0/150
提交評論