package com.myshoop; //会员类 public class Member { private String name; //姓名 private int num; //会员卡号 private String password; //密码 private int score; //积分 private String carDate; //开卡日期 public Member() { super(); } public Member(String name, int num, String password, int score, String carDate) { super(); this.name = name; this.num = num; this.password = password; this.score = score; this.carDate = carDate; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public String getCarDate() { return carDate; } public void setCarDate(String carDate) { this.carDate = carDate; } }
package com.myshoop; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Random; import java.util.Scanner; //业务类 public class MemberDao { Member m = null; static Scanner sc = new Scanner(System.in); // 写一个集合用来存储会员用户的 List<Member> memberList = new ArrayList<Member>(); // 写方法 // 开始菜单 // 菜单 public void menu() { System.out.println("***欢迎来到新嘉隆线上超市******"); System.out.println("**********************"); System.out.println("1、积分累计 2、积分兑换 3、查询剩余积分 4、修改密码 5、开卡 6、退出"); System.out.println("请选择:"); } // 开始 public void start() { do { menu(); String choose = sc.next(); switch (choose) { case "1": // 积分累计 scoreAdd(); if (scoreAdd() == true) { System.out.println("积分累计成功!Q"); } else if (scoreAdd() == false) { System.out.println("积分累计失败!N"); } continue; case "2": // 积分兑换 scoreChange(); if (scoreChange() == true) { System.out.println("积分兑换成功!"); } else if (scoreChange() == false) { System.out.println("积分兑换失败!"); } continue; case "3": // 查询剩余积分 finaScore(); continue; case "4": // 修改密码 changePassword(); continue; case "5": // 开卡 openCar(); continue; case "6": // 退出 System.out.println("亲! 谢谢您的使用,欢迎下次再次使用!"); break; default: System.out.println("亲! 找不到您要的选线 ,请重新输入?(^_-)"); continue; } break; } while (true); } // * 查询用户是否存在 如果存在返回用户 根据会员卡号和密码查找用户是否存在 不存在则为null public Member hasMember(int num, String password) { for (Member member : memberList) { if (member.getNum() == num && member.getPassword().equalsIgnoreCase(password)) { return member; } } return null; } // 1、积分累计 public boolean scoreAdd() { System.out.println("请输入卡号:"); int num = sc.nextInt(); System.out.println("请输入密码:"); String password = sc.next(); if (hasMember(num, password) != null) { System.out.println("请输入您的消费金额(0.1元相当于100积分)"); int score = sc.nextInt(); m.setScore(m.getScore() + score * 1000); System.out.println("恭喜用户获得积分:" + m.getScore()); } else { System.out.println("该用户不存在,请重新输入:"); } return true; } // 2、积分兑换 public boolean scoreChange() { System.out.println("请输入卡号:"); int num = sc.nextInt(); System.out.println("请输入密码:"); String password = sc.next(); if(hasMember(num,password)!=null) { System.out.println("请输入您要兑换的积分数(1积分相当于0.001元)"); int score = sc.nextInt(); if(score<=m.getScore()) { m.setScore(m.getScore()-score); System.out.println("恭喜用户兑换积分成功! 兑换的金额是::"+"\n"+score*0.001+"元"); }else { System.out.println("用户的积分不够!"); } }else { System.out.println("该用户不存在(或用户名和密码错误),请重新输入:"); } return true; } // 3、查询剩余积分 public void finaScore() { System.out.println("请输入卡号:"); int num = sc.nextInt(); System.out.println("请输入密码:"); String password = sc.next(); if(hasMember(num,password)!=null) { System.out.println(m.getScore()); }else { System.out.println("该用户不存在,请重新输入:"); } } // 4、修改密码 public void changePassword() { System.out.println("请输入卡号:"); int num = sc.nextInt(); System.out.println("请输入密码:"); String password = sc.next(); if(hasMember(num,password)!=null) { System.out.println("请输入你要修改的密码:"); String newPassword = sc.next(); m.setPassword(newPassword); System.out.println("修改密码成功!"); }else { System.out.println("该用户不存在,请重新输入:"); } } // 5、开卡 用户要先开卡才能使用 // 随机生成卡号 public int creatNum() { Random random = new Random(); int num = random.nextInt(100000000); // 确保卡号的唯一性 如果集合中存在 那么就再随机生成一个 if (m.getNum() == num) { num = random.nextInt(100000000); } return num; } public void openCar() { m = new Member(); System.out.println("请输入用户名:"); m.setName(sc.next()); // 随机生成卡号 写一个方法 int carNum = creatNum(); m.setNum(carNum); // 确保用户名和密码正确 // 输入密码 密码的长度不能小于6 System.out.print("请输入注册密码: "); String password; boolean boo = true; do { password = sc.next(); if (password.length() < 6) { System.out.print("输入的密码长度小于6,亲! 请重新输入: "); boo = false; } else { boo = true; m.setPassword(password); } } while (!boo); // 注册成功! 赠送100积分 m.setScore(100); // 开卡日期: Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String carDate = simpleDateFormat.format(date); m.setCarDate(carDate); // 提醒用户操作成功! 卡号是多少 System.out.println("亲! 您的会员开通成功! 您的卡号是:" + carNum); memberList.add(m); } }
package com.myshoop; public class Test { //测试类 public static void main(String[] args) { MemberDao dao = new MemberDao(); dao.start(); } }
原文地址:https://www.cnblogs.com/bichen-01/p/11415478.html
时间: 2024-10-21 07:45:39