超市会员管理系统-使用序列化和反序列化-把会员资料保存在本地

目录

主程序

 1 package com.xiangmu.chaoshihuiyuan.text;
 2 /**
 3  * 超市会员管理系统-测试类
 4  * 使用序列化和反序列化
 5  * 把会员资料保存在本地
 6  */
 7 
 8 public class Test {
 9     public static void main(String[] args) {
10         new Yewu().start();
11     }
12 }

序列化

 1 package com.xiangmu.chaoshihuiyuan.text;
 2 
 3 import java.io.FileNotFoundException;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 import java.io.ObjectOutputStream;
 7 
 8 /**
 9  * 把对象序列化
10  */
11 public class Xuliehua {
12 
13     public void xuliehua(int kahao,Huiyuan1 hy){
14         ObjectOutputStream oos=null;
15         FileOutputStream fos=null;
16         try {
17             fos=new FileOutputStream("c:\\kkk\\zhanghao\\"+kahao+".txt");
18             oos=new ObjectOutputStream(fos);
19             oos.writeObject(hy);
20         } catch (FileNotFoundException e) {
21             e.printStackTrace();
22         } catch (IOException e) {
23             e.printStackTrace();
24         }finally {
25             try {
26                 oos.close();
27                 fos.close();
28             } catch (IOException e) {
29                 e.printStackTrace();
30             }
31         }
32     }
33 }

反序列化

 1 package com.xiangmu.chaoshihuiyuan.text;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.IOException;
 6 import java.io.ObjectInputStream;
 7 
 8 /**
 9  * 反序列化
10  */
11 public class Fanxuliehua {
12     //反序列化方法,返回会员
13     public Huiyuan1 fanxuliehua(int kahao){
14         Huiyuan1 huiyuan=null;
15         ObjectInputStream ois=null;
16         FileInputStream fis=null;
17         try {
18             fis=new FileInputStream("c:\\kkk\\zhanghao\\"+kahao+".txt");
19             ois=new ObjectInputStream(fis);
20             huiyuan=(Huiyuan1)ois.readObject();
21 
22         } catch (FileNotFoundException e) {
23             e.printStackTrace();
24         } catch (IOException e) {
25             e.printStackTrace();
26         } catch (ClassNotFoundException e) {
27             e.printStackTrace();
28         } finally {
29             try {
30                 ois.close();
31                 fis.close();
32             } catch (IOException e) {
33                 e.printStackTrace();
34             }
35         }
36         return huiyuan;
37     }
38 }

