本次Java测试主要是做一个与ATM相似的系统,用文本文件来作为用户数据库,实现存款、取款、转账、修改密码、查询余额的功能。
在做这次测试之前老师并没有讲解与Java相关的知识,所以这就需要我们自学Java,考验我们的自学能力的同时,也检测了我们的自学成果。
首先,先定义一个账户类,里面包含账号、姓名、操作时间、流水金额、密码、操作类型、账户余额成员,还有各个成员的设置、得到函数。
import java.util.Scanner; import java.io.*; import java.io.IOException; import java.nio.file.Paths; public class Account { private String accountID; private String accountname; private String operatedate;//操作时间 private int operatetype;//存储操作 private String accountpassword; private int accountbalance;//储存账户余额 private int amount; public void setaccountID(String a) { accountID = a; } public void setaccountname(String a) { accountname = a; } public void setoperatedate(String a) { operatedate = a; } public void setoperatetype(int a) { operatetype = a; } public void setaccountpassword(String a) { accountpassword = a; } public void setaccountbalance(int a) { accountbalance = a; } public void setamount(int a) { amount = a; } public String getaccountID() { return accountID; } public String getaccountname() { return accountname; } public String getoperatedate() { return operatedate; } public int getoperatetype() { return operatetype; } public String getaccountpassword() { return accountpassword; } public int getaccountbalance() { return accountbalance; } public int getamount() { return amount; } public Account() { } }
然后定义另一个类,用来存放其他函数和主函数及类AccountManage,在做测试时要用到文件流的输入和输出,因为以前在学习Java时没有看到也没有学到有关文件流的输入和输出之类的东西,所以只能在做测试时,现场去上网查询,简单掌握了Scanner类中的文件流,然后再类中写了两个函数分别用来将accountinformation.txt和accountlist.txt中的数据读入到对象中,还有两个函数分别用来将操作或修改后的对象数据写入到accountinformation.txt和accountlist.txt两个文件中。主函数中,函数主体是do循环语句里套上switch语句,do循环语句用来反复执行任务,switch语句用来进行任务的选择。在写程序时,发现Java里不能直接进行字符串与字符串之间或字符与字符之间的比较(就是不可以用“==“号来比较),如果想要进行比较,则需要调用函数来进行比较(如a.compareTo(b) == 0,及比较a、b是否相等)。循环执行switch语句并在执行一个case后选择下一个执行任务后,进行switch后数字的修改,然后继续循环执行。虽然写完了程序,但是将修改或执行后的对象数据写入文件的功能还没有实现,在接下来的时间我会将其完善。
1 class AccountManager{ 2 static Account in[] =new Account[5]; 3 4 static void infile1(Account a[])throws IOException{ //对文件1中的用户信息进行录入 5 Scanner in = new Scanner(Paths.get("accountinformation.txt")); 6 int i=0; 7 while(in.hasNextLine()) { 8 a[i]=new Account(); 9 String data1 = in.next(); 10 String data2 = in.next(); 11 String data3 = in.next(); 12 int data4=in.nextInt(); 13 a[i].setaccountID(data1); 14 a[i].setaccountname(data2); 15 a[i].setaccountpassword(data3); 16 a[i].setaccountbalance(data4); 17 i++; 18 } 19 in.close(); 20 } 21 static void infile2(Account a[])throws IOException{ //对文件2中的用户信息进行录入 22 Scanner in = new Scanner(Paths.get("accountlist.txt")); 23 int i=0; 24 while(in.hasNextLine()) { 25 a[i]=new Account(); 26 String data1 = in.next(); 27 String data2 = in.next(); 28 String data3 = in.next(); 29 int data4=in.nextInt(); 30 int data5=in.nextInt(); 31 a[i].setaccountID(data1); 32 a[i].setaccountname(data2); 33 a[i].setoperatedate(data3); 34 a[i].setoperatetype(data4); 35 a[i].setamount(data5); 36 i++; 37 } 38 in.close(); 39 } 40 static void outfile1(Account a[],int len) throws IOException { //将信息进行输入到文件1中 41 PrintWriter out = new PrintWriter("accountinformation.txt"); 42 int i = 0; 43 for(i=0;i<len-1;i++) { 44 if(a[i]!=null)out.print(a[i].getaccountID()+"\n"+a[i].getaccountname()+"\n"+a[i].getaccountpassword()+"\n"+a[i].getaccountbalance()+"\n"); 45 } 46 out.flush(); 47 out.close(); 48 } 49 void outfile2(Account a[],int len) throws IOException { //将信息进行输入到文件2中 50 PrintWriter out = new PrintWriter("accountinformation.txt"); 51 int i = 0; 52 for(i=0;i<len-1;i++) { 53 if(a[i]!=null)out.print(a[i].getaccountID()+"\n"+a[i].getaccountname()+"\n"+a[i].getoperatedate()+"\n"+a[i].getoperatetype()+"\n"+a[i].getamount()+"\n"); 54 } 55 out.flush(); 56 out.close(); 57 } 58 59 60 public static void main(String args[]) throws IOException{ 61 infile1(in); 62 infile2(in); 63 for(int i = 0;i < 5; i++) { 64 in[i].setaccountpassword("123"); 65 } 66 Scanner sc =new Scanner(System.in); 67 int j = 1; 68 int g = 0; 69 do { 70 switch(j) { 71 case 1:{ 72 do { 73 System.out.println("**************************************************"); 74 System.out.println("欢迎使用中国工商银行自动柜员系统"); 75 System.out.println("**************************************************"); 76 System.out.println("请输入您的账号:"); 77 System.out.println("**************************************************"); 78 //添加输入 79 String accid; 80 accid = sc.nextLine(); 81 int temp1 = 0; 82 int temp2 = 0; 83 int temp3 = 0; 84 for(int i = 0; i < 5; i++) { 85 if((in[i].getaccountID().compareTo(accid) != 0) && (accid.length() == in[i].getaccountID().length())) { 86 temp1++; 87 } 88 } 89 if(temp1 == 5) 90 System.out.println("该账号不存在" + "\n"); 91 for(int i = 0; i < 5; i++) { 92 if((in[i].getaccountID().compareTo(accid) != 0) && (accid.length() != in[i].getaccountID().length())) { 93 temp2 = 1; 94 } 95 } 96 if(temp2 == 1) 97 System.out.println("该卡不是工行卡" + "\n"); 98 for(int i = 0; i < 5; i++) { 99 if(in[i].getaccountID().compareTo(accid) == 0) { 100 j = 2; 101 g = i; 102 } 103 } 104 } while(j != 2); 105 } 106 break; 107 case 2:{ 108 if(j == 2) { 109 int k = 0; 110 do { 111 int temp = 0; 112 System.out.println("*********************************************************"); 113 System.out.println("欢迎" + in[g].getaccountID() + "使用中国工商银行自助柜员系统"); 114 System.out.println("*********************************************************"); 115 System.out.println("请输入您的密码:"); 116 System.out.println("*********************************************************"); 117 //添加输入 118 String accpass; 119 accpass = sc.nextLine(); 120 for(int i = 0; i < 5; i++) { 121 if((in[i].getaccountpassword().compareTo(accpass) == 0)&&(in[i] != null)) { 122 j = 3; 123 } 124 else if((in[i].getaccountpassword().compareTo(accpass) != 0)&&(in[i] != null)) { 125 temp++; 126 } 127 } 128 if(temp != 0) { 129 System.out.println("密码录入错误" + "\n"); 130 k++; 131 } 132 } while((k != 3) && (j != 3)); 133 if(k == 3) { 134 System.out.println("该账号三次录入密码错误,该卡已被系统没收,请与工行及时联系处理" + "\n"); 135 j = 1; 136 } 137 } 138 } 139 break; 140 case 3:{ 141 if(j == 3) { 142 int f = 0; 143 String temp2; 144 String temp1; 145 System.out.println("***************************************************************\r\n" + 146 "欢迎"+in[g].getaccountID()+"使用中国工商银行自助柜员系统\r\n" + 147 "****************************************************************\r\n" + 148 "1、存款;\r\n" + 149 "2、取款;\r\n" + 150 "3、转账汇款;\r\n" + 151 "4、修改密码;\r\n" + 152 "5、查询余额;\r\n" + 153 "****************************************************************"); 154 f = sc.nextInt(); 155 switch(f) { 156 case 1:{ 157 int a; 158 int b; 159 int c; 160 do { 161 System.out.println("***************************************************************\r\n" + 162 "欢迎"+in[g].getaccountID()+"使用中国工商银行自助柜员系统\r\n" + 163 "****************************************************************\r\n" + 164 "请输入存款金额;\r\n" + 165 " \r\n" + 166 "****************************************************************"); 167 a = sc.nextInt(); 168 c = a + in[g].getamount(); 169 b = a % 1; 170 if(b == 0) 171 in[g].setamount(c); 172 else 173 System.out.println("输入金额有误"); 174 break; 175 } while(true); 176 System.out.println("输入字母“q”,则返回系统输入账号界面;输入正整数,则跳转"); 177 178 temp2 = sc.nextLine(); 179 if(temp2.compareTo("q") == 0) { 180 j =1; 181 break; 182 } 183 else { 184 System.out.println("***************************************************************\r\n" + 185 "欢迎"+in[g].getaccountID()+"使用中国工商银行自助柜员系统\r\n" + 186 "****************************************************************\r\n" + 187 "当前账户存款操作成功。\r\n" + 188 "当前账户余额为:"+in[g].getamount()+"\r\n" + 189 "****************************************************************"); 190 System.out.println("输入字母“q”,则返回系统输入账号界面"); 191 } 192 temp1 = sc.nextLine(); 193 if(temp1.compareTo("q") == 0) { 194 j = 1; 195 } 196 197 } 198 break; 199 case 2:{ 200 System.out.println("***************************************************************\r\n" + 201 "欢迎"+in[g].getaccountID()+"使用中国工商银行自助柜员系统\r\n" + 202 "****************************************************************"+ 203 "当前账户每日可以支取2万元。\r\n" + 204 "1、100元\r\n" + 205 "2、500元\r\n" + 206 "3、1000元\r\n" + 207 "4、1500元\r\n" + 208 "5、2000元\r\n" + 209 "6、5000元;\r\n" + 210 "7、其他金额\r\n" + 211 "8、退卡\r\n" + 212 "9、返回\r\n" + 213 "****************************************************************"); 214 int tt; 215 int qt; 216 int js = 0; 217 tt = sc.nextInt(); 218 int a[] = {100, 500, 1000, 1500, 2000, 5000}; 219 int c; 220 switch(tt) { 221 case 1:{ 222 if(in[g].getamount() < 100) { 223 System.out.println("账户余额不足"); 224 } 225 else 226 js = 1; 227 } 228 break; 229 case 2:{ 230 if(in[g].getamount() < 500) { 231 System.out.println("账户余额不足"); 232 } 233 else 234 js = 1; 235 } 236 break; 237 case 3:{ 238 if(in[g].getamount() < 1000) { 239 System.out.println("账户余额不足"); 240 } 241 else 242 js = 1; 243 } 244 break; 245 case 4:{ 246 if(in[g].getamount() < 1500) { 247 System.out.println("账户余额不足"); 248 } 249 else 250 js = 1; 251 } 252 break; 253 case 5:{ 254 if(in[g].getamount() < 2000) { 255 System.out.println("账户余额不足"); 256 } 257 else 258 js = 1; 259 } 260 break; 261 case 6:{ 262 if(in[g].getamount() < 5000) { 263 System.out.println("账户余额不足"); 264 } 265 else 266 js = 1; 267 } 268 break; 269 case 7:{ 270 System.out.println("***************************************************************\r\n" + 271 "欢迎"+in[g].getaccountID()+"使用中国工商银行自助柜员系统\r\n" + 272 "****************************************************************\r\n" + 273 "请输入取款金额:\r\n" + 274 "****************************************************************"); 275 qt = sc.nextInt(); 276 if(in[g].getamount() < in[g].getamount()) { 277 System.out.println("账户余额不足"); 278 } 279 else 280 js = 1; 281 } 282 break; 283 case 8:{ 284 j = 1; 285 } 286 break; 287 case 9:{ 288 j = 3; 289 } 290 break; 291 } 292 if(js == 1) { 293 c = in[g].getamount() - a[tt - 1]; 294 in[g].setamount(c); 295 System.out.println("***************************************************************\r\n" + 296 "欢迎"+in[g].getaccountID()+"使用中国工商银行自助柜员系统\r\n" + 297 "****************************************************************\r\n" + 298 "当前账户取款操作"+a[tt - 1]+"\r\n" + 299 "当前账户余额为:"+c+"\r\n" + 300 "****************************************************************"); 301 } 302 303 } 304 break; 305 case 3:{ 306 System.out.println("***************************************************************\r\n" + 307 "欢迎"+in[g].getaccountID()+"使用中国工商银行自助柜员系统\r\n" + 308 "****************************************************************\r\n" + 309 "请输入转账金额;\r\n" + 310 "****************************************************************"); 311 int zhuan; 312 String ss; 313 String zh; 314 String yn; 315 zhuan = sc.nextInt(); 316 if(in[g].getamount() < zhuan) { 317 System.out.println("账户余额不足"); 318 } 319 else { 320 System.out.println("输入被转账账户:"); 321 zh = sc.nextLine(); 322 System.out.println("***************************************************************\r\n" + 323 "欢迎"+in[g].getaccountID()+"使用中国工商银行自助柜员系统\r\n" + 324 "****************************************************************\r\n" + 325 "请确认是否向"+zh+"转账"+zhuan+"元。\r\n" + 326 "****************************************************************"); 327 System.out.println("输入“N”表示不确认转账,输入“Y”表示确认转账"); 328 yn = sc.nextLine(); 329 if(yn.compareTo("Y") == 0) { 330 for(int i = 0; i < 5; i++) { 331 if((in[i].getaccountID().compareTo(zh) == 0)&&(in[i] != null)) { 332 int rr; 333 int uu; 334 rr = in[i].getamount() + zhuan; 335 uu = in[g].getamount() - zhuan; 336 in[i].setamount(rr); 337 in[g].setamount(uu); 338 } 339 } 340 System.out.println("***************************************************************\r\n" + 341 "欢迎"+in[g].getaccountID()+"使用中国工商银行自助柜员系统\r\n" + 342 "****************************************************************\r\n" + 343 "当前账户向"+zh+"成功转账"+zhuan+"元。\r\n" + 344 "当前账户余额为:"+in[g].getamount()+"\r\n" + 345 "****************************************************************"); 346 System.out.println("输入字母“q”,则返回系统输入账号界面"); 347 ss = sc.nextLine(); 348 if(ss.compareTo("q") == 0) { 349 j = 1; 350 } 351 } 352 else if(yn.compareTo("N") == 0) { 353 j = 3; 354 } 355 } 356 } 357 break; 358 case 4:{ 359 int m = 0; 360 String ss; 361 do { 362 System.out.println("***************************************************************\r\n" + 363 "欢迎"+in[g].getaccountID()+"使用中国工商银行自助柜员系统\r\n" + 364 "****************************************************************\r\n" + 365 "请输入当前密码:\r\n" + 366 "请输入修改密码:\r\n" + 367 "请输入确认密码:\r\n"); 368 String x1, x2, x3; 369 x1 = sc.nextLine(); 370 x2 = sc.nextLine(); 371 x3 = sc.nextLine(); 372 if(in[g].getaccountpassword().compareTo(x1) != 0) { 373 System.out.println("当前密码录入错误"); 374 } 375 else if(x2.compareTo(x3) != 0) { 376 System.out.println("修改密码与确认密码不一致"); 377 } 378 else { 379 in[g].setaccountpassword(x2); 380 System.out.println("***************************************************************\r\n" + 381 "欢迎"+in[g].getaccountID()+"使用中国工商银行自助柜员系统\r\n" + 382 "****************************************************************\r\n" + 383 "当前账户密码修改成功\r\n" + 384 "****************************************************************"); 385 m = 1; 386 } 387 } while(m == 0); 388 System.out.println("输入字母“q”,则返回系统输入账号界面"); 389 ss = sc.nextLine(); 390 if(ss.compareTo("q") == 0) { 391 j = 1; 392 } 393 } 394 break; 395 case 5:{ 396 String ss; 397 System.out.println("***************************************************************\r\n" + 398 "欢迎"+in[g].getaccountID()+"使用中国工商银行自助柜员系统\r\n" + 399 "****************************************************************\r\n" + 400 "当前账户余额为:"+in[g].getamount()+"元"); 401 System.out.println("输入字母“q”,则返回系统输入账号界面"); 402 ss = sc.nextLine(); 403 if(ss.compareTo("q") == 0) { 404 j = 1; 405 } 406 } 407 break; 408 }//小 switch 409 } 410 } 411 break; 412 }//大 switch 413 } while(true);//大 do while 414 } 415 }