随机生成用户信息工具类

说明

一个工具类,帮助随机生成简略的用户信息。特定场合下有用。

代码示例


import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 随机生成用户信息工具类
 *
 * @author: YH
 * @create: 2020-08-21 14:44
 **/
public class RandomUserInfoUtil {
    private static String[] familyName = new String[]{"刘", "张", "李", "胡", "沈", "朱", "钱", "王", "伍", "赵", "孙", "吕", "马", "秦", "毛", "成", "梅", "黄", "郭", "杨", "季", "童", "习", "郑",
            "吴", "周", "蒋", "卫", "尤", "何", "魏", "章", "郎", " 唐", "汤", "苗", "孔", "鲁", "韦", "任", "袁", "贺", "狄朱"};

    private static String[] secondName = new String[]{"艺昕", "红薯", "明远", "天蓬", "三丰", "德华", "歌", "佳", "乐", "天", "燕子", "子牛", "海", "燕", "花", "娟", "冰冰", "丽娅", "大为", "无为", "渔民", "大赋",
            "明", "远平", "克弱", "亦菲", "靓颖", "富城", "岳", "先觉", "牛", "阿狗", "阿猫", "辰", "蝴蝶", "文化", "冲之", "悟空", "行者", "悟净", "悟能", "观", "音", "乐天", "耀扬", "伊健", "炅", "娜", "春花", "秋香", "春香",
            "大为", "如来", "佛祖", "科比", "罗斯", "詹姆屎", "科神", "科蜜", "库里", "卡特", "麦迪", "乔丹", "魔术师", "加索尔", "法码尔", "南斯", "伊哥", "杜兰特", "保罗", "杭州", "爱湘", "湘湘", "昕", "函", "鬼谷子", "膑", "荡",
            "子家", "德利优视", "会谈", "轨迹", "超"};

    private static String[] telFirst = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");

    public static String base = "abcdefghijklmnopqrstuvwxyz0123456789";

    private static final String[] email_suffix = "@gmail.com,@yahoo.com,@msn.com,@hotmail.com,@aol.com,@ask.com,@live.com,@qq.com,@0355.net,@163.com,@163.net,@263.net,@3721.net,@yeah.net,@googlemail.com,@126.com,@sina.com,@sohu.com,@yahoo.com.cn".split(",");

    private static String[] imgList = new String[]{
            "https://t1.hxzdhn.com/uploads/allimg/20150727/pcxcwij20nw.jpg",
            "https://t1.hxzdhn.com/uploads/allimg/20150727/pcxcwij20nw.jpg",
            "https://t1.hxzdhn.com/uploads/allimg/20150727/shx2sl1qh53.jpg",
            "https://t1.hxzdhn.com/uploads/allimg/20150727/shx2sl1qh53.jpg",
            "https://t1.hxzdhn.com/uploads/allimg/20150727/njvjtexina3.jpg",
            "https://t1.hxzdhn.com/uploads/allimg/20150727/njvjtexina3.jpg",
            "https://t1.hxzdhn.com/uploads/allimg/20150727/dc2d3pcpwev.jpg",
            "https://t1.hxzdhn.com/uploads/allimg/20150727/dc2d3pcpwev.jpg",
            "https://t1.hxzdhn.com/uploads/allimg/20150727/2ztid5ylnc4.jpg",
            "https://t1.hxzdhn.com/uploads/allimg/20150727/2ztid5ylnc4.jpg",
            "https://t1.hxzdhn.com/uploads/allimg/20150727/ihjhvrwft5o.jpg",
            "https://t1.hxzdhn.com/uploads/allimg/20150727/ihjhvrwft5o.jpg"
    };
    

    public static int getNum(int start, int end) {
        return (int) (Math.random() * (end - start + 1) + start);
    }

    /**
     * 随机姓名
     *
     * @return
     */
    public static String nextName() {
        return familyName[new Random().nextInt(familyName.length)] + secondName[new Random().nextInt(secondName.length)];
    }

    /**
     * 随机头像
     *
     * @return
     */
    public static String nextImg() {
        return imgList[new Random().nextInt(imgList.length)];
    }

    /**
     * 返回Email
     *
     * @return
     */
    public static String nextEmail() {
        int length = getNum(6, 9);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = (int) (Math.random() * base.length());
            sb.append(base.charAt(number));
        }
        sb.append(email_suffix[(int) (Math.random() * email_suffix.length)]);
        return sb.toString();
    }

    /**
     * 返回手机号码
     */
    public static String nextTel() {
        int index = getNum(0, telFirst.length - 1);
        String first = telFirst[index];
        String second = String.valueOf(getNum(1, 888) + 10000).substring(1);
        String third = String.valueOf(getNum(1, 9100) + 10000).substring(1);
        return first + second + third;
    }

    /**
     * 获取信息集合
     * 
     * @return
     */
    public static Map<String, String> getInfo() {
        Map<String, String> map = new HashMap<>();
        map.put("name:", nextName());
        map.put("tel:", nextTel());
        map.put("email:", nextEmail());
        map.put("img:", nextImg());
        return map;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            System.out.println(getInfo());
        }
    }
}

运行结果展示

posted @ 2020-08-21 17:27  YangJavaer  阅读(398)  评论(0编辑  收藏  举报