0308作业02
1,注册
package com.kgc.zuoye.zy0308.zy02; import java.util.Map; import java.util.Scanner; //开卡 public class Newcard { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { Member member = new Member(); Map map =(Map)new ObjectInputStreamDemo().ois(); System.out.println("请输入姓名:"); member.setName(sc.next()); System.out.println("请输入账号:"); member.setCard(sc.next()); System.out.println("请输入密码:"); member.setPassword(sc.next()); System.out.println("注册成功!"); map.put(member.getCard(),member); new ObjectOutputStreamDemo().oos(map); } }
2.登录
1 package com.kgc.zuoye.zy0308.zy02; 2 3 import java.util.Map; 4 import java.util.Scanner; 5 6 //登录 7 public class Register { 8 static Scanner sc=new Scanner(System.in); 9 public static void main(String[] args){ 10 11 Map<String,Member> map=(Map)new ObjectInputStreamDemo().ois(); 12 System.out.println("请输入登录账号:"); 13 String card=sc.next(); 14 System.out.println("请输入密码:"); 15 String password=sc.next(); 16 17 if (map.containsKey(card)){ 18 if (map.get(card).getPassword().equals(password)){ 19 System.out.println("登录成功"); 20 }else{ 21 System.out.println("密码错误"); 22 } 23 }else{ 24 System.out.println("账号不存在"); 25 } 26 } 27 }
3.会员
1 package com.kgc.zuoye.zy0308.zy02; 2 3 import java.io.Serializable; 4 5 //会员 6 public class Member implements Serializable { 7 private String name; 8 private String card; 9 private String password; 10 11 @Override 12 public String toString() { 13 return "Member{" + 14 "name='" + name + '\'' + 15 ", card='" + card + '\'' + 16 ", password='" + password + '\'' + 17 '}'; 18 } 19 20 public String getName() { 21 return name; 22 } 23 24 public void setName(String name) { 25 this.name = name; 26 } 27 28 public String getCard() { 29 return card; 30 } 31 32 public void setCard(String card) { 33 this.card = card; 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 Member() { 45 } 46 47 public Member(String name, String card, String password) { 48 this.name = name; 49 this.card = card; 50 this.password = password; 51 } 52 }
4.序列化
1 package com.kgc.zuoye.zy0308.zy02; 2 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.io.ObjectOutputStream; 6 import java.util.Map; 7 8 //序列化 9 public class ObjectOutputStreamDemo { 10 11 public void oos(Map map){ 12 ObjectOutputStream oos=null; 13 14 try { 15 oos=new ObjectOutputStream(new FileOutputStream("c:\\kkk\\zh.txt")); 16 oos.writeObject(map); 17 oos.flush(); 18 } catch (IOException e) { 19 e.printStackTrace(); 20 }finally { 21 try { 22 if (oos!=null){ 23 oos.flush(); 24 } 25 } catch (IOException e) { 26 e.printStackTrace(); 27 } 28 } 29 } 30 }
5.反序列化
package com.kgc.zuoye.zy0308.zy02; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.util.HashMap; import java.util.Map; //反序列化 public class ObjectInputStreamDemo { public Map<String,Member> ois(){ Map<String,Member> map=new HashMap(); ObjectInputStream ois=null; try { ois=new ObjectInputStream(new FileInputStream("c:\\kkk\\zh.txt")); map=(Map)ois.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }finally { try { if (ois!=null){ ois.close(); } } catch (IOException e) { e.printStackTrace(); } } return map; } }
6.登录运行结果