Posted on 2006-10-19 10:57
久城 阅读(735)
评论(7) 编辑 收藏 所属分类:
JavaTest
写一个模拟的银行存储系统,能够实现用户的登陆,存钱,取钱,转帐等功能。必须用I/O流在DOS下实现过程。
起了个大早开始写,郁闷啊都写到11点了才出来..
虽然SIMPLE,但是也有不少收获!特别是I/O流。以前学的很不扎实,还得好好看看资料再总结下!
先把代码帖下,哈哈!
代码如下:
package com.neusoft.test;
/**//*
*Title 模拟实现银行存储系统
*@author realsmy
*2006-10-19 10:50
*/
import java.io.*;
class ZhangHu {
private String name;
private String password;
private int money;
ZhangHu(String name, int money) {
this.name = name;
this.money = money;
}
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 class Bank_Test {
String name;
String password;
String pw;
int money;
int choose;
int a, b;// 存取的金额,临时变量
File fl;
ZhangHu user, user2;// 帐户对象
String toname;// 转入帐户名
String c;// 临时存储转入帐户密码
public Bank_Test() {
login();
user = new ZhangHu(name, password, money);
while (true) {
cunqu();
}
}
// 实现登陆方法login
public void login() {
System.out.println("您好,欢迎光临赵家银行!请输入您的帐户号码:");
while (true) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
name = in.readLine();
} catch (IOException e) {
}
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 + "元");
break;
} else {
System.out.println("对不起,您输入的密码不正确,请重新输入帐户:");
continue;
}
}
}
// 实现存取方法cunqu
public void cunqu() {
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);
}
// 取钱
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);
}
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);
}
if (choose == 4) {
System.exit(0);
}
}
// 存入文件
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) {
new Bank_Test();
}
}
欢迎来访!^.^!
本BLOG仅用于个人学习交流!
目的在于记录个人成长.
所有文字均属于个人理解.
如有错误,望多多指教!不胜感激!