第一次嘗試使用JAVA編寫的ATM機程序


package study;

 

import java.util.Scanner;

 

public class ATM {

         private static int[] users = { 111111, 222222, 333333, 444444, 555555,

                            666666, 777777, 888888, 999999 };// 定義數據庫賬戶組

         private static int[] passwords = { 111111, 222222, 333333, 444444, 555555,

                            666666, 777777, 88888, 999999 };// 定義數據庫密碼組

         private static double[] money = { 99999, 88888, 77777, 66666, 55555, 44444,

                            33333, 22222, 11111 };// 定義數據庫賬戶初始余額組

         private static int maxRemainingMoney = 150000;// 初始化時ATM機內默認的剩余金額是15萬

         private static int maxMoney = 200000;// 最大金額容量為20萬

         private static int maxDrawingMoney = 100000;// 最大取款限額為10萬

         private static int I;// 定義登錄賬戶在數據庫賬戶組的下標值

 

         // 主方法

         public static void main(String[] args) {

                   login();// 跳轉至驗證功能

         }

 

         // 驗證賬號和密碼(無參無返回值方法)

         public static void login() {

                   for (int i = 1; i <= 3; i++) {

                            System.out.println("請輸入賬號:");// 提示用戶輸入賬戶

                            Scanner userSc = new Scanner(System.in);

                            int userIn = userSc.nextInt();

                            System.out.println("請輸入密碼:");// 提示用戶輸入密碼

                            Scanner passwordSc = new Scanner(System.in);

                            int passwordIn = passwordSc.nextInt();

                            for (int j = 0; j < users.length; j++) {// 判斷輸入賬戶和密碼是否匹配數據庫

                                     if (userIn == users[j] && passwordIn == passwords[j]) {

                                               I = j;// 匹配成功,將當前賬戶在數據庫中的賬戶組的下標值賦值給I

                                               welcome();// 跳轉至歡迎界面

                                     } else if (i == 1 && j == users.length - 1) {// 第一次輸入錯誤

                                               System.out.println("賬號或密碼錯誤,請重新輸入!");

                                     } else if (i == 2 && j == users.length - 1) {// 第二次輸入錯誤

                                               System.out.println("賬號或密碼錯誤,請重新輸入!");

                                               System.out.println("這是您的最后一次輸入機會,若賬號或密碼錯誤,將鎖定您的銀行卡");

                                     } else if (j == users.length - 1) {// 第三次輸入錯誤

                                               System.out.println("您的錯誤次數超過限制,請聯系相關銀行!");

                                               System.exit(0);// 三次錯誤后,程序終止

                                     }

                            }

                   }

         }

 

         // 歡迎界面(無參無返回值方法)

         public static void welcome() {

                   System.out.println("—————————————————————————————————————");

                   System.out.println("|1.查詢余額                                                                                3.存款|");

                   System.out.println("|check account                                                                        deposit|");

                   System.out.println("|                                              歡迎使用                                     4.轉賬|");

                   System.out.println("|                                             自助ATM機                                transfer|");

                   System.out.println("|2.取款                                                                                      5.退出|");

                   System.out.println("|withdraw                                                                                     exit|");

                   System.out.println("—————————————————————————————————————");

                   choose();// 跳轉至選擇功能

         }

 

         // 執行選擇功能(無參無返回值方法)

         public static void choose() {

                   Scanner sc = new Scanner(System.in);

                   int n = sc.nextInt();

                   switch (n) {

                   case 1:

                            System.out.println("您的余額為" + money[I] + " RMB");// 顯示余額

                            before();// 跳轉到返回界面

                   case 2:

                            drawing();// 取款

                            before();// 跳轉到返回界面

                   case 3:

                            deposit();// 存款

                            before();// 跳轉到返回界面

                   case 4:

                            beforeTransfer();// 轉賬

                            before();// 跳轉到返回界面

                  case 5:

                            end();// 跳轉至退出界面

                   default:

                            System.out.println("您的輸入有誤,請重新輸入!");// 若輸入的數字不在1~5范圍內,則提示錯誤

                            System.out.println("1.查詢余額  2.取款  3.存款  4.退出");

                            choose();// 重新執行本方法

                   }

         }

 

         // 返回功能(無參無返回值方法)

         public static void before() {

                   System.out.println("——————————————————————————————————");

                   System.out.println("|1.返回主界面/return                                                    2.退出/exit|");

                   System.out.println("——————————————————————————————————");

                   Scanner sc = new Scanner(System.in);

                   int n = sc.nextInt();

                   switch (n) {

                   case 1:

                            welcome();// 跳轉至歡迎界面

                   case 2:

                            end();// 跳轉至退出界面

                   default:

                            System.out.println("您的輸入有誤,請重新輸入!");// 若輸入的數字不為1或2,則提示錯誤

                            before();// 重新執行本方法

                   }

         }

 

