導航:首頁 > 編程語言 > javaatm系統

javaatm系統

發布時間:2025-04-09 22:32:25

『壹』 關於用JAVA編程ATM模擬程序問題

/*
要求:使用字元用戶界面。當輸入給定的卡號和密碼(初始卡號和密碼為123456)時,系統能登錄ATM櫃員機系統,用戶可以按照以下規則進行:
1、查詢余額:初始余額為10000元
2、ATM取款:每次取款金額為100的倍數,總額不超過5000元,支取金額不允許透支。
3、ATM存款:不能出現負存款。
4、修改密碼:新密碼長度不小於6位,不允許出現6位完全相同的情況,只有舊密碼正確,新密碼符合要求,且兩次輸入相同的情況下才可以成功修改密碼。
(卡號密碼余額放到文件中)
*/

public class ATM {

private Account acc;

private File dataFile;
private FileWriter fw;
private BufferedWriter bw;

private String filePath = "./data.txt";

public ATM() {
this.acc = new Account();
try {
this.dataFile = new File(this.filePath);
if (!this.dataFile.exists()) {
this.dataFile.createNewFile();
}
this.fw = new FileWriter(this.filePath);
this.bw = new BufferedWriter(this.fw);
} catch (IOException io) {
System.err.println("Cannot open file");
io.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
new ATM().interact();
}

public void interact() {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Account #: ");
String temp = br.readLine();
System.out.println("Password: ");
String temp2 = br.readLine();
if (!this.acc.isValid(Long.parseLong(temp.trim()), temp2.trim()) {
System.err.println("Wrong password");
return;
}
System.out.println("1. Account Inquery.");
System.out.println("2. Withdraw");
System.out.println("3. Deposit.");
System.out.println("4. Change Password.");
System.out.println("5. Export to File.");
System.out.println("0. Exit.");
int c = 100;
while (c != 0) {
String str = br.readLine();
try {
int c = Integer.parseInt(str.trim());
} catch (NumberFormatException nfe) {
System.err.println("Invalid choice");
continue;
}
switch (c) {
case 0:
System.out.println("Thank you");
break;
case 1:
System.out.println("Balance: " + this.acc.balanceInquery());
break;
case 2:
System.out.println("How much? ");
String temp = br.readLine();
try {
long ammount = Long.parseLong(temp.trim());
this.acc.withdraw(ammount);
break;
} catch (NumberFormatException nfe) {
System.err.println("Invalid amount");
continue;
}
case 3:
System.out.println("How much? ");
String temp = br.readLine();
try {
long ammount = Long.parseLong(temp.trim());
this.acc.deposit(ammount);
break;
} catch (NumberFormatException nfe) {
System.err.println("Invalid amount");
continue;
}
case 4:
System.out.println("Old password: ");
String temp = br.readLine();
System.out.println("New password: ");
String temp2 = br.readLine();
this.acc.changePassword(temp, temp2);
break;
case 5:
this.bw.write(this.acc.toString());
break;
default:
break;
}
}
}

}

class Account {

private long accNo = 123456;
private String pass = "123456";
private long balance = 10000;

public Account() {

}

public boolean isValid(long accNo, String pass) {
return (this.accNo == accNo) && (pass.equals(this.pass));
}

public void changePassword(String oldPass, String password) {
if (!oldPass.equals(this.pass)) {
System.err.println("Wrong password.");
return;
}
if (password.length < 6) {
System.err.println("Password too short");
return;
}
if (password.equals(this.pass)) {
System.err.println("Password cannot be the same.");
return;
}
this.pass = password;
}

public long balanceInquery() {
return this.balance;
}

public void withdraw(long amount) {
if (amount > 5000 || amount < 0) {
System.err.println("Withdraw limit: $0-$5000");
return;
}
if ((amount % 100) != 0) {
System.err.println("The amount has to be a proct of 100");
return;
}
long newBalance = this.balance - amount;
if (newBalance < 0) {
System.err.println("Not enough money in the account");
return;
}
this.balance = newBalance;
}

public void deposit(long amount) {
if (amount < 0) {
System.err.println("Cannot deposit negative amount");
return;
}
this.balance += amount;
}

public String toString() {
return ("Account #: " + this.accNo + "\n" + "Password: " + this.pass + "\n" + "Balance: " + this.balance);
}
}

閱讀全文

與javaatm系統相關的資料

熱點內容
程序員放棄後會怎樣 瀏覽:159
河北模具編程 瀏覽:177
adb查找命令 瀏覽:308
安卓手機視頻文件夾怎麼打開 瀏覽:302
平板加密手機後怎麼關閉 瀏覽:556
流媒體伺服器應該注意什麼 瀏覽:526
d8命令編譯 瀏覽:942
壓縮包解壓需要多少空間 瀏覽:138
如何查找app屬性 瀏覽:380
android人臉識別技術 瀏覽:304
pc104編程 瀏覽:328
二維碼反編譯破解推廣 瀏覽:673
修改伺服器的mac地址 瀏覽:520
好玩的編程軟體 瀏覽:891
編程語言創始人有錢嗎 瀏覽:796
短視頻app怎麼獲客 瀏覽:8
查看雲伺服器的應用 瀏覽:427
javadump工具 瀏覽:558
程序員16g 瀏覽:421
程序員沒有辦法成為top怎麼辦 瀏覽:196