/*System.out.println("对不起,已超出您的信用额度!
");*/
JOptionPane.showMessageDialog(null,"对不起,已超出您的信用额度!
",
"信息提示",JOptionPane.ERROR_MESSAGE);
}
else
{
balance-=money;
}
}
}
Bank类
packagecom.qx;
importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Properties;
importjavax.swing.JOptionPane;
/**
*Bank类
*编写Bank类,属性:
1.当前所有的账户对象的集合,存放在数组中
2.当前账户数量
方法:
1.用户开户,需要的参数:
id,密码,密码确认,姓名,身份证号码,账户类型,返回新创建的Account对象的账号,
提示:
用s1.equals(s2)可以比较s1,s2两个字符串的值是否相等.账户类型是一个整数,为0的时候表示储蓄账户,为1的时候表示信用账户
2.用户登录,参数:
id,密码返回登录账户的账号
3.用户存款,参数:
id,存款数额,返回void
4.用户取款,参数:
id,取款数额,返回void
5.查询余额,参数:
id,返回该账户的余额double
用户会通过调用Bank对象以上的方法来操作自己的账户,请分析各个方法需要的参数
*/
publicclassBank{
/*privateAccount[]accounts=newAccount[20];*/
privateListaccountsList;
privateintnumber;//账户数目
privateintid=1001;//确定银行账号从1001开始生成,即第一个账户的账号是1001
//构造函数
publicBank(){
accountsList=newArrayList();
number=0;
BufferedReaderbufReader=null;
Propertiesprops=System.getProperties();
Stringpath=props.getProperty("user.dir");
try{
bufReader=newBufferedReader(newFileReader(newFile(path,"account.txt")));
Strings=bufReader.readLine();
while(s!
=null){
String[]str=s.split(",");
if(str[4].equals("0"))
{
AccountsavingAcc=newSavingAccount(Long.parseLong(str[0]),
str[1].toString(),str[2].toString(),
str[3].toString(),str[4].toString(),
Double.parseDouble(str[5]));
accountsList.add(savingAcc);
}
else
{
AccountcreditAcc=newCreditAccount(Long.parseLong(str[0]),
str[1].toString(),str[2].toString(),
str[3].toString(),str[4].toString(),
Double.parseDouble(str[5]),5000);
accountsList.add(creditAcc);
}
number++;
id++;
s=bufReader.readLine();
}
}catch(NumberFormatExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
try{
if(bufReader!
=null)
{
bufReader.close();
}
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
//getXxx,setXxx
publicListgetAccounts(){
returnaccountsList;
}
publicvoidsetAccounts(Listaccounts){
this.accountsList=accounts;
}
publicintgetNumber(){
returnnumber;
}
publicvoidsetNumber(intnumber){
this.number=number;
}
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
/**
*开户
*/
publicAccountopenAccount(Stringpasswd1,Stringpasswd2,Stringname,
StringpersonId,Stringtype){
//创建一个新账户
Accountaccount=null;
//判断两次密码是否一致
if(passwd1.equals(passwd2)){
//若一致,再判断账户类型(根据type的值)
if(type.equals("1")){
//可令开始余额为10,信用额度为5000
account=newCreditAccount(id,passwd1,name,personId,type,10,5000);
}
else
{
account=newSavingAccount(id,passwd1,name,personId,type,10);
}
//将账户存入账户集合accountsList中
accountsList.add(account);
JOptionPane.showMessageDialog(null,"开户成功","信息提示",
JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"您的卡号为:
"+id+"\n"+
"您的密码为:
"+passwd1+"\n"+"您的户名为:
"+name+"\n"+
"您的身份证号为:
"+personId+"\n"+"您的账户类型为:
"+type+"\n","信息提示",
JOptionPane.INFORMATION_MESSAGE);
account.accountType=type;
number++;
id++;
returnaccount;//此时开户成功
}
else
{
JOptionPane.showMessageDialog(null,"对不起!
您两次密码输入不匹配,开户失败",
"信息提示",JOptionPane.ERROR_MESSAGE);
returnnull;//此时开户失败
}
}
/**
*保存数据
*/
publicvoidsaveAccountDate(){
BufferedWriterbufWriter=null;
try{
Propertiesprops=System.getProperties();
Stringpath=props.getProperty("user.dir");
Strings=null;
bufWriter=newBufferedWriter(newFileWriter(newFile(path,"account.txt")));
for(Iteratoriterator=accountsList.iterator();iterator.hasNext();)
{//若存在账户
Accountacc=(Account)iterator.next();
//写入账户信息到account.txt
bufWriter.write(acc.id+",");
bufWriter.write(acc.getPassword()+",");
bufWriter.write(acc.getName()+",");
bufWriter.write(acc.getPersonId()+",");
bufWriter.write(acc.getAccountType()+",");
bufWriter.write(Double.toString(acc.getBalance()));
bufWriter.newLine();
}
bufWriter.flush();//清空缓存中的内容
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
try{
if(bufWriter!
=null){
bufWriter.close();
}
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
/**
*登录验证
*/
publicAccountverifyAccount(longid,Stringpassword){
Accountaccount=null;
Accountacc=null;
for(Iteratoriterator=accountsList.iterator();iterator.hasNext();)
{//若存在账户
acc=(Account)iterator.next();
if(acc!
=null){
if(id==acc.getId()&&password.equals(acc.getPassword())){
account=acc;
break;
}
}
else
{
break;
}
}
returnaccount;
}
/**
*转账验证(方法的重载)
*/
publicAccountverifyAccount(longid){
Accountaccount=null;
Accountacc=null;
for(Iteratoriterator=accountsList.iterator();