(精品)银行管理系统JAVA.doc

上传人:可**** 文档编号:69706273 上传时间:2023-01-07 格式:DOC 页数:17 大小:174.51KB
返回 下载 相关 举报
(精品)银行管理系统JAVA.doc_第1页
第1页 / 共17页
(精品)银行管理系统JAVA.doc_第2页
第2页 / 共17页
点击查看更多>>
资源描述

《(精品)银行管理系统JAVA.doc》由会员分享,可在线阅读,更多相关《(精品)银行管理系统JAVA.doc(17页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、1) Account.java类package com.etp;import java.util.*;public abstract class Account protected long id;protected String password;protected String name;protected String personId;protected String email;protected double balance;Overridepublic boolean equals(Object obj) boolean rt=false;if(this=obj)rt=true;

2、if(obj instanceof Account)Account anotherAcc=(Account)obj;if(this.id=anotherAcc.id)rt=true;return rt;Overridepublic int hashCode() return password.hashCode()+name.hashCode()+personId.hashCode()+email.hashCode();Overridepublic String toString() return id+;+password+;+name+;+personId+;+email+;+balance

3、;/* 无参构造体 */public Account() /* 有参构造体,用于初始化对象的必要属性 */public Account(long id, String password, String name, String personId,String email, double balance)this.setId(id);this.setPassword(password);this.setName(name);this.setPersonId(personId);this.setEmail(email);this.setBalance(balance);/* 存款方法 */publ

4、ic void deposit(double money) balance+=money;/* 取款方法,抽象类,行为不确定,有储蓄账户和信用账户之分 * throws BalanceNotEnoughException */public abstract void withdraw(double money) throws BalanceNotEnoughException;/* 属性的封装 */public void setId(long id) this.id = id;public long getId() return id;public void setPassword(Strin

5、g password) this.password = password;public String getPassword() return password;public void setName(String name) this.name = name;public String getName() return name;public void setPersonId(String personId) this.personId = personId;public String getPersonId() return personId;public void setEmail(St

6、ring email) this.email = email;public String getEmail() return email;public void setBalance(double balance) this.balance = balance;public double getBalance() return balance;2) ATM.java类package com.etp;import java.util.*;import java.io.*;public class ATM private Bank bank;public ATM() /* 初始化Bank类,此时调

7、用Bank的构造函数,其中默认的两个测试用户的信息被添加到Account类中 */bank = new Bank();public static void main(String args) ATM atm = new ATM();atm.mainMenu();private void mainMenu() boolean outflag = true;do System.out.println( ATM服务系统);System.out.println(*);System.out.println( 1. 登 录);System.out.println( 2. 开 户);System.out.p

