av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Java模擬實現ATM機

瀏覽:84日期:2022-08-15 11:37:03

Java模擬ATM機,供大家參考,具體內容如下

實現登錄,查詢,轉賬,取款,修改密碼,退出功能。

源碼

package bank;import java.io.*;import java.util.Scanner;//ATM類public class Atm { private String[] user;//用戶全部信息 private double money;//修改錢數 private double userMoney;//用戶的錢 private String newPassword; private String userInFo; private int index; private int a =0; private int count = 10; public void show(){//顯示界面 index = logIn(); if(index != -1){ working(); } } private String[] newStringUser(String[] str){ count=count+10; String[] newUser = new String[count]; for(int i=0;i<a;i++) newUser[i] = str[i]; return newUser; } private void getUser(){//從文件獲取全部用戶 String str; String[] strings = new String[count]; File file = new File('srcbankuser'); FileReader fileReader = null; BufferedReader bufferedReader = null; try{ fileReader = new FileReader(file); bufferedReader = new BufferedReader(fileReader); while((str = bufferedReader.readLine())!=null){ if(a<=count) strings[a++] = str; else strings = newStringUser(strings); } user = new String[a]; for(int i=0;i<a;i++) user[i] = strings[i]; strings = null; }catch(Exception e){ e.printStackTrace(); if((fileReader!=null)&&(bufferedReader!=null)){ try { bufferedReader.close(); fileReader.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } private int logIn(){//用戶登錄 getUser(); String name,password,user; String[] a; int i = 0; int number = -1; Scanner input = new Scanner(System.in); a:while(i<3){ System.out.println('請輸入用戶名:'); name = input.nextLine(); System.out.println('請輸入用戶密碼:'); password = input.nextLine(); user = name + '*' + password; for(int j=0;j<this.user.length;j++){ a = this.user[j].split('*'); userInFo = a[0]+'*'+a[1]; if(userInFo.equals(user)){ number = j; break a; } } i++; System.out.println('賬戶或密碼錯誤請重新輸入。。。'); } if(number!=-1){ System.out.println('登錄成功'); try{ Thread.sleep(1000); }catch(Exception e){ e.printStackTrace(); } } else System.out.println('您已輸入錯誤三次,卡已被吞!請到銀行柜臺詢問!'); return number; } private int anthorLogin(){//查詢轉賬用戶是否存在 Scanner input = new Scanner(System.in); String antherUserName; String[] a; int x=-1; System.out.println('請輸入要轉賬的用戶名:'); antherUserName = input.nextLine(); for(int i=0;i<user.length;i++){ a = this.user[i].split('*'); if(a[0].equals(antherUserName)){ x=i; break; } } return x; } private void show1(){ System.out.println('**********************'); System.out.println('t歡迎使用ATM'); System.out.println('1,賬戶余額查詢n2,存錢n3,取錢n4,轉賬n5,修改用戶密碼n6,退出系統n'); System.out.println('**********************'); } private void changeUser(int x){//改變用戶數組里的數據 String[] str = user[index].split('*'); if(x==1) user[index] = str[0]+'*'+newPassword+'*'+str[2]; else user[index] = str[0]+'*'+str[1]+'*'+userMoney; } private void working(){//atm辦理的業務 String number; setMoney(); do{ show1(); System.out.println('請輸入要辦理的業務序號:'); Scanner input = new Scanner(System.in); number = input.nextLine(); switch(number){ case '1': look(); break; case '2': saveMoney(); break; case '3': getMoney(); break; case '4': giveMoney(); break; case '5': changePassword(); break; case '6': System.out.println('歡迎下次光臨!'); write(); break; default: System.out.println('您輸入有誤,請重新輸入。。。。'); } }while(!number.equals('6')); } private void setMoney(){ String u = user[index]; userMoney = Double.parseDouble(u.split('*')[2]); } private void look(){//辦理查看余額業務 System.out.println('用戶余額為:'+userMoney); try{ Thread.sleep(2000); }catch(Exception e){ e.printStackTrace(); } } private void saveMoney(){//辦理存錢業務 money = howMuch('存錢'); userMoney = userMoney+money; changeUser(2); look(); if(isContinue()) saveMoney(); } private void getMoney(){//辦理取錢業務 money = howMuch('取錢'); if(money <= userMoney){ userMoney = userMoney-money; changeUser(2); look(); if(isContinue()) getMoney(); } else System.out.println('您的余額不足!'); } private void giveMoney(){//辦理轉賬業務 int anthorIndex = anthorLogin(); if(anthorIndex!=-1){ money = howMuch('轉賬'); if(money <= userMoney){ userMoney = userMoney - money; changeUser(2); String anthorUser = user[anthorIndex]; String[] str =anthorUser.split('*'); double money1 = Double.parseDouble(str[2]); money = money + money1; user[anthorIndex] = str[0]+'*'+str[1]+'*'+money; System.out.println('轉賬成功!'); look(); } else System.out.println('您的余額不足!'); } else System.out.println('該用戶不存在。。。。'); } private double howMuch(String str){ System.out.println('歡迎辦理'+str+'業務。。。。。。'); System.out.println('請輸入金額(只能是整數且是100的倍數,最多為10000):'); Scanner input = new Scanner(System.in); double money = input.nextDouble(); if(money%10==0) return money; else{ System.out.println('您輸入有誤!'); return 0.0; } } private void changePassword(){//辦理修改密碼業務 System.out.println('請輸入新密碼:'); Scanner input = new Scanner(System.in); newPassword = input.nextLine(); changeUser(1); System.out.println('密碼修改成功!'); } private boolean isContinue(){ System.out.println('是否繼續辦理該項業務?(請輸入Y(y)/N(n))'); Scanner input = new Scanner(System.in); String str = input.nextLine(); if(str.equalsIgnoreCase('y')) return true; else return false; } private void write(){ String str = ''; String s; for(int i=0;i<user.length;i++){ s = user[i]; if(i!=user.length-1) str = str + s + 'n'; else str = str + s; } File file = new File('srcbankuser'); FileWriter out = null; try { out = new FileWriter(file); out.write(str); out.flush(); } catch (IOException e) { e.printStackTrace(); }finally{ if(out != null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }}

