Java ATM模擬系統報告
阿新 • • 發佈:2017-10-09
deposit 測試 write oat else file trace 情況下 text
設計內容:
本系統采用JAVA語言編寫測試完成,基本上在字符界面下模擬了ATM系統的相關實現。
本系統主要模擬銀行ATM機系統功能,主要有查閱、取款、存款、添加用戶、刪除用戶等功能。
技術說明:
項目目錄下必須有一個已經錄入的用戶文件,名為ATMUsers (無後綴名,壓縮包內已有一份),否則將報錯。
當輸入用戶的卡號和密碼時,系統能登錄ATM櫃員機系統,用戶可以按照以下規則進行:
查詢余額。
ATM取款:每次取款金額不得大於100,總額不得超過余額。
ATM存款:不能出現負存款。
當輸入用戶名000000與密碼000000時,能登錄ATM櫃員機系統的管理員賬戶,管理員可以按照以下規則進行:
列出用戶:列出ATM系統上的所有用戶。
添加用戶:卡號必須為10位,新密碼長度必須為6位,只有卡號與密碼都符合要求的情況下才可以添加用戶。
刪除用戶:輸入卡號以刪除卡號對應用戶的記錄,刪除前必須鍵入Y以確認。
源代碼:
import java.util.*; import java.io.*; public class ATM { static int number; static int usernum = 0; static boolean isAdmin = false; public static Scanner kbd = new Scanner(System.in);public static void readLength() throws IOException { BufferedReader file = null; try { file = new BufferedReader(new InputStreamReader(new FileInputStream("ATMUsers"))); String line = null; int count = 0; while ((line = file.readLine()) != null) count++; number = count; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (file != null) { try { file.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void readUsers(String[][] users) throws IOException { BufferedReader file = null; try { file = new BufferedReader(new InputStreamReader(new FileInputStream("ATMUsers"))); String line = null; int count = 0; while ((line = file.readLine()) != null) { String data = line; String[] strArray = data.split(" "); users[0][count] = strArray[0]; users[1][count] = strArray[1]; users[2][count] = strArray[2]; count++; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (file != null) { try { file.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static String checkID(String acctNum, String pwd, String[][] users) { String result = "錯誤!"; for(int i = 0;i < number;i++) { if (acctNum.equals("000000") && pwd.equals("000000")) { isAdmin = true; return "0"; } if (acctNum.equals(users[0][i]) && pwd.equals(users[1][i])) { result = users[2][i]; usernum = i; } } return result; } public static int menu() { int menuChoice; do { System.out.print("\n請選擇想要執行的操作:\n" + "**********************" + "\n* 一、余額\t二、存款 *\n" + "* *" + "\n* 三、取款\t四、退出 *\n" + "**********************\n"); menuChoice = kbd.nextInt(); if (menuChoice < 1 || menuChoice > 4){ System.out.println("錯誤!"); } }while (menuChoice < 1 || menuChoice > 4); return menuChoice; } public static int adminMenu() { int menuChoice; do { System.out.print("\n請選擇想要執行的操作:\n" + "******************************" + "\n* 一、讀取用戶\t二、添加用戶 *\n" + "* *" + "\n* 三、刪除用戶\t四、退出管理 *\n" + "******************************\n"); menuChoice = kbd.nextInt(); if (menuChoice < 1 || menuChoice > 4){ System.out.println("錯誤!"); } }while (menuChoice < 1 || menuChoice > 4); return menuChoice; } public static void displayBalance(double x) { System.out.printf("\n您的余額為 $%.2f\n", x); } public static double deposit(double x, double y, String[][] users) { double depositAmt = y, currentBal = x; double newBalance = depositAmt + currentBal; System.out.printf("您現在的余額為:$%.2f\n", newBalance); users[2][usernum] = "" + newBalance; return newBalance; } public static double withdraw(double x, double y, String[][] users) { double withdrawAmt = y, currentBal = x, newBalance; newBalance = currentBal - withdrawAmt; users[2][usernum] = "" + newBalance; System.out.printf("您現在的余額為:$%.2f\n",newBalance); return newBalance; } public static String[][] addUser(String[][] users) throws IOException { System.out.print("\n請輸入您想要添加的用戶卡號:"); String data1 = kbd.next(); if(data1.length() != 10) { System.out.print("卡號長度不符!"); return users; } for(int i = 0;i < number;i++) { if(data1 == users[0][i]) System.out.print("已有同名用戶!"); return users; } System.out.print("\n請輸入密碼:"); String data2 = kbd.next(); if(data2.length() != 6) { System.out.print("密碼長度不符!"); } System.out.print("\n請輸入初始資金:"); String data3 = kbd.next(); String[][] newusers = new String[3][number + 1]; for(int i = 0; i < number;i++) { newusers[0][i] = users[0][i]; newusers[1][i] = users[1][i]; newusers[2][i] = users[2][i]; } newusers[0][number] = data1; newusers[1][number] = data2; newusers[2][number] = data3; FileWriter fileWriter = new FileWriter("ATMUsers",true); BufferedWriter bufferWriter = new BufferedWriter(fileWriter); bufferWriter.newLine(); bufferWriter.write(data1); bufferWriter.write(‘ ‘); bufferWriter.write(data2); bufferWriter.write(‘ ‘); bufferWriter.write(data3); bufferWriter.close(); number++; System.out.print("\n添加成功!"); return newusers; } public static void deleteUser(String[][] users) throws IOException { System.out.print("\n請輸入您想要刪除的用戶卡號:"); String todelete = kbd.next(); int i; boolean exist = false; for(i = 0;i < number;i++) { if(todelete.equals(users[0][i])) { exist = true; break; } } if(exist != true) { System.out.print("\n錯誤:用戶不存在!"); return; } System.out.print("\n確認要刪除該用戶嗎?(鍵入Y以確認)"); String check = kbd.next(); if(!check.equals("y") && !check.equals("Y")) { return; } int j; for(j = i;j < number - 1;j++) { users[0][j] = users[0][j + 1]; users[1][j] = users[1][j + 1]; users[2][j] = users[2][j + 1]; } number--; System.out.print("\n刪除成功!"); } public static void remakeFile(String[][] users) throws IOException { File file = new File("ATMUsers"); file.delete(); File newfile = new File("ATMUsers"); newfile.createNewFile(); FileWriter fileWriter = new FileWriter(newfile.getName(),true); BufferedWriter bufferWriter = new BufferedWriter(fileWriter); for(int i = 0;i < number;i++) { if(i != 0) bufferWriter.newLine(); bufferWriter.write(users[0][i]); bufferWriter.write(‘ ‘); bufferWriter.write(users[1][i]); bufferWriter.write(‘ ‘); bufferWriter.write(users[2][i]); } bufferWriter.close(); } public static void main(String[] args) throws IOException { String accNum, pass, origBal = "錯誤!"; int count = 0, menuOption = 0; double depositAmt = 0, withdrawAmt = 0, currentBal = 0; readLength(); String[][] users = new String[3][number]; readUsers(users); do{ System.out.print("請輸入賬戶名:"); accNum = kbd.next(); System.out.print("請輸入密碼:"); pass = kbd.next(); origBal = checkID(accNum, pass,users); count++; if (count >= 3 && origBal.equals("錯誤!")){ System.out.print("嘗試次數已達上限!"); System.exit(0); } if (!(origBal.equals("錯誤!"))){ if(isAdmin == false) System.out.println("\n您的余額為 $" + Float.parseFloat(users[2][usernum])); if(isAdmin == true) System.out.println("\n您正以管理員身份登錄。"); } else System.out.println(origBal); }while(origBal.equals("錯誤!")); currentBal=Double.parseDouble(origBal); while (menuOption != 4 && isAdmin == true) { menuOption = adminMenu(); switch (menuOption) { case 1: for (int i = 0; i < number; i++){ System.out.println("用戶" + (i + 1) + ": " + users[0][i]); } continue; case 2: users = addUser(users); continue; case 3: deleteUser(users); continue; case 4: System.out.print("\n正在退出……"); remakeFile(users); System.exit(0); break; } } while (menuOption != 4 && isAdmin == false) { menuOption= menu(); switch (menuOption) { case 1: displayBalance(currentBal); continue; case 2: System.out.print("\n請輸入您想要存儲的數額:$ "); depositAmt = kbd.nextDouble(); if(depositAmt < 0.0) { System.out.print("\n不得進行負存款!"); continue; } currentBal=deposit(depositAmt, currentBal,users); continue; case 3: System.out.print("\n請輸入您想要提取的數額:$ "); withdrawAmt = kbd.nextDouble(); while(withdrawAmt > 100.0 || withdrawAmt > currentBal) { System.out.print("錯誤:一次取款太多!" + "\n請輸入較小的數額:$"); withdrawAmt = kbd.nextDouble(); } currentBal = withdraw(currentBal, withdrawAmt,users); continue; case 4: System.out.print("\n正在退出……"); remakeFile(users); System.exit(0); break; } } } }
Java ATM模擬系統報告