1 import java.util.Scanner; 2 3 /**ATM存取款機*/ 4 public class ATM { 5 6 public static void main(String[] args) { 7 /**初始化用戶信息*/ 8 String user = "zhan";//用戶名 9 String pwd = "123";//密碼 10 float money = 100f ;//初始余額 11 welcome(); 12 if (login(user,pwd)){ 13 //登錄成功 14 Scanner sc = new Scanner(System.in); 15 while (true){ 16 System.out.println("1、查詢 2、存款 3、取款 4、修改密碼 5、退出"); 17 switch (sc.nextInt()){ 18 case 1 : 19 searchMoney(money); 20 break; 21 case 2 : 22 money += saveMoney(); 23 break; 24 case 3 : 25 money -= getMoney(money); 26 break; 27 case 4 : 28 pwd = changePwd(pwd); 29 break; 30 case 5 : System.exit(0); break; 31 default :;break; 32 } 33 } 34 }else{ 35 //登錄失敗 36 //System.out.println("登錄失敗,系統退出!"); 37 System.exit(0); 38 } 39 } 40 41 /** 42 * 歡迎登陸界面 43 * 44 * */ 45 public static void welcome (){ 46 System.out.println("***********************"); 47 System.out.println("——歡迎登陸———"); 48 System.out.println("***********************"); 49 System.out.println("***********************"); 50 System.out.println("***********************"); 51 } 52 /** 53 * 登陸--檢測用戶名和密碼 54 * 55 * */ 56 public static boolean login (String user ,String pwd){ 57 Scanner sc = new Scanner (System.in); 58 59 for (int i = 3 ;i>0;i--){ 60 System.out.println("請輸入用戶名:"); 61 String checkUser = sc.next();//用戶輸入 用戶名 62 System.out.println("請輸入密碼:"); 63 String checkPwd = sc.next();//用戶輸入 密碼 64 // .equals()匹配字符串 65 if (user.equals (checkUser) && pwd.equals (checkPwd) ){ 66 System.out.println("登陸成功!"); 67 return true ; 68 }else { 69 if ( i ==1 ){ 70 System.out.println("你的卡被吞!!找相關人員"); 71 return false ; 72 } 73 System.out.println("用戶名或密碼錯誤!今日剩余次數:"+ (i-1)); 74 } 75 } 76 return false ; 77 } 78 /** 79 *查詢余額 80 * 81 * */ 82 public static void searchMoney (float money){ 83 System.out.println("你的余額為"+money); 84 } 85 /** 86 *存款 87 * 88 * */ 89 public static float saveMoney (){ 90 System.out.println("請輸入存款金額:"); 91 Scanner sc = new Scanner (System.in); 92 float saveMoney = sc.nextFloat(); 93 94 if (saveMoney > 10000){ 95 System.out.println("單次最大存款金額為1000.0元"); 96 saveMoney=0; 97 }else if (saveMoney < 0){ 98 System.out.println("不能存負數的錢!!"); 99 saveMoney=0; 100 }else if (saveMoney %100!= 0){ 101 System.out.println("不能存零錢!!"); 102 saveMoney=0; 103 }else{ 104 System.out.println("存款成功!"); 105 } 106 return saveMoney ; 107 } 108 /** 109 *取款 110 * 111 * */ 112 public static float getMoney (float money){ 113 System.out.println("請輸入取款金額:"); 114 Scanner sc = new Scanner (System.in); 115 float getMoney = sc.nextFloat(); 116 117 if (getMoney > 10000){ 118 System.out.println("單次最大取款金額為1000.0元"); 119 getMoney=0; 120 }else if (getMoney < 0){ 121 System.out.println("不能取負數的錢!!"); 122 getMoney=0; 123 }else if (getMoney %100!= 0){ 124 System.out.println("不能取零錢!!"); 125 getMoney=0; 126 }else if (money <getMoney ){ 127 System.out.println("余額不足!!"); 128 getMoney=0; 129 }else { 130 System.out.println("取款成功!"); 131 } 132 return getMoney ; 133 } 134 135 /** 136 *修改密碼 137 * 138 * */ 139 public static String changePwd(String oldPwd) { 140 System.out.println("請輸入舊密碼:"); 141 Scanner sc = new Scanner (System.in); 142 String pwd = sc.next(); 143 144 if (pwd.equals(oldPwd)){ 145 //老密碼正確; 146 System.out.println("請輸入新密碼:"); 147 String newPwd_1= sc.next(); 148 System.out.println("請再次輸入新密碼:"); 149 String newPwd_2= sc.next(); 150 if (newPwd_1.equals(newPwd_2)){ 151 System.out.println("密碼修改成功!"); 152 return newPwd_1 ; 153 }else{ 154 System.out.println("兩次密碼不一致,請重新修改!"); 155 return oldPwd ; 156 } 157 }else { 158 System.out.println("舊密碼輸入錯誤,請重新修改!"); 159 } 160 return oldPwd ; 161 } 162 }