Posted on 2006-10-19 10:57
久城 閱讀(737)
評論(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僅用于個人學習交流!
目的在于記錄個人成長.
所有文字均屬于個人理解.
如有錯誤,望多多指教!不勝感激!