本文主要是介绍第五次项目《超市会员管理Plus》,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
超市会员管理系统
先写架构,在补全方法,找一个好头开始一步步完善。
/*** 会员类* @author 周太阳* @version 1.0* @date 2019年4月23日 下午4:32:27*/
class Member {/**姓名*/private String name;/**卡号*/private int cardId;/**密码*/private String password;/**开卡时间*/private Date registDate;/**积分*/private int score;public Member() {super();}public Member(String name, int cardId, String password,Date registDate, int score) {super();this.name = name;this.cardId = cardId;this.password = password;this.registDate = registDate;this.score = score;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getCardId() {return cardId;}public void setCardId(int cardId) {this.cardId = cardId;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Date getRegistDate() {return registDate;}public void setRegistDate(Date registDate) {this.registDate = registDate;}public int getScore() {return score;}public void setScore(int score) {this.score = score;}@Overridepublic String toString() {StringBuilder sb = new StringBuilder();SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");sb.append("姓名:"+name).append("\n卡号:"+cardId).append("\n积分:"+score).append("\n开卡时间:"+format.format(registDate));return sb.toString();}
}/*** 超市会员管理Plus* @author 周太阳* @version 1.0* @date 2019年4月22日 下午5:22:16*/
class SuperMarketMember {static Scanner input = new Scanner(System.in);/**开卡*/public void createCard() {System.out.println("***欢迎进入注册系统***");System.out.print("请输入您的姓名:");String userName = input.next();do {System.out.print("请输入密码(密码长度不能小于6!):");String userPwd = input.next();if (userPwd.length() < 6) {System.out.println("密码长度不能小于6!");continue;}else {System.out.print("请再次输入密码:");String again = input.next();// 判断再次输入密码是否正确if (again.equals(userPwd)) {// 生成随机卡号int userNo = createRandom();// 创建用户Member member = new Member(userName, userNo, userPwd, new Date(), UserData.GIVE_SCORE);// 存入Set集合UserData.user.add(member);// 时间转换SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");// 获得转换后的时间String regisertDate = format.format(member.getRegistDate());System.out.println("开卡成功!请牢记您的卡号和密码!系统赠送您"+member.getScore()+"积分,您的卡号是:"+userNo+"\n开卡时间是:"+regisertDate);break;}else {System.out.println("两次输入的密码不相同!");continue;}}}while(true);}/**登陆*/public void login() {System.out.print("请请输入卡号:");Integer inputUserNo = input.nextInt();System.out.print("请输入密码:");String inputUserPwd = input.next();// 判断用户账号是否存在且密码是否和用户输入相同Member user = userInfo(inputUserNo, inputUserPwd);if (user != null) {// 给予对象并执行菜单selectFunction(user);}else {System.out.println("您的账号或密码错误!");}}/**菜单*/public void menu() {System.out.println("******************☆欢迎进入主菜单☆************************");System.out.println("1、积分累计 2、积分兑换 3、查询剩余积分 4、修改密码 5、退出");System.out.println("************************************************************");System.out.print("请选择:");}/**选择功能*/public void selectFunction(Member user) {outer:do {menu();int chooise = input.nextInt();// 菜单选项switch(chooise) {case 1:// 积分累计addScore(user);break;case 2:// 积分兑换exchangeScore(user);break;case 3:// 查询积分searchScore(user);break;case 4:// 修改密码modifyPwd(user);System.out.println("密码已修改,请重新登录!");break outer;case 5:// 退出System.out.println("已登出");break outer;default:System.out.println("输入错误!");break;}}while(true);}/**Set循环判断用户对象是否存在,且账号密码是否正确*/public Member userInfo(Integer inputUserNo,String inputUserPwd) {// 创建迭代器Iterator<Member> iterator = UserData.user.iterator();// 查找对象声明变量valueMember user = null;// 循环while(iterator.hasNext()) {user = iterator.next();// 如果找到用户输入卡号和Set中的对象卡号一致就停止if (user.getCardId() == inputUserNo) {break;}}// 判断卡号和密码是否正确if (user.getCardId() == inputUserNo && user.getPassword().equals(inputUserPwd)) {// 返回对应卡号的对象return user;}return null;}/**积分累计*/public void addScore(Member user) {System.out.println("请输入您此次的消费金额(消费1元累计1积分):");int expense = input.nextInt();if (expense > 100) {// 当前积分int score = user.getScore();// 将新的积分装进Set集合user.setScore(score+expense);System.out.println("您此次消费"+expense+"元"+"兑换积分:"+expense+"分\n当前积分为:"+user.getScore());}else {System.out.println("100以下的积分不予累计!多购些商品吧~");}}/**积分兑换*/public void exchangeScore(Member user) {System.out.println("您的积分是:"+user.getScore());do {System.out.println("请输入您需要兑换的积分(100分抵0.1元,不足100的积分不抵用):");int exchange = input.nextInt();// 判断积分是否合理if (exchange <= user.getScore() || exchange > 0) { // 积分取100整int money = Math.floorDiv(exchange, 100);// 消费积分int expense = money*100;// 新的积分user.setScore(user.getScore()-expense);System.out.printf("您的此次消费中已用"+expense+"积分抵消%.2f元。\n\r",money*0.1);break;}else {System.out.println("您输入的积分错误,请输入正确的积分!");continue;}}while(true);}/**查询剩余积分*/public void searchScore(Member user) {System.out.println(user);}/**修改密码*/public void modifyPwd(Member user) {do {System.out.print("请输入您的密码:");String userPwd = input.next();// 判断密码是否输入正确if (user.getPassword().equals(userPwd)) {System.out.print("请输入您的新密码:");String newPws = input.next();if (newPws.length() >= 6) {user.setPassword(newPws);System.out.println("密码修改成功!");break;}else {System.out.println("密码不能小于6位!");continue;}}else {System.out.println("密码输入错误!请重新输入!");continue;}}while(true);}/**生成不重复的随机卡号*/public int createRandom() {Random random = new Random();// 当随机数在集合中的对象存在时,重新生成int userRandom;again:while(true) {userRandom = random.nextInt(90000000)+10000000;for (Member user : UserData.user) {// 如果存在相同卡号,继续生成随机号if (user.getCardId() == userRandom) {continue again;}}// 卡号不一样退出循环break;}return userRandom;}}/*** 数据库* @author 周太阳* @version 1.0* @date 2019年4月23日 下午8:53:23*/
class UserData {// 储存每个用户信息Member member = new Member();static Set<Member> user = new HashSet<Member>();// 赠送积分public static final int GIVE_SCORE = 100;// 测试用户static{Member member = new Member();member.setName("篮球高手蔡徐坤");member.setCardId(1234);member.setPassword("1234");member.setScore(-10000);user.add(member);}
}/*** 用户类* @author 周太阳* @version 1.0* @date 2019年4月23日 下午8:52:51*/
public class Test {public static void main(String[] args) {Scanner input = new Scanner(System.in);SuperMarketMember smm = new SuperMarketMember();System.out.println("******************☆欢迎进入超市会员管理系统Plus☆****************************");do {System.out.println("1、登陆\n2、开卡\n3、退出");// 判断登陆或开卡int num = input.nextInt();if (num == 1) {// 登陆smm.login();}else if(num == 2){// 开卡smm.createCard();}else {// 退出System.out.println("欢迎下次光临!!!O(∩_∩)O~~");System.exit(0);}}while(true);}
}
这篇关于第五次项目《超市会员管理Plus》的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!