Java實(shí)現(xiàn)銀行ATM系統(tǒng)
用Java模擬一個(gè)銀行ATM系統(tǒng),供大家參考,具體內(nèi)容如下
系統(tǒng)功能介紹:
使用面向?qū)ο蟮木幊趟枷?,盡可能模擬真實(shí)世界中的銀行ATM業(yè)務(wù)流程。
main方法里通過(guò)調(diào)用一行代碼,完成整個(gè)業(yè)務(wù)流程的順序調(diào)用。
加入了身份證號(hào),手機(jī)號(hào)等元素,雖然他們不涉及銀行卡的業(yè)務(wù)處理,但它們是真實(shí)世界中辦理銀行卡的必需條件,這些在代碼中也有所體現(xiàn)。
為了盡可能使得隨機(jī)生成的身份證號(hào)碼和手機(jī)號(hào)碼和銀行卡號(hào)切合實(shí)際,還手動(dòng)設(shè)計(jì)了一個(gè)工具類(lèi),用來(lái)生成隨機(jī)的號(hào)碼。
其中涉及到的知識(shí),包括但不限于static修飾符的使用,集合的使用,權(quán)限修飾符的控制,類(lèi)與類(lèi)之間關(guān)系的設(shè)計(jì),類(lèi)構(gòu)建的屬性初始化控制,Scanner輸入控制,工具類(lèi)BigDecimal和DecimalFormat的使用,隨機(jī)數(shù)的生成等。
但其實(shí)最難的還是設(shè)計(jì),類(lèi)與類(lèi)之間關(guān)系的設(shè)計(jì),類(lèi)的屬性和方法的設(shè)計(jì),它們都至關(guān)重要,它們可以說(shuō)是面向?qū)ο缶幊痰暮诵?。一旦屬性或方法設(shè)計(jì)得不合理,程序即使可以運(yùn)行,但它的開(kāi)發(fā)過(guò)程必然是違背人的正常思維的,也會(huì)使得后期的更改和維護(hù)變得異常棘手。
編寫(xiě)這個(gè)程序相對(duì)來(lái)講還是很有挑戰(zhàn)性的,雖然用到的知識(shí)不多,但也很考究你的設(shè)計(jì)能力和思維能力。
全部代碼
銀行系統(tǒng)類(lèi)BankSystem:
public class BankSystem {
private static ATM_Machine atm = new ATM_Machine();
private static Set<BankCard> allBankCard = new HashSet<>();
public static void addBanCard(BankCard card){
allBankCard.add(card);
}
public static Set getAllBanCard(){
return allBankCard;
}
public static boolean loginATM(ATM_Machine atm){
Scanner input = new Scanner(System.in);
String cardNum;
System.out.println("請(qǐng)輸入您的銀行卡號(hào):(誤操作,按#號(hào)鍵敲回車(chē)可再次回到系統(tǒng)主頁(yè)面)");
cardNum = input.nextLine();
if (cardNum.equals("#")){
start();
}
ArrayList<BankCard> allBankCard = new ArrayList<>(BankSystem.getAllBanCard());
BankCard card = null;
boolean isExist = false;
for (int i = 0;i<allBankCard.size();i++){
if(allBankCard.get(i).getCARDNUM().equals(cardNum)){
card = allBankCard.get(i);//卡號(hào)存在,實(shí)例化該銀行卡
isExist = true;
}
}
if (isExist == false){
System.err.println("您輸入的銀行卡號(hào)不存在,請(qǐng)重新輸入");
loginATM(atm);//遞歸調(diào)用
}
String name = card.getHolder().getName();
System.out.println("歡迎"+name+"使用本公司智能提示ATM系統(tǒng)\n請(qǐng)輸入您的銀行卡密碼:");
int password = input.nextInt();
int n = 3;//3次輸入密碼的機(jī)會(huì)
while (n>0) {
if (!atm.loginCheck(card,password)) {
n--;
System.err.println("您輸入的密碼錯(cuò)入,請(qǐng)重新輸入,您還有" + n + "次輸入機(jī)會(huì)!");
if(n==0){
atm.exit();
return false;
}
password = input.nextInt();
}else{
System.out.println("密碼正確,正在啟動(dòng)系統(tǒng)···");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
atm.showOperation(card);
return true;
}
}
atm.exit();
return false;
}
public static void start(){
System.out.println("\t=============歡迎進(jìn)入我們的銀行系統(tǒng)=============\t");
System.out.println("1.沒(méi)有銀行卡,我要辦理\t\t\t2.已有銀行卡,我要通過(guò)ATM辦理一些業(yè)務(wù)");
System.out.println("3.退出整個(gè)銀行系統(tǒng)");
System.out.println("請(qǐng)選擇您的操作:");
Scanner input = new Scanner(System.in);
//只需要輸入名字,即可辦理銀行卡。身份證號(hào),手機(jī)號(hào),銀行卡號(hào),自動(dòng)隨機(jī)生成(只是為了最大限度模擬現(xiàn)實(shí)情景的同時(shí),又不讓測(cè)試時(shí)輸入太多繁瑣的東西)
String option = input.nextLine();
if (option.equals("1")){
Customer customer = new Customer();
customer.registerIdentify();
BankCard card = customer.applyBankCard(customer.getIdentityCard(), customer.getPhoneNum());
//allBankCard.add(card);//將新辦理的銀行卡存入系統(tǒng)中
//辦理完銀行卡會(huì)在applyBankCard方法內(nèi)自動(dòng)存入系統(tǒng)中
System.out.println("您的銀行卡已經(jīng)辦理完畢,即將再次進(jìn)入系統(tǒng)···");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
start();
}else if(option.equals("2")){
loginATM(atm);
}else if (option.equals("3")){
System.out.println("您慢走,感謝你的到來(lái),歡迎下次再來(lái)辦理業(yè)務(wù)");
}else {
System.err.println("您輸入了不合法的字符");
start();
}
}
}
ATM_Machine類(lèi):
public class ATM_Machine {
private boolean isFirstShow = true;
public boolean loginCheck(BankCard card,int password){
int truePassword = card.getPassword();
if (truePassword == password){
return true;
}
return false;
}
public void showTitle(){
System.out.println("------------ATM系統(tǒng)------------");
}
//登錄成功后,系統(tǒng)開(kāi)始工作
public void showOperation(BankCard card){
//登錄,查詢(xún)余額,存款,取款,轉(zhuǎn)賬,銷(xiāo)戶(hù),開(kāi)戶(hù),退出
Scanner input = new Scanner(System.in);
if(isFirstShow) {
this.showTitle();
isFirstShow = false;
}
System.out.println("1.查詢(xún)余額\t\t\t2.存款");
System.out.println("3.轉(zhuǎn)賬 \t\t\t4.取款");
System.out.println("5.銷(xiāo)戶(hù) \t\t\t6.退出");
System.out.println("請(qǐng)輸入您要進(jìn)行的操作:");
int option = input.nextInt();
input.nextLine();
switch (option){
case 6:exit();
BankSystem.start();
break;
case 1:showBalance(card);
break;
case 2:showDeposit(card);
break;
case 3:showTransfer(card);
break;
case 4:showWithdrawal(card);
break;
case 5:closeAccount(card);
break;
default:
System.err.println("您輸入了不合法字符,建議您重新輸入!");
showOperation(card);//遞歸調(diào)用
}
}
//退出系統(tǒng),密碼輸入錯(cuò)誤3次,或者交易完成后,應(yīng)該退出系統(tǒng)
public void exit(){
System.out.println("ATM系統(tǒng)關(guān)閉");
}
public static void main(String[] args) {
ATM_Machine a = new ATM_Machine();
}
public BigDecimal queryBalance(BankCard card){
return card.getBalance();
}
public void showBalance(BankCard card){
this.showTitle();
System.out.println("您的余額為:"+queryBalance(card));
this.showOperation(card);
}
//deposit存款
public void deposit(BankCard card,int amount){
card.setBalance(card.getBalance().add(new BigDecimal(amount)));
}
public void showDeposit(BankCard card){
this.showTitle();
int amount = checkAmountInput("存款",20000);//一次最多存2萬(wàn)
deposit(card,amount);//執(zhí)行存款操作
System.out.println("存款成功,您當(dāng)前卡內(nèi)余額為:"+card.getBalance());
showOperation(card);
}
//取款:withdrawal
public void withdrawal(BankCard card,int amount){
card.setBalance(card.getBalance().subtract(new BigDecimal(amount)));
}
public void showWithdrawal(BankCard card){
this.showTitle();
int amount = checkAmountInput("取款",3000);//一次最多存2萬(wàn)
withdrawal(card,amount);//執(zhí)行存款操作
System.out.println("取款成功,您當(dāng)前卡內(nèi)余額為:"+card.getBalance());
showOperation(card);
}
//銷(xiāo)戶(hù):closing an account
public void closeAccount(BankCard card ){
Scanner input = new Scanner(System.in);
System.err.println("銷(xiāo)戶(hù)是一個(gè)危險(xiǎn)操作,你的所有余額將被清空,卡號(hào)將會(huì)回收,您確定要繼續(xù)嗎?");
System.out.println("1.是的,繼續(xù)\t2.取消該操作");
String s = input.nextLine();
if (s.equals("1")){
BankSystem.getAllBanCard().remove(card);
card = null;
System.out.println("您已經(jīng)成功銷(xiāo)戶(hù)");
exit();
BankSystem.start();
}else if (s.equals("2")){
showOperation(card);
}else{
System.err.println("您輸入了不合法字符!!!\n麻煩您重復(fù)確認(rèn)您的操作:");
closeAccount(card);//遞歸調(diào)用
}
}
//轉(zhuǎn)賬:transfer
public void transfer(BankCard myCard,BankCard anotherCard,int amount){
myCard.setBalance(myCard.getBalance().subtract(new BigDecimal(amount)));
anotherCard.setBalance(anotherCard.getBalance().add(new BigDecimal(amount)));
}
public void showTransfer(BankCard myCard){
Scanner input = new Scanner(System.in);
System.out.println("請(qǐng)輸入對(duì)方的銀行卡號(hào):");
String cardNum = input.nextLine();
ArrayList<BankCard> allBankCard = new ArrayList<>(BankSystem.getAllBanCard());
BankCard anotherCard = null;
boolean isExist = false;
for (int i = 0;i<allBankCard.size();i++){
if(allBankCard.get(i).getCARDNUM().equals(cardNum)){
anotherCard = allBankCard.get(i);//卡號(hào)存在,實(shí)例化該銀行卡
isExist = true;
break;
}
}
if (isExist == false){
System.err.println("您輸入的銀行卡號(hào)不存在,請(qǐng)重新輸入");
showTransfer(myCard);//遞歸調(diào)用
}
int amount = checkAmountInput("轉(zhuǎn)賬",5000);//轉(zhuǎn)賬每次只能轉(zhuǎn)5000
if (myCard.getBalance().compareTo(new BigDecimal(amount)) == -1){
System.err.println("對(duì)不起,您的余額不足");
System.out.println();
showOperation(myCard);
}
transfer(myCard,anotherCard,amount);
System.out.println("轉(zhuǎn)賬成功!請(qǐng)繼續(xù)您的操作");
showOperation(myCard);
}
//====================================
private int checkAmountInput(String operation,int maxAmount){
Scanner input = new Scanner(System.in);
int amount;
System.out.println("請(qǐng)輸入您要"+operation+"的金額(一次不得超過(guò)"+maxAmount+"):");
while (true) {
try {
amount = input.nextInt();
input.nextLine();
if (amount % 100 != 0) {
System.err.println(operation+"數(shù)目必須是100的整數(shù)倍,請(qǐng)重新輸入:");
continue;
}
if (amount > maxAmount) {
System.err.println(operation+"數(shù)目一次不得超過(guò)"+maxAmount+",請(qǐng)重新輸入:");
continue;
}
break;
} catch (InputMismatchException e) {
input.nextLine();//當(dāng)用戶(hù)輸入的類(lèi)型不匹配,清空輸入棧
System.err.println(operation+"數(shù)額必須是整數(shù),請(qǐng)重新輸入:");
}
}
return amount;
}
//===================================================
}
銀行卡BankCard類(lèi):
public class BankCard {
/*對(duì)于static關(guān)鍵字的理解:如果某一個(gè)屬性,設(shè)置了static修飾符,就意味著,不管創(chuàng)建多少個(gè)對(duì)象,該屬性卻只有一份,被所有對(duì)象所共享
一旦某一個(gè)對(duì)象修改了這個(gè)屬性,其他對(duì)象的這個(gè)屬性都會(huì)改變,因?yàn)檫@個(gè)屬性是公共的。
所以: 千萬(wàn)要注意:static修飾符不可以濫用?。?
*/
private BigDecimal balance = new BigDecimal("0");//余額
private String CARDNUM;//卡號(hào),一般是19位的,純數(shù)字的
private Customer holder;//持有者
private int password;//密碼
public static boolean checkIsExistThisBankCardNum(String bankCardNum){
boolean flag = false;
ArrayList<BankCard> allBanCard = new ArrayList<>(BankSystem.getAllBanCard());
for (int i = 0;i<allBanCard.size();i++){
while (allBanCard.get(i).getCARDNUM().equals(bankCardNum)){
flag = true;//存在這個(gè)銀行卡號(hào)了,需要重新生成,雖然概率很低。
}
}
return flag;
}
public BankCard(String IDNum,String phoneNum,int password){
//需要一個(gè)身份證號(hào),和一個(gè)手機(jī)號(hào)碼
this.holder = IdentityManageSystem.getIdentifyMsg().get(IDNum);
String cardNum = new GenerateRandomString().generateIntString(19);//19位隨機(jī)數(shù)字
while (checkIsExistThisBankCardNum(cardNum)){
//存在這個(gè)銀行卡號(hào)了,需要重新生成,雖然概率很低。
cardNum = new GenerateRandomString().generateIntString(19);//19位隨機(jī)數(shù)字
}
this.CARDNUM = cardNum;
this.password = password;
//設(shè)置開(kāi)戶(hù)隨機(jī)金額(模擬一下)
Double money = new Random().nextDouble()*100+1;
DecimalFormat df = new DecimalFormat("#.00");
String bd = df.format(money);
this.balance = new BigDecimal(bd);
}
public boolean equals(Object obj){
if (this==obj){
return true;
}
if (obj instanceof BankCard){
BankCard newCard = (BankCard)obj;
if (newCard.getCARDNUM().equals(this.CARDNUM))
//只要卡號(hào)相同,就認(rèn)為是同一張銀行卡
return true;
}
return false;
}
public int hashCode(){
return this.CARDNUM.hashCode();
}
public BigDecimal getBalance() {
return balance;
}
public String getCARDNUM() {
return CARDNUM;
}
public Customer getHolder() {
return holder;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
public void setPassword(int password) {
this.password = password;
}
public int getPassword() {
return password;
}
}
顧客Customer類(lèi):
public class Customer {
private IdentityCard identityCard;//身份證
private String phoneNum;//手機(jī)號(hào)
private String name;//客戶(hù)的姓名
private int age;//年齡
{
GenerateRandomString g = new GenerateRandomString();
phoneNum = "1"+ g.generateIntString(10);//第一個(gè)數(shù)是1,后面10位隨機(jī)的電話號(hào)碼
}
public Customer(){
Scanner input = new Scanner(System.in);
System.out.println("您好,請(qǐng)問(wèn)您叫什么名字?:");
String name = input.nextLine();
this.name = name;
}
public Customer(String IDNum){
//判斷身份證號(hào)對(duì)不對(duì),不是在這里判斷!
}
//注冊(cè)身份證
public IdentityCard registerIdentify(){
if (this.getName()!=null){
IdentityCard card = new IdentityCard(this);
this.identityCard = card;
IdentityManageSystem.getIdentifyMsg().put(card.getIDNum(),this);//注冊(cè)完身份證,把鍵值對(duì)身份證號(hào)-姓名放入身份管理系統(tǒng)
return this.identityCard;
}
System.err.println("您還沒(méi)有名字呢");
return null;
}
public BankCard applyBankCard(IdentityCard card,String phoneNum){
Scanner input = new Scanner(System.in);
System.out.println("顧客"+this.getName()+"身份證號(hào):"+this.getIdentityCard().getIDNum()+
"電話號(hào)碼:"+this.getPhoneNum()+"\n正在申請(qǐng)銀行卡···\n請(qǐng)?jiān)O(shè)置您的銀行卡密碼(規(guī)則:密碼必須是6位純數(shù)字):");
int password;
while (true) {
try {
password = input.nextInt();
break;
} catch (InputMismatchException e) {
input.nextLine();//把多余的回車(chē)讀取走
System.err.println("您輸入的密碼不合法,請(qǐng)重新輸入:");
}
}
String pswStr = String.valueOf(password);
while (pswStr.length()!=6) {
System.err.println("您輸入的密碼長(zhǎng)度不是6位,請(qǐng)重新輸入:");
password = input.nextInt();
pswStr = String.valueOf(password);
}
//需要一個(gè)身份證(而不是號(hào)碼),和一個(gè)手機(jī)號(hào)碼
BankCard bankCard = new BankCard(card.getIDNum(), phoneNum,password);
// BankSystem.getAllBanCard().add(bankCard);//辦理完后自動(dòng)添加到系統(tǒng)庫(kù)中
BankSystem.addBanCard(bankCard);
System.out.println("銀行卡辦理完畢,您的卡號(hào)是:"+bankCard.getCARDNUM()+"\n請(qǐng)妥善保管您的銀行卡,牢記密碼?。?!");
return bankCard;
}
public IdentityCard getIdentityCard() {
if(this.identityCard==null){
System.err.println("您還沒(méi)有注冊(cè)身份證,趕快申領(lǐng)一個(gè)吧!");
}
return identityCard;
}
public String getPhoneNum() {
return phoneNum;
}
public String getName() {
return name;
}
}
身份證類(lèi)IdentityCard:
/**
* 這個(gè)類(lèi)用來(lái)描述身份證
*/
public class IdentityCard {
private String IDNum;//身份證號(hào),一般是18位的,最后一位可以是字母
private Customer holder;//持有者
public IdentityCard(Customer holder) {
//構(gòu)建一個(gè)身份證,必須有持有者才行
//身份證號(hào),我?guī)湍汶S機(jī)生成
GenerateRandomString g = new GenerateRandomString();
this.IDNum = g.generateIDNum();
this.holder = holder;
}
public String getIDNum() {
return IDNum;
}
public Customer getHolder() {
return holder;
}
}
身份管理系統(tǒng)類(lèi)IdentityManageSystem :
public class IdentityManageSystem {
private static Map<String,Customer> identityMsg = new HashMap<>();
public static Map<String,Customer> getIdentifyMsg(){
return identityMsg;
}
}
自設(shè)計(jì)的工具類(lèi)GenerateRandomString:
public class GenerateRandomString {
private char[] str = "qwertyuiopasdfghjklzxcvbnm147258369".toCharArray();
private String[] head = new String []{"140829","142732"};//兩種常見(jiàn)的身份證號(hào)頭部
public String generateIntString(int length){
StringBuilder intString = new StringBuilder();
for (int i = 0;i<length;i++){
intString.append(new Random().nextInt(10));
}
return intString.toString();
}
public String generateIDNum(){
String idHead = head[new Random().nextInt(2)];//身份證號(hào)頭部
String year = String.valueOf((int)(Math.random()*121)+1900);//1900--2020,1900到2021但不包括2021
String month = String.valueOf((int)(Math.random()*12)+1);//1到13但不包括13
month = this.complement(month);
String day = String.valueOf((int)(Math.random()*30)+1);//1到31但不包括31
day = this.complement(day);
String randomstr = generateIntString(3);//身份證的后4位的前3位隨機(jī)數(shù)字
String last = String.valueOf(str[new Random().nextInt(str.length)]);//身份證的最后一位,隨機(jī)字母或數(shù)字
StringBuilder builder = new StringBuilder();
builder.append(idHead).append(year).append(month).append(day).append(randomstr).append(last);
return builder.toString();
}
private String complement(String monthOrDay){
//補(bǔ)全月份或日期,如果字符串長(zhǎng)度小于2,就往前面加個(gè)0補(bǔ)全
if (monthOrDay.length()==1){
String result = "0".concat(monthOrDay);
return result;
}
return monthOrDay;
}
}
測(cè)試類(lèi):
public class MyTest {
public static void main(String[] args) {
BankSystem.start();
}
}
示例截圖





更多學(xué)習(xí)資料請(qǐng)關(guān)注專(zhuān)題《管理系統(tǒng)開(kāi)發(fā)》。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)簡(jiǎn)單控制臺(tái)版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系統(tǒng)
- Java實(shí)現(xiàn)ATM系統(tǒng)超全面步驟解讀建議收藏
- java模擬實(shí)現(xiàn)銀行ATM機(jī)操作
- 用Java實(shí)現(xiàn)簡(jiǎn)單ATM機(jī)功能
- java方法實(shí)現(xiàn)簡(jiǎn)易ATM功能
- 基于Java開(kāi)發(fā)實(shí)現(xiàn)ATM系統(tǒng)
相關(guān)文章
SpringBoot整合mybatis結(jié)合pageHelper插件實(shí)現(xiàn)分頁(yè)
在本篇文章里小編給大家整理的是關(guān)于SpringBoot整合mybatis使用pageHelper插件進(jìn)行分頁(yè)操作相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。2020-02-02
利用Maven入手Spring Boot第一個(gè)程序詳解
這篇文章主要給大家介紹了關(guān)于如何利用Maven入手Spring Boot第一個(gè)程序的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02
java 將byte中的有效長(zhǎng)度轉(zhuǎn)換為String的實(shí)例代碼
下面小編就為大家?guī)?lái)一篇java 將byte中的有效長(zhǎng)度轉(zhuǎn)換為String的實(shí)例代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11
gradle項(xiàng)目中資源文件的相對(duì)路徑打包技巧必看
這篇文章主要介紹了gradle項(xiàng)目中資源文件的相對(duì)路徑打包技巧必看篇,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
第三方網(wǎng)站微信登錄java代碼實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了第三方網(wǎng)站微信登錄的java代碼實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04