         // 退出界面(無參無返回值方法)

         public static void end() {

                   System.out.println("———————————————————————————————————————");

                   System.out.println("|感謝您的使用,請保管好您的銀行卡,再見                                                       |");

                   System.out.println("|Thanks for using                                                                                     |");

                   System.out.println("———————————————————————————————————————");

                   System.exit(0);// 退出后程序終止

         }

 

         // 實現取款功能(無參無返回值方法)

         public static void drawing() {

                   System.out.println("請輸入取款金額:");// 提示用戶輸入取款金額

                   Scanner sc = new Scanner(System.in);

                   int n = sc.nextInt();

                   if (n == 0) {

                            System.out.println("金額不能為零,請重新輸入");

                            drawing();// 用戶輸入金額為零,重新開始執行本方法

                   } else if (n % 100 != 0) {

                            System.out.println("金額應為整百數,請重新輸入");

                            drawing();// 用戶輸入金額不為整百,重新開始執行本方法

                   } else if (n < 0) {

                            System.out.println("金額應大於零,請重新輸入");

                            drawing();// 用戶輸入金額小於零,重新開始執行本方法

                   } else if (n > money[I]) {

                            System.out.println("您卡內的余額不足,請重新輸入");

                            drawing();// 用戶輸入金額超過余額,重新開始執行本方法

                   } else if (n > maxDrawingMoney) {

                            System.out.println("您輸入的金額超出取款限度,請重新輸入");

                            drawing();// 用戶輸入金額超出取款限度,重新開始執行本方法

                   } else if (n > maxRemainingMoney) {

                            System.out.println("抱歉,本機剩余金額不足,請重新輸入");

                            drawing();// 本機余額不足,重新開始執行本方法

                   } else {// 若金額合法,則打印確定提示

                            System.out.println("您取款的金額為" + n + " RMB");

                            System.out.println("1.確定  2.重新輸入");

                            Scanner sc1 = new Scanner(System.in);

                            int m = sc1.nextInt();

                            if (m == 1) {// 用戶選擇確定,開始取款

                                     System.out.println("正在出鈔,請稍候");

                                     money[I] = money[I] - n;// 取款后當前賬戶余額減少

                                     System.out.println("請取走您的鈔票");

                                     System.out.println("當前卡內余額" + money[I] + " RMB");// 顯示當前賬戶余額

                                     before();// 跳轉到返回界面

                            } else if (m == 2) {

                                     drawing();// 用戶選擇重新輸入,重新開始執行本方法

                            } else {

                                     System.out.println("您的輸入有誤,請重新輸入!");

                                     drawing();// 用戶輸入錯誤,重新開始執行本方法

                            }

                   }

         }

 

         // 實現存款功能(無參無返回值方法)

         public static void deposit() {

                   System.out.println("請輸入存款金額,並將鈔票整理好后放入存鈔口");// 提示用戶輸入存款金額

                   Scanner sc = new Scanner(System.in);

                   int n = sc.nextInt();

                   if (n == 0) {

                            System.out.println("金額不能為零,請重新輸入");

                            deposit();// 用戶輸入金額為零,重新開始執行本方法

                   } else if (n % 100 != 0) {

                            System.out.println("金額應為整百數,請重新輸入");

                            deposit();// 用戶輸入金額不為整百,重新開始執行本方法

                   } else if (n < 0) {

                            System.out.println("金額應大於零,請重新輸入");

                            deposit();// 用戶輸入金額小於零,重新開始執行本方法

                   } else if ((n + money[I]) > maxMoney) {

                            System.out.println("抱歉,存入后超出本機最大金額容量,請重新輸入:");

                            deposit();// //用戶輸入金額超過本機容量,重新開始執行本方法

                   } else {// 若金額合法,則打印確定提示

                            System.out.println("您存款的金額為" + n + " RMB");

                            System.out.println("1.確定  2.重新輸入");

                            Scanner sc1 = new Scanner(System.in);

                            int m = sc1.nextInt();

                            if (m == 1) {// 用戶選擇確定,開始存款

                                     System.out.println("正在存鈔,請稍候");

                                     money[I] = money[I] + n;// 存款后當前賬戶余額增加

                                     System.out.println("您成功存入" + n + " RMB");

                                     System.out.println("當前卡內余額" + money[I] + " RMB");// 顯示當前賬戶余額

                                     before();// 跳轉到返回界面

                            } else if (m == 2) {

                                     deposit();// 用戶選擇重新輸入,重新開始執行本方法

                            } else {

                                     System.out.println("您的輸入有誤,請重新輸入!");

                                     deposit();// 用戶輸入錯誤,重新開始執行本方法

                            }

                   }

         }

 

         // 轉賬判斷(無參有返回值方法)

