这是我的第一篇博客

这也是我学习以来的第一个项目

虽然不完善,但也是我用一天的时间写出来的

首先写了一个类,这个类软件自动生成,比较简单

 1 package com.itheima;
 2 
 3 public class Account {
 4 
 5 
 6     private String cardId;
 7     private String userName; //用户名
 8     private String passWord; //密码
 9     private double money;    //余额
10     private double qutoaMoney;  //取出限额
11 
12     public String getCardId() {
13         return cardId;
14     }
15 
16     public void setCardId(String cardId) {
17         this.cardId = cardId;
18     }
19 
20     public String getUserName() {
21         return userName;
22     }
23 
24     public void setUserName(String userName) {
25         this.userName = userName;
26     }
27 
28     public String getPassWord() {
29         return passWord;
30     }
31 
32     public void setPassWord(String passWord) {
33         this.passWord = passWord;
34     }
35 
36     public double getMoney() {
37         return money;
38     }
39 
40     public void setMoney(double money) {
41         this.money = money;
42     }
43 
44     public double getQutoaMoney() {
45         return qutoaMoney;
46     }
47 
48     public void setQutoaMoney(double qutoaMoney) {
49         this.qutoaMoney = qutoaMoney;
50     }
51 }

然后写入口类

 1 package com.itheima;
 2 
 3 
 4 import java.util.ArrayList;
 5 import java.util.Random;
 6 import java.util.Scanner;
 7 
 8 /*
 9 * ATM的入口类
10 * */
11 public class ATMSystem {
12 
13     public static void main(String[] args){
14 
15         ArrayList<Account> accounts = new ArrayList<>();
16         //展示系统
17         Scanner sc = new Scanner(System.in);
18 
19         while (true) {
20 
21             System.out.println("============龙龙ATM系统=============");
22             System.out.println("1.账号登录");
23             System.out.println("2.账号注册");
24 
25             System.out.println("请您选择操作:");
26 
27             int command = sc.nextInt();
28 
29             switch(command){
30                 case 1:
31                     //用户登录操作
32                     login(accounts,sc);
33                     break;
34                 case 2:
35                     //用户注册操作
36                     register(accounts,sc);
37                     break;
38 
39                 default:
40                     System.out.println("您的输入的操作命令不存在......");
41             }
42         }
43     }

先写了用户注册的方法,要有用户才能登陆

 1   /*
 2 * 系统开户操作
 3 *
 4 * */
 5     private static void register(ArrayList<Account> accounts, Scanner sc) {
 6         System.out.println("============系统开户操作=============");
 7 
 8         while (true) {
 9             System.out.println("请您认真阅读协议,是否同意继续开户:");
10             System.out.println("1.我已认真阅读协议,同意开户");
11             System.out.println("2.不同意,返回首页");
12             int commandd = sc.nextInt();
13 
14             switch(commandd){
15                 case 1:
16                     //同意开户
17                     //创建一个账户对象
18                     Account account = new Account();
19                     //录入当前账户信息
20                     System.out.println("请输入账户用户名:");
21                     String userName = sc.next();
22                     account.setUserName(userName);
23 
24                     while (true) {
25                         System.out.println("请您输入账户密码:");
26                         String passWord = sc.next();
27                         System.out.println("请您输入确认密码:");
28                         String okpassWord = sc.next();
29                         if(okpassWord.equals(passWord)){
30                             //密码确认通过,注入给账户对象
31                             account.setPassWord(okpassWord);
32                             break;
33                         }else{
34                             System.out.println("您输入的两次密码不一致,请重新设置......");
35                         }
36                     }
37 
38                     System.out.println("请您输入账户当次限额:");
39                     double quotaMoney = sc.nextDouble();
40                     account.setQutoaMoney(quotaMoney);
41 
42                     //为账户随机生成一个8位卡号且不重复
43                     String cardId = getRandomcardId(accounts);
44                     //注入卡号
45                     account.setCardId(cardId);
46 
47                     accounts.add(account);
48                     System.out.println("恭喜您,"+userName+"先生/女士,您的卡号为"+cardId+",请妥善保管。");
49                 case 2:
50                     //返回上一级操作
51                     return;
52                 default:
53                     System.out.println("您的输入的操作命令不存在......");
54                     continue;
55             }
56         }
57 
58     }

写生成随机8个数字的卡号,也就是相当于每一个用户的id,而且每个卡号都不能重复

 1 /*
 2 * 生成随机卡号
 3 * */
 4     private static String getRandomcardId(ArrayList<Account> accounts) {
 5         Random random = new Random();
 6         String cardId = null;
 7         while (true) {
 8             cardId = "";
 9             for(int i=0; i<8; i++){
10                 cardId += random.nextInt(10);
11             }
12             //判断卡号是否重复
13             Account acc = getaccountBycardId(cardId,accounts);//检查accounts容器里面有没有生成的这个卡号的方法
14             if(acc == null){
15                 break;
16             }
17         }
18 
19         return cardId;
20     }

随机生成出来的卡号传到容器中检查是否存在

 1 /*
 2 * 检查accounts容器里面有没有生成的这个卡号的方法
 3 * */
 4     private static Account getaccountBycardId(String cardId,ArrayList<Account> accounts) {
 5 
 6         for(int i=0; i<accounts.size(); i++){
 7             Account acc = accounts.get(i);
 8             if(acc.getCardId().equals(cardId)) return acc;
 9         }
10         return null;
11     }

注册完账户,下一步前往登陆,接着写login()方法

 1   /*
 2     * 用户登录界面
 3     * */
 4     private static void login(ArrayList<Account> accounts, Scanner sc) {
 5         System.out.println("============系统登录界面=============");
 6         if(accounts.size()==0){
 7             System.out.println("当前系统无账户,请先前往注册。");
 8             return;
 9         }
10 
11         while (true) {
12             System.out.println("请您输入您的卡号:");
13             String cardId = sc.next();
14             Account acc = getaccountBycardId(cardId,accounts);//去容器ArrayList中找到对应卡号地址返回出来操作
15 
16             if(acc != null){
17                 //账号存在
18                 System.out.println("请输入您的密码:");
19                 while (true) {
20                     String passWord = sc.next();
21                     if(acc.getPassWord().equals(passWord)){
22                         //登录成功
23                         System.out.println("恭喜您,"+acc.getUserName()+"先生/女士进入龙龙ATM自助系统");
24                         System.out.println("您的卡号为:"+acc.getCardId());
25                         showUserCommand(acc,sc,accounts);//进入操作页面的方法
26                         return;
27                     }else{
28                         System.out.println("您输入的密码有误,请重新输入:");
29                     }
30                 }
31             }else{
32                 //账号不存在
33                 System.out.println("系统中无此账户卡号,请您选择操作:");
34                 while (true) {
35                     System.out.println("1.继续登录");
36                     System.out.println("2.前往注册");
37                     int command = sc.nextInt();
38                     switch (command){
39                         case 1:
40                             break;
41                         case 2:
42                             register(accounts,sc);
43                             return;
44                         default:
45                             System.out.println("您输入的操作有误,请您重新选择操作。");
46                     }
47                     break;
48                 }
49                 continue;
50             }
51         }
52 
53 
54     }

进入到用户操作页面

 1 /*
 2 * 用户操作页面
 3 * */
 4     private static void showUserCommand(Account acc, Scanner sc,ArrayList<Account> accounts) {
 5         while (true) {
 6             System.out.println("============用户操作界面=============");
 7             System.out.println("1.查询账户");
 8             System.out.println("2.存款");
 9             System.out.println("3.取款");
10             System.out.println("4.转账");
11             System.out.println("5.修改密码");
12             System.out.println("6.退出");
13             System.out.println("7.注销账户");
14             System.out.println("请您选择操作:");
15             int command = sc.nextInt();
16 
17             switch(command){
18                 case 1:
19                     //1.查询账户
20                     showAccount(acc,sc);
21                    break;
22                 case 2:
23                     //2.存款
24                     dispositMoney(acc,sc);
25                    break;
26                 case 3:
27                     //3.取款
28                     drawMoney(acc,sc);
29                     break;
30                 case 4:
31                     //4.转账
32                     transferMoney(sc,acc,accounts);
33                     break;
34                 case 5:
35                     //5.修改密码
36                     updatapassWord(sc,acc);
37                     return;
38                 case 6:
39                     //6.退出
40                     System.out.println("退出成功,欢迎下次光临");
41                     login(accounts,sc);
42                 case 7:
43                     //7.注销账户
44                     deleteAccount(sc,acc,accounts);
45                     return;
46                 default:
47                     System.out.println("您输入的操作有误,请您重新选择操作。");
48                     continue;
49             }
50         }
51 
52 
53     }

剩下的就是查询账户存款取款转账修改密码退出注销账户的各个操作功能了

 

  1     /**
  2      * 1、查询账户功能
  3      * @param acc 当前用户对象
  4      * @param sc 扫描器
  5      */
  6     private static void showAccount(Account acc, Scanner sc) {
  7         System.out.println("============用户信息界面=============");
  8         System.out.println("用户名:"+acc.getUserName());
  9         System.out.println("卡号:"+acc.getCardId());
 10         System.out.println("余额:"+acc.getMoney());
 11         System.out.println("取现额度:"+acc.getQutoaMoney());
 12         System.out.println("按任意键返回.....");
 13         String next = sc.next();
 14         if(next != null) return;
 15     }
 16 
 17     /**
 18      * 2、用户存款功能
 19      * @param acc 当前对象
 20      * @param sc 扫描器
 21      */
 22     private static void dispositMoney(Account acc, Scanner sc) {
 23         System.out.println("============用户存钱操作=============");
 24         System.out.println("请输入您的存款金额:");
 25         double money = sc.nextDouble();
 26         acc.setMoney(acc.getMoney()+money);
 27         System.out.println("成功存入金额:"+money);
 28     }
 29 
 30     /**
 31      * 3、取钱功能
 32      * @param acc 当前对象
 33      * @param sc 扫描器
 34      */
 35     private static void drawMoney(Account acc, Scanner sc) {
 36         System.out.println("============用户取钱操作=============");
 37         if(acc.getMoney()<100){
 38             System.out.println("您的账户余额不足100元");
 39             return;
 40         }
 41         System.out.println("请输入您的取款金额:");
 42         while (true) {
 43             double money = sc.nextDouble();
 44             if(money>acc.getMoney()){
 45                 System.out.println("您当前的账户余额不足");
 46                 showAccount(acc,sc);
 47             }else if(money>acc.getQutoaMoney()){
 48                 System.out.println("您的取款金额超出取款限额,请重新输入取款金额:");
 49             }else{
 50                 System.out.println("您成功取出金额"+money+"元");
 51                 acc.setMoney(acc.getMoney()-money);
 52                 showAccount(acc, sc);
 53                 return;
 54             }
 55         }
 56     }
 57 
 58     /**
 59      * 4、用户转账功能  转账功能相对复杂一点,判断比较多
 60      * @param sc 扫描器
 61      * @param acc 自己的账户对象
 62      * @param accounts 全部账户的集合
 63      */
 64     private static void transferMoney(Scanner sc, Account acc, ArrayList<Account> accounts) {
 65         System.out.println("============用户转账操作=============");
 66         //判断系统中有没有2个以上地账户
 67         if(accounts.size()<2){
 68             System.out.println("当前系统中,不足两个账户,不能进行转账,先去注册一个吧");
 69             return;
 70         }
 71 
 72         if(acc.getMoney()<1){
 73             System.out.println("您的账户余额为:"+acc.getMoney()+",不能进行转账");
 74             return;
 75         }
 76 
 77         while (true) {
 78             System.out.println("请输入对方卡号:");
 79             String cardId = sc.next();
 80             //判断是不是自己的卡号
 81             if(cardId.equals(acc.getCardId())){
 82                 System.out.println("不能输入自己的卡号");
 83                 continue;
 84             }
 85             //判断该卡号是否存在
 86             Account herAcc = getaccountBycardId(cardId,accounts);
 87             if(herAcc == null){
 88                 System.out.println("对不起,你输入的这个账号不存在");
 89             }else{
 90                 //这个这个卡号对象存在,继续认证姓氏
 91                 String userName = herAcc.getUserName();
 92                 String top = "*"+userName.substring(1);
 93                 System.out.println("请你输入["+top+"]的姓氏");
 94                 String perName = sc.next();
 95                 //认证姓氏是否输入正确
 96                 if(userName.startsWith(perName)){
 97                     while (true) {
 98                         System.out.println("请输入转账金额:");
 99                         double money = sc.nextDouble();
100                         //判断转账金额是否不足
101                         if(money>acc.getMoney()){
102                             System.out.println("您的账户余额不足,你的余额为:"+acc.getMoney()+"元");
103                             continue;
104                         }else{
105                             //开始转账
106                             acc.setMoney(acc.getMoney()-money);
107                             herAcc.setMoney(herAcc.getMoney()+money);
108                             System.out.println("转账成功!您的账户余额为:"+acc.getMoney()+"元");
109                             return;
110                         }
111                     }
112                 }else{
113                     System.out.println("您的输入有误");
114                 }
115             }
116         }
117     }
118 
119    /**
120      * 5、修改密码功能
121      * @param sc
122      * @param acc
123      */
124     private static void updatapassWord(Scanner sc, Account acc) {
125         System.out.println("============用户修改操作=============");
126         while (true) {
127             System.out.println("请您输入原密码:");
128             String oldpassWord = sc.next();
129             if(oldpassWord.equals(acc.getPassWord())){
130                 while (true) {
131                     System.out.println("请您输入新密码:");
132                     String newpassWord = sc.next();
133                     System.out.println("请您确认新密码:");
134                     String okpassWord = sc.next();
135                     //判断两次输入密码是否一致
136                     if(newpassWord.equals(okpassWord)){
137                         acc.setPassWord(okpassWord);
138                         return;
139                     }else{
140                         System.out.println("您输入的两次密码不一致");
141                         continue;
142                     }
143                 }
144             }else{
145                 System.out.println("您输入的密码错误");
146             }
147         }
148     }
149 
150     /**
151      * 6、退出功能
152      * 退出功能比较简单,写在了showUserCommand()(用户选择操作页面)方法中,直接return就行了
153      */
154 
155     /**
156      * 7、注销账户功能
157      * @param sc 扫描器
158      * @param acc 当前账户对象
159      * @param accounts 账户集合容器
160      */
161     private static void deleteAccount(Scanner sc, Account acc, ArrayList<Account> accounts) {
162         System.out.println("============用户注销操作=============");
163         while (true) {
164             System.out.println("您确定注销当前账户吗? y/n");
165             String ok = sc.next();
166             switch (ok){
167                 case "y":
168                     if(acc.getMoney()>0){
169                         System.out.println("您的账户还有余额:"+acc.getMoney()+"元,请先将余额取走再进行销户...");
170                         showUserCommand(acc,sc,accounts);//返回操作页面
171                     }else{
172                         System.out.println("注销账户成功!");
173                         accounts.remove(acc);
174                         return;
175                     }
176 
177                 case "n":
178                     showUserCommand(acc,sc,accounts);//返回操作页面
179                 default:
180                     System.out.println("您输入的操作有误,请重新输入");
181                     continue;
182             }
183         }
184     }

有很多缺点,但值得记录一下