已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Account.javapackage com.buaa.account;public abstract class Account private int id;private String userName;private double balance;public static int CurrentID = 100;/开户时调用的构造方法public Account(String userName, double balance) this.userName = userName;this.balance = balance;id = CurrentID+;/读取文件加载数据时用的构造方法public Account(int id, String userName, double balance) this.id = id;this.userName = userName;this.balance = balance;public static int getCurrentID() return CurrentID;public static void setCurrentID(int currentID) CurrentID += currentID;public int getId() return id;public void setId(int id) this.id = id;public String getUserName() return userName;public void setUserName(String userName) this.userName = userName;public double getBalance() return balance;public void setBalance(double balance) this.balance = balance;/ 存款public void deposit(double money) balance += money;/ 取款public void withdraw(double money) throws BalanceNotEnoughException if (balance money) throw new BalanceNotEnoughException(余额不足!); else balance -= money;Overridepublic String toString() / TODO Auto-generated method stubreturn id + t + userName + t + balance;public abstract double interest();BalanceNotEnoughException.javapackage com.buaa.account;public class BalanceNotEnoughException extends Exception public BalanceNotEnoughException(String message) super(message);Bank.javapackage com.buaa.account;import java.util.Collection;import java.util.LinkedHashMap;import java.util.Map;public class Bank /所有的用户信息都存放在此map中private Map map = new LinkedHashMap();/开户,向map中加入一个Account对象public void insertAccount(Account account) map.put(account.getId(), account);/以数组的形式返回map中的所有数据public Account getAllAccount() Collection c = map.values();Account a = c.toArray(new Account0);return a;public Account getAccount(int id) return map.get(id);Client.javapackage com.buaa.account;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Client /* * param args */public static void main(String args) / TODO Auto-generated method stubint select = 0 ;String fileName = Account.dat;Bank bank = new Bank();DataAccount.dataLoad(fileName, bank);Account acc = null;BufferedReader br = new BufferedReader(new InputStreamReader(System.in);do try System.out.println(请选择: 1-管理员 2-用户 0-退出);select = Integer.parseInt(br.readLine();if(select=1) System.out.println(管理员模块菜单);System.out.println(1. 开户);System.out.println(2. 查询所有账户);System.out.println(0. 退出);System.out.print(请输入菜单号:);select = Integer.parseInt(br.readLine();switch(select) case 1: System.out.print(请输入开户户名:);String userName = br.readLine();System.out.print(请输入开启金额:);double balance = Double.parseDouble(br.readLine();System.out.print(请选择利息类型(1.固定利率 2.浮动利率));int type = Integer.parseInt(br.readLine();if(type=1) acc = new FixedAccount(userName, balance);else if(type=2) acc = new FloatAccount(userName, balance);bank.insertAccount(acc);DataAccount.savedData(fileName, bank);System.out.println(开户成功!);break;case 2:System.out.println(账户idt账户名t账户余额);Account array = bank.getAllAccount();for(Account a : array) System.out.println(a.getId() + t + a.getUserName() + t + a.getBalance();break;case 0:default:break;else if(select=2) System.out.println(用户模块菜单);System.out.println(1. 存款);System.out.println(2. 取款);System.out.println(3. 查看本账户利息);System.out.println(0. 退出);System.out.print(请先输入一个账户ID:);int id = Integer.parseInt(br.readLine();acc = bank.getAccount(id);if(acc=null) System.out.println(该账户不存在!);else System.out.println(acc);System.out.print(请输入菜单号:);select = Integer.parseInt(br.readLine();double money = 0;switch(select) case 1:System.out.print(请输入存钱的数目:);money = Double.parseDouble(br.readLine();acc.deposit(money);DataAccount.savedData(fileName, bank);System.out.println(acc.getUserName() + 成功地存入$ + money);System.out.println(acc);break;case 2:System.out.print(请输入取钱的数目:);money = Double.parseDouble(br.readLine();acc.withdraw(money);DataAccount.savedData(fileName, bank);System.out.println(acc.getUserName() + 成功地取出$ + money);System.out.println(acc);break;case 3:System.out.println(acc.getUserName() + 的利息是 + erest();break;case 0:default:break; catch (NumberFormatException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (BalanceNotEnoughException e) / TODO Auto-generated catch blocke.printStackTrace();System.out.println(-);while(select!=0);try br.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();DataAccount.javapackage com.buaa.account;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class DataAccount public static void dataLoad(String fileName, Bank bank) try BufferedReader br = new BufferedReader(new FileReader(fileName);String s;int count = 0;while (s = br.readLine() != null) String str = s.split(,);Account acc = null;if (str3.equals(1) acc = new FixedAccount(Integer.parseInt(str0), str1,Double.parseDouble(str2); else if (str3.equals(2) acc = new FloatAccount(Integer.parseInt(str0), str1,Double.parseDouble(str2);bank.insertAccount(acc);count+;br.close();Account.setCurrentID(count); catch (FileNotFoundException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();public static void savedData(String fileName, Bank bank) try Account acc = bank.getAllAccount();BufferedWriter bw = new BufferedWriter(new FileWriter(fileName);String s;for (int i = 0; i acc.length; i+) s = acci.getId() + , + acci.getUserName() + ,+ acci.getBalance();if (acci instanceof FixedAccount) s += , + 1; else if (acci instanceof FloatAccount) s += , + 2;bw.write(s);bw.newLine();bw.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();FixedAccount.javapackage com.buaa.account;public class FixedAccount extends Account public FixedAccount(String userName, double balance) super(userN
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 农业机械应用技术专业评估
- 贷款担保合作协议范本
- 2026年湖南生物机电职业技术学院单招职业技能测试必刷测试卷附答案
- 2026年三门峡职业技术学院单招职业倾向性测试必刷测试卷及答案1套
- 2026年云南省临沧地区单招职业适应性考试必刷测试卷及答案1套
- 2026年云南商务职业学院单招综合素质考试必刷测试卷及答案1套
- 2026年开封职业学院单招综合素质考试题库新版
- 2026年青海省海东地区单招职业适应性考试题库新版
- 2026年湖南电气职业技术学院单招职业倾向性测试题库必考题
- 2026年白城职业技术学院单招综合素质考试必刷测试卷及答案1套
- 围墙粉刷施工方案(3篇)
- 2025山东泰山财产保险股份有限公司总公司及分支机构校园招聘、社会招聘笔试备考试题及答案解析
- 数控技术专业介绍
- 2025至2030中国黑龙江省养老机构行业产业运行态势及投资规划深度研究报告
- “华能工匠杯”电力市场交易技能竞赛考试题库(附答案)
- 吸引力法则培训课件
- 做课件教学的步骤
- 2025年饮料gmp试题及答案
- 低碳景观设计策略-洞察及研究
- 局工作秘密管理暂行办法
- 《“1+X”无人机摄影测量》课件-项目三 像控点采集
评论
0/150
提交评论