前言介紹:
最近很多同學找我?guī)兔ψ鲆恍┱n程設計或Web前端大作業(yè)、其中控制臺項目應該是初學者必須經(jīng)歷的一個過程、作為java初學者這個控制臺版本的模擬ATM銀行管理系統(tǒng)十分合適、用到的技術也是最簡單的JavaList集合、變量的聲明、對象的創(chuàng)建、一些基本的while語句、switch語句、循環(huán)遍歷等基礎語法。一個最基礎版本的控制臺模擬ATM銀行管理系統(tǒng)也就是CRUD功能、 直接上完整代碼****
創(chuàng)建抽象類Card類
abstract class Card { int cardId; public Card() { super(); } public Card(int cardId) { super(); this.cardId = cardId; } public abstract String getCardId(); public abstract void setCardId(int cardId); }
創(chuàng)建普通銀行類繼承Card
public class CommonCard extends Card{ public CommonCard() { super(); } public CommonCard(int cardId) { super(); this.cardId = cardId; } @Override public String getCardId() { return "普通銀行卡卡號為:" cardId; } @Override public void setCardId(int cardId) { this.cardId = cardId; } }
創(chuàng)建普通信用卡繼承Card
package com.railway.modules.base.test;public class CreditCard extends Card{ public CreditCard() { super(); } public CreditCard(int cardId) { super(cardId); } @Override public String getCardId() { return "信用卡卡號為:" cardId; } @Override public void setCardId(int cardId) { this.cardId = cardId; }}
創(chuàng)建普通醫(yī)??ɡ^承Card
package com.railway.modules.base.test;public class MedicalInsuranceCard extends Card{ public MedicalInsuranceCard() { super(); } public MedicalInsuranceCard(int cardId) { super(cardId); } @Override public String getCardId() { return "醫(yī)??ㄌ枮椋?#034; cardId; } @Override public void setCardId(int cardId) { this.cardId = cardId; } }
ATM主要方法業(yè)務類邏輯實現(xiàn)
package com.railway.modules.base.test;import java.util.Scanner;public class ATM { Scanner input = new Scanner(System.in); private User[] user=new User[100]; User atm = new User(); private User usr; private int accountId = 1; private Card card; public void Select() { while(true) { System.out.println("=======ATM管理系統(tǒng)======="); System.out.print("1.開戶n2.登錄n3.取款n4.存款n5.余額查詢n6.修改密碼n7.查看用戶信息n8.退出登錄n9.退出系統(tǒng)n請選擇需要選擇的項目:"); switch(input.nextInt()) { case 1: setAccount(); //開戶 break; case 2: usr=userLogin(); //登錄 break; case 3: drawMoney(); //取款 break; case 4: depositMoney(); //存款 break; case 5: showBalance(); //余額查詢 break; case 6: revisePassword(); //修改密碼 break; case 7: lookAccount(); //查看用戶信息 break; case 8: cancel();//退出登錄 break; case 9: System.out.println("======================="); System.out.println("歡迎下次登錄該系統(tǒng)!"); //退出系統(tǒng) System.exit(0); break; default: System.out.println("======================="); System.out.println("輸入錯誤!請重新輸入!"); break; } } } public void setAccount(){ //開戶 if (usr!=null) { System.out.println("======================="); System.out.println("請先退出上一個賬戶!"); } else{ boolean x = true; while(x) { System.out.println("======================="); System.out.print("1.普通銀行卡n2.信用卡n3.醫(yī)??╪0.返回n請選擇開戶類型:"); switch(input.nextInt()) { case 1: card = new CommonCard(); card.setCardId(accountId); x=false; break; case 2: card = new CreditCard(); card.setCardId(accountId); x=false; break; case 3: card = new MedicalInsuranceCard(); card.setCardId(accountId); x=false; break; case 0: Select(); default: System.out.println("======================="); System.out.println("輸入錯誤!請重新輸入!"); break; } } System.out.print("請輸入姓名:"); String name=input.next(); System.out.print("請輸入性別:"); String sex=input.next(); System.out.print("請輸入年齡:"); String age=input.next(); System.out.print("請輸入身份證號:"); String ID=input.next(); System.out.print("請輸入銀行卡要設置的密碼:"); String password1=input.next(); System.out.print("請再次輸入密碼:"); String password2=input.next(); if(password1.equals(password2)){ user[accountId]=new User(name,sex,age,password1,ID,0,card); System.out.println("======================="); System.out.println("開戶成功!你的卡號為:" accountId); accountId ; } else { System.out.println("======================="); System.out.println("兩次密碼不同!請重新開始!"); } } } public User userLogin(){ //登錄 if (usr!=null) { System.out.println("======================="); System.out.println("請先退出上一個賬戶!"); return usr; } else{ System.out.print("請輸入卡號:"); int aId=input.nextInt(); System.out.print("請輸入密碼:"); String password=input.next(); for(int i=0;i<user.length;i ){ if (user[i]!=null) { if (aId==user[i].getCard()&&password.equals(user[i].getPassword())) { System.out.println("======================="); System.out.println(user[i].getName() ",歡迎登錄!"); return user[i]; } } } System.out.println("======================="); System.out.println("卡號或密碼有誤"); return null; } } public void drawMoney(){ //取款 if(usr==null){ System.out.println("======================="); System.out.println("請先登錄賬戶!"); } else{ System.out.print("請輸入取款金額:"); double blc=input.nextDouble(); while(blc>usr.getBalance()) { System.out.println("======================="); System.out.print("取款金額大于余額,請重新輸入取款金額:"); blc=input.nextDouble(); } while(blc<=0){ System.out.println("======================="); System.out.print("取款金額錯誤,請重新輸入取款金額:"); blc=input.nextDouble(); } usr.setBalance(usr.getBalance()-blc); System.out.println("======================="); System.out.println("取款成功!"); System.out.println("賬戶余額:" usr.getBalance()); } } public void depositMoney(){ //存款 if(usr==null){ System.out.println("======================="); System.out.println("請先登錄賬戶!"); } else{ System.out.print("請輸入存款金額:"); double dsm=input.nextDouble(); while(dsm<=0){ System.out.println("======================="); System.out.print("輸入金額錯誤,請重新輸入:"); dsm=input.nextDouble(); } usr.setBalance(usr.getBalance() dsm); System.out.println("======================="); System.out.println("存款成功!"); System.out.println("賬戶余額:" usr.getBalance()); } } public void showBalance(){ //余額查詢 if(usr==null){ System.out.println("======================="); System.out.println("請先登錄賬戶!"); } else{ System.out.println("======================="); System.out.println("賬戶余額:" usr.getBalance()); } } public void revisePassword(){ //修改密碼 if(usr==null){ System.out.println("======================="); System.out.println("請先登錄賬戶!"); } else{ System.out.print("請輸入原密碼:"); String password=input.next(); for(int i=2;i>=0;i--) { if(password.equals(usr.getPassword())) break; if(i==0) { System.out.println("三次密碼輸入錯誤!即將退出賬戶!"); cancel(); Select(); } System.out.println("密碼錯誤!你還有" i "次機會!"); System.out.print("請輸入原密碼:"); password=input.next(); } System.out.print("驗證成功,請輸入新密碼:"); String password1=input.next(); System.out.print("請再次輸入密碼:"); String password2=input.next(); while(!password1.equals(password2)) { System.out.println("======================="); System.out.print("兩次密碼不同,請重新輸入新密碼:"); password1=input.next(); System.out.print("請再次輸入密碼:"); password2=input.next(); } usr.setPassword(password1); System.out.println("======================="); System.out.println("修改成功!"); } } public void lookAccount() { //查看用戶信息 if(usr==null){ System.out.println("======================="); System.out.println("請先登錄賬戶!"); } else{ System.out.println("======================="); System.out.println("姓名:" usr.getName()); System.out.println("性別:" usr.getSex()); System.out.println(usr.getCardId()); System.out.println("身份證號:" usr.getID()); System.out.println("余額:" usr.getBalance()); } } public void cancel() { if(usr==null){ System.out.println("======================="); System.out.println("請先登錄賬戶!"); } else{ usr=null; System.out.println("======================="); System.out.println("賬戶退出成功!"); } }}
main方法測試啟動類
public class TestATM { public static void main(String[] args) { ATM atm = new ATM(); atm.Select(); }}
控制臺打印:
=======ATM管理系統(tǒng)=======1.開戶2.登錄3.取款4.存款5.余額查詢6.修改密碼7.查看用戶信息8.退出登錄9.退出系統(tǒng)請選擇需要選擇的項目:1=======================1.普通銀行卡2.信用卡3.醫(yī)???.返回請選擇開戶類型:1請輸入姓名:admin請輸入性別:男請輸入年齡:22請輸入身份證號:513922199555555555請輸入銀行卡要設置的密碼:123456請再次輸入密碼:123456=======================開戶成功!你的卡號為:1=======ATM管理系統(tǒng)=======1.開戶2.登錄3.取款4.存款5.余額查詢6.修改密碼7.查看用戶信息8.退出登錄9.退出系統(tǒng)請選擇需要選擇的項目:2請輸入卡號:1請輸入密碼:123456=======================admin,歡迎登錄!=======ATM管理系統(tǒng)=======1.開戶2.登錄3.取款4.存款5.余額查詢6.修改密碼7.查看用戶信息8.退出登錄9.退出系統(tǒng)請選擇需要選擇的項目:
很基礎、 適合java初學者來進行練習以及當做課程作業(yè)來使用
版權聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶自發(fā)貢獻,該文觀點僅代表作者本人。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如發(fā)現(xiàn)本站有涉嫌抄襲侵權/違法違規(guī)的內(nèi)容, 請發(fā)送郵件至 舉報,一經(jīng)查實,本站將立刻刪除。