8、rintln( 0. 退 出);System.out.println(*);System.out.print(请选择操作:);Scanner scanner = new Scanner(System.in);String firstChoice;do firstChoice = scanner.next();if (firstChoice.matches(0-2$) break; elseSystem.out.println(选择输入错误!); while (true);boolean rt = true;out: do switch (Integer.parseInt(firstChoice

9、) case 1:/* 进入登录界面或登录成功界面 */secondMenu();break out;case 2:/* 开户 ,捕捉createAccount方法抛出的所有异常 */try bank.createAccount(); catch (RegisterException e) System.out.println(e.toString();rt = false;break;case 0:rt = false;outflag = false;break;default:break; while (rt); while (outflag);System.out.println(谢谢您

10、的使用!);/* 登录成功后的二级菜单 */private void secondMenu() Scanner scanner = new Scanner(System.in);Account account = null;out: dotry /* 输入卡号,输入exit可以直接退出系统 */System.out.print(输入卡号:);String cardNo0;long cardNo;out1: do cardNo0 = scanner.next();if (cardNo0.equals(exit) | cardNo0.equals(EXIT) break out; else if

11、(cardNo0.matches(0-9_+$) cardNo = Long.parseLong(cardNo0);break out1; elseSystem.out.println(请输入数字类型的ID!); while (true);/* 输入密码,输入exit可以直接退出系统 */System.out.print(密码:);String password = scanner.next();if (password.equals(exit) | password.equals(EXIT) break out;account = bank.login(cardNo, password);b

12、reak out; catch (LoginException e) System.out.println(e.toString();while (true);if (account != null) out: do System.out.println( ATM操作界面);System.out.println(*);System.out.println( 1. 存款操作);System.out.println( 2. 取款操作);System.out.println( 3. 查询余额);System.out.println( 4. 设置透支额度);System.out.println( 5.

13、 统计银行总余额);System.out.println( 6. 查询本人信息);System.out.println( 0. 退 出);System.out.println(*);System.out.print(请选择操作:);scanner = new Scanner(System.in);String sc;do sc = scanner.next();if (sc.matches(0-6+$) break; elseSystem.out.println(选择操作输入错误!); while (true);switch (Integer.parseInt(sc) case 1:/* 存款

14、 */* 最外层的循环用来询问用户是否继续。 */do /* 金额必须为数字类型,不能存取低于一元金额 */do System.out.println(请输入存款金额:);String money = scanner.next();if (money.matches(0-9_+$) bank.deposit(account, Double.parseDouble(money);break; elseSystem.out.println(金额必须为数字!); while (true);System.out.println(存款结束!继续存款(Y)/结束存款(输入任意字符);Scanner s =

15、 new Scanner(System.in);String choice = s.next();if (choice.equals(Y) | choice.equals(y)continue;elsebreak; while (true);break;case 2:/* 取款 */* 最外层的循环用来询问用户是否继续。 */do /* 金额必须为数字类型,不能存取低于一元金额 */do System.out.println(请输入取款金额:);String money = scanner.next();if (money.matches(0-9_+$) try bank.withdraw(a

16、ccount, Double.parseDouble(money); catch (BalanceNotEnoughException e) / TODO Auto-generated catch blocke.printStackTrace();break; elseSystem.out.println(金额必须是数字类型!); while (true);System.out.println(取款结束!继续取款(Y)/结束取款(输入任意字符);Scanner s = new Scanner(System.in);String choice = s.next();if (choice.equa

17、ls(Y) | choice.equals(y)continue;elsebreak; while (true);break;case 3:/* 查看余额 */bank.showBalance(account);break;case 4:/* 设置透支额度 */bank.setCeiling(account);break;case 5:/* 统计银行总余额,调用Bank类中的totalBalance方法 */bank.totalBalance(account);break;case 6:/* Account类中覆盖的toString方法使用 */System.out.println(accou

18、nt.toString();break;case 0:/* 退出的时候调用写入的方法,把更新的用户信息覆盖掉原来Accounts,txt中旧的数据 */bank.writeIntoTxt();break out;default:System.out.println(选择操作输入错误!);break; while (true);3) Bank.java类package com.etp;import java.util.*;import java.io.*;import java.util.*;public class Bank List list;public Bank() list = new

19、 LinkedList();long id;String password, name, personId, email;double balance, ceiling;Properties p = System.getProperties();String workpath = p.getProperty(user.dir);BufferedReader br = null;try File sourceFile = new File(workpath, Accounts.txt);br = new BufferedReader(new FileReader(sourceFile);Stri

20、ng str = br.readLine();String singleInfo = new String10;while (str != null) /* 以分号为分隔符,把字符串分为字段数组存放 */singleInfo = str.split(;);if (singleInfo.length = 6) id = Long.parseLong(singleInfo0);password = singleInfo1;name = singleInfo2;personId = singleInfo3;email = singleInfo4;balance = Double.parseDoubl

21、e(singleInfo5);Account obj = new SavingAccount(id, password, name,personId, email, balance);list.add(obj);/* 第line行的长度是7,说明有ceiling字段,是信用卡账户 */if (singleInfo.length = 7) id = Long.parseLong(singleInfo0);password = singleInfo1;name = singleInfo2;personId = singleInfo3;email = singleInfo4;balance = Do

22、uble.parseDouble(singleInfo5);ceiling = Double.parseDouble(singleInfo6);Account obj = new CreditAccount(id, password, name,personId, email, balance, ceiling);list.add(obj);str = br.readLine(); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); finally try

23、 if (br != null)br.close(); catch (IOException e) e.printStackTrace();/* 登录功能,登录成功返回Account类型,不成功account引用为空 */public Account login(long cardNo, String password) throws LoginException Account account = null;int l = list.size();int i = 0;for (i = 0; i list.size(); i+) if (list.get(i).getId() = cardNo

24、& list.get(i).getPassword().equals(password) /* 如果登录成功,返回account引用指向所在的账户对象,账户信息都在该对象中存放 */account = list.get(i);break;if (i = l)throw new LoginException(卡号或密码错误!);return account;/* * 存款功能,方法在deposit中定义,ATM无论传来CreditAccount还是SavingAccount的对象, * 都只有父类Account实现的方法deposit方法可供调用,此处不存在动态实现多态 */public voi

25、d deposit(Account account, double money) account.deposit(money);/* * 取款,体现出动态多态,如果在ATM中传来account引用指向CreditAccount类的对象则调用其中的withdraw方法, * 指向SavingAccount的对象则调用SavingAccount中实现的方法,不确定性,在运行过程中动态的选择。 */public void withdraw(Account account, double money)throws BalanceNotEnoughException account.withdraw(m

26、oney);/* 显示当前余额 */public void showBalance(Account account) do System.out.println(尊敬的 + account.getName() + !您的当前余额:+ account.getBalance() + RMB);System.out.println(返回上一级菜单(输入任意字符);Scanner s = new Scanner(System.in);s.next();/* 查询成功,光标等待,任意键弹出上一级菜单 */break; while (true);/* 用户开户 */public void createAc

27、count() throws RegisterException out0: do Scanner input = new Scanner(System.in);long id;String id0;do /* 输入ID号时可以输入exit直接退出到菜单 */System.out.print(输入用户ID号:);id0 = input.next();if (id0.equals(exit) | id0.equals(EXIT)break out0;else if (id0.matches(0-9_+$) id = Long.parseLong(id0);if (isAccountExist(i

28、d) = -1)/* 等于-1用户不存在,可以添加 */break;elsethrow new RegisterException(用户账户已经存在!); elseSystem.out.println(请输入数字类型的ID号!); while (true);String pass1, pass2;do System.out.print(输入密码:);pass1 = input.next();System.out.print(请确认密码:);pass2 = input.next();if (pass1.equals(pass2) break; elsethrow new RegisterExce

29、ption(两次密码输入不一样!); while (true);System.out.print(请输入真实姓名:);String name = input.next();System.out.print(请输入身份证号:);String personId;do personId = input.next();/* 身份证号格式为17位数字+一个数字或者一个X结尾 */if (personId.matches(0-917+(0-9|X)+$)break;elseSystem.out.println(身份证格式不正确!); while (true);String email;do System.

30、out.print(请输入邮箱:);email = input.next();/* 匹配输入是否是邮箱格式! */if (email.matches(a-zA-Z0-9_)+(a-zA-Z0-9_)+.(a-zA-Z0-9_)+$) break; elseSystem.out.println(邮箱格式不正确!); while (true);String accountType;do System.out.print(账户类型(1.信用账户 0.储蓄账户):);accountType = input.next();if (accountType.equals(1) /* * 如果账户类型是1,把

31、上述的内容存储在信用卡类 默认余额为0.0RMB 默认透支额度是0.0RMB * 无论添加的是信用卡账户还是储蓄账户,都存在accounts数组中 ,count+ */Account o = new CreditAccount(id, pass2, name, personId,email, 0.0, 0.0);list.add(o);break; else if (accountType.equals(0) /* * 如果账户类型是0,把上述的内容存储在储蓄类中 默认余额为0.0RMB */Account o = new SavingAccount(id, pass2, name, pers

32、onId,email, 0.0);list.add(o);break; else System.out.println(请选择账户类型!); while (true);/* 开户成功后就把开户信息更新到Accounts.txt文件中。 */writeIntoTxt();System.out.println(开户成功!请选择服务!);break out0; while (true);/* 用户开户时判断用户是否已经存在 */public int isAccountExist(long id) int result = -1;for (int i = 0; i list.size(); i+) i

33、f (list.get(i).id = id) result = i;return result;/* 设置透支额度 */public void setCeiling(Account account) do /* 判断设置透支额度的账户是否是信用账户 */if (account instanceof CreditAccount) System.out.println(请输入透支额度:);Scanner scanner = new Scanner(System.in);String ceiling = scanner.next();if (ceiling.matches(0-9_+$) /* a

34、ccount类型不确定,向下转型 */CreditAccount ca = (CreditAccount) account;/* 转型后能调用子类CreditAccount的属性方法设置透支额度 */ca.setCeiling(Double.parseDouble(ceiling);System.out.println(设置成功!);break; elseSystem.out.println(金额必须是数字类型!); else System.out.println(您不是信用账户用户!);break; while (true);System.out.println(返回上一级菜单(输入任意字符

35、);Scanner s = new Scanner(System.in);s.next();/* 设置成功,光标等待,任意键弹出上一级菜单 */* 统计账户余额总数,能查询银行总额的必须为银行初始设定的用户才能访问 */public void totalBalance(Account account) if (account.getId() = 1111 | account.getId() = 2222) double result = 0.0;/* 获得Account类型数组中的所有对象的存款余额属性 */for (int i = 0; i list.size(); i+) result +

36、= list.get(i).getBalance();System.out.println(银行存款总余额: + result + RMB); elseSystem.out.println(对不起,您权限不足!);System.out.println(返回上一级菜单(输入任意字符);Scanner s = new Scanner(System.in);s.next();/* 设置成功,光标等待,任意键弹出上一级菜单 */* 写进Accounts.txt文本文件用新的值覆盖 */public void writeIntoTxt() Properties p = System.getPropert

37、ies();String workpath = p.getProperty(user.dir);BufferedWriter bw = null;try File file = new File(workpath, Accounts.txt);bw = new BufferedWriter(new FileWriter(file);for (int i = 0; i list.size(); i+) /* Account覆盖方法toString() */String str = list.get(i).toString();bw.write(str);/* 新建一行 */if (i != list.size() - 1)bw.newLine(); catch (IOException e) e.printStackTrace(); finally try if (bw != null) bw.flus

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 初中资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