Posted on 2006-10-21 09:24
久城 阅读(425)
评论(2) 编辑 收藏
对前天写的程序进行了修改,主要在于填加一个管理员。能够实现帐户的填加,删除和冻结。
迷糊啊,快吐血了...
/*
*Title 模拟实现银行存储系统
*用户实现存钱,取钱,转帐..
*填加了一个管理员,实现简单的帐户填加,删除,冻结...
*@author realsmy
*2006-10-21 9:16
*/
import java.io.*;
import java.util.*;
//定义帐户类
class ZhangHu {
private String name;
private String password;
private int money;
ZhangHu(){
}
ZhangHu(String name,String password, int money){
this.name = name;
this.password = password;
this.money = money;
}
public void setM(int a){
money = money + a;
System.out.println("存储了"+a+"元,帐户"+name+"尚有余额"+money+"元");
}
public void getM(int a){
if(a > money){
System.out.println("对不起,您的金额不足"+a+"元");
}
else{
money = money - a;
System.out.println("取得了"+a+"元,帐户"+name+"尚有余额"+money+"元");
}
}
public String getName(){
return name;
}
public String getPassword(){
return password;
}
public int getMoney(){
return money;
}
public void setName(String name){
this.name = name;
}
public void setPassword(String password){
this.password = password;
}
public void setMoney(int money){
this.money = money;
}
}
//定义管理员类
class Manager
{
//private static Vector vec = new Vector();
//Enumeration vc = vec.elements();
public void add(){
ZhangHu zh = new ZhangHu();
try{
System.out.println("请输入帐户名:");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
zh.setName(in.readLine());
System.out.println("请输入密码:");
while(true){
zh.setPassword(in.readLine());
System.out.println("请再次输入密码:");
if(zh.getPassword().equals(in.readLine())){
break;
}
else{
System.out.println("两次输入的密码不一致,请重新输入密码:");
continue;
}
}
System.out.println("请输入该帐户的金额:");
zh.setMoney(Integer.parseInt(in.readLine()));
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(zh.getName()+".txt")));//创建文件
writer.println(zh.getPassword());
writer.println(zh.getMoney());
writer.flush() ;
System.out.println("帐户"+zh.getName()+"已经创建成功");
}catch(IOException e){}
}
public void del(){
ZhangHu zh2;
String name = null;
//int p = 0;//判断是否删除成功
System.out.println("请输入您要删除的帐户:");
while(true){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
name = in.readLine();
}catch(IOException e){}
//while(vc.hasMoreElements()){
File fl = new File(name+".txt");
if(fl.exists()){
fl.delete();
System.out.println("帐户"+name+"删除成功");
break;
}
else{
System.out.println("您所输入的帐户并不存在,请重新输入:");
}
}
}
public void froze(){
ZhangHu zh3 = new ZhangHu();
String name = null;
System.out.println("请输入您要冻结的帐户:");
while (true){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
name = in.readLine();
}catch(IOException e){}
File fl = new File(name+".txt");
if (fl.exists()){//如果该帐户存在,则读取该帐户的数据
try{
BufferedReader reader = new BufferedReader(new FileReader( name + ".txt"));
zh3.setPassword(reader.readLine());
zh3.setMoney(Integer.parseInt(reader.readLine()));
}catch(IOException e){}
File fr = new File("frozen");
if(!fr.exists()){
try{
fr.mkdir();//创建一个此目录的文件夹
}catch(SecurityException e){}
}
File frfl = new File(fr,name+".txt");
try{
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(frfl)));
writer.println(zh3.getPassword());
writer.println(zh3.getMoney());
writer.flush() ;
}catch(IOException e){}
fl.delete();
System.out.println("帐户"+name+"冻结成功!");
break;
}
else{
System.out.println("您所输入的帐户并不存在,请重新输入:");
}
}
}
}
//银行模拟系统
public class Bank_Test{
String name;
String password;
String pw;
int money;
int choose;
int a,b;//存取的金额,临时变量
int type;//登陆身份
File fl;
ZhangHu user,user2;//帐户对象
Manager manager;
//Manager manager;//管理员对象
String toname;//转入帐户名
String c;//临时存储转入帐户密码
public Bank_Test(){
if(login() == 1){
manager_login();
manage();
}
else{
if(user_login()){
user = new ZhangHu(name,password,money);
cunqu();
}
}
}
//实现登陆方法login
public int login(){
System.out.println("您好,欢迎光临赵家银行!\n请选择您的登陆身份:1. 管理员 2. 用户");
while(true){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
type = Integer.parseInt(in.readLine());
}catch(IOException e){}
if (type == 1){
return 1;
}
else if (type == 2)
{
return 2;
}
else{
System.out.println("输入错误,请重新输入:");
continue;
}
}
}
//实现管理员登陆方法manager_login()
public void manager_login(){
System.out.println("请输入管理员帐号:");
while(true){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
name = in.readLine();
}catch(IOException e){}
if (name.equals("admin")){
System.out.println("请输入密码:");
while(true){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
password = in.readLine();
}catch(IOException e){}
if (password.equals("admin")){
System.out.println("登陆成功!欢迎您admin!");
break;
}
else{
System.out.println("密码错误,请重新输入:");
}
}
break;
}
else{
System.out.println("对不起,您输入的帐号并不存在,请重新输入:");
continue;
}
}
}
//实现管理员管理方法manage()
public void manage(){
manager = new Manager();
while(true){
System.out.println("请选择您要进行的操作:\n1. 填加帐户 2. 删除帐户 3. 冻结帐户 4. 退出");
while(true){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
choose = Integer.parseInt(in.readLine());
}catch(IOException e){}
if(choose == 1){
manager.add();
break;
}
else if(choose == 2){
manager.del();
break;
}
else if(choose == 3){
manager.froze();
break;
}
else if (choose ==4){
System.exit(0);
}
else{
System.out.println("输入错误,请重新输入:");
continue;
}
}
}
}
//实现用户登陆方法user_login()
public boolean user_login(){
System.out.println("尊敬的用户您好,欢迎光临赵家银行!请输入您的帐户号码:");
while(true){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
name = in.readLine();
}catch(IOException e){}
fl = new File("frozen",name+".txt");
if(fl.exists()){
System.out.println("对不起,您的帐户已被冻结!");
return false;
}
fl = new File(name+".txt");
//判断帐户是否存在
if(!fl.exists()){
System.out.println("对不起,您输入的帐户并不存在,请重新输入:");
continue;
}
//帐户存在,开始判断密码
try{
System.out.println("请输入您的密码:");
BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in));
password = in2.readLine();
}catch(IOException e){}
//取文件中的密码
try{
BufferedReader reader = new BufferedReader(new FileReader( name + ".txt"));
pw = reader.readLine();
money = Integer.parseInt(reader.readLine());
}catch(IOException e){}
//判断密码
if(password.equals(pw)){
System.out.println("登陆成功");
System.out.println("您的用户尚有余额"+money+"元");
return true;
}
else{
System.out.println("对不起,您输入的密码不正确,请重新输入帐户:");
continue;
}
}
}
//实现user存取方法cunqu
public void cunqu(){
while(true){
System.out.println("请选择您要进行的操作:");
System.out.println("1. 存钱 2. 取钱 3. 转帐 4. 退出 ");
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
choose = Integer.parseInt(in.readLine());
}catch(IOException e){}
//存钱
if(choose == 1){
System.out.println("请输入你要存储的金额:");
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
a = Integer.parseInt(in.readLine());
}catch(IOException e){}
user.setM(a);
//infile(user);
}
//取钱
else if(choose == 2){
System.out.println("请输入你要取得的金额:");
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
a = Integer.parseInt(in.readLine());
}catch(IOException e){}
user.getM(a);
infile(user);
}
else if(choose == 3){
System.out.println("请输入你要转入的帐户:");
while(true){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
toname= in.readLine();
}catch(IOException e){}
fl = new File(toname+".txt");
//判断帐户是否存在
if(!fl.exists()){
System.out.println("对不起,您输入的帐户并不存在,请重新输入:");
continue;
}
else{
break;
}
}
System.out.println("请输入你要转入的金额:");
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
a = Integer.parseInt(in.readLine());
}catch(IOException e){}
user.getM(a);
infile(user);
try{
BufferedReader reader = new BufferedReader(new FileReader( toname + ".txt"));
c = reader.readLine();
b = Integer.parseInt(reader.readLine());
}catch(IOException e){}
user2 = new ZhangHu(toname,c,b);
user2.setM(a);
infile(user2);
}
else if(choose == 4){
System.exit(0);
}
else{
System.out.println("输入不正确。");
continue;
}
}
}
//存入文件
public void infile(ZhangHu p){
try{
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(p.getName()+".txt")));
writer.println(p.getPassword());
writer.println(p.getMoney());
writer.flush() ;
}catch(IOException e){}
}
public static void main(String [] args){
Bank_Test a = new Bank_Test();
}
}
欢迎来访!^.^!
本BLOG仅用于个人学习交流!
目的在于记录个人成长.
所有文字均属于个人理解.
如有错误,望多多指教!不胜感激!