当前位置:文档之家› 银行管理系统JAVA

银行管理系统JAVA

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;if(obj instanceof Account){Account anotherAcc=(Account)obj;if(this.id==anotherAcc.id)rt=true;}return rt;}@Overridepublic int hashCode() {returnpassword.hashCode()+name.hashCode()+personId.hashCode()+email.hashCode();}@Overridepublic String toString() {return id+";"+password+";"+name+";"+personId+";"+email+";"+balance;}/** 无参构造体*/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);}/** 存款方法*/public 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(String password) {this.password = password;}public String getPassword() {return password;}public void setName(String name) { = name;}public String getName() {return name;public void setPersonId(String personId) {this.personId = personId;}public String getPersonId() {return personId;}public void setEmail(String 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类,此时调用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.println(" 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)) {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("谢谢您的使用!");}/** 登录成功后的二级菜单*/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 (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);break 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. 统计银行总余额");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:/* 存款*//* 最外层的循环用来询问用户是否继续。

相关主题