Java模擬實(shí)現(xiàn)ATM機(jī)
更新時(shí)間:2021年03月22日 11:03:06 作者:點(diǎn)丶錯(cuò)了。。
這篇文章主要為大家詳細(xì)介紹了Java模擬實(shí)現(xiàn)ATM機(jī),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
Java模擬ATM機(jī),供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)登錄,查詢,轉(zhuǎn)賬,取款,修改密碼,退出功能。
源碼
package bank; import java.io.*; import java.util.Scanner; //ATM類 public class Atm { private String[] user;//用戶全部信息 private double money;//修改錢數(shù) 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("src\\bank\\user"); 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("請(qǐng)輸入用戶名:"); name = input.nextLine(); System.out.println("請(qǐng)輸入用戶密碼:"); 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("賬戶或密碼錯(cuò)誤請(qǐng)重新輸入。。。"); } if(number!=-1){ System.out.println("登錄成功"); try{ Thread.sleep(1000); }catch(Exception e){ e.printStackTrace(); } } else System.out.println("您已輸入錯(cuò)誤三次,卡已被吞!請(qǐng)到銀行柜臺(tái)詢問!"); return number; } private int anthorLogin(){//查詢轉(zhuǎn)賬用戶是否存在 Scanner input = new Scanner(System.in); String antherUserName; String[] a; int x=-1; System.out.println("請(qǐng)輸入要轉(zhuǎn)賬的用戶名:"); 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,轉(zhuǎn)賬\n5,修改用戶密碼\n6,退出系統(tǒng)\n"); System.out.println("**********************"); } private void changeUser(int x){//改變用戶數(shù)組里的數(shù)據(jù) 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辦理的業(yè)務(wù) String number; setMoney(); do{ show1(); System.out.println("請(qǐng)輸入要辦理的業(yè)務(wù)序號(hào):"); 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("您輸入有誤,請(qǐng)重新輸入。。。。"); } }while(!number.equals("6")); } private void setMoney(){ String u = user[index]; userMoney = Double.parseDouble(u.split("\\*")[2]); } private void look(){//辦理查看余額業(yè)務(wù) System.out.println("用戶余額為:"+userMoney); try{ Thread.sleep(2000); }catch(Exception e){ e.printStackTrace(); } } private void saveMoney(){//辦理存錢業(yè)務(wù) money = howMuch("存錢"); userMoney = userMoney+money; changeUser(2); look(); if(isContinue()) saveMoney(); } private void getMoney(){//辦理取錢業(yè)務(wù) money = howMuch("取錢"); if(money <= userMoney){ userMoney = userMoney-money; changeUser(2); look(); if(isContinue()) getMoney(); } else System.out.println("您的余額不足!"); } private void giveMoney(){//辦理轉(zhuǎn)賬業(yè)務(wù) int anthorIndex = anthorLogin(); if(anthorIndex!=-1){ money = howMuch("轉(zhuǎn)賬"); 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("轉(zhuǎn)賬成功!"); look(); } else System.out.println("您的余額不足!"); } else System.out.println("該用戶不存在。。。。"); } private double howMuch(String str){ System.out.println("歡迎辦理"+str+"業(yè)務(wù)。。。。。。"); System.out.println("請(qǐng)輸入金額(只能是整數(shù)且是100的倍數(shù),最多為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(){//辦理修改密碼業(yè)務(wù) System.out.println("請(qǐng)輸入新密碼:"); Scanner input = new Scanner(System.in); newPassword = input.nextLine(); changeUser(1); System.out.println("密碼修改成功!"); } private boolean isContinue(){ System.out.println("是否繼續(xù)辦理該項(xiàng)業(yè)務(wù)?(請(qǐng)輸入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("src\\bank\\user"); 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(); } }
//創(chuàng)建user文件當(dāng)數(shù)據(jù)庫 張三*456*100.0 李四*123*300.0 王五*789*200.0
package bank; //測(cè)試類 public class Text { public static void main(String[] args){ Bank bank =new Bank(); User user = new User(); bank.welcome(user); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- java實(shí)現(xiàn)銀行ATM管理系統(tǒng)
- Java實(shí)現(xiàn)基礎(chǔ)銀行ATM系統(tǒng)
- Java基于控制臺(tái)界面實(shí)現(xiàn)ATM系統(tǒng)
- Java簡(jiǎn)單實(shí)現(xiàn)銀行ATM系統(tǒng)
- 用Java實(shí)現(xiàn)簡(jiǎn)單ATM機(jī)功能
- java GUI實(shí)現(xiàn)ATM機(jī)系統(tǒng)(3.0版)
- java實(shí)現(xiàn)ATM機(jī)系統(tǒng)(2.0版)
- Java項(xiàng)目實(shí)現(xiàn)模擬ATM機(jī)
- Java使用嵌套循環(huán)模擬ATM機(jī)取款業(yè)務(wù)操作示例
- Java實(shí)現(xiàn)ATM機(jī)操作系統(tǒng)
相關(guān)文章
使用nacos實(shí)現(xiàn)自定義文本配置的實(shí)時(shí)刷新
我們都知道,使用Nacos時(shí),如果將Bean使用@RefreshScope標(biāo)注之后,這個(gè)Bean中的配置就會(huì)做到實(shí)時(shí)刷新,本文給大家介紹了如何使用nacos實(shí)現(xiàn)自定義文本配置的實(shí)時(shí)刷新,需要的朋友可以參考下2024-05-05詳解SpringMVC 基礎(chǔ)教程 簡(jiǎn)單入門實(shí)例
這篇文章主要介紹了詳解SpringMVC 基礎(chǔ)教程 簡(jiǎn)單入門實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12LeetCode -- Path Sum III分析及實(shí)現(xiàn)方法
這篇文章主要介紹了LeetCode -- Path Sum III分析及實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10Spring MVC通過添加自定義注解格式化數(shù)據(jù)的方法
這篇文章主要給大家介紹了關(guān)于Spring MVC通過添加自定義注解格式化數(shù)據(jù)的方法,文中先對(duì)springmvc 自定義注解 以及自定義注解的解析進(jìn)行了詳細(xì)的介紹,相信會(huì)對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11jvm中指定時(shí)區(qū)信息user.timezone問題及解決方式
同一份程序使用時(shí)間LocalDateTime類型,在國(guó)內(nèi)和國(guó)外部署后,返回的時(shí)間信息前端使用出問題,這篇文章主要介紹了jvm中指定時(shí)區(qū)信息user.timezone問題及解決方法,需要的朋友可以參考下2023-02-02