基于Java開發(fā)實(shí)現(xiàn)ATM系統(tǒng)
本文實(shí)例為大家分享了Java開發(fā)實(shí)現(xiàn)ATM系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
一.業(yè)務(wù)分析
通過使用Java面向?qū)ο蟮幕A(chǔ)知識,開發(fā)一個ATM系統(tǒng),實(shí)現(xiàn)存款,取款,轉(zhuǎn)賬,修改密碼,注銷賬戶等功能。
二.開發(fā)準(zhǔn)備
首先,創(chuàng)建一個用戶類,為建立用戶對象做準(zhǔn)備,用戶類主要包括用戶卡號(系統(tǒng)隨機(jī)生成),用戶名,賬戶密碼,余額,取現(xiàn)額度。并搭建構(gòu)造器,以及get,set。
public class user { ? ? private String cardId ; ? ? //卡號 ? ? private String username; ? ?//用戶名 ? ? private String password; ? ?//密碼 ? ? private double money; ? ? ? //余額 ? ? private double qumoney; ? ? //取現(xiàn)額度 ? ? ? ? public user(String cardId, String username, String password, ?double qumoney) { ? ? ? ? this.cardId = cardId; ? ? ? ? this.username = username; ? ? ? ? this.password = password; ? ? ? ? this.qumoney = qumoney; ? ? } ? ? ? public user() { ? ? } ? ? ? public String getCardId() { ? ? ? ? return cardId; ? ? } ? ? ? public void setCardId(String cardId) { ? ? ? ? this.cardId = cardId; ? ? } ? ? ? public String getUsername() { ? ? ? ? return username; ? ? } ? ? ? public void setUsername(String username) { ? ? ? ? this.username = username; ? ? } ? ? ? public String getPassword() { ? ? ? ? return password; ? ? } ? ? ? public void setPassword(String password) { ? ? ? ? this.password = password; ? ? } ? ? ? public double getMoney() { ? ? ? ? return money; ? ? } ? ? ? public void setMoney(double money) { ? ? ? ? this.money = money; ? ? } ? ? ? public double getQumoney() { ? ? ? ? return qumoney; ? ? } ? ? ? public void setQumoney(double qumoney) { ? ? ? ? this.qumoney = qumoney; ? ? } }
三.創(chuàng)建測試類,寫入main方法
public class ATMSystem { ? ? public static void main(String[] args) { ? ? ? ? ArrayList<user> users = new ArrayList<>(); ? ? ? ? Main(users); ? ? }
四.將主界面設(shè)置成Main方法,設(shè)計(jì)主界面,包括登錄賬戶,注冊賬戶,并且設(shè)置登錄方法,注冊方法,再調(diào)用。
public static void Main(ArrayList<user> users) { ? ? ? ? System.out.println("==========歡迎進(jìn)入xx銀行ATM系統(tǒng)=========="); ? ? ? ? while (true) { ? ? ? ? ? ? System.out.println("請選擇操作:"); ? ? ? ? ? ? System.out.println("1.登錄賬戶"); ? ? ? ? ? ? System.out.println("2.注冊賬戶"); ? ? ? ? ? ? Scanner sc = new Scanner(System.in); ? ? ? ? ? ? int command = sc.nextInt(); ? ? ? ? ? ? switch (command) { ? ? ? ? ? ? ? ? case 1: ? ? ? ? ? ? ? ? //登錄 ? ? ? ? ? ? ? ? ? ? denglu(users, sc); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case 2: ? ? ? ? ? ? ? ? //注冊 ? ? ? ? ? ? ? ? ? ? zhuce(users, sc); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? System.out.println("功能不存在!"); ? ? ? ? ? ? } ? ? ? ? } ? ? }
運(yùn)行結(jié)果:
五.將注冊賬戶寫成方法
public static void zhuce(ArrayList<user> users, Scanner sc) { ? ? ? ? System.out.println("==========注冊賬戶=========="); ? ? ? ? String password = ""; ? ? ? ? String password2 = ""; ? ? ? ? System.out.println("請輸入您的賬號名稱:"); ? ? ? ? sc = new Scanner(System.in); ? ? ? ? String name = sc.next(); ? ? ? ? while (true) { ? ? ? ? ? ? System.out.println("請輸入您的賬號密碼:"); ? ? ? ? ? ? password = sc.next(); ? ? ? ? ? ? System.out.println("請您再次輸入密碼:"); ? ? ? ? ? ? password2 = sc.next(); ? ? ? ? ? ? if (password2.equals(password)) { ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? System.out.println("兩次密碼輸入不一致!,請重新輸入"); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? //生成卡號 ? ? ? ? System.out.println("請輸入賬號當(dāng)次限額"); ? ? ? ? double qumoney = sc.nextDouble(); ? ? ? ? String cardId = kahao(users); ? ? ? ? user u1 = new user(cardId, name, password, qumoney); ? ? ? ? users.add(u1); ? ? ? ? System.out.println("恭喜開戶成功!,您的卡號是" + u1.getCardId() + ",請您保管"); ? ? ? }
其中,涉及到生成卡號,以及查詢系統(tǒng)生成的卡號是否相同,需要再寫兩個方法:
//生成卡號 ? ? public static String kahao(ArrayList<user> users) { ? ? ? ? while (true) { ? ? ? ? ? ? Random rs = new Random(); ? ? ? ? ? ? String cardId = ""; ? ? ? ? ? ? for (int i = 0; i < 8; i++) { ? ? ? ? ? ? ? ? cardId += rs.nextInt(10); ? ? ? ? ? ? } ? ? ? ? ? ? user a = getcardId(cardId, users); ? ? ? ? ? ? if (a == null) { ? ? ? ? ? ? ? ? //無重復(fù) ? ? ? ? ? ? ? ? return cardId; ? ? ? ? ? ? } ? ? ? ? } ? ? }
//查詢卡號 ? ? public static user getcardId(String kahao, ArrayList<user> users) { ? ? ? ? for (int i = 0; i < users.size(); i++) { ? ? ? ? ? ? user a = users.get(i); ? ? ? ? ? ? if (a.getCardId().equals(kahao)) { ? ? ? ? ? ? ? ? return a; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return null; ? ? }
六.將登錄賬戶寫成方法,并設(shè)計(jì)主界面
public static void denglu(ArrayList<user> users, Scanner sc) { ? ? ? ? System.out.println("==========登錄賬戶=========="); ? ? ? ? if (users.size() == 0) { ? ? ? ? ? ? System.out.println("系統(tǒng)中無賬戶!請先注冊"); ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? while (true) { ? ? ? ? ? ? System.out.println("請輸入您的卡號:"); ? ? ? ? ? ? String cardId = sc.next(); ? ? ? ? ? ? String password = ""; ? ? ? ? ? ? user acc = getcardId(cardId, users); ? ? ? ? ? ? if (acc != null) { ? ? ? ? ? ? ? ? while (true) { ? ? ? ? ? ? ? ? ? ? System.out.println("請輸入您的密碼:"); ? ? ? ? ? ? ? ? ? ? password = sc.next(); ? ? ? ? ? ? ? ? ? ? //判斷密碼是否正確 ? ? ? ? ? ? ? ? ? ? if (acc.getPassword().equals(password)) { ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("登錄成功!,歡迎卡號" + acc.getCardId() + "的貴賓" + acc.getUsername() + "進(jìn)入系統(tǒng)"); ? ? ? ? ? ? ? ? ? ? ? ? //展示系統(tǒng)登錄后的操作界面 ? ? ? ? ? ? ? ? ? ? ? ? jiemian(sc, acc, users); ? ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("密碼錯誤!請重新輸入"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? System.out.println("不存在該卡號!"); ? ? ? ? ? ? } ? ? ? ? } ? ? }
六.1編寫 展示主界面方法
public static void jiemian(Scanner sc, user acc, ArrayList<user> users) { ? ? ? ? while (true) { ? ? ? ? ? ? System.out.println("==========歡迎進(jìn)入xx銀行用戶操作界面==========="); ? ? ? ? ? ? System.out.println("請您輸入操作命令:"); ? ? ? ? ? ? System.out.println("1.查詢:"); ? ? ? ? ? ? System.out.println("2.存款:"); ? ? ? ? ? ? System.out.println("3.取款:"); ? ? ? ? ? ? System.out.println("4.轉(zhuǎn)賬:"); ? ? ? ? ? ? System.out.println("5.修改密碼:"); ? ? ? ? ? ? System.out.println("6.退出:"); ? ? ? ? ? ? System.out.println("7.注銷當(dāng)前賬戶"); ? ? ? ? ? ? sc = new Scanner(System.in); ? ? ? ? ? ? int commend = sc.nextInt(); ? ? ? ? ? ? switch (commend) { ? ? ? ? ? ? ? ? case 1: ? ? ? ? ? ? ? ? ? ? atm1(acc); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case 2: ? ? ? ? ? ? ? ? ? ? atm2(acc, sc); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case 3: ? ? ? ? ? ? ? ? ? ? atm3(acc, sc); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case 4: ? ? ? ? ? ? ? ? ? ? atm4(users, acc, sc); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case 5: ? ? ? ? ? ? ? ? ? ? atm5(acc, sc); ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? case 6: ? ? ? ? ? ? ? ? ? ? System.out.println("歡迎下次來到ATM系統(tǒng)"); ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? case 7: ? ? ? ? ? ? ? ? ? ? System.out.println("是否確認(rèn)注銷賬戶?按1確認(rèn),按2取消注銷"); ? ? ? ? ? ? ? ? ? ? int tf = sc.nextInt(); ? ? ? ? ? ? ? ? ? ? if (tf == 1) { ? ? ? ? ? ? ? ? ? ? ? ? users.remove(acc); ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("注銷成功!"); ? ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("取消注銷"); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? System.out.println("不存在該功能!"); ? ? ? ? ? ? } ? ? ? ? } ? ? ? }
七.接下來,依次編寫各個功能的方法
//查詢功能 ? ? private static void atm1(user acc) { ? ? ? ? System.out.println("========當(dāng)前賬戶詳情========="); ? ? ? ? System.out.println("卡號:" + acc.getCardId()); ? ? ? ? System.out.println("姓名:" + acc.getUsername()); ? ? ? ? System.out.println("余額:" + acc.getMoney()); ? ? ? ? System.out.println("當(dāng)次取款限額:" + acc.getQumoney()); ? ? } ? ? ? //存款功能 ? ? private static void atm2(user acc, Scanner sc) { ? ? ? ? System.out.println("=========存款操作========="); ? ? ? ? System.out.println("請您輸入存款金額:"); ? ? ? ? double money = sc.nextDouble(); ? ? ? ? acc.setMoney(acc.getMoney() + money); ? ? ? ? System.out.println("存款成功!"); ? ? ? ? atm1(acc); ? ? ? } ? ? ? //取款 ? ? private static void atm3(user acc, Scanner sc) { ? ? ? ? System.out.println("=========取款操作========="); ? ? ? ? if (acc.getMoney() < 100) { ? ? ? ? ? ? System.out.println("您的賬戶余額不足100元!無法取款"); ? ? ? ? ? ? return; ? ? ? ? } else { ? ? ? ? ? ? while (true) { ? ? ? ? ? ? ? ? System.out.println("請您輸入取款金額:"); ? ? ? ? ? ? ? ? double money = sc.nextDouble(); ? ? ? ? ? ? ? ? if (money > acc.getMoney()) { ? ? ? ? ? ? ? ? ? ? System.out.println("賬戶余額不足!"); ? ? ? ? ? ? ? ? ? ? System.out.println("繼續(xù)取款按1,退出取款按2"); ? ? ? ? ? ? ? ? ? ? int commend = sc.nextInt(); ? ? ? ? ? ? ? ? ? ? switch (commend) { ? ? ? ? ? ? ? ? ? ? ? ? case 1: ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? case 2: ? ? ? ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } else if (money > acc.getQumoney()) { ? ? ? ? ? ? ? ? ? ? System.out.println("超過了當(dāng)次取款限額!,每次最多可以取" + acc.getQumoney() + "元"); ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? System.out.println("取款成功!"); ? ? ? ? ? ? ? ? ? ? acc.setMoney(acc.getMoney() - money); ? ? ? ? ? ? ? ? ? ? atm1(acc); ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? ? //轉(zhuǎn)賬 ? ? private static void atm4(ArrayList<user> users, user acc, Scanner sc) { ? ? ? ? System.out.println("=========轉(zhuǎn)賬操作========="); ? ? ? ? //判斷系統(tǒng)中是否有兩個以上賬戶 ? ? ? ? if (users.size() < 2) { ? ? ? ? ? ? System.out.println("系統(tǒng)中無其他賬戶,不可以轉(zhuǎn)賬!"); ? ? ? ? ? ? return; ? ? ? ? } else { ? ? ? ? ? ? if (acc.getMoney() == 0) { ? ? ? ? ? ? ? ? System.out.println("您的賬戶余額為0,無法轉(zhuǎn)賬"); ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? while (true) { ? ? ? ? ? ? ? ? ? ? System.out.println("請輸入對方的卡號:"); ? ? ? ? ? ? ? ? ? ? String dui = sc.next(); ? ? ? ? ? ? ? ? ? ? user c = getcardId(dui, users); ? ? ? ? ? ? ? ? ? ? if (c != null) { ? ? ? ? ? ? ? ? ? ? ? ? //判斷賬戶對象是否為自己 ? ? ? ? ? ? ? ? ? ? ? ? if (c.getCardId().equals(acc.getCardId())) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("不能給自己轉(zhuǎn)賬!"); ? ? ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? //確認(rèn)對方姓氏 ? ? ? ? ? ? ? ? ? ? ? ? ? ? String name = "*" + c.getUsername().substring(1); ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("請您確認(rèn)[" + name + "]的姓氏"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? String xingshi = sc.next(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (c.getUsername().startsWith(xingshi)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("輸入正確!"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? while (true) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("請您輸入轉(zhuǎn)賬金額:"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? double money = sc.nextDouble(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (money > acc.getMoney()) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("余額不足!"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("繼續(xù)轉(zhuǎn)賬按1,退出轉(zhuǎn)賬按2"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int commend = sc.nextInt(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? switch (commend) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case 1: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case 2: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? acc.setMoney(acc.getMoney() - money); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? c.setMoney(c.getMoney() + money); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("為" + c.getUsername() + "用戶" + "轉(zhuǎn)賬成功"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? atm1(acc); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("對不起,您輸入的信息有誤!"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("對不起,您輸入卡號有誤!"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? ? //修改密碼操作 ? ? private static void atm5(user acc, Scanner sc) { ? ? ? ? System.out.println("=========修改密碼操作========="); ? ? ? ? while (true) { ? ? ? ? ? ? System.out.println("請輸入您的舊密碼:"); ? ? ? ? ? ? String oldpassword = sc.next(); ? ? ? ? ? ? String newpassword = ""; ? ? ? ? ? ? if (oldpassword.equals(acc.getPassword())) { ? ? ? ? ? ? ? ? while (true) { ? ? ? ? ? ? ? ? ? ? System.out.println("請輸入新密碼:"); ? ? ? ? ? ? ? ? ? ? newpassword = sc.next(); ? ? ? ? ? ? ? ? ? ? if (newpassword.equals(oldpassword)) { ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("新密碼不能與舊密碼重復(fù)!"); ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("請?jiān)俅屋斎朊艽a:"); ? ? ? ? ? ? ? ? ? ? ? ? String okpassword = sc.next(); ? ? ? ? ? ? ? ? ? ? ? ? if (newpassword.equals(okpassword)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("修改密碼成功!"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? acc.setPassword(newpassword); ? ? ? ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("兩次密碼輸入不一致!"); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else{ ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("舊密碼輸入錯誤!"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? } ? ? }
至此,所有功能完成。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)簡單控制臺版ATM系統(tǒng)
- Java實(shí)現(xiàn)基礎(chǔ)銀行ATM系統(tǒng)
- Java基于控制臺界面實(shí)現(xiàn)ATM系統(tǒng)
- Java簡單實(shí)現(xiàn)銀行ATM系統(tǒng)
- java實(shí)現(xiàn)簡單銀行ATM系統(tǒng)
- Java實(shí)現(xiàn)ATM系統(tǒng)超全面步驟解讀建議收藏
- Java實(shí)現(xiàn)銀行ATM系統(tǒng)
- java模擬實(shí)現(xiàn)銀行ATM機(jī)操作
- 用Java實(shí)現(xiàn)簡單ATM機(jī)功能
- java方法實(shí)現(xiàn)簡易ATM功能
相關(guān)文章
Java中的synchronized和ReentrantLock的區(qū)別詳細(xì)解讀
這篇文章主要介紹了Java中的synchronized和ReentrantLock的區(qū)別詳細(xì)解讀,synchronized是Java內(nèi)建的同步機(jī)制,所以也有人稱其為 IntrinsicLocking,它提供了互斥的語義和可見性,當(dāng)一個線程已經(jīng)獲取當(dāng)前鎖時(shí),其他試圖獲取的線程只能等待或者阻塞在那里,需要的朋友可以參考下2024-01-01Spring?加載?Application?Context五種方式小結(jié)
這篇文章主要介紹了Spring?加載?Application?Context五種方式小結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。2022-01-01Spring MVC實(shí)現(xiàn)mysql數(shù)據(jù)庫增刪改查完整實(shí)例
這篇文章主要介紹了Spring MVC實(shí)現(xiàn)mysql數(shù)據(jù)庫增刪改查完整實(shí)例,從創(chuàng)建一個web項(xiàng)目開始,分享了項(xiàng)目結(jié)構(gòu)以及具體Java代碼和前端頁面等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以了解下。2017-12-12