标签:style blog http color ar os 使用 java sp
该程序主要是模仿ATM机的过程主要是为了练习面向对象
主要有三个类 ATM和 Bank和 Account 类
先粘贴代码如下
ATM
1 package com.hpu.bai; 2 import java.util.Scanner; 3 public class ATM { 4 Bank bank = Bank.getBank(); 5 Scanner input = new Scanner(System.in); 6 Account nowAcc ; 7 private boolean answer(String s){ 8 if("Y".equals(s)){ 9 return true; 10 } 11 return false; 12 } 13 public void start(){ 14 System.out.println("欢迎使用自助终端"); 15 System.out.println("-----------"); 16 System.out.println("1.开户"); 17 System.out.println("2.插卡"); 18 System.out.println("3.系统登录"); 19 System.out.println("4.退出系统"); 20 System.out.println("-----------"); 21 System.out.println("请输入你要操作的选项"); 22 String index = input.next(); 23 if("1".equals(index)){ 24 //创建新账户 25 creatNew(); 26 }else if("2".equals(index)){ 27 //登录账户 28 login(); 29 }else if("3".equals(index)){ 30 //系统管理 31 adminLogin(); 32 }else if("4".equals(index)){ 33 //退出系统 34 System.out.println("谢谢你的使用"); 35 System.exit(0);; 36 }else { 37 //System.out.println("你的输入有误,请重新输入"); 38 } 39 start(); 40 } 41 private void adminLogin() { 42 System.out.println("请输入管理员密码"); 43 String pwd = input.next(); 44 if(pwd.equals("admin")){ 45 adminMenu(); 46 }else{ 47 System.out.println("密码错误"); 48 } 49 } 50 private void adminMenu() { 51 System.out.println("管理员在线"); 52 System.out.println("-------------"); 53 System.out.println("1.显示所有账户"); 54 System.out.println("2.冻结账户"); 55 System.out.println("3.恢复账户"); 56 System.out.println("4.安全退出"); 57 System.out.println("-------------"); 58 String index = input.next(); 59 if("1".equals(index)){ 60 //显示所用账户 61 allAcc(); 62 }else if("2".equals(index)){ 63 //冻结账户 64 disableAcc(); 65 }else if("3".equals(index)){ 66 //恢复账户 67 enableAcc(); 68 }else if("4".equals(index)){ 69 //安全退出 70 System.out.println("你已经安全退出"); 71 start(); 72 } 73 adminMenu(); 74 } 75 private void enableAcc() { 76 System.out.println("当前不可用账户"); 77 Account[] accs = bank.getAccounts(); 78 for (Account account : accs) { 79 if(account != null && !account.isStatus()){ 80 System.out.println("账户名称\t是否可用"); 81 System.out.println(account.getName()+"\t"+(account.isStatus()?"是":"否")); 82 } 83 } 84 System.out.println("请输入你要恢复的账户名称"); 85 String name = input.next(); 86 Account disAcc = bank.getAccountByName(name); 87 if(disAcc != null && !disAcc.isStatus()){ 88 System.out.println("确定要恢复该账户(Y/N)"); 89 if(answer(input.next())){ 90 disAcc.setStatus(true); 91 System.out.println("恢复成功"); 92 }else{ 93 System.out.println("该账户未被恢复"); 94 allAcc(); 95 } 96 }else{ 97 System.out.println("输入有误"); 98 } 99 } 100 101 private void disableAcc() { 102 System.out.println("当前可用账户"); 103 Account[] accs = bank.getAccounts(); 104 for (Account account : accs) { 105 if(account != null && account.isStatus()){ 106 System.out.println("账户名称\t是否可用"); 107 System.out.println(account.getName()+"\t"+(account.isStatus()?"是":"否")); 108 } 109 } 110 System.out.println("请输入你要冻结的账户名称"); 111 String name = input.next(); 112 Account disAcc = bank.getAccountByName(name); 113 if(disAcc != null && disAcc.isStatus()){ 114 System.out.println("确定要冻结该账户(Y/N)"); 115 if(answer(input.next())){ 116 disAcc.setStatus(false); 117 System.out.println("冻结成功"); 118 }else{ 119 System.out.println("该账户未被冻结"); 120 allAcc(); 121 } 122 }else{ 123 System.out.println("输入有误"); 124 } 125 } 126 127 private void allAcc() { 128 Account[] accs = bank.getAccounts(); 129 for (Account account : accs) { 130 if(account != null){ 131 System.out.println("账户名称\t是否可用"); 132 System.out.println(account.getName()+"\t"+(account.isStatus()?"是":"否")); 133 } 134 } 135 136 } 137 private void login() { 138 System.out.println("请输入你的账户名字"); 139 String name = input.next(); 140 nowAcc = bank.getAccountByName(name); 141 if(nowAcc == null){ 142 System.out.println("你输入的账户不存在,请重新输入"); 143 login(); 144 }else{ 145 System.out.println("请输入账户密码"); 146 String pwd = input.next(); 147 if(nowAcc.getPassword().equals(pwd)){ 148 System.out.println("登录成功"); 149 userMenu(); 150 }else{ 151 System.out.println("你输入的密码错误,请重新输入"); 152 } 153 } 154 155 } 156 private void userMenu() { 157 System.out.println("欢迎"+nowAcc.getName()+"的到来"); 158 System.out.println("-----------"); 159 System.out.println("1.查询余额"); 160 System.out.println("2.存款"); 161 System.out.println("3.取款"); 162 System.out.println("4.转账"); 163 System.out.println("5.修改密码"); 164 System.out.println("6.退出登录"); 165 System.out.println("-----------"); 166 System.out.println("请输入你要操作的选项"); 167 String index = input.next(); 168 if("1".equals(index)){ 169 //查询余额 170 queryMoney(); 171 172 }else if("2".equals(index)){ 173 //存款 174 saveMoney(); 175 }else if("3".equals(index)){ 176 //取款 177 drawMoney(); 178 }else if("4".equals(index)){ 179 //转账 180 transMoney(); 181 }else if("5".equals(index)){ 182 //修改密码 183 rePwd(); 184 }else if("6".equals(index)){ 185 start(); 186 }else{ 187 System.out.println("你的输入有误,请重新输入"); 188 } 189 userMenu(); 190 } 191 192 private void rePwd() { 193 System.out.println("请输入原始密码"); 194 String pwd = input.next(); 195 if(nowAcc.getPassword().equals(pwd)){ 196 System.out.println("请输入新密码"); 197 String pwd1 = input.next(); 198 System.out.println("请再次输入新密码"); 199 String pwd2 = input.next(); 200 if(pwd1.equals(pwd2)){ 201 nowAcc.setPassword(pwd1); 202 System.out.println("修改密码成功"); 203 }else{ 204 System.out.println("两次密码输入不一样"); 205 rePwd(); 206 } 207 } 208 209 } 210 private void transMoney() { 211 System.out.println("请输入你要转入的账户名字"); 212 String name = input.next(); 213 if(nowAcc.getName().equals(name)){ 214 System.out.println("不能够给自己转账,请重新输入"); 215 }else{ 216 Account transAcc = bank.getAccountByName(name); 217 if(transAcc == null){ 218 System.out.println("你输入的账户不存在"); 219 }else{ 220 System.out.println("请输入你要转入的金额"); 221 Float money = input.nextFloat(); 222 if(money > 0){ 223 if(money < nowAcc.getMoney()){ 224 System.out.println("请确保资金安全,你确实要转账(Y/N)"); 225 if(answer(input.next())){ 226 nowAcc.setMoney(nowAcc.getMoney()-money); 227 transAcc.setMoney(transAcc.getMoney()+money); 228 System.out.println("转账成功"); 229 queryMoney(); 230 } 231 }else{ 232 System.out.println("余额不足,请重新输入"); 233 } 234 }else{ 235 System.out.println("你输入的金额有误"); 236 } 237 238 } 239 } 240 } 241 private void drawMoney() { 242 //queryMoney(); 243 System.out.println("请输入你要取的金额"); 244 Float money = input.nextFloat(); 245 if(money > 0){ 246 if(money > nowAcc.getMoney()){ 247 System.out.println("当前余额不足"); 248 drawMoney(); 249 }else{ 250 nowAcc.setMoney(nowAcc.getMoney()-money); 251 System.out.println("取款成功"); 252 queryMoney(); 253 } 254 }else{ 255 System.out.println("你输入的金额有误,请重新输入"); 256 } 257 } 258 259 private void saveMoney() { 260 System.out.println("请输入你要存入的金额"); 261 Float money = input.nextFloat(); 262 if(money>0){ 263 nowAcc.setMoney(nowAcc.getMoney()+money); 264 System.out.println("存款成功"); 265 queryMoney(); 266 }else{ 267 System.out.println("你输入的金额有误,请重新输入"); 268 } 269 } 270 private void queryMoney() { 271 System.out.println("你当前的余额为¥: "+nowAcc.getMoney()); 272 userMenu(); 273 } 274 private void creatNew() { 275 //验证账户是否可用 276 System.out.println("请输入你所开账户的名字"); 277 String name = input.next(); 278 Account acc = bank.getAccountByName(name); 279 if(acc == null){ 280 //账户可用后提示可以输入密码 281 System.out.println("检测你输入的账户可用"); 282 System.out.println("请输入你要设置的密码"); 283 String pwd1 = input.next(); 284 System.out.println("请再次输入你要设置的密码"); 285 String pwd2 = input.next(); 286 if(pwd1.equals(pwd2)){ 287 Account newAcc = new Account(); 288 newAcc.setName(name); 289 newAcc.setPassword(pwd2); 290 System.out.println("你是否要存入现金(Y/N)"); 291 if(answer(input.next())){ 292 System.out.println("请输入你要存入的金额"); 293 Float money = input.nextFloat(); 294 newAcc.setMoney(money); 295 }else{ 296 newAcc.setMoney(0F); 297 } 298 newAcc.setStatus(true); 299 //要将新开好的账户传输到银行账户当中去 300 if(bank.accSave(newAcc)){ 301 System.out.println("开户成功,请登录"); 302 }else{ 303 System.out.println("服务器忙,请稍后"); 304 } 305 }else{ 306 System.out.println("你两次输入的密码不同,请重新输入"); 307 creatNew(); 308 } 309 }else{ 310 System.out.println("你所开户的名字已重复,请重新输入开户名字"); 311 creatNew(); 312 } 313 } 314 }
Bank类
1 package com.hpu.bai; 2 3 public class Bank { 4 private Bank(){} 5 private static Bank bank = null; 6 public static Bank getBank() { 7 if(bank == null){ 8 bank = new Bank(); 9 } 10 return bank; 11 } 12 13 public void setAccounts(Account[] accounts) { 14 this.accounts = accounts; 15 } 16 private Account[] accounts = new Account[100]; 17 18 public Account[] getAccounts() { 19 return accounts; 20 } 21 public Account getAccountByName(String name){ 22 for (int i = 0; i < accounts.length; i++) { 23 if(accounts[i] != null && accounts[i].getName().equals(name)){ 24 return accounts[i]; 25 } 26 } 27 return null; 28 } 29 public boolean accSave(Account a){ 30 for (int i = 0; i < accounts.length; i++) { 31 if(accounts[i]== null){ 32 accounts[i] = a; 33 return true; 34 } 35 } 36 return false; 37 } 38 }
Account类
1 package com.hpu.bai; 2 3 public class Account { 4 private String name; 5 private String password; 6 private Float money ; 7 private boolean status; 8 public String getName() { 9 return name; 10 } 11 public void setName(String name) { 12 this.name = name; 13 } 14 public String getPassword() { 15 return password; 16 } 17 public void setPassword(String password) { 18 this.password = password; 19 } 20 public Float getMoney() { 21 return money; 22 } 23 public void setMoney(Float money) { 24 this.money = money; 25 } 26 public boolean isStatus() { 27 return status; 28 } 29 public void setStatus(boolean status) { 30 this.status = status; 31 } 32 33 }
最后还有个test类
1 package com.hpu.bai; 2 3 public class Test { 4 public static void main(String[] args) { 5 ATM atm = new ATM(); 6 atm.start(); 7 } 8 }
标签:style blog http color ar os 使用 java sp
原文地址:http://www.cnblogs.com/neruda/p/4090959.html