         public static int beforeTransfer() {

                   int II = -1;// 定義轉入賬戶在數據庫賬戶組的下標值

                   System.out.println("請輸入需要轉賬的賬戶:");// 提示用戶輸入轉入賬戶

                   Scanner sc = new Scanner(System.in);

                   int a = sc.nextInt();

                   if (a == users[I]) {// 判斷用戶輸入的轉入賬戶是否為當前登錄賬戶

                            System.out.println("您不能給自己轉賬!");// 若是則提示不能給自己轉賬

                            beforeTransfer();// 出現錯誤,重新開始執行本方法

                   }

                   for (int i = 0; i < users.length; i++) {// 將用戶輸入的轉入賬戶匹配數據庫中的賬戶組

                            if (a == users[i]) {// 若匹配成功,則打印確定提示

                                     System.out.println("您轉賬的賬戶為" + users[i]);

                                     System.out.println("1.確定  2.重新輸入");

                                     Scanner sc1 = new Scanner(System.in);

                                     int n = sc1.nextInt();

                                     if (n == 1) {// 用戶選擇確定,執行跨行判斷

                                               II = i;

                                               if ((I >= 5 && II < 5) || (I < 5 && II >= 5)) {// 判斷是否為跨行轉賬,是則顯示手續費提示

                                                        System.out.println("跨行轉賬將收取您1%的手續費,不足1元按1元收取,每筆轉賬最多收取50元");

                                               }

                                               transfer(II);// 跳轉至轉賬功能

                                     } else if (n == 2) {

                                               beforeTransfer();// 用戶選擇重新輸入,重新開始執行本方法

                                     } else {

                                               System.out.println("您的輸入有誤,請重新輸入!");

                                               beforeTransfer();// 用戶輸入錯誤,重新開始執行本方法

                                     }

                            } else if (i == users.length - 1) {

                                     System.out.println("您輸入的賬戶不存在,請重新輸入!");

                                     beforeTransfer();// 用戶輸入的賬戶不存在,重新開始執行本方法

                            }

                   }

                   return II;// 返回轉入賬戶在數據庫賬戶組的下標值,以便轉賬功能中引用

         }

 

         // 實現轉賬功能(帶參無返回值方法)

         public static void transfer(int II) {

                   System.out.println("請輸入轉賬金額:");// 提示用戶輸入轉賬金額

                   Scanner sc = new Scanner(System.in);

                   float n = sc.nextFloat();

                   if (n == 0) {

                            System.out.println("金額不能為零,請重新輸入");

                            transfer(II);// 用戶輸入金額為零,重新開始執行本方法

                   } else if (n < 0) {

                            System.out.println("金額應大於零,請重新輸入");

                            transfer(II);// 用戶輸入金額小於零,重新開始執行本方法

                   } else if (n > money[I]) {

                            System.out.println("您卡內的余額不足,請重新輸入");// 用戶余額不足,重新開始執行本方法

                            transfer(II);

                   } else {// 若金額合法,則打印確定提示

                            System.out.println("您轉賬的金額為" + n + " RMB");

                            System.out.println("1.確定  2.重新輸入");

                            Scanner sc1 = new Scanner(System.in);

                            int m = sc1.nextInt();

                            if (m == 1) {// 用戶選擇確定,開始轉賬

                                     System.out.println("轉賬成功");

                                     if ((I >= 5 && II < 5) || (I < 5 && II >= 5)) {// 判斷是否為跨行轉賬,是則收取手續費

                                               if (n * 0.01 < 1) {

                                                        money[I] = money[I] - 1;// 不足1元按1元收取手續費

                                               } else if (n * 0.01 > 50) {

                                                        money[I] = money[I] - 50;// 超過50元按50元收取手續費

                                               } else {

                                                        money[I] = money[I] - n * 0.01;// 正常收取手續費

                                               }

                                     }

                                     money[I] = money[I] - n;// 當前賬戶扣除轉賬的錢

                                     money[II] = money[II] + n;// 被轉賬戶增加轉賬的錢

                                     System.out.println("當前卡內余額" + money[I] + " RMB");// 打印當前賬戶扣款后的余額

                                     for (int i = 0; i < money.length; i++) {// 打印所有賬戶余額,目的是判斷程序是否正確執行

                                               System.out.print("第" + (i + 1) + "位用戶目前的余額為" + money[i]

                                                                 + " ");

                                               System.out.println();

                                     }

                                     before();// 跳轉到返回界面

                            } else if (m == 2) {

                                     transfer(II);// 用戶選擇重新輸入,重新開始執行本方法

                            } else {

                                     System.out.println("您的輸入有誤,請重新輸入!");

                                     transfer(II);// 用戶輸入錯誤,重新開始執行本方法

                            }

                   }

         }

 

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM