本文主要是介绍Java小项目——超市会员管理系统(简洁明了),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.解题思路:
- 先运用面向对象的思想抽象出两个类:业务类,会员类
- 运用集合中的ArrayList存储对象中的信息
- 需要有一个循环的菜单来供人选择,菜单中的功能有:
1.积分累计 2.积分兑换 3.查询剩余积分 4.修改密码 5.开卡 6.退出
- 首先要完成开卡功能,不然其他的功能没办法使用
- 退出功能最简单,直接跳出循环就行
- 积分累计功能直接用setScore()方法进行累加;积分兑换使用setScore()方法进行减操作;查询剩余积分功能getScore()方法进行查询;修改密码使用setPassword()方法进行修改
2.注意:
- 除了开卡功能,其他所有功能都需要在进行前验证一下用户和密码,输入正确的账号和密码之后才能进行操作。
3.代码:
代码通过start()开始
业务类:
public class MemberServer {List<Member> memberList = new ArrayList<>();Scanner scanner = new Scanner(System.in);public void menu(){System.out.println("****************************欢迎进入超市会员管理系统******************************");System.out.println("1.积分累计 2.积分兑换 3.查询剩余积分 4.修改密码 5.开卡 6.退出");System.out.println("********************************************************************************");System.out.print("请选择:");}//首先弄主页面public void start(){boolean f = true;while (f){menu();int choose = scanner.nextInt();switch (choose){case 1://积分累计addScore();break;case 2://积分兑换UseScore();break;case 3:searchScore();//查询剩余积分break;case 4://修改密码ChangePassword();break;case 5://开卡Member member = OpenId();System.out.println("姓名 \t会员卡号 \t 剩余积分\t 开卡日期\t");System.out.println(member.getName()+"\t"+member.getCardId()+"\t"+member.getScore()+"\t"+member.getDateTime());break;case 6:f = false;break;}}}/*** 首先开卡**/public Member OpenId(){System.out.print("请输入姓名");String name = scanner.next();System.out.print("请输入密码");String password = scanner.next();//判断密码位数while (true){if (password.length()<6){System.out.print("密码长度不能小于6位,请重新输入密码:");password = scanner.next();}else {System.out.println("密码设置成功");break;}}//生成idMember member = new Member(name, getRandomId(),password);//把这个对象放入list集合memberList.add(member);System.out.println("恭喜开卡成功!!!默认100积分");return member;}/*** 生成随机ID* @return*/public int getRandomId(){Random random = new Random();int id = 10000000 + random.nextInt(90000000);//设置范围
//不让生成的id重复for(int i = 0;i<memberList.size();i++){Member member = memberList.get(i);//通过编号来找对象if (member.getCardId()==id){//发现重复的id = 10000000 + random.nextInt(90000000);i = 0;//重新设置并且重新挨个比较}}return id;}public int addScore(){System.out.print("请输入您此次消费金额(消费1元累计一分)");int score = scanner.nextInt();//这里没有对象应该怎样调用get//这个需要验证munber是否存在Member member = idExit();member.setScore(member.getScore()+score);System.out.println("姓名 \t会员卡号 \t 剩余积分\t 开卡日期\t");System.out.println(member.getName()+"\t"+member.getCardId()+"\t"+member.getScore()+"\t"+member.getDateTime());//返回分数return member.getScore();}public Member idExit(){System.out.print("请输入您的会员卡号");int CardId = scanner.nextInt();System.out.print("请输入您的会员密码");String password = scanner.next();for (Member mumber:memberList){if (mumber.getCardId()==CardId&&mumber.getPassword().equals(password)){//都对上,有这个对象return mumber;}}//找不到返回nullreturn null;}public int searchScore(){Member member = idExit();//查询积分System.out.println("姓名 \t会员卡号 \t 剩余积分\t 开卡日期\t");System.out.println(member.getName()+"\t"+member.getCardId()+"\t"+member.getScore()+"\t"+member.getDateTime());return member.getScore();}/*** 修改密码* @return*/public void ChangePassword(){Member member = idExit();System.out.print("请输入新密码:");String password = scanner.next();member.setPassword(password);System.out.print("您的新密码是:"+password);}public void UseScore(){Member member = idExit();System.out.println("请选择您需要兑换使用的积分(100积分抵用0.1元,不足100的积分不做抵用):");int i = scanner.nextInt();if (i<100){System.out.println("不足100积分,不做抵押");} else if (i>member.getScore()) {member.setScore(member.getScore()-i);System.out.println("您的积分不够");} else{int i1 = i % 1000;System.out.println("您的消费金额中使用会员积分抵消"+i1+"元");member.setScore(member.getScore()-i);System.out.println("积分兑换成功");}}
}
会员类:
public class Member {private String name;//姓名private int cardId;//卡号private String password;//密码private int score;//积分private String dateTime;//开卡时间public Member(String name, int cardId, String password) {this.name = name;this.cardId = cardId;this.password = password;this.score = 100;//默认100积分this.dateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss").format(LocalDateTime.now());}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 int getScore() {return score;}public void setScore(int score) {this.score = score;}public String getDateTime() {return dateTime;}public void setDateTime(String dateTime) {this.dateTime = dateTime;}
}
测试类(主方法):
通过调用业务类中的start方法开始
package MyBighomework;public class Test {public static void main(String[] args) {//首先进入系统MemberServer memberServer = new MemberServer();memberServer.start();}
}
这篇关于Java小项目——超市会员管理系统(简洁明了)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!