////////////////////////////////////////////////////////////////////////////////////////////////////
import java.io.*;
import java.text.*;
import java.util.*;
public class BankMain {
public static void main(String[] args)
{
Business b = new Business();
b.mainmenu();
}
}
class Business {
//用于存储账户信息
static ArrayList<Account> Acc = new ArrayList<Account>();
//定义日志对象,用于操作文件
Log log = new Log();
static Scanner sc = new Scanner(System.in);
//读取文件信息,将账户信息组合成对象存入Acc集合
public void readIn()
{
log.readIn();
}
//主页 实现开户、登录、退出功能
public void mainmenu()
{
readIn();
while(true)
{
System.out.println("------------银行综合业务平台------------");
System.out.println(" ");
System.out.println(" 1、开户 2、登录 3、退出 ");
System.out.println(" ");
System.out.println("-----------------请选择-----------------");
String s = sc.next();
String regex = "^[1-3]$";
if(!s.matches(regex))
{
System.out.println("输入有误,请重新选择:");
continue;
}
switch(s)
{
case "1" :
create();
break;
case "2" :
login();
break;
case "3" :
log.writeIn();
System.exit(0);
}
}
}
/**
* 开户
* 将信息存入对象
* 对象添加到Acc集合
* 将Acc集合写入文件
*/
public void create()
{
Account acc = null;
System.out.println("账号:");
System.out.println(getAccount());
String password1 = "";
String password2 = "";
while(true)
{
System.out.println("请输入密码:(6位数数字)");
String regPass = "[0-9]{6}";
password1 = sc.next();
if(!password1.matches(regPass))
{
System.out.println("密码格式不正确,请重新输入!");
continue;
}
System.out.println("请再次输入密码:");
password2 = sc.next();
if(password1.equals(password2))
{
break;
}
else
{
System.out.println("两次密码不一致!是否重新输入?Y/N");
while(true)
{
String sel = sc.next();
if(sel.equals("Y") || sel.equals("y"))
break;
else if(sel.equals("N") || sel.equals("n"))
return;
else
System.out.println("您的输入有误,请重新输入:");
}
}
}
String name = "";
while(true)
{
System.out.println("请输入用户名:(2-4位汉字)");
name = sc.next();
String regCh = "^[\u4e00-\u9fa5]{2,4}$";
if(!name.matches(regCh))
{
System.out.println("用户名格式不正确,请重新输入!");
continue;
}
else
{
break;
}
}
String tel = "";
while(true)
{
System.out.println("请输入电话号码:");
tel = sc.next();
String regTel = "^[1][0-9]{10}$";
if(tel.matches(regTel))
{
break;
}
else
{
System.out.println("aa");
System.out.println("手机号码格式不正确,请重新输入!");
continue;
}
}
double balance = 0;
String balances = "";
while(true)
{
System.out.println("请输入开户金额:");
balances = sc.next();
String regBal = "\\d{1,12}(\\.\\d{1,2})?";
if(!balances.matches(regBal))
{
System.out.println("输入金额不正确,请重新输入:");
continue;
}
else
{
balance = Double.parseDouble(balances);
break;
}
}
System.out.println("确认开户?Y/N");
while(true)
{
String sel = sc.next();
if(sel.equals("Y") || sel.equals("y"))
{
acc = new Account(String.valueOf(getAccount()),password1,name,tel,balance,1);
Acc.add(acc);log.writeRecord(acc.getAccount(), "开户");
log.writeIn();
System.out.println("开户成功!");
log.writeRoot(acc.getAccount(),"开户");
break;
}
else if(sel.equals("N") || sel.equals("n"))
{
return;
}
else
{
System.out.println("输入有误,请重新输入!");
}
}
personal(acc);
}
//登录,根据账户名和密码进行登录,并且判断账户状态
public void login()
{
int count = 0;//用于锁定账户,超过三次锁定
if(Acc.size() == 0)
{
System.out.println("无账户!");
return;
}
while(true)
{
System.out.println("请输入账号:");
int index = getIndex();
if(index == -1)
return;
Account acc = Acc.get(index);
if(acc.getState() == -1)
{
System.out.println("账户已锁定!");
log.writeIn();
return ;
}
System.out.println("请输入密码:");
String password = sc.next();
if(!acc.getPassword().equals(password))
{
count++;
if(count > 2)
{
acc.setState(-1);
}
System.out.println("密码错误!");
continue;
}
System.out.println("登录成功!");
log.writeRoot(acc.getAccount(),"登录");
personal(acc);
return;
}
}
//个人主页
public void personal(Account acc)
{
while(true)
{
System.out.println("----------个人银行业务菜单----------");
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(" 7、注销登录 ");
System.out.println(" ");
System.out.println("---------------请选择---------------");
String regex = "^[1-7]$";
String sel = sc.next();
if(!sel.matches(regex))
{
System.out.println("输入有误,请重新输入:");
continue;
}
switch(sel)
{
case "1" :
deposit(acc);//存款
break;
case "2" :
withdraw(acc);//取款
break;
case "3" :
query(acc);//查询余额
break;
case "4" :
carry_over(acc);//转账
break;
case "5" :
revise(acc);//修改密码
break;
case "6" :
record(acc);//查询操作记录
break;
case "7" :
logout(acc);//注销登录
return;
}
}
}
//存款
public void deposit(Account acc)
{
String balances = "";
double balance = 0;
while(true)
{
System.out.println("请输入存款金额:");
balances = sc.next();
String regBal = "\\d{1,12}(\\.\\d{1,2})?";
if(!balances.matches(regBal))
{
System.out.println("输入金额不正确,请重新输入:");
continue;
}
else
{
balance = Double.parseDouble(balances);
break;
}
}
System.out.println("存款金额:" + balance +"\n确认存入?Y/N");
while(true)
{
String sel = sc.next();
if(sel.equals("Y") || sel.equals("y"))
{
break;
}
else if(sel.equals("N") || sel.equals("n"))
{
return;
}
else
{
System.out.println("输入有误,请重新输入!");
}
}
acc.setBalance(acc.getBalance() + balance);
System.out.println("存款成功!");
log.writeRecord(acc.getAccount(), "存款:+" + balance);
log.writeIn();
log.writeRoot(acc.getAccount(),"存款");
}
//取款
public void withdraw(Account acc)
{
String balances = "";
double balance = 0;
while(true)
{
System.out.println("请输入取款金额:");
balances = sc.next();
String regBal = "\\d{1,12}(\\.\\d{1,2})?";
if(!balances.matches(regBal))
{
System.out.println("输入金额不正确,请重新输入:");
continue;
}
else
{
balance = Double.parseDouble(balances);
break;
}
}
if(acc.getBalance() - balance < 0)
{
System.out.println("余额不足!");
return ;
}
System.out.println("取款金额:" + balance +"\n确认取出?Y/N");
while(true)
{
String sel = sc.next();
if(sel.equals("Y") || sel.equals("y"))
{
break;
}
else if(sel.equals("N") || sel.equals("n"))
{
return;
}
else
{
System.out.println("输入有误,请重新输入!");
}
}
acc.setBalance(acc.getBalance() - balance);
System.out.println("取款成功!");
log.writeRecord(acc.getAccount(), "取款:-" + balance);
log.writeIn();
log.writeRoot(acc.getAccount(),"取款");
}
//查询余额
public void query(Account acc)
{
System.out.println("账号:" + acc.toString().split(":")[0]);
System.out.println("密码:" + acc.toString().split(":")[1]);
System.out.println("姓名:" + acc.toString().split(":")[2]);
System.out.println("电话:" + acc.toString().split(":")[3]);
System.out.println("余额:" + acc.toString().split(":")[4]);
System.out.println("状态:" + acc.toString().split(":")[5]);
log.writeRecord(acc.getAccount(), "查询用户信息");
log.writeIn();
}
//转账
public void carry_over(Account acc)
{
System.out.println("请输入要转入的账号:");
int index = getIndex();
if(-1 == index)
return ;
Account tacc = Acc.get(index);
double balance = 0;
String balances = "";
while(true)
{
System.out.println("请输入转账金额:");
balances = sc.next();
String regBal = "\\d{1,12}(\\.\\d{1,2})?";
if(!balances.matches(regBal))
{
System.out.println("输入金额不正确,请重新输入:");
continue;
}
else
{
balance = Double.parseDouble(balances);
break;
}
}
if(acc.getBalance() - balance < 0)
{
System.out.println("余额不足!");
return ;
}
System.out.println("要转入的账户:" + tacc.getAccount());
System.out.println("转入金额:" + balance);
System.out.println("确认转入?Y/N");
while(true)
{
String sel = sc.next();
if(sel.equals("Y") || sel.equals("y"))
{
break;
}
else if(sel.equals("N") || sel.equals("n"))
{
return;
}
else
{
System.out.println("输入有误,请重新输入!");
}
}
tacc.setBalance(tacc.getBalance() + balance);
acc.setBalance(acc.getBalance() - balance);
System.out.println("转账成功");
log.writeRecord(tacc.getAccount(), "转账:+" + balance );
log.writeRecord(acc.getAccount(), "转账:-" + balance );
log.writeIn();
log.writeRoot(acc.getAccount(),"转账");
}
//修改密码
public void revise(Account acc)
{
System.out.println("请输入原密码");
String password = sc.next();
if(!password.equals(acc.getPassword()))
{
System.out.println("密码不正确!");
return;
}
while(true)
{
System.out.println("请输入密码:");
String password1 = sc.next();
System.out.println("请再次输入密码:");
String password2 = sc.next();
if(password1.equals(password2))
{
acc.setPassword(password1);
log.writeRecord(acc.getAccount(), "修改密码");
log.writeIn();
System.out.println("修改密码成功!");
log.writeRoot(acc.getAccount(),"修改密码");
return ;
}
else
{
System.out.println("两次密码不一致!是否重新输入?Y/N");
while(true)
{
String sel = sc.next();
if(sel.equals("Y") || sel.equals("y"))
break;
else if(sel.equals("N") || sel.equals("n"))
return;
else
System.out.println("您的输入有误,请重新输入:");
}
}
}
}
//查询交易记录
public void record(Account acc)
{
log.readRecord(acc.getAccount());
log.writeRecord(acc.getAccount(), "查询交易记录");
log.writeIn();
log.writeRoot(acc.getAccount(),"查询交易记录");
}
//注销登录
public void logout(Account acc)
{
log.writeIn();
System.out.println("注销成功!");
log.writeRoot(acc.getAccount(),"退出登录");
}
//根据账户获取脚标并返回脚标
public int getIndex()
{
while(true)
{
String account = sc.next();
for(int index = 0; index < Acc.size(); index++)
{
Account acc = Acc.get(index);
if(acc.getAccount().equals(account))
{
return index;
}
}
System.out.println("账号不存在!");
return -1;
}
}
//获取账号,用于开户
public int getAccount()
{
if(Acc.size() == 0)
{
return 10001;
}
else
{
return Acc.size()+10001;
}
}
}
class Log {
//将文件信息读入Acc集合
public void readIn()
{
File f = new File("D:\\Bank\\");
BufferedReader bufr = null;
String msg = "";
File f2 = null;
try
{
if(!f.exists())
{
f.mkdirs();
}
f2 = new File("D:\\Bank\\BankMsg.txt");
if(!f2.exists())
f.createNewFile();
bufr = new BufferedReader(new FileReader(f2));
while((msg = bufr.readLine()) != null)
{
String[] strArr = msg.split(":");
Business.Acc.add(new Account(strArr[0],strArr[1],strArr[2],strArr[3],Double.parseDouble(strArr[4]),Integer.parseInt(strArr[5])));
}
}
catch (IOException ex){}
finally
{
if(bufr != null)
{
try
{
bufr.close();
}
catch(IOException ex){}
}
}
}
//查询交易记录
public void readRecord(String account)
{
File f = new File("D:\\Bank\\Account");
File f2 = null;
BufferedReader bufr = null;
String msg = "";
try
{
if(!f.exists())
{
f.mkdirs();
}
f2 = new File("D:\\Bank\\Account\\" + account + ".txt");
if(!f2.exists())
f.createNewFile();
bufr = new BufferedReader(new FileReader(f2));
while((msg = bufr.readLine()) != null)
{
System.out.println(msg);
}
}
catch (IOException ex){}
finally
{
if(bufr != null)
{
try
{
bufr.close();
}
catch(IOException ex){}
}
}
}
//管理员日志
public void writeRoot(String account,String record)
{
File f = new File("D:\\Bank\\Account");
BufferedWriter bufw = null;
File f2 = null;
try
{
if(!f.exists())
{
f.mkdirs();
}
f2 = new File("D:\\Bank\\Account\\root.txt");
if(!f2.exists())
f.createNewFile();
bufw = new BufferedWriter(new FileWriter(f2,true));
bufw.write(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
bufw.newLine();
bufw.write("账号:" + account + "\n操作类型:" + record);
bufw.newLine();
bufw.flush();
}
catch(IOException ex)
{}
finally
{
if(bufw != null)
{
try
{
bufw.close();
}
catch(IOException ex)
{}
}
}
}
//将Acc集合写入文件
public void writeIn()
{
File f = new File("D:\\Bank");
BufferedWriter bufw = null;
File f2 = null;
try
{
if(!f.exists())
{
f.mkdirs();
}
f2 = new File("D:\\Bank\\BankMsg.txt");
if(!f2.exists())
f.createNewFile();
bufw = new BufferedWriter(new FileWriter(f2));
for(Account acc : Business.Acc)
{
bufw.write(acc.toString());
bufw.newLine();
bufw.flush();
}
}
catch(IOException ex){}
finally
{
if(bufw != null)
{
try
{
bufw.close();
}
catch(IOException ex){}
}
}
}
//添加用户操作记录
public void writeRecord(String account,String record)
{
File f = new File("D:\\Bank\\Account");
BufferedWriter bufw = null;
File f2 = null;
try
{
if(!f.exists())
{
f.mkdirs();
}
f2 = new File("D:\\Bank\\Account\\" + account + ".txt");
if(!f2.exists())
f.createNewFile();
bufw = new BufferedWriter(new FileWriter(f2,true));
bufw.write(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
bufw.newLine();
bufw.write(record);
bufw.newLine();
bufw.flush();
}
catch(IOException ex)
{}
finally
{
if(bufw != null)
{
try
{
bufw.close();
}
catch(IOException ex)
{}
}
}
}
}
class Account {
private String account; //账户
private String password;//密码
private String name;//用户名
private String tel;//电话
private double balance;//余额
private int state;//账户状态
@Override
public String toString() {
return account + ":" + password + ":" + name + ":" + tel
+ ":" + balance + ":" + state;
}
public Account() {
super();
}
public Account(String account, String password, String name, String tel, double balance, int state) {
super();
this.account = account;
this.password = password;
this.name = name;
this.tel = tel;
this.balance = balance;
this.state = state;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
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 getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
}