本文實例為大家分享了java實現ATM取款項目的具體代碼,供大家參考,具體內容如下
項目要求:
1、用戶需要從控制臺輸入賬號密碼,賬號或者密碼不正確報異常
2、每日取款的金額有限制(100,30000),否則報異常
3、每次取款都要有記錄,并在下一次取款時顯示出來
思路:
1、先在“銀行類”里生成一些用戶(跳過了注冊環節)
2、可使用List集合存儲取款日志
3、可使用Map集合將“用戶名”和對應的用戶信息建立“鍵值關系”
4、使用while循環控制流程
項目實現代碼:
管理類(含main方法):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
import java.util.Scanner; public class Manager { public static void main(String[] args) { Scanner input = new Scanner(System.in); //創建BankServer()類對象 BankServer server = new BankServer(); //創建User類對象,先令其為空 User user = null ; //調用creatAccount()方法生成四個用戶 server.creatAccount(); //while循環控制流程 while ( true ){ System.out.println( "請輸入您的賬號:" ); String id = input.nextLine(); System.out.println( "請輸入您的密碼:" ); String password = input.nextLine(); try { user=server.chick(id, password); //調用chick()方法,讓user有意義 System.out.println( "===========歡迎進入銀行取款系統===========" ); System.out.println( "您的賬戶余額為:" +user.getMoney()); while ( true ){ System.out.println( "請輸入您的取款金額(必須大于100,小于30000):" ); double money = input.nextDouble(); server.getMoney(user, money); input.nextLine(); System.out.println( "請問您需要繼續取款嗎? Y or N" ); String selec = input.nextLine(); if (selec.equalsIgnoreCase( "N" )){ System.out.println( "歡迎使用!" ); break ; } } } catch (Exception e) { System.out.println( ">>> 賬號/密碼錯誤!請重新登錄輸入 <<<" ); } } } } |
用戶類(User類):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
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; //用戶賬號名 private String passward; //用戶密碼 private List<String> list= new ArrayList<>() ; //用于存儲用戶取款記錄的日志集合 //無參構造方法 public User() { super (); } //有參構造方法 public User(String id, String passward, double money) { super (); this .balance = money; this .id = id; this .passward = passward; } //各個屬性的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; } /** * 因為賬戶的密碼被裝在map集合中,不方便直接訪問,所以在User類中定義一個檢查密碼是否正確的方法 * 其返回一個布爾類型的值 * @param password * @return */ public boolean checkPassword(String password){ if (password.equals( this .passward)){ return true ; } else { return false ; } } /** * 與上面的方法同理,判斷取款金額是否合理的方法,返回一個布爾類型的值 * @param money * @return * @throws Exception */ public boolean getMoney( double money) throws Exception { if (money < 100 || money > 5000 ) { //規定每次取款金額的范圍,超過這個范圍則拋出異常 throw new Exception( "取款金額不合理!取款金額必須大于100,小于5000" ); } if ( this .balance < money) { //這個判斷條件被許多人忽略,當賬戶內余額不足時,拋出異常 throw new Exception( "您的余額不足!" ); } //從帳號余額中扣除相應金額 this .balance -= money; //將取款記錄到日志中 String logStr = User.currentTime()+ "\t取款 " + money + "元, 余額:" + this .balance; this .list.add(logStr); //將記錄添加進list集合中 return true ; //返回true } /** * 獲取當前時間的方法 * @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); } } |
銀行服務類(BankServer類):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
import java.util.HashMap; import java.util.Map; /** * 銀行服務類,實現用戶的創建 * @author * */ public class BankServer { //聲明四個常量表示賬戶用戶名 final String AC1 = "1111-111111-1111AA" ; final String AC2 = "2222-222222-2222BB" ; final String AC3 = "3333-333333-3333CC" ; final String AC4 = "4444-444444-4444DD" ; //聲明四個常量,用于存儲密碼,每個賬戶有一個專屬的密碼 final String PASSWORD1 = "111111" ; final String PASSWORD2 = "222222" ; final String PASSWORD3 = "333333" ; final String PASSWORD4 = "444444" ; //聲明一個靜態常量規定初始金額為100000元 public static final Double BALANCE = 100000.00 ; //創建一個map集合用于存儲用戶賬號和對應的賬戶信息 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)); } /** * 比較用戶從控制臺輸入的數據是否和銀行預先存儲的賬戶一樣 * @param id * @param password * @return * @throws Exception */ public User chick(String id,String password) throws Exception{ User Account = null ; //此時的Account對象為null //密碼已經被放進map集合中,不好取出,所以需要在User類中定義一個checkPassword方法,其返回的是布爾類型的值(詳情見User類) if (map.containsKey(id) ){ Account=map.get(id); //如果賬戶名對上了,則調用map集合的get()方法給Account對象賦值 if (Account.checkPassword(password)){ System.out.println( "登錄成功!" ); //如果密碼也對上了,提示“登錄成功”,否則拋出異常 } else { throw new Exception( "密碼錯誤" ); } } else { throw new Exception( "賬號/密碼錯誤!" ); } return Account; //返回一個Account對象 } /** * 這是一個取錢并將記錄存入list集合的方法 * @param loginUA * @param money */ public void getMoney(User user, double money) { try { //用戶余額已經被放進map集合中,同理,在User類中定義一個getMoney()方法,其返回的是布爾類型的值 if (user.getMoney(money)){ System.out.println( "取款成功,用戶當前余額:" +user.getBalance()); System.out.println( "==============================" ); //輸出日志 for (String str : user.getList()) { System.out.println(str); } } } catch (Exception e) { System.out.println(e.getMessage()); } } } |
運行結果1(正確地輸入了賬號密碼):
運行結果2錯誤地輸入了賬號密碼):
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。