1 public class Account {
2 private String cardId;
3 private String userName;
4 private String passWord;
5 private double money;
6 private double quotaMoney;
7
8 public Account() {
9 }
10
11 public Account(String cardId, String userName, String passWord, double quotaMoney) {
12 this.cardId = cardId;
13 this.userName = userName;
14 this.passWord = passWord;
15 this.money = money;
16 this.quotaMoney = quotaMoney;
17 }
18
19 public void setCardId(String cardId) {
20 this.cardId = cardId;
21 }
22
23 public void setUserName(String userName) {
24 this.userName = userName;
25 }
26
27 public void setPassWord(String passWord) {
28 this.passWord = passWord;
29 }
30
31 public void setMoney(double money) {
32 this.money = money;
33 }
34
35 public void setQuotaMoney(double quotaMoney) {
36 this.quotaMoney = quotaMoney;
37 }
38
39 public String getCardId() {
40 return cardId;
41 }
42
43 public String getUserName() {
44 return userName;
45 }
46
47 public String getPassWord() {
48 return passWord;
49 }
50
51 public double getMoney() {
52 return money;
53 }
54
55 public double getQuotaMoney() {
56 return quotaMoney;
57 }
58 }
1 import java.util.ArrayList;
2 import java.util.Random;
3 import java.util.Scanner;
4
5 public class ATMSys {
6 public static void main(String[] args) {
7 // 准备容器,存储账户
8 ArrayList<Account> accounts = new ArrayList<>();
9 showMain(accounts);
10 }
11
12 public static void showMain(ArrayList<Account> accounts){
13 // 准备首页:注册、登录
14 Scanner sc = new Scanner(System.in);
15 while (true) {
16 System.out.println("======请选择你想做的操作======");
17 System.out.println("1.登录");
18 System.out.println("2.注册");
19 int command = sc.nextInt();
20 switch (command){
21 case 1:
22 login(accounts, sc);
23 break;
24 case 2:
25 register(accounts, sc);
26 break;
27 default:
28 System.out.println("不支持当前操作!!!");
29 }
30 }
31 }
32
33 /**
34 * 完成登录
35 */
36 private static void login(ArrayList<Account> accounts, Scanner sc) {
37
38 if (accounts.size() == 0){
39 System.out.println("还没有账户请注册后再来登录");
40 return;
41 }
42 while (true) {
43 System.out.println("输入登陆账号:");
44 String cardId = sc.next();
45 // 根据卡号查询账户
46 Account acc = getAccountByCardId(cardId, accounts);
47 if (acc != null){
48 while (true) {
49 System.out.println("请输入密码:");
50 String password = sc.next();
51 // 判断密码是否正确
52 if (password.equals(acc.getPassWord())){
53 // 登陆成功、进入主页
54 System.out.println("登录成功");
55 showUserCommand(accounts, sc, acc);
56 return; // 结束登录操作
57 }else {
58 System.out.println("密码有误请检查后重新输入");
59 }
60 }
61 }else {
62 System.out.println("账号或密码有误请检查后重新输入");
63 }
64 }
65 }
66
67 private static void showUserCommand(ArrayList<Account> accounts, Scanner sc, Account acc) {
68 while (true) {
69 System.out.println("==========用户操作页面=========");
70 System.out.println("1、查询账户");
71 System.out.println("2、存款");
72 System.out.println("3、取款");
73 System.out.println("4、转账");
74 System.out.println("5、修改密码");
75 System.out.println("6、退出");
76 System.out.println("7、注销账户");
77
78 System.out.println("请输入操作命令");
79 int command = sc.nextInt();
80 switch (command){
81 case 1:
82 // 查询账户
83 showAccount(acc);
84 break;
85 case 2:
86 // 存款
87 depositMoney(acc, sc);
88 break;
89 case 3:
90 // 取款
91 drawMoney(acc, sc);
92 break;
93 case 4:
94 // 转账
95 transferMoney(accounts, acc, sc);
96 break;
97 case 5:
98 // 修改密码
99 updatePassword(acc, sc);
100 return;
101 case 6:
102 // 退出
103 System.out.println("欢迎下次光临");
104 return;
105 case 7:
106 // 注销账户
107 accounts.remove(acc);
108 System.out.println("账户以消除成功");
109 return;
110 default:
111 System.out.println("暂无此命令");
112 }
113 }
114 }
115
116 private static void updatePassword(Account acc, Scanner sc) {
117 System.out.println("=====修改密码=====");
118 while (true) {
119 System.out.println("请输入登录密码");
120 String okPassWord = sc.next();
121 // 判断密码是否正确
122 if (acc.getPassWord().equals(okPassWord)){
123 // 修改成新密码
124 while (true) {
125 System.out.println("请输入新密码");
126 String newPassWord = sc.next();
127 System.out.println("请输入确认密码");
128 String okNewPassWord = sc.next();
129 if (newPassWord.equals(okNewPassWord)){
130 acc.setPassWord(newPassWord);
131 System.out.println("密码修改成功,请重新登录");
132 return;
133 }else {
134 System.out.println("输入的密码错误请重新输入");
135 }
136 }
137 }else {
138 System.out.println("输入的密码不正确");
139 }
140 }
141 }
142
143 /**
144 * 转账
145 */
146 private static void transferMoney(ArrayList<Account> accounts, Account acc, Scanner sc) {
147 // 判断系统中是否有两个用户
148 if (accounts.size() < 2){
149 System.out.println("没有转账目标,无法转账");
150 return;
151 }
152 // 判断账户是否有钱
153 if (acc.getMoney() == 0){
154 System.out.println("账户暂无余额,无法转账操作");
155 }
156 // 转账操作
157 while (true) {
158 System.out.println("请输入对方账户");
159 String cardId = sc.next();
160 Account account = getAccountByCardId(cardId, accounts);
161 // 判断账户是否存在
162 if (account != null){
163 // 判断账户是否合规
164 if (account.getCardId().equals(acc.getCardId())){
165 System.out.println("不支持给自己账户转账");
166 }else {
167 // 确认对方姓氏
168 String name = "*" + account.getUserName().substring(1);
169 System.out.println("请确认【" + name + "】的姓氏");
170 String preName = sc.next();
171 // 认证用户开始转账
172 if (account.getUserName().startsWith(preName)){
173 // startsWith()方法用于检测字符串是否以指定的前缀开始。
174 // 开始转帐
175 System.out.println("请输入转账金额");
176 double money = sc.nextDouble();
177 // 判断是否超过自己账户的金额
178 if (money > acc.getMoney()){
179 System.out.println("账户余额不足为对方转账,账户余额:" + acc.getMoney());
180 }else {
181 // 转账
182 acc.setMoney(acc.getMoney() - money);
183 account.setMoney(account.getMoney() + money);
184 System.out.println("转账给账户:" + account.getUserName() + "转账:" + money + "元");
185 showAccount(acc);
186 return;
187 }
188 }else {
189 System.out.println("认证失败,请检查后在操作");
190 }
191 }
192 }else {
193 System.out.println("账户卡号有问题,暂不支持转账");
194 }
195 }
196 }
197
198 private static void drawMoney(Account acc, Scanner sc) {
199 System.out.println("==========取款操作============");
200 if (acc.getMoney() >= 100){
201 System.out.println("请输入你的取款金额");
202 double money = sc.nextDouble();
203 // 判断取款金额是否超过限额
204 if (money > acc.getQuotaMoney()){
205 System.out.println("你的取款金额超过当次限额,当次限额:" + acc.getQuotaMoney() + "\t元");
206 }else {
207 // 判断当前余额是否足够取款
208 if (acc.getMoney() >= money){
209 acc.setMoney(acc.getMoney() - money);
210 System.out.println("恭喜取款:\t" + money + "\t元" + "成功。");
211 System.out.println("账户余额:" + acc.getMoney());
212 return;
213 }else {
214 System.out.println("余额不足!!!");
215 }
216 }
217 }else {
218 System.out.println("ATM最少取款 100 元整,只能整百取款");
219 }
220 }
221
222 private static void depositMoney(Account acc, Scanner sc) {
223 System.out.println("========存款操作=========");
224 double money = sc.nextDouble();
225 // 将金额存到账户中去
226 acc.setMoney(acc.getMoney() + money);
227 System.out.println("存款完成\t\t" + "存款金额:" + acc.getMoney() + "\t元");
228 }
229
230 private static void showAccount(Account acc) {
231 System.out.println("========账户详情=======");
232 System.out.println("卡号:" + acc.getCardId());
233 System.out.println("用户名:" + acc.getUserName());
234 System.out.println("余额:" + acc.getMoney());
235 System.out.println("当次限额:" + acc.getQuotaMoney());
236 }
237
238 /**
239 * 完成注册
240 */
241 private static void register(ArrayList<Account> accounts, Scanner sc) {
242
243 System.out.println("==========注册开户===========");
244 // 输入账号
245 System.out.println("请输入要注册的账号:");
246 String name = sc.next();
247
248 String password = "";
249 while (true) {
250 System.out.println("请输入账户密码:");
251 password = sc.next();
252 System.out.println("请在此输入密码确保密码一致:");
253 String okPasssword = sc.next();
254 if (okPasssword.equals(password)){
255 // 一致
256 System.out.println("注册开户成功");
257 break;
258 }else {
259 System.out.println("密码予确认密码不一致请重新输入");
260 }
261 }
262
263 // 当次限额
264 System.out.println("请输入当此限额:");
265 Double quotaMoney = sc.nextDouble();
266 // 生成卡号
267 String cardId = createCardId(accounts);
268 // 创建一个对象封装账户信息
269 Account account = new Account(cardId, name, password, quotaMoney);
270 // 将对象添加到集合中去
271 accounts.add(account);
272 System.out.println("开户成功!你得卡号是:" + account.getCardId() + "\t恭喜恭喜");
273
274 }
275
276 /**
277 * 生成八位数账号
278 */
279 public static String createCardId(ArrayList<Account> accounts){
280 // 随机生成八位数卡号
281 while (true) {
282 String cardId = "";
283 Random r = new Random();
284 for (int i = 0; i < 8; i++) {
285 cardId += r.nextInt(10);
286 }
287
288 // 判断卡号是否重复
289 Account acc = getAccountByCardId(cardId, accounts);
290 if (acc == null){
291 return cardId;
292 }
293 }
294 }
295
296 /**
297 * 据卡号查询对象查询账户是否存在
298 */
299 public static Account getAccountByCardId(String cardId,ArrayList<Account> accounts){
300 // 根据卡号查询对象
301 for (int i = 0; i < accounts.size(); i++) {
302 Account acc = accounts.get(i);
303 if (acc.getCardId().equals(cardId)){
304 return acc;
305 }
306 }
307 return null;// 查无此号,为充分
308 }
309 }