业务类

  1 package com.xiangmu.chaoshihuiyuan.text;
  2 /**
  3  * 业务类
  4  */
  5 
  6 import util.Time;
  7 import util.Yanzhengma;
  8 
  9 import java.io.File;
 10 import java.util.*;
 11 
 12 public class Yewu {
 13     Scanner sc = new Scanner(System.in);
 14     Xuliehua xlh=new Xuliehua();//创建序列化对象
 15     Fanxuliehua fxlh=new Fanxuliehua();//创建反序列化对象
 16     File file=null;
 17     Huiyuan1 hy1=null;
 18 
 19 
 20     //    Map<Integer, Huiyuan> map = new HashMap<>();
 21     Random rd = new Random();
 22     int skahao=0;
 23 
 24 
 25     boolean flg1 = true;
 26     boolean flg2 = true;
 27     boolean flg3 = true;
 28     int num;
 29     String mima;
 30     String mima1;
 31     String mima2;
 32 
 33 
 34     //开始
 35     public void start() {
 36         int a = 0;
 37         int b = 0;
 38         String coo1;
 39         System.out.println("---------欢迎进入超市会员管理系统-------");
 40         do {
 41             System.out.println("---------------------------------------");
 42             System.out.println("1.开卡\t2.登录\t3.退出");
 43             System.out.println("---------------------------------------");
 44             System.out.print("请选择:");
 45 
 46                 coo1 = sc.next();
 47 
 48             switch (coo1) {
 49                 case "1":
 50                     //开卡
 51                     kaika();
 52                     break;
 53                 case "2":
 54                     //登录
 55                     denglu();
 56 
 57                     while (flg2) {
 58                         String coo2;
 59                         System.out.println("---------------------------------------------------------------");
 60                         System.out.println("1.积分查询\t2.积分累计\t3.积分兑换\t4.修改密码\t5.回到首页\t6.退出");
 61                         System.out.println("---------------------------------------------------------------");
 62                         System.out.print("请选择:");
 63 
 64                             coo2= sc.next();
 65 
 66                         switch (coo2) {
 67                             case "1":
 68                                 //查询
 69                                 chaxun();
 70                                 break;
 71                             case "2":
 72                                 //累计
 73                                 if (leiji()) {
 74                                     System.out.println("积分累计成功");
 75                                 } else {
 76                                     System.out.println("积分累计失败");
 77                                 }
 78                                 break;
 79                             case "3":
 80                                 //兑换
 81                                 if (duihuan()) {
 82                                     System.out.println("积分兑换成功");
 83                                 } else {
 84                                     System.out.println("积分兑换失败");
 85                                 }
 86                                 break;
 87                             case "4":
 88                                 //修改
 89                                 xiugai();
 90 
 91                                 break;
 92                             case "5":
 93                                 System.out.println("回到首页!");
 94                                 flg1 = true;
 95                                 flg2 = false;
 96                                 break;
 97                             case "6":
 98                                 System.out.println("谢谢使用");
 99                                 flg1=false;
100                                 flg2=false;
101                                 break;
102                             default:
103                                 System.out.println("输入错误,请重新输入");
104                                 flg1 = false;
105                                 break;
106                         }
107                     }
108                     break;
109                 case "3":
110                     flg1 = false;
111                     System.out.println("谢谢使用");
112                     break;
113                 default:
114                     System.out.println("输入错误,请重新输入");
115 
116                     break;
117             }
118         } while (flg1);
119     }
120 
121     //开卡
122     Huiyuan1 hy=null;
123     public void kaika() {
124          hy = new Huiyuan1();
125         System.out.print("请输入注册姓名:");
126         hy.setName(sc.next());
127         int a = 0;
128         while (a == 0) {
129             System.out.print("请输入密码:");
130             mima1 = sc.next();
131             while (mima1.length() < 6) {
132                 System.out.print("密码不能小于6位,请重新输入:");
133                 mima1 = sc.next();
134             }
135             System.out.print("请再一次输入密码:");
136             mima2 = sc.next();
137             if (mima2.equals(mima1)) {
138                 System.out.print("会员注册成功!");
139                 hy.setMima(mima1);
140                 a = -1;
141             } else {
142                 System.out.println("两次输入密码不一致,请重新输入");
143             }
144         }
145         System.out.println("系统赠送您100积分!");
146         hy.setJifen(100);
147         //卡号
148         int kahao = kahao();
149         hy.setKahao(kahao);
150         System.out.println("您的会员卡号是:" + kahao);
151         hy.setRiqi(new Time().time());
152 //        map.put(kahao,hy);
153         xlh.xuliehua(kahao,hy);//把会员序列化
154 
155     }
156 
157 
158     //随机生成卡号
159     public int kahao() {
160         Random rd = new Random();
161         file=new File("c:\\kkk\\zhanghao\\"+num+".txt");
162         num = rd.nextInt(99999999);
163       //  while (num < 10000000||map.containsKey(num)) {
164         while(num<10000000||file.exists()){
165             num = rd.nextInt(99999999);
166         }
167        return num;
168 
169     }
170 
171     //登录验证
172     public void denglu() {
173 
174         Scanner sc=new Scanner(System.in);
175         do {
176             System.out.print("请输入卡号:");
177             try{
178                 skahao = sc.nextInt();
179             }catch (InputMismatchException e){
180                 System.err.println("请正确输入卡号");
181                 e.printStackTrace();
182                 flg2 = false;
183                 break;
184             }
185             Huiyuan1 huiyuan1=fxlh.fanxuliehua(skahao);
186             if (panduan(file,skahao)) {//判断有没有卡号
187                 System.out.print("请输入密码:");
188                 mima = sc.next();
189                 String yzm = new Yanzhengma().yanzhengma(4);
190                 System.out.println("验证码是:"+yzm);
191                 System.out.print("请输入验证码:" );
192                 String yzm1=sc.next();
193                 if ( huiyuan1.getMima().equals(mima)) {
194                     if (yzm.equalsIgnoreCase(yzm1)) {
195                         System.out.println("登录成功!");
196                         flg2 = true;
197                         flg3 = false;
198                     } else {
199                         System.out.println("验证码错误,重新登录");
200                         flg2 = false;
201                         flg3 = true;
202                     }
203                 } else {
204                     System.out.println("密码错误,返回首页");
205                     flg3 = false;
206                 }
207             } else {
208                 System.out.println("未找到此卡号,请返回首页开卡");
209                 flg2 = false;
210                 //登录失败,返回首页a = 0  b = -1;
211             }
212         } while (flg3);
213     }
214 
215     private boolean panduan(File file,int kahao) {
216         file=new File("c:\\kkk\\zhanghao\\"+kahao+".txt");
217         if (file.exists()){
218             return true;
219         }else{
220             return false;
221         }
222     }
223 
224 
225     //积分累计
226     public boolean leiji() {
227         Huiyuan1 hy1=fxlh.fanxuliehua(skahao);
228         Scanner sc=new Scanner(System.in);
229         double jifen1=0;
230         System.out.print("请输入消费的金额(每10元可积1分):");
231         try{
232             jifen1=sc.nextDouble();
233         }catch (InputMismatchException e){
234             System.err.println("请正确输入金额!");
235             e.printStackTrace();
236 
237         }
238 
239         int jifen=((int)jifen1/10);
240         hy1.setJifen((hy1.getJifen()+jifen));
241         xlh.xuliehua(skahao,hy1);
242         System.out.println("本次获得"+jifen+"积分,当前积分总共是"+hy1.getJifen()+"分");
243         return true;
244     }
245 
246     //积分兑换
247     public boolean duihuan() {
248         Huiyuan1 hy1=fxlh.fanxuliehua(skahao);
249         Scanner sc=new Scanner(System.in);
250         double jifen=0;
251         System.out.println("-------积分兑换--------");
252         System.out.println("欢迎会员"+hy1.getName()+",您当前可兑换积分为:"+hy1.getJifen()+"分");
253         System.out.println("温馨提示!每10分可以兑换1元!");
254         System.out.print("请输入要兑换的积分数量:");
255         try {
256             jifen = sc.nextDouble();
257         }catch (InputMismatchException e){
258             System.err.println("请输入正确的积分");
259             e.printStackTrace();
260 
261         }
262         if (hy1.getJifen()>=jifen){
263             int yuan=(int)(jifen/10);
264           hy1.setJifen((hy1.getJifen()-((int)jifen)));
265             System.out.println("可以抵用"+yuan+"元,当前剩余积分:"+hy1.getJifen());
266             xlh.xuliehua(skahao,hy1);
267             return true;
268         }else{
269             System.out.println("积分不足!");
270             return false;
271         }
272     }
273 
274     //积分查询
275     public void chaxun() {
276         Huiyuan1 hy1=fxlh.fanxuliehua(skahao);
277         System.out.println("------------------------------------");
278         System.out.println("姓名\t\t会员卡号\t\t剩余积分\t\t注册时间");
279         System.out.println(hy1.getName() + "\t\t" +
280                hy1.getKahao() + "\t\t" +
281                 hy1.getJifen() + "\t\t" +
282                 hy1.getRiqi());
283     }
284 
285     //修改密码
286     public void xiugai() {
287         Huiyuan1 hy1=fxlh.fanxuliehua(skahao);
288         System.out.println("-----------------");
289         System.out.print("请输入旧密码:");
290         mima = sc.next();
291         System.out.print("请输入新密码:");
292         mima1 = sc.next();
293         while (mima1.length() < 6) {
294             System.out.print("密码不能小于6位,请重新输入:");
295             mima1 = sc.next();
296         }
297         System.out.print("请再一次输入密码:");
298         mima2 = sc.next();
299         if (hy1.getMima().equals(mima)) {
300             if (mima1.equals(mima2)) {
301                 hy1.setMima(mima1);
302                 xlh.xuliehua(skahao,hy1);
303                 System.out.println("密码修改成功!请重新登录!");
304                 flg2 = false;
305             } else {
306                 System.out.println("两次密码输入不一样,修改失败");
307             }
308         } else {
309             System.out.println("密码输入错误,修改失败");
310         }
311 
312     }
313 }

会员类

 1 package com.xiangmu.chaoshihuiyuan.text;
 2 
 3 import java.io.Serializable;
 4 
 5 /**
 6  * 会员类
 7  * 姓名-卡号-密码-积分-开卡日期
 8  */
 9 public class Huiyuan1 implements Serializable {
10     private String name;
11     private int kahao;
12     private String mima;
13     private int jifen;
14     private String riqi;
15 
16     public String getName() {
17         return name;
18     }
19 
20     public void setName(String name) {
21         this.name = name;
22     }
23 
24     public int getKahao() {
25         return kahao;
26     }
27 
28     public void setKahao(int kahao) {
29         this.kahao = kahao;
30     }
31 
32     public String getMima() {
33         return mima;
34     }
35 
36     public void setMima(String mima) {
37         this.mima = mima;
38     }
39 
40     public int getJifen() {
41         return jifen;
42     }
43 
44     public void setJifen(int jifen) {
45         this.jifen = jifen;
46     }
47 
48     public String getRiqi() {
49         return riqi;
50     }
51 
52     public void setRiqi(String riqi) {
53         this.riqi = riqi;
54     }
55 
56     public Huiyuan1() {
57     }
58 
59     public Huiyuan1(String name, int kahao, String mima, int jifen, String riqi) {
60         this.name = name;
61         this.kahao = kahao;
62         this.mima = mima;
63         this.jifen = jifen;
64         this.riqi = riqi;
65     }
66 
67 
68 }

运行结果

本地文档

猜你喜欢

转载自www.cnblogs.com/Fkkkkk/p/10481584.html
今日推荐