第一次尝试使用JAVA编写的ATM机程序

时间:2021-09-21 16:21:37

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);// 用户输入错误,重新开始执行本方法

                            }

                   }

         }

 

}