java模擬實現(xiàn)銀行ATM機操作
java模擬銀行ATM機操作(基礎版),供大家參考,具體內(nèi)容如下
實現(xiàn)的功能需求:
修改密碼之后,就會自動退出登錄,再重新登錄,若登錄成功才能驗證修改密碼成功,這里用到 【跳出指定循環(huán)】, 其他功能都是基本操作,作為入門入手程序。
準備兩個實體類(一個銀行類,一個用戶類),一個測試類,注意,這里暫且存儲了兩個用戶,這里可以優(yōu)化,暫且不優(yōu)化了
Blank.java
package com.demo2; public class Blank { ? ? /*數(shù)組模擬數(shù)據(jù)庫后臺,并初始化*/ ? ? private User[] users = new User[20]; ? ? int size; ? ? /*接收當前登錄的用戶*/ ? ? User user; ? ? public User getUser() { ? ? ? ? return user; ? ? } ? ? public Blank (){ } ? ? /*初始化用戶*/ ? ? public ?void ?init(){ ? ? ? ?User user1= new User("890012","43042419990201","劉軍","7519","15116497809","18000"); ? ? ? ?User user2= new User("890013","43042419990821","一凡","1314","118842320","13000"); ? ? ? ?/*初始化*/ ? ? ? ?users[0] = user1; ? ? ? ?users[1] =user2; ? ? ? ?size=2; ? ? } ? ? /*用戶登錄*/ ? ? public boolean login(String cardNo, String passwd){ ? ? ? ? for(int i=0; i<size; i++){ ? ? ? ? ? ? if(users[i].getCardNo().equals(cardNo) && users[i].getPasswd().equals(passwd) ){ ? ? ? ? ? ? ? ? user = users[i]; ? ? ? ? ? ? ? ? return true; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return ?false; ? ? } ? ? /*存款*/ ? ? public void save(String ?balance){ ? ? ? ? int money =Integer.parseInt(user.getBalance()); ? ? ? ? money += Integer.parseInt(balance); ? ? ? ? user.setBalance((money+"")); ? ? ? ? System.out.println("存款成功"); ? ? } ? ? /*取款*/ ? ? public void widthDraw(String balance){ ? ? ? ? int money =Integer.parseInt(user.getBalance()); ? ? ? ? money -= Integer.parseInt(balance); ? ? ? ? user.setBalance((money+"")); ? ? } ? ? /*轉(zhuǎn)賬*/ ? ? public ?void trans(String cardNo,String balance){ ? ? ? ? int i=0; ? ? ? ? for(i=0; i<size;i++){ ? ? ? ? ? ? if(users[i].getCardNo().equals(cardNo)){ ? ? ? ? ? ? ? ? /*保存沒轉(zhuǎn)賬之前的數(shù)*/ ? ? ? ? ? ? ? ? ?int ?oldmoney =Integer.parseInt(user.getBalance()) ; ? ? ? ? ? ? ? ? ?int oldanothermoney = Integer.parseInt(users[i].getBalance()); ? ? ? ? ? ? ? ? ?/*轉(zhuǎn)賬,調(diào)用當前登錄用戶的取款方法*/ ? ? ? ? ? ? ? ? ?widthDraw(balance); ? ? ? ? ? ? ? ? ?/*目標賬戶的余額變化*/ ? ? ? ? ? ? ? ? int money =Integer.parseInt(users[i].getBalance()); ? ? ? ? ? ? ? ? money += Integer.parseInt(balance); ? ? ? ? ? ? ? ? users[i].setBalance((money+"")); ? ? ? ? ? ? ? ? /*這里做轉(zhuǎn)賬成功的判斷*/ ? ? ? ? ? ? ? ? ?int userseflmoney =oldmoney - ?Integer.parseInt(user.getBalance()); ? ? ? ? ? ? ? ? ?int useraothermoney = Integer.parseInt(users[i].getBalance())-oldanothermoney; ? ? ? ? ? ? ? ? if(userseflmoney == useraothermoney){ ? ? ? ? ? ? ? ? ? ? System.out.println("轉(zhuǎn)賬成功"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? if(i>=size){ ? ? ? ? ? ? System.out.println("輸入的卡號有誤"); ? ? ? ? } ? ? } ? ? /*查詢余額*/ ? ? public void query(){ ? ? ? ? System.out.println("【賬戶余額】: "+user.getBalance()); ? ? } ? ? /*修改密碼*/ ? ? public void ?modifyPassword(String passwd){ ? ? ? ? user.setPasswd(passwd); ? ? ? ? System.out.println("修改密碼成功,請重新登錄"); ? ? } }
User.java
package com.demo2; public class User { ? ? private String cardNo ; ? ? private String identity; ? ? private String username; ? ? private String passwd; ? ? private String phone; ? ? private String balance; ? ? public User(){} ? ? public User(String cardNo, String identity, String username, String passwd, String phone, String balance) { ? ? ? ? this.cardNo = cardNo; ? ? ? ? this.identity = identity; ? ? ? ? this.username = username; ? ? ? ? this.passwd = passwd; ? ? ? ? this.phone = phone; ? ? ? ? this.balance = balance; ? ? } ? ? public String getCardNo() { ? ? ? ? return cardNo; ? ? } ? ? public void setCardNo(String cardNo) { ? ? ? ? this.cardNo = cardNo; ? ? } ? ? public String getIdentity() { ? ? ? ? return identity; ? ? } ? ? public void setIdentity(String identity) { ? ? ? ? this.identity = identity; ? ? } ? ? public String getUsername() { ? ? ? ? return username; ? ? } ? ? public void setUsername(String username) { ? ? ? ? this.username = username; ? ? } ? ? public String getPasswd() { ? ? ? ? return passwd; ? ? } ? ? public void setPasswd(String passwd) { ? ? ? ? this.passwd = passwd; ? ? } ? ? public String getPhone() { ? ? ? ? return phone; ? ? } ? ? public void setPhone(String phone) { ? ? ? ? this.phone = phone; ? ? } ? ? public String getBalance() { ? ? ? ? return balance; ? ? } ? ? public void setBalance(String balance) { ? ? ? ? this.balance = balance; ? ? } }
BlankTest.java
package com.demo2; import java.util.Scanner; public class BankTest { ? ? public static void main(String[] args) { ? ? ? ? Blank blank = new Blank(); ? ? ? ? /*初始化*/ ? ? ? ? blank.init(); ? ? ? ? Scanner sc =new Scanner(System.in); ? ? ? ?while(true) { ? ? ? ? ? ? System.out.println("========== 中國銀行歡迎你 ========= "); ? ? ? ? ? ? System.out.println(" ? ? ? ? ? ?【請先登錄】 ? ?"); ? ? ? ? ? ? System.out.print("【請輸入卡號】:"); ? ? ? ? ? ? String carNo = sc.next(); ? ? ? ? ? ? System.out.print("【請輸入密碼】:"); ? ? ? ? ? ? String passwd = sc.next(); ? ? ? ? ? ? if (blank.login(carNo, passwd)) { ? ? ? ? ? ? ? ? System.out.println("登錄成功: 【賬號:歡迎】:"+ blank.getUser().getUsername()); ? ? ? ? ? ? ? ?// User user = blank.getUser(); ? ? ? ? ? ? ? prof: ?while(true) { ? ? ? ? ? ? ? ? ? ? System.out.println("========== 1,【存款】 ? ? ?2,【取款】 ? ? ?3,【轉(zhuǎn)賬】 ? ========= "); ? ? ? ? ? ? ? ? ? ? System.out.println("========== 4,【查詢余額】 ?5,【修改密碼】 ? 6,【退出】 ? ========= "); ? ? ? ? ? ? ? ? ? ? System.out.print("【請選擇業(yè)務功能】:"); ? ? ? ? ? ? ? ? ? ? int num = sc.nextInt(); ? ? ? ? ? ? ? ? ? ? if (num == 1) { ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("【進入存款操作】"); ? ? ? ? ? ? ? ? ? ? ? ? System.out.print("【輸入存款金額】:"); ? ? ? ? ? ? ? ? ? ? ? ? int money = sc.nextInt(); ? ? ? ? ? ? ? ? ? ? ? ? while (true) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (money <= 0) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("【請輸入正確的存款金額】"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? blank.save(money + ""); ? ? ? ? ? ? ? ? ? ? } else if (num == 2) { ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("【進入取款操作】"); ? ? ? ? ? ? ? ? ? ? ? ? System.out.print("【輸入取款金額】:"); ? ? ? ? ? ? ? ? ? ? ? ? int money = sc.nextInt(); ? ? ? ? ? ? ? ? ? ? ? ? blank.widthDraw(money+""); ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("取款成功"); ? ? ? ? ? ? ? ? ? ? } else if (num == 3) { ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("【進入轉(zhuǎn)賬操作】"); ? ? ? ? ? ? ? ? ? ? ? ? System.out.print("【輸入轉(zhuǎn)賬卡號】:"); ? ? ? ? ? ? ? ? ? ? ? ? String cardNo ?=sc.next(); ? ? ? ? ? ? ? ? ? ? ? ? System.out.print("【輸入轉(zhuǎn)款金額】"); ? ? ? ? ? ? ? ? ? ? ? ? int money = sc.nextInt(); ? ? ? ? ? ? ? ? ? ? ? ? blank.trans(cardNo, money+""); ? ? ? ? ? ? ? ? ? ? } else if (num == 4) { ? ? ? ? ? ? ? ? ? ? ? ? blank.query(); ? ? ? ? ? ? ? ? ? ? } else if (num == 5) { ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("【進入改密操作】"); ? ? ? ? ? ? ? ? ? ? ? ? System.out.print("【輸入重置密碼】:"); ? ? ? ? ? ? ? ? ? ? ? ? String newpasswd =sc.next(); ? ? ? ? ? ? ? ? ? ? ? ? blank.modifyPassword(newpasswd); ? ? ? ? ? ? ? ? ? ? ? ? break prof; ? ? ? ? ? ? ? ? ? ? } else if (num == 6) { ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("【賬號】: 退出成功"); ? ? ? ? ? ? ? ? ? ? ? ? break prof; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? System.out.println("卡號或者密碼輸入不正確"); ? ? ? ? ? ? ? } ? ? ? ?} ? ? } }
程序運行結(jié)果:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ScheduledThreadPoolExecutor巨坑解決
這篇文章主要為大家介紹了使用ScheduledThreadPoolExecutor遇到的巨坑解決示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02對ArrayList和LinkedList底層實現(xiàn)原理詳解
今天小編就為大家分享一篇對ArrayList和LinkedList底層實現(xiàn)原理詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10JAVA發(fā)送HTTP請求,返回HTTP響應內(nèi)容,應用及實例代碼
這篇文章主要介紹了JAVA發(fā)送HTTP請求,返回HTTP響應內(nèi)容,應用及實例代碼,需要的朋友可以參考下2014-02-02