银行账户管理系统-课程设计报告
课 程 设 计
课程设计名称: java 课程设计 专 业 班 级 : 计科13级03班 学 生 姓 名 : 余金梁 学 号 : [1**********]9 指 导 教 师 : 王社伟 课程设计时间: 2015.6.22-2015.7.3
专业课程设计任务书
银行帐户管理系统
一、需求分析
银行账户管理系统中,主要有以下功能:
1. 添加账户:主要增加账户的账号,姓名,类型以及所存的金额。 2. 登陆账户:根据输入的账号以及密码,验证判断其账户的类型以进行操作。 3. 存款:输入账号,并向该账号存钱。
4. 取款:输入账号和密码,根据不同类型的账户进行取款或者透支取款。 5. 转账:输入账号和密码,并向另外一个存在的账号转账。
6. 查询账户余额:根据输入的账号和密码,查询并显示出该账户的剩余金额。
二、概要设计
图1 系统模块流程图
三、运行环境、开发语言
运行环境:Windows 8.1,DOS ,Eclipse 开发语言:Java
四、详细设计
1 程序清单
表1 ATM 类
表2 Bank类
表3 SavingAccount类
表4 CresitAccount类
2 主要代码
2.1 ATM类
进入银行账户管理系统,进行操作。
图2:银行账户管理系统主界面
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; public class ATM { //属性 private Bank bank; //构造函数 public ATM() { bank = new Bank();
} //main方法 public static void main(String[] args){ ATM atm = new ATM();//实例化ATM Bank bank = atm.bank; //标号,判断是否退出一级菜单 boolean firstFlag = true; while(firstFlag) { //一级菜单 System.out.println("******欢迎使用XXX 银行模拟ATM 系统,
请按如下步骤操作******");
System.out.println(" *1.用已有账户登录"); 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 = 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 系统,请按如下步骤操作******");
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 = 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(System.in); System.out.print("请输入您的取款金额:"); double money2 = scanner.nextDouble(); bank.withdraw(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) { scanner = new Scanner(System.in); System.out.print("请输入您要转入账户的金额:"); double money = scanner.nextLong(); if(money
} } else { System.out.println("抱歉,没有找到您要转入的账户信息!请核
} break;
case 5: secondFlag = false; break; default: System.out.println("没有该选项,请重新选择!"); break; } } catch (Exception e) { System.out.println("选择输入不合法,请重新选择!"); } } } else { System.out.println("没有此账户,请先开户!"); } break;
case 2://账号id 由银行自动提供(从1001递增) scanner = new Scanner(System.in); System.out.print("请输入账户密码:"); String passwd1 = scanner.next(); scanner = new Scanner(System.in); System.out.print("请再输入账户密码:"); String passwd2 = scanner.next(); scanner = new Scanner(System.in); System.out.print("请输入户主姓名:"); String name = scanner.next(); scanner = new Scanner(System.in); System.out.print("请输入户主身份证号码:"); String personId = scanner.next(); scanner = new Scanner(System.in); System.out.print("请输入账户类型(0储蓄,1信用):"); int type = scanner.nextInt(); bank.openAccount(passwd1, passwd2, name, personId, type); break; case 3: bank.saveAccountDate(); firstFlag = false; break; default : System.out.println("没有该选项,请重新选择!"); break; } } catch (Exception e) {
}
}
System.out.println("选择输入不合法,请重新选择!"); } }
System.out.println("谢谢使用!");
2.2 Bank类
对系统添加新账户,登陆已有账户,进行操作。
图3 添加或登陆账户页面
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 Account[20]; private int number;//账户数目 private int id = 1001;//确定银行账号从1001开始生成,即第一个账户的账号是
1001
//构造函数
public Bank(){ accounts=new Account[20];//以后不足时扩容。 number = 0; BufferedReader bufReader = null; Properties props=System.getProperties(); String path=props.getProperty("user.dir"); try { bufReader=new BufferedReader(new FileReader(new
File(path,"account.txt")));
String s = bufReader.readLine(); while(s != null){ String[] str = s.split(","); if(str[4].equals("0")) { Account savingAcc = new
SavingAccount(Long.parseLong(str[0]),
str[1].toString(), str[2].toString(), str[3].toString(),Integer.parseInt(str[4]), Double.parseDouble(str[5])); accounts[number] = savingAcc; } else { Account creditAcc = new
CreditAccount(Long.parseLong(str[0]),
str[1].toString(), str[2].toString(), str[3].toString(),Integer.parseInt(str[4]), Double.parseDouble(str[5]),5000); accounts[number] = creditAcc; } number ++; id++; s = bufReader.readLine(); } } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{
try { if(bufReader != null) { bufReader.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //getXxx,setXxx public Account[] getAccounts() { return accounts; } 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, String name, String personId, int type){ //创建一个新账户 Account account = null; //判断两次密码是否一致 if(passwd1.equals(passwd2)){ //若一致,再判断账户类型(根据type 的值) if(type == 1){ //可令开始余额为10, 信用额度为5000 account = new CreditAccount(id, passwd1, name, personId, type, 10, 5000);
}
else
{
account = new SavingAccount(id, passwd1, name, personId, type, 10); }
//将账户存入账户数组accounts[]中
//判断是否超出存储空间
if(number >= accounts.length)
{ //扩容
Account[] newAccounts = new Account[accounts.length*2];
//copy原来的相关数据
System.arraycopy(accounts, 0, newAccounts, 0, accounts.length); //将newAccounts[]赋给accounts[]
accounts = newAccounts;
accounts[number] = account;
}
else
{
accounts[number] = account;
}
System.out.println("开户成功!!!账户信息见下");
System.out.println("您的卡号为:"+id+"\n"+"您的密码为:
"+passwd1+"\n"+"您的户名为:"+name+"\n"+"您的身份证号为:"+personId+"\n"+"您的账户类型为:"+type+"\n");
account.accountType = type;
number++;
id++;
return account;//此时开户成功
}
else
{
System.out.println("对不起!您两次密码输入不匹配,开户失败!!!"); return null;//此时开户失败
}
}
/**
* 保存数据
*/
public void saveAccountDate(){
BufferedWriter bufWriter=null;
try {
Properties props=System.getProperties();
String path=props.getProperty("user.dir");
bufWriter=new BufferedWriter(new FileWriter(new
File(path,"account.txt")));
for(int i = 0;i
{ //若存在账户
if(accounts[i] != null)
{
//写入账户信息到account.txt
bufWriter.write(accounts[i].id+",");
bufWriter.write(accounts[i].getPassword()+",");
bufWriter.write(accounts[i].getName()+",");
bufWriter.write(accounts[i].getPersonId()+",");
bufWriter.write(accounts[i].getAccountType()+",");
bufWriter.write(Double.toString(accounts[i].getBalance()));
bufWriter.newLine();
}
else
{
break;
}
}
bufWriter.flush();//清空缓存中的内容
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(bufWriter!=null){
bufWriter.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 登录验证
*/
public Account verifyAccount(long id, String password){
Account account = null;
for(int i = 0;i
{ //若存在账户
if(accounts[i] != null)
{ //验证id 号和password
if(id == accounts[i].getId() &&
password.equals(accounts[i].getPassword()))
{
account = accounts[i];
break;
}
}
else
{
break;
}
}
return account;
}
/**
* 转账验证(方法的重载)
*/
public Account verifyAccount(long id){
Account account = null;
for(int i = 0;i
{ //若存在账户
if(accounts[i] != null)
{ //验证id 号和password
if(id == accounts[i].getId())
{
account = accounts[i];
break;
}
}
else
{
break;
}
}
return account;
}
/**
* 转账
*/
public void transferAccount(Account account1, Account account2, double money){ account1.withdraw(money);
account2.deposit(money);
}
/**
* 存款 */ public void deposit(Account account, double money){ account.deposit(money); } /** * 取款 */ public void withdraw(Account account, double money){ account.withdraw(money); }
2.3 SavingAccount 类
对储蓄卡用户进行取款,转账,查看余额等操作。
图4 储蓄卡用户操作页面
public class SavingAccount extends Account{
//构造函数
public SavingAccount() {
super();
}
public SavingAccount(long id, String password, String name,
String personId,int accountType, double balance) {
super(id, password, name, personId, accountType, balance);
}
//对父类的withdraw()实现 public void withdraw(double money){ if(balance
2.4 CresitAccount 类
对信用卡账户进行透支取款,转账,查看余额等操作。
图5 信用卡用户操作页面
public class CreditAccount extends Account{
private int ceiling;
//构造函数
public CreditAccount(){
super();
}
public CreditAccount(long id, String password, String name,
String personId,int accountType, double balance, int ceiling){
super(id, password, name, personId, accountType, balance);
}
} //getXxx,setXxx方法 public int getCeiling() { return ceiling; } public void setCeiling(int ceiling) { this.ceiling = ceiling; } //实现父类的withdraw() public void withdraw(double money){ if((balance + ceiling)
2.5 Account类
对账户进行存款等操作。
图6 账户存款页面
public abstract class Account {
//属性
protected long id;
protected String password;
protected String personId; protected int accountType; protected double balance; //构造方法 public Account(){ //super(); } public Account(long id, String password, String name, String personId,
int accoutType,double balance) {
super();
this.id = id;
this.password = password;
this.name = name;
this.personId = personId;
this.accountType = accountType;
this.balance = balance;
}
//getXxx,setXxx方法
public long getId() {
return id;
}
public 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) {
this.name = name;
}
public String getPersonId() {
return personId;
}
public void setPersonId(String personId) {
this.personId = personId;
}
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(double money){
balance += money;
}
/**
* 取款(取款方式由账户类型决定,所以设为抽象方法,相应的Account 类应设为抽象类)
*/
public abstract void withdraw(double money);
}
五、调试与分析
问题1:遇到的第一个问题是如何用进行异常处理,根据查资料以及问同学才学会;
问题2:是在写取钱这一模块,由于信用卡与储蓄卡两者有着能否透支的区别,所以,无法在一个函数方法中实现,经过自己仔细的设计调试,才在不同的类下面分开构造;
问题3:在贷款和还钱这一模块,在还钱的时候,忘记设置贷款表内的数据与还钱要同步。
六、功能测试
图7 添加账户
图8 登录账户
图9 存款
图10 取款
图11 转账
图12 查看账户余额
七、结论与心得
这次实验可以说是完美的也可以说是不完美的,完美的就是在这次实验中熟练掌握了用java 中的一些比较精髓的东西,比如说调用函数,文件等操作,同时也让自己更进一步的熟练的编写程序,可以说这次实验对自己的java 学习是完美的。但是这次实验也是不完美的,感觉实验的时间太短,中间还有很多考试,以至于自己写实验的时间减少了许多,感觉自己还有好多功能可以添加比如说交易历史的浏览,统计,还有就是贷款,还款等等。
经过这次试验,让我深深的感受到了“一分耕耘,一分收获”,写程序的确是挺消耗时间的,不过自己也在熟练或掌握一些知识。可以说这实验有很大的收获。
八、参考资料
[1]耿祥义、张跃平. JAVA2实用教程(修定),清华大学出版社,2001.10
[2]袁然、郑自国等著.java 案例开发集锦,电子工业出版社,2005.9
[3]刘继承、王社伟等.Java 程序设计及实验,清华大学出版社,2012.9.
[4]赵付青等.Java 面向对象程序设计, 国防工业出版社,2010.2