package bank;//銀行類public class Bank { private Atm atm = new Atm(); public void welcome(User user){ System.out.println('歡迎使用atm'); user.useAtm(atm); }}

package bank;//用戶類public class User { public void useAtm(Atm atm){ atm.show(); }}

//創建user文件當數據庫張三*456*100.0李四*123*300.0王五*789*200.0

package bank;//測試類public class Text { public static void main(String[] args){ Bank bank =new Bank(); User user = new User(); bank.welcome(user); }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 亚洲视频精品 | 黑丝一区 | 中文字幕一区二区三区在线观看 | 国产成人一区二区三区 | 日本中文字幕网站 | a免费视频| 国产97视频| 精品视频久久 | 香蕉视频色版 | 日韩欧美黄色 | 中文字幕免费高清 | 欧美日韩免费视频 | 中文字幕第一区综合 | 国产精品二区一区二区aⅴ污介绍 | 成人深夜| 91亚洲国产成人久久精品麻豆 | 黄色一级视频网站 | 人人草在线视频 | 日韩激情网站 | 艳妇诱春(第5部分)(h) | 国产又粗又猛又爽又黄 | 欧美顶级黄色大片免费 | 午夜大片 | 久久精品福利 | 日韩免费精品视频 | 国产精品久久久999 成人在线国产 | 国产精品久久久久久久久免费桃花 | 91观看| 国产浮力第一页 | 在线一区二区视频 | 久久久久久中文字幕 | 成人福利视频 | 三级视频在线 | 久久精品视 | 精品日韩一区二区三区 | 精品久久久久久久久久久 | 亚洲精品999 | 91福利在线视频 | 成人精品三级av在线看 | 国产高清在线观看 | 成人三级视频在线观看 |