項(xiàng)目要求:
成都創(chuàng)新互聯(lián)公司是一家網(wǎng)站設(shè)計(jì)公司,集創(chuàng)意、互聯(lián)網(wǎng)應(yīng)用、軟件技術(shù)為一體的創(chuàng)意網(wǎng)站建設(shè)服務(wù)商,主營產(chǎn)品:響應(yīng)式網(wǎng)站設(shè)計(jì)、成都品牌網(wǎng)站建設(shè)、成都全網(wǎng)營銷。我們專注企業(yè)品牌在網(wǎng)站中的整體樹立,網(wǎng)絡(luò)互動(dòng)的體驗(yàn),以及在手機(jī)等移動(dòng)端的優(yōu)質(zhì)呈現(xiàn)。成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、移動(dòng)互聯(lián)產(chǎn)品、網(wǎng)絡(luò)運(yùn)營、VI設(shè)計(jì)、云產(chǎn)品.運(yùn)維為核心業(yè)務(wù)。為用戶提供一站式解決方案,我們深知市場(chǎng)的競(jìng)爭激烈,認(rèn)真對(duì)待每位客戶,為客戶提供賞析悅目的作品,網(wǎng)站的價(jià)值服務(wù)。
1、用戶需要從控制臺(tái)輸入賬號(hào)密碼,賬號(hào)或者密碼不正確報(bào)異常
2、每日取款的金額有限制(100,30000),否則報(bào)異常
3、每次取款都要有記錄,并在下一次取款時(shí)顯示出來
思路:
1、先在“銀行類”里生成一些用戶(跳過了注冊(cè)環(huán)節(jié))
2、可使用List集合存儲(chǔ)取款日志
3、可使用Map集合將“用戶名”和對(duì)應(yīng)的用戶信息建立“鍵值關(guān)系”
4、使用while循環(huán)控制流程
項(xiàng)目實(shí)現(xiàn)代碼:
管理類(含main方法):
import java.util.Scanner;
public class Manager {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//創(chuàng)建BankServer()類對(duì)象
BankServer server = new BankServer();
//創(chuàng)建User類對(duì)象,先令其為空
User user = null;
//調(diào)用creatAccount()方法生成四個(gè)用戶
server.creatAccount();
//while循環(huán)控制流程
while(true){
System.out.println("請(qǐng)輸入您的賬號(hào):");
String id = input.nextLine();
System.out.println("請(qǐng)輸入您的密碼:");
String password = input.nextLine();
try {
user=server.chick(id, password);//調(diào)用chick()方法,讓user有意義
System.out.println("===========歡迎進(jìn)入銀行取款系統(tǒng)===========");
System.out.println("您的賬戶余額為:"+user.getMoney());
while(true){
System.out.println("請(qǐng)輸入您的取款金額(必須大于100,小于30000):");
double money = input.nextDouble();
server.getMoney(user, money);
input.nextLine();
System.out.println("請(qǐng)問您需要繼續(xù)取款嗎? Y or N");
String selec = input.nextLine();
if(selec.equalsIgnoreCase("N")){
System.out.println("歡迎使用!");
break;
}
}
} catch (Exception e) {
System.out.println(">>> 賬號(hào)/密碼錯(cuò)誤!請(qǐng)重新登錄輸入 <<<");
}
}
}
}
用戶類(User類):
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class User {
private double balance;//用戶賬戶金額
private String id;//用戶賬號(hào)名
private String passward;//用戶密碼
private List<String> list=new ArrayList<>() ;//用于存儲(chǔ)用戶取款記錄的日志集合
//無參構(gòu)造方法
public User() {
super();
}
//有參構(gòu)造方法
public User(String id, String passward,double money) {
super();
this.balance = money;
this.id = id;
this.passward = passward;
}
//各個(gè)屬性的set和get方法
public double getMoney() {
return balance;
}
public void setMoney(int money) {
this.balance = money;
}
public String getAccount() {
return id;
}
public void setAccount(String account) {
this.id = account;
}
public String getPassward() {
return passward;
}
public void setPassward(String passward) {
this.passward = passward;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
/**
* 因?yàn)橘~戶的密碼被裝在map集合中,不方便直接訪問,所以在User類中定義一個(gè)檢查密碼是否正確的方法
* 其返回一個(gè)布爾類型的值
* @param password
* @return
*/
public boolean checkPassword(String password){
if(password.equals(this.passward)){
return true;
}else{
return false;
}
}
/**
* 與上面的方法同理,判斷取款金額是否合理的方法,返回一個(gè)布爾類型的值
* @param money
* @return
* @throws Exception
*/
public boolean getMoney(double money) throws Exception {
if (money < 100 || money > 5000) { //規(guī)定每次取款金額的范圍,超過這個(gè)范圍則拋出異常
throw new Exception("取款金額不合理!取款金額必須大于100,小于5000");
}
if (this.balance < money) {//這個(gè)判斷條件被許多人忽略,當(dāng)賬戶內(nèi)余額不足時(shí),拋出異常
throw new Exception("您的余額不足!");
}
//從帳號(hào)余額中扣除相應(yīng)金額
this.balance -= money;
//將取款記錄到日志中
String logStr = User.currentTime()+"\t取款 " + money +"元, 余額:"+this.balance;
this.list.add(logStr); //將記錄添加進(jìn)list集合中
return true; //返回true
}
/**
* 獲取當(dāng)前時(shí)間的方法
* @return
*/
public static String currentTime(){
long ct = System.currentTimeMillis();
Date d = new Date(ct);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:ss:mm");
return sdf.format(d);
}
}
銀行服務(wù)類(BankServer類):
import java.util.HashMap;
import java.util.Map;
/**
* 銀行服務(wù)類,實(shí)現(xiàn)用戶的創(chuàng)建
* @author
*
*/
public class BankServer {
//聲明四個(gè)常量表示賬戶用戶名
final String AC1 = "1111-111111-1111AA";
final String AC2 = "2222-222222-2222BB";
final String AC3 = "3333-333333-3333CC";
final String AC4 = "4444-444444-4444DD";
//聲明四個(gè)常量,用于存儲(chǔ)密碼,每個(gè)賬戶有一個(gè)專屬的密碼
final String PASSWORD1 = "111111";
final String PASSWORD2 = "222222";
final String PASSWORD3 = "333333";
final String PASSWORD4 = "444444";
//聲明一個(gè)靜態(tài)常量規(guī)定初始金額為100000元
public static final Double BALANCE = 100000.00;
//創(chuàng)建一個(gè)map集合用于存儲(chǔ)用戶賬號(hào)和對(duì)應(yīng)的賬戶信息
Map<String, User> map = new HashMap<>();
/**
* 生成用戶的方法,將用戶存入map集合中
*/
public void creatAccount(){
map.put(AC1, new User(AC1,PASSWORD1,BALANCE));
map.put(AC2, new User(AC2,PASSWORD2,BALANCE));
map.put(AC3, new User(AC3,PASSWORD3,BALANCE));
map.put(AC4, new User(AC4,PASSWORD4,BALANCE));
}
/**
* 比較用戶從控制臺(tái)輸入的數(shù)據(jù)是否和銀行預(yù)先存儲(chǔ)的賬戶一樣
* @param id
* @param password
* @return
* @throws Exception
*/
public User chick(String id,String password) throws Exception{
User Account = null; //此時(shí)的Account對(duì)象為null
//密碼已經(jīng)被放進(jìn)map集合中,不好取出,所以需要在User類中定義一個(gè)checkPassword方法,其返回的是布爾類型的值(詳情見User類)
if(map.containsKey(id) ){
Account=map.get(id); //如果賬戶名對(duì)上了,則調(diào)用map集合的get()方法給Account對(duì)象賦值
if(Account.checkPassword(password)){
System.out.println("登錄成功!"); //如果密碼也對(duì)上了,提示“登錄成功”,否則拋出異常
}else{
throw new Exception("密碼錯(cuò)誤");
}
}else{
throw new Exception("賬號(hào)/密碼錯(cuò)誤!");
}
return Account; //返回一個(gè)Account對(duì)象
}
/**
* 這是一個(gè)取錢并將記錄存入list集合的方法
* @param loginUA
* @param money
*/
public void getMoney(User user, double money) {
try {
//用戶余額已經(jīng)被放進(jìn)map集合中,同理,在User類中定義一個(gè)getMoney()方法,其返回的是布爾類型的值
if(user.getMoney(money)){
System.out.println("取款成功,用戶當(dāng)前余額:"+user.getBalance());
System.out.println("==============================");
//輸出日志
for (String str : user.getList()) {
System.out.println(str);
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
運(yùn)行結(jié)果1(正確地輸入了賬號(hào)密碼):

運(yùn)行結(jié)果2錯(cuò)誤地輸入了賬號(hào)密碼):

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)頁名稱:java實(shí)現(xiàn)ATM取款項(xiàng)目
URL標(biāo)題:http://chinadenli.net/article30/gsposo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、建站公司、全網(wǎng)營銷推廣、網(wǎng)站策劃、網(wǎng)站排名、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)