java lesson20homework

  1 package com.xt.lesson20;
  2 /**
  3  * 简易自动提款机
  4 
  5 1. 创建用户类User(包含卡号、姓名、密码、余额等属性),用户开卡时录入的姓名和密码(自动分配一个卡号、初始金额设置为0)。
  6 2. 使用ArrayList或LinkedList存储全部注册用户
  7 形式如:ArrayList<User> userList = new ArrayList<User>();
  8 3. 实现功能
  9 (1)存款
 10 (2)取款(如果余额不足要提示)
 11 (3)查询余额(只能查看当前卡号的余额)
 12 4. 技术要求,使用异常处理用户的错误输入(即程序保护容错功能)。
 13 
 14  * @author LENOVO
 15  *
 16  */
 17 public class User {
 18     private  String ID;//卡号
 19     private   String name;//姓名
 20     private  int password;//密码
 21     private  int balance;//余额
 22     
 23         //构造方法
 24     public User(String ID, String name, int password, int balance) {
 25         super();
 26         this.ID = ID;
 27         this.name = name;
 28         this.password = password;
 29         this.balance = balance;
 30     }
 31     
 32     
 33     
 34 
 35     /**
 36      * @return the iD
 37      */
 38     public String getID() {
 39         return ID;
 40     }
 41 
 42 
 43 
 44 
 45     /**
 46      * @param iD the iD to set
 47      */
 48     public void setID(String ID) {
 49         this.ID = ID;
 50     }
 51 
 52 
 53 
 54 
 55     /**
 56      * @return the name
 57      */
 58     public String getName() {
 59         return name;
 60     }
 61 
 62 
 63 
 64 
 65     /**
 66      * @param name the name to set
 67      */
 68     public void setName(String name) {
 69         this.name = name;
 70     }
 71 
 72 
 73 
 74 
 75     /**
 76      * @return the password
 77      */
 78     public int getPassword() {
 79         return password;
 80     }
 81 
 82 
 83 
 84 
 85     /**
 86      * @param password the password to set
 87      */
 88     public void setPassword(int password) {
 89         this.password = password;
 90     }
 91 
 92 
 93 
 94 
 95     /**
 96      * @return the balance
 97      */
 98     public int getBalance() {
 99         return balance;
100     }
101 
102 
103 
104 
105     /**
106      * @param balance the balance to set
107      */
108     public void setBalance(int balance) {
109         this.balance = balance;
110     }
111 
112 
113 
114 
115     //存款功能的实现
116     public boolean saveMoney(int m){
117         System.out.println("正在验钞,请稍后.......");
118         if(m<0){
119             if(checkMoney(-m)){
120                 return true;
121             }else{
122                 return false;
123             }
124             
125         }else if(m>0){
126             this.balance+=m;
127             System.out.println("操作成功!");
128             return true;
129         }
130         return false;
131     }
132     
133     //取款操作
134     public boolean checkMoney(int m){
135         if(m<0){
136             if(saveMoney(-m)){
137                 return true;
138             }else{
139                 return false;
140             }
141             
142         }else if(m>0){
143             System.out.println("请输入密码......");
144             if(checkPassword()){
145             this.balance-=m;
146             System.out.println("操作成功!");
147             return true;
148             }
149         }
150         return false;
151     }
152     //查询余额
153     public void  search(){
154         System.out.println("正在处理,请稍后......");
155         if(ID.equals(this.ID)){
156             System.out.println("账户"+ID+"的余额为:"+balance);
157         }else{
158             System.out.println("您输入的账户不存在,请认真核查后重新输入!");
159         }
160     }
161     
162     //检验密码操作
163     public boolean checkPassword(){
164         int i;
165         for(i=0;i<3;i++){
166             if(i>0){
167                 System.out.println("密码输入错误,请重新输入密码!");
168             }
169             int pas=EnterNum.enterNum();
170             if(pas==password){
171                 return true;
172             }
173         }
174         if(i==3)
175             System.out.println("密码输入错误3次,此次操作中断!");
176         return false;
177     }
178     //选择操作方法
179     public void print(){
180         System.out.println("----------------------欢迎使用自动取款机------------------------");
181         System.out.println("[存款----1]");
182         System.out.println("[取款----2]");
183         System.out.println("[查询----3]");
184         System.out.println("[退卡----0]");
185     }
186 
187 }
188 
189 
190 
191 
192 
193 
194 //写一个类,进行对输入数字的判断。
195 
196 package com.xt.lesson20;
197 
198 import java.util.Scanner;
199 
200 public class EnterNum {
201     static Scanner scanner=new Scanner(System.in);
202     public static String enterStr(){
203         String str="";
204         try{
205             str=scanner.next();
206         }catch(Exception e){
207             
208         }    
209         return str;
210 
211     }
212     public static  int enterNum(){
213         int m = 0;
214         
215         try{
216              m=Integer.parseInt(enterStr());
217         }catch(Exception e){
218             System.out.println("您输入的数据不合法!请输入整数......");
219         }
220         return m;
221     }
222 
223 }
224 
225 
226 
227 
228 //写一个主方法,进行判断操作的选择。
229 
230 package com.xt.lesson20;
231 
232 import java.util.ArrayList;
233 
234 public class MachineTest {
235     
236     
237     public static void main(String[] args) {
238         ArrayList<User> userList=new ArrayList<User>();
239         userList.add(new User("未知","未知",0,0));//默认值
240         userList.add(new User("200981501","聂庆平",5264,7000));
241         userList.add(new User("200981512","黎明",1562,17000));
242         first:while(true){
243                 userList.get(0).print();
244                 int a=EnterNum.enterNum();
245             second:    switch (a){
246                 case 1:{
247                     System.out.println("请输入您要存钱的账号:");
248                     String id=EnterNum.enterStr();
249                     int i;
250                     for (i=0;i<userList.size();i++){
251                         if(id.equals(userList.get(i).getID())){
252                             System.out.println("请输入您存的钱数:");
253                             int m=EnterNum.enterNum();
254                             if(userList.get(i).saveMoney(m)){
255                             break second;
256                             }
257                         }
258                     }
259                     if(i==userList.size()){
260                         System.out.println("您输入的账户不存在!");
261                         break second;
262                     }
263                 }
264                 case 2:{
265                     System.out.println("请输入您要取钱的账号:");
266                     String id=EnterNum.enterStr();
267                     int i;
268                     for(i=0;i<userList.size();i++){
269                         if(id.equals(userList.get(i).getID())){
270                             System.out.println("请输入您取的钱数:");
271                             int m=EnterNum.enterNum();
272                             userList.get(i).checkMoney(m);
273                             break second;
274                         }
275                     }
276                     if(i==userList.size()){
277                         System.out.println("您输入的账户不存在!");
278                         break second;
279                 }
280                 }
281                 case 3:{
282                     System.out.println("请输入您的卡号!");
283                     String id=EnterNum.enterStr();
284                     int i;
285                     for(i=0;i<userList.size();i++){
286                         if(id.equals(userList.get(i).getID())){
287                             userList.get(i).search();
288                             break second;
289                         }
290                     }
291                     if(i==userList.size()){
292                         System.out.println("您输入的账户不存在!");
293                         break second;
294                 }
295                 }
296                 case 0:{
297                     System.out.println("感谢您的使用,再见!");
298                     System.exit(0);
299                 }
300             
301             }    
302             
303         }
304     }
305 
306 }

 

posted @ 2017-08-14 14:24  木&子  阅读(262)  评论(0编辑  收藏  举报