HashMap集合综合运动小Demo(会员积分商城管理系统)

会员积分商城管理系统

/**
 * 实体类
 * @author jiangguolin
 *
 */
public class VipUser {
    
    private String phone;//手机号
    private String password;//密码
    private long score;//积分
    private Date startTime;//开户时间
    
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public long getScore() {
        return score;
    }
    public void setScore(long score) {
        this.score = score;
    }
    public Date getStartTime() {
        return startTime;
    }
    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }
    
    public VipUser(String phone, String password, long score, Date startTime) {
        super();
        this.phone = phone;
        this.password = password;
        this.score = score;
        this.startTime = startTime;
    }

}

/**
 * 功能类
 * @author jiangguolin
 *
 */
public class Function {
    //泛型
    private Map<String, VipUser> map = new HashMap<String, VipUser>();
    

    public Map<String, VipUser> getMap() {
        return map;
    }

    public void setMap(Map<String, VipUser> map) {
        this.map = map;
    }
    
    /*
     *  初始化,给集合里面添加一些元素
     *  代码块--具有代码执行的优先级
     */
    {
        
            //检测异常捕获异常
            try {
                map.put("134", new VipUser("134", "111", 8888, new SimpleDateFormat("yyyy-MM-dd").parse("2016-11-11")));
                map.put("158", new VipUser("158", "123", 9999, new SimpleDateFormat("yyyy-MM-dd").parse("2017-9-3")));
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        
    }
    
    /**
     * 写一个登录的方法
     * @param phone
     * @param password
     * @return
     */
    public VipUser login(String phone,String password){
        //判断map集合里面是否包含phone这个键
        if(!map.containsKey(phone)){
            System.out.println("此电话号不存在!");
            return null;
        }
        //如果代码执行到这里了,说明电话号是存在的
        VipUser vipuser = map.get(phone);
        if(password.equals(vipuser.getPassword())){
            return vipuser;
        }
        //如果代码执行到这里了,说明密码输错了
        System.out.println("密码输入错误");
        return null;
    }

    /**
     * 写一个注册的方法
     */
    public void register(String phone,String password) {
        
        //随机生成会员积分
        long score = (long) (Math.random()*10000);
        //生成当前系统时间
        Date date = new Date();
        
        //打包出一个对象
        VipUser vipUser = new VipUser(phone, password, score, date);
        map.put(phone, vipUser);
        System.out.println("注册成功,您获取了"+score+"分积分,您可以使用"+phone+"这个电话号登录了");
        
    }
    
    /**
     * 登录成功后的展示菜单
     */
    public void showMenu(){
        System.out.println();
        System.out.println("===========会员您好===========");
        System.out.println("\ta,个人信息查看");
        System.out.println("\tb,注销登录");
        System.out.println("请选择(a,b):");
    }
    
    /**
     * 个人信息查看
     */
      public void queryUser(VipUser vip){
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
          Date date = vip.getStartTime();
          String riqi = sdf.format(date);
          System.out.println("您的手机号是:"+vip.getPhone());
          System.out.println("您的密码是:"+vip.getPassword());
          System.out.println("您的积分是:"+vip.getScore());
          System.out.println("您的开户日期是:"+riqi);
      }
    
     public void zhangshi(){
         System.out.println();
        System.out.println("=====欢迎来澳门皇家赌场会员管理系统======");
        System.out.println("\t1,用户登录");
        System.out.println("\t2,注册开户");
        System.out.println("\t3,退出程序");
        System.out.println("请选择你的操作:");
     }

}

/**
 * 测试类
 * @author jiangguolin
 *
 */
public class VipApp {

    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        //定义一个变量
        String ab = "";
        
        //得到功能类的对象,对象名是fun
        Function fun = new Function();
        
        //无限循环,因为条件一直是true
        while(true){
            
            //清空的编程思想
            ab = "";
            
            fun.zhangshi();
            
            int choose = input.nextInt();
            
            switch (choose) {
            case 1:
                System.out.println("请输入你的手机号:");
                String number = input.next();
                System.out.println("请输入你的密码:");
                String pass = input.next();
                VipUser vu = fun.login(number, pass);
                if(vu != null){
                    System.out.println("登录成功!");
                    
                    while(!ab.equals("b")){
                        fun.showMenu();
                        ab = input.next();
                        switch (ab) {
                        case "a":
                            fun.queryUser(vu);
                            break;
                        case "b":
                            
                            break;
                        default:
                            System.out.println("输入有误,请重新输入:");
                            break;
                        }
                    }
                
                }
                //如果vu是空的,我们不用说登录失败,什么都不用做,因为在写方法的时候我们已经校验过了
                    break;
            case 2:
                
                System.out.println("请填写你的手机号:");
                number = input.next();
                System.out.println("请填写你的密码:");
                pass = input.next();
                fun.register(number, pass);
                break;
            case 3:
                System.out.println("您已退出,谢谢使用");
                return;
                
            default:
                System.out.println("输入有误,没有这个功能,请重新输入:");
                break;
            }
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值