Java 自动创建会员信息--包含用户名,卡号,密码,电话号码
Posted twuxian
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 自动创建会员信息--包含用户名,卡号,密码,电话号码相关的知识,希望对你有一定的参考价值。
|--需求说明
1、自动注册一堆会员账号并保存为本地文件
2、关闭程序,载入本地文件,可以用注册出来的会员账号及密码登录
|--实现方式
1、使用随机的方式生成会员信息
2、使用ObjectOutputStream和ObjectInputStream 对会员信息进行保存和读取
|--代码内容
1 package cn.save; 2 3 import java.io.Serializable; 4 5 /** 6 * @auther::9527 7 * @Description: 用户类 8 * @program: shi_yong 9 * @create: 2019-08-02 15:45 10 */ 11 public class User implements Serializable 12 private String name; 13 private String cardId; 14 private String phoneNum; 15 private String passWord; 16 17 public User() 18 19 20 public User(String name, String cardId, String phoneNum, String passWord) 21 this.name = name; 22 this.cardId = cardId; 23 this.phoneNum = phoneNum; 24 this.passWord = passWord; 25 26 27 public String getName() 28 return name; 29 30 31 public void setName(String name) 32 this.name = name; 33 34 35 public String getCardId() 36 return cardId; 37 38 39 public void setCardId(String cardId) 40 this.cardId = cardId; 41 42 43 public String getPhoneNum() 44 return phoneNum; 45 46 47 public void setPhoneNum(String phoneNum) 48 this.phoneNum = phoneNum; 49 50 51 public String getPassWord() 52 return passWord; 53 54 55 public void setPassWord(String passWord) 56 this.passWord = passWord; 57 58 59 @Override 60 public String toString() 61 return "User" + 62 "name=‘" + name + ‘\\‘‘ + 63 ", cardId=‘" + cardId + ‘\\‘‘ + 64 ", phoneNum=‘" + phoneNum + ‘\\‘‘ + 65 ", passWord=‘" + passWord + ‘\\‘‘ + 66 ‘‘; 67 68
1 package cn.save; 2 3 import com.sun.xml.internal.txw2.output.DumpSerializer; 4 5 import java.util.ArrayList; 6 import java.util.HashMap; 7 import java.util.Map; 8 import java.util.Random; 9 10 /** 11 * @auther::9527 12 * @Description: 13 * @program: shi_yong 14 * @create: 2019-08-02 15:48 15 */ 16 public class Meth 17 18 //创建HashMap的方法 19 public HashMap<String, User> getMap(String name, String cardId, String phoneNum, String passWord) 20 HashMap<String, User> map = new HashMap<>(); 21 User user = new User(); 22 user.setName(name); 23 user.setCardId(cardId); 24 user.setPhoneNum(phoneNum); 25 user.setPassWord(passWord); 26 27 map.put(user.getName(),user); 28 29 30 31 return map; 32 33 34 //批量创建会员卡,创建的会员信息是传参加1 35 public HashMap<String, User> getMap(int num) 36 HashMap<String, User> map = new HashMap<>(); 37 //恒定创建一个tom的会员信息,以便记忆 38 User us1 = new User("tom","cp936","15393571586","95273"); 39 map.put(us1.getName(),us1); 40 for (int i = 0; i < num; i++) 41 //String name, String cardId, String phoneNum, String passWord 42 User user = new User(); 43 user.setName(name()); 44 user.setCardId(cardId()); 45 user.setPhoneNum(phoneNum()); 46 user.setPassWord(getPWD()); 47 map.put(user.getName(),user); 48 49 50 return map; 51 52 53 54 55 //随机生成6位密码的方法 56 public String getPWD() 57 String code = ""; 58 for (int i = 0; i < 6; i++) 59 //设定布尔值,根据布尔值确定本轮使用数字还是使用字母 60 boolean boo = ((int) (Math.random() * 2)) == 0 ? true : false; 61 if (boo) 62 //如果布尔为真,则本次密码加一个数字 63 code += (int) (Math.random() * 10); 64 else 65 int temp = ((int) (Math.random() * 2)) == 0 ? 65 : 97; 66 code += (char) ((int) (Math.random() * 26) + temp); 67 68 69 return code; 70 71 72 //随机创建帐号的方法 73 public String cardId() 74 Random random = new Random(); 75 String cardId = String.valueOf(random.nextInt(99999999)); 76 return cardId; 77 78 79 //生成随机电话号码的方法 80 public String phoneNum() 81 //给予真实的初始号段,号段是在百度上面查找的真实号段 82 String[] start = "133", "149", "153", "173", "177", 83 "180", "181", "189", "199", "130", "131", "132", 84 "145", "155", "156", "166", "171", "175", "176", "185", "186", "166", "134", "135", 85 "136", "137", "138", "139", "147", "150", "151", "152", "157", "158", "159", "172", 86 "178", "182", "183", "184", "187", "188", "198", "170", "171"; 87 //随机出一个号段 88 String fistNum = start[(int) (Math.random() * start.length)]; 89 //随机出一个尾号 90 String lastNum = ""; 91 for (int i = 0; i < 8; i++) 92 lastNum += (int) (Math.random() * 10); 93 94 //手机号就是手机号段+尾号 95 String phoneNum = fistNum + lastNum; 96 return phoneNum; 97 98 99 //创建用户名的方法 100 public String name() 101 String code = ""; 102 for (int i = 0; i < 4; i++) 103 code += (char) ((int) (Math.random() * 26) + 97); 104 105 return code; 106 107 108 109 110 111 112
1 package cn.save; 2 3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.ObjectOutputStream; 7 import java.util.HashMap; 8 9 /** 10 * @auther::9527 11 * @Description: 保存批量注册的信息的程序入口 12 * @program: shi_yong 13 * @create: 2019-08-02 17:46 14 */ 15 public class Save 16 public static void main(String[] args) 17 HashMap<String, User> map = new HashMap<>(); 18 Meth meth = new Meth(); 19 //随机创建10+1组帐号密码 20 map = meth.getMap(10); 21 22 //创建对象输出的方式 23 ObjectOutputStream oos = null; 24 FileOutputStream fos = null; 25 26 try 27 fos = new FileOutputStream("d:/userinfo"); 28 oos = new ObjectOutputStream(fos); 29 oos.writeObject(map); 30 oos.flush(); 31 catch (FileNotFoundException e) 32 e.printStackTrace(); 33 catch (IOException e) 34 e.printStackTrace(); 35 finally 36 try 37 oos.close(); 38 fos.close(); 39 System.out.println("文件已保存并关闭相应进程"); 40 catch (IOException e) 41 e.printStackTrace(); 42 43 44 45
1 package cn.save; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 import java.io.ObjectInputStream; 7 import java.util.HashMap; 8 import java.util.Scanner; 9 10 /** 11 * @auther::9527 12 * @Description: 13 * @program: shi_yong 14 * @create: 2019-08-02 15:45 15 */ 16 public class Register 17 public static void main(String[] args) 18 Scanner scanner = new Scanner(System.in); 19 ObjectInputStream ois = null; 20 FileInputStream fis = null; 21 HashMap<String,User> map = null; 22 23 try 24 fis = new FileInputStream("d:/userinfo"); 25 ois = new ObjectInputStream(fis); 26 27 map = (HashMap<String, User>)ois.readObject(); 28 29 System.out.println("请输入会员名"); 30 String name = scanner.next(); 31 System.out.println("请输入你的密码"); 32 String pwd = scanner.next(); 33 34 if (map.containsKey(name)) 35 if (map.get(name).getPassWord().equals(pwd)) 36 System.out.println("登录成功,你的会员信息如下: \\n卡号"+map.get(name).getCardId() 37 +"\\n电话号码:"+map.get(name).getPhoneNum()); 38 39 40 System.out.println("登录成功,你的会员信息如下: \\n卡号"+map.get(name).toString()); 41 42 else 43 System.out.println("没有找到这个会员名"); 44 45 46 47 48 49 catch (FileNotFoundException e) 50 e.printStackTrace(); 51 catch (IOException e) 52 e.printStackTrace(); 53 catch (ClassNotFoundException e) 54 e.printStackTrace(); 55 56 57 58
|--运行结果
以上是关于Java 自动创建会员信息--包含用户名,卡号,密码,电话号码的主要内容,如果未能解决你的问题,请参考以下文章