ArrayList集合模拟实现ATM系统

 1 //定义账户类,封装用户的账户信息
 2 public class Account {
 3     private String id; //卡号
 4     private String name; //姓名
 5     private String password; //密码
 6     private double balance; //余额
 7     private double quota; //限额
 8 
 9     public Account() {
10     }
11 
12     public Account(String id, String name, String password, double balance, double quota) {
13         this.id = id;
14         this.name = name;
15         this.password = password;
16         this.balance = balance;
17         this.quota = quota;
18     }
19 
20     public String getId() {
21         return id;
22     }
23 
24     public void setId(String id) {
25         this.id = id;
26     }
27 
28     public String getName() {
29         return name;
30     }
31 
32     public void setName(String name) {
33         this.name = name;
34     }
35 
36     public String getPassword() {
37         return password;
38     }
39 
40     public void setPassword(String password) {
41         this.password = password;
42     }
43 
44     public double getBalance() {
45         return balance;
46     }
47 
48     public void setBalance(double balance) {
49         this.balance = balance;
50     }
51 
52     public double getQuota() {
53         return quota;
54     }
55 
56     public void setQuota(double quota) {
57         this.quota = quota;
58     }
59 }

系统页:

  1 public class ATMSystem {
  2     public static void main(String[] args) {
  3         //定义一个ArrayList集合用于存放每一个账户对象
  4         ArrayList<Account> accounts = new ArrayList<>();
  5 
  6         //首页:登录、开户
  7         showMain(accounts);
  8     }
  9 
 10     public static void showMain(ArrayList<Account> accounts){
 11         System.out.println("###########欢迎来到首页#############");
 12         System.out.println("请选择:");
 13         System.out.println("1、登录");
 14         System.out.println("2、开户");
 15         Scanner sc = new Scanner(System.in);
 16         Random rand = new Random();
 17         while (true) {
 18             String input = sc.next();
 19             switch (input){
 20                 case "1":
 21                     //登录
 22                     login(accounts,sc);
 23                     break;
 24 
 25                 case "2":
 26                     //开户
 27                     register(accounts,sc,rand);
 28                     break;
 29                 default:
 30                     System.out.println("您的操作不被支持,请重新选择");
 31             }
 32         }
 33     }
 34 
 35     private static void register(ArrayList<Account> accounts, Scanner sc, Random rand) {
 36         //创建Account对象,封装用户数据
 37         Account account = new Account();
 38         System.out.println("###########注册界面############");
 39         //循环11次随机生成卡号
 40         String id = "";
 41         String allNumbers = "0123456789";
 42         while (true) {
 43             for (int i = 0; i < 11; i++) {
 44                 int eachRandom = rand.nextInt(10); //随机数0--9
 45                 id += allNumbers.charAt(eachRandom);
 46             }
 47             //查询是否与系统卡号相同
 48             boolean ret = idisRepetition(accounts,id);
 49             if(!ret){
 50                 account.setId(id); //设置卡号
 51                 break;
 52             }
 53         }
 54         System.out.println("请输入您的姓名:");
 55         String inputName = sc.next();
 56         account.setName(inputName); //设置姓名
 57         while (true) {
 58             System.out.println("请输入开户密码:");
 59             String inputPassword = sc.next();
 60             System.out.println("确认密码:");
 61             String affirmPassword = sc.next();
 62             //检查两次密码输入是否一致
 63             if(inputPassword.equals(affirmPassword)){
 64                 //说明两次密码输入一致,可以作为账户密码,随便一个密码即可
 65                 account.setPassword(inputPassword);  //设置密码
 66                 break;
 67             }
 68             else {
 69                 System.out.println("两次密码输入不一致,请重新设置");
 70             }
 71         }
 72         System.out.println("请输入单次取现额度:");
 73         double inputQuota = sc.nextDouble();
 74         account.setQuota(inputQuota); //设置单次取现额度
 75 
 76         //把account对象添加到ArrayList集合中去
 77         accounts.add(account);
 78 
 79         //提示用户开户成功:
 80         System.out.println("#######开户成功########");
 81         System.out.println("您的卡号为:" + account.getId() + ",请妥善保管");
 82 
 83         //跳转到登录界面让用户登录
 84         showMain(accounts);
 85     }
 86 
 87     private static boolean idisRepetition(ArrayList<Account> accounts, String id){
 88         for (int i = 0; i < accounts.size(); i++) {
 89             //说明有重复的卡号
 90             if(id.equals(accounts.get(i).getId())){
 91                 return true;
 92             }
 93         }
 94         return false; //说明没有重复的卡号
 95     }
 96 
 97     private static void login(ArrayList<Account> accounts, Scanner sc) {
 98         System.out.println("###########登录界面###########");
 99         while (true) {
100             System.out.println("请输入卡号:");
101             String inputId = sc.next();
102             System.out.println("请输入密码:");
103             String inputPassword = sc.next();
104             //根据用户输入的卡号和密码从accounts集合中查找该账户
105             //创建Account账户类对象接收查询结果,该结果用于后期该账户对象的详细信息
106             Account ret = searchIdAndPwdFromAccounts(accounts,inputId,inputPassword);
107             if(ret == null){
108                 System.out.println("登录失败(可能原因):账户不存在 || 用户名、密码输入错误");
109                 showMain(accounts);
110             }
111             else {
112                 System.out.println("登录成功");
113                 System.out.println("当前账户为:" + ret.getId());
114                 //登录成功之后展示用户操作界面
115                 showUserCommand(ret,sc,accounts);
116                 //用户选择退出账户即把showUserCommand干掉,然后展示首页
117                 showMain(accounts);
118             }
119         }
120     }
121 
122     private static void showUserCommand(Account account, Scanner sc, ArrayList<Account> accounts) {
123         System.out.println("###########用户操作界面###########");
124         while (true) {
125             System.out.println("请选择交易类型:");
126             System.out.println("1、查询账户详细信息");
127             System.out.println("2、存款");
128             System.out.println("3、取款");
129             System.out.println("4、转账");
130             System.out.println("5、修改密码");
131             System.out.println("6、退出");
132             System.out.println("7、注销账户");
133             int input = sc.nextInt();
134             switch (input){
135                 case 1:
136                     //展示用户详细信息;
137                     showUserDetail(account);
138                     break;
139                 case 2:
140                     //存款
141                     saveMoney(account,sc);
142                     break;
143                 case 3:
144                     //取款
145                     withdrawl(account,sc);
146                     break;
147                 case 4:
148                     //转账
149                     transferAccounts(accounts,account,sc);
150                     break;
151                 case 5:
152                     //修改密码
153                     modifyPassword(account,sc);
154                     break;
155                 case 6:
156                     //退出
157                     System.out.println("退出系统");
158                     return;
159                 case 7:
160                     //注销账户
161                     removeAccount(accounts,account);
162                     return;
163                     //break;
164                 default:
165                     System.out.println("当前操作不被支持,请重新选择");
166             }
167         }
168     }
169 
170     //注销账户
171     private static void removeAccount(ArrayList<Account> accounts, Account account) {
172         accounts.remove(account);
173         System.out.println("该账户已被注销");
174     }
175 
176     //修改密码
177     private static void modifyPassword(Account account, Scanner sc) {
178         System.out.println("##########修改密码界面############");
179         while (true) {
180             System.out.println("请输入原来的密码进行确认");
181             String laterPassword = sc.next();
182             if(laterPassword.equals(account.getPassword())){
183                 while (true){
184                     if(laterPassword.equals(account.getPassword())){
185                         //相等
186                         System.out.println("请输入新密码:");
187                         String modifyPassword = sc.next();
188                         System.out.println("确认新密码:");
189                         String repetitionPassword = sc.next();
190                         if(modifyPassword.equals(repetitionPassword)){
191                             //两次密码输入一致
192                             account.setPassword(modifyPassword);
193                             System.out.println("密码修改成功");
194                             return;
195                         }
196                         else {
197                             System.out.println("两次密码输入不一致,请重新设置");
198                             break;
199                         }
200                     }
201                 }
202             }
203             else {
204                 System.out.println("密码输入有误,请重新输入");
205             }
206         }
207     }
208 
209     //转账
210     private static void transferAccounts(ArrayList<Account> accounts, Account account, Scanner sc) {
211         System.out.println("###########转账界面##############");
212         while (true) {
213         System.out.println("请输入对方账户的卡号:");
214         String inputId = sc.next();
215         //根据卡号在账户类集合中查找是否存在该账户,ret为对方账户对象,account为自己账户对象
216         Account ret = searchIdFromAccounts(accounts,inputId);
217         if(ret == null){
218             System.out.println("当前系统不存在该账户");
219         }
220         else {
221             //确认对方账户姓名:
222             String name = ret.getName();
223             String shieldName = name.substring(1);
224                 System.out.println("请输入对方的姓氏进行确认:" + "*" + shieldName);
225                 String familyName = sc.next();
226                 if(name.startsWith(familyName)){
227                         //确认成功,开始进行转账操作
228                         System.out.println("请输入转账金额:");
229                         double transferMoney = sc.nextDouble();
230                         if(transferMoney > account.getBalance()){
231                             System.out.println("余额不足,当前账户余额为:" + account.getBalance());
232                             System.out.println("请重新输入转账金额");
233                         }
234                         else {
235                             if(transferMoney > account.getQuota()){
236                                 System.out.println("单次转账额度大于限额,转账失败!" + "单次取现额度为:" + account.getQuota());
237                                 System.out.println("请重新输入转账金额");
238                             }
239                             else {
240                                 account.setBalance(account.getBalance() - transferMoney); //当前账户:原余额 - 取走的钱
241                                 ret.setBalance(ret.getBalance() + transferMoney); //对方账户: 原余额 + 存入的钱
242                                 System.out.println("已成功转账");
243                                 System.out.println("当前账户余额为:" + account.getBalance());
244                                 break;
245                             }
246                         }
247                 }
248                 else {
249                     System.out.println("姓氏错误,请重新输入");
250                 }
251             }
252         }
253     }
254 
255     //根据卡号查找账户
256     private static Account searchIdFromAccounts(ArrayList<Account> accounts, String inputId) {
257         for (int i = 0; i < accounts.size(); i++) {
258             if(inputId.equals(accounts.get(i).getId())){
259                 //说明存在该账户对象
260                 return accounts.get(i);
261             }
262         }
263         return null;
264     }
265 
266     //取款
267     private static void withdrawl(Account account, Scanner sc) {
268         System.out.println("#########取款页面###########");
269         System.out.println("请输入取款金额:");
270         while (true) {
271             double withdrawlMoney = sc.nextDouble();
272             if(withdrawlMoney > account.getBalance()){
273                 System.out.println("余额不足,当前账户余额为:" + account.getBalance());
274                 System.out.println("请重新输入取款金额");
275             }
276             else {
277                 if(withdrawlMoney > account.getQuota()){
278                     System.out.println("单次取现额度大于限额,取款失败!" + "单次取现额度为:" + account.getQuota());
279                     System.out.println("请重新输入取款金额");
280                 }
281                else {
282                     account.setBalance(account.getBalance() - withdrawlMoney); //原余额 - 取走的钱
283                     System.out.println("已成功取款");
284                     System.out.println("当前账户余额为:" + account.getBalance());
285                     break;
286                 }
287             }
288         }
289 
290     }
291 
292     //存款
293     private static void saveMoney(Account account,Scanner sc) {
294         System.out.println("###########存款页面############");
295         System.out.println("请输入您要存入的金额:");
296         double saveMoney = sc.nextDouble();
297         account.setBalance(account.getBalance() + saveMoney); //余额加存入的钱
298         System.out.println("存款完成!");
299         System.out.println("当前账户余额为:" + account.getBalance());
300     }
301 
302     //账户详情:根据账户对象得出
303     private static void showUserDetail(Account account) {
304         System.out.println("##########账户详情页##########");
305         System.out.println("卡号:" + account.getId());
306         System.out.println("姓名:" + account.getName());
307         System.out.println("余额:" + account.getBalance());
308         System.out.println("单次交易限额:" + account.getQuota());
309     }
310 
311     //通过卡号和密码从accounts集合中查找该账户对象
312     private static Account searchIdAndPwdFromAccounts(ArrayList<Account> accounts, String inputId, String inputPassword) {
313         for (int i = 0; i < accounts.size(); i++) {
314             if(accounts.get(i).getId().equals(inputId) && accounts.get(i).getPassword().equals(inputPassword)){
315                 //说明存在该账户
316                 return accounts.get(i);
317             }
318         }
319         return null;
320     }
321 }

运行示例(由于功能较为繁杂,在此只罗列出大概的功能):

 

 

 

 

 

 

posted @ 2022-04-17 00:57  羽梦齐飞  阅读(131)  评论(0)    收藏  举报