借助集合框架来实现超市会员管理系统,实现以下功能:
1.开卡 2.积分累计 3.查询剩余积分 4.积分兑换 5.修改密码 6.退出
--------------------------------------------------------------------------------------------------
展示:
--------------------------------------------------------------------------------------------------
1.创建会员Member类
package dh05.test;
//会员类
public class Member {private String name;//会员名private int id;//会员卡号private String pwd;//会员密码private String firstDate;//开卡日期private int scores;//积分//构造函数-------------------------------public Member() {}public Member(String name, int id, String pwd, String firstDate, int scores) {this.name = name;this.id = id;this.pwd = pwd;this.firstDate = firstDate;this.scores = scores;}//-----------------------------------@Overridepublic String toString() {return "Member [会员名=" + name + ", 卡号=" + id + ", 密码=" + pwd + ", 开卡日期=" + firstDate + ", 积分="+ scores + "]";}//set&get-----------------------------public String getName() {return name;}public void setName(String name) {this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public String getFirstDate() {return firstDate;}public void setFirstDate(String firstDate) {this.firstDate = firstDate;}public int getScores() {return scores;}public void setScores(int scores) {this.scores = scores;}}
--------------------------------------------------------------------------------------------------
2.创建会员服务类
部分分析:
- 每次进行操作的时候,都需要对集合中的数据进行查询,因此需要写查询会员是否存在集合的方法,便于后续操作的使用
//验证会员是否存在
public Member hasMember(Member m) {
Member member = null;//如果输入的ID跟密码都正确,则证明该会员存在,可以进行下一步的操作
for (Member everyMember : list) {
if (m.getId() == everyMember.getId() && m.getPwd().equals(everyMember.getPwd())) {
member = everyMember;
}
}
return member;
} - 循环打印系统信息便于操作
//show方法
public void show() {
System.out.println("**********欢迎进入超市会员管理系统**********");
System.out.println("1.开卡 2.积分累计 3.查询剩余积分 4.积分兑换 5.修改密码 6.退出");
System.out.println("************************************");
System.out.print("请选择:");
} - 生成指定范围的随机数
n=rand.nextInt(max-min+1)+min (范围)
详细代码如下:
//会员业务类import java.util.List;
import java.util.Random;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Scanner;public class MemberSer {Scanner sc = new Scanner(System.in);List<Member> list = new ArrayList<Member>();// 建立集合,member泛型/** 1.开卡 ---》赠送积分100 2.积分累计 3.查询剩余积分 4.积分兑换 5.修改密码 6.退出*/public void start() {boolean isIn = true;do {show();switch (sc.nextInt()) {case 1:// 开卡newVIP();continue;case 2:// 积分累计if (addScores()) {System.out.println("积分累计成功!");}continue;case 3:// 查询剩余积分lookScores();continue;case 4:// 积分兑换excScores();continue;case 5:// 修改密码if (changePwd()) {System.out.println("密码修改成功!");}continue;case 6:// 退出System.out.println("感谢使用!");isIn = false;continue;default:System.out.println("输入有误,请重新输入!");continue;}} while (isIn);}//------------------------------------
//show方法public void show() {System.out.println("**********欢迎进入超市会员管理系统**********");System.out.println("1.开卡 2.积分累计 3.查询剩余积分 4.积分兑换 5.修改密码 6.退出");System.out.println("************************************");System.out.print("请选择:");}//验证会员是否存在public Member hasMember(Member m) {Member member = null;for (Member everyMember : list) {if (m.getId() == everyMember.getId() && m.getPwd().equals(everyMember.getPwd())) {member = everyMember;}}return member;}// 开卡方法public void newVIP() {Member member = new Member();// 定义member对象// 1.开卡 ---》赠送积分100// 生成随机卡号(6位卡号)System.out.print("请输入注册姓名:");member.setName(sc.next());System.out.print("请输入注册密码:");// 会员卡密码不能低于6位boolean bool = true;while (bool) {String pwd = sc.next();if (pwd.length() < 6) {System.out.print("密码长度不能小于6位,请重新输入密码:");continue;} else {member.setPwd(pwd);bool = false;}}// 记录第一次开卡时间Date date = new Date();// 格式化日期SimpleDateFormat formater = new SimpleDateFormat("yyyy年MM月dd日");String firstDate = formater.format(date);member.setFirstDate(firstDate);// 存第一次开卡时间member.setId(createID());// 存IDmember.setScores(100);// 初始化积分100// 将注册成功的会员添加到会员集合中,并提示开卡成功list.add(member);System.out.println("开卡成功,赠送100积分,您的会员卡号为:" + member.getId());}//生成会员卡号public int createID() {// 生成6位随机数// n=rand.nextInt(max-min+1)+min (范围)int min = 100000;int max = 999999;Random random = new Random();int newID = random.nextInt(max - min + 1) + min;// 保证生成的会员卡号和之前已有的的不重复for (Member member : list) {if (member.getId() == newID) {newID = random.nextInt(max - min + 1) + min;}}return newID;}//积分累计 public boolean addScores() {boolean bool = true;//累计积分是否成功Member member = new Member();System.out.print("请输入您的会员卡号:");member.setId(sc.nextInt());System.out.print("请输入您的会员密码:");member.setPwd(sc.next());member = hasMember(member);//找member对象if (member != null) {// 进行加的操作System.out.print("请输入本次消费金额(积分取整比率 1:1)");int money = sc.nextInt();member.setScores(member.getScores() + money);bool = true;System.out.println("积分累计成功,您当前的积分为:" + member.getScores());} else {System.out.println("会员卡号或密码错误,无法累计积分!");bool = false;}return bool;}//查询剩余积分public void lookScores() {Member member = new Member();System.out.print("请输入您的会员卡号:");member.setId(sc.nextInt());System.out.print("请输入您的会员密码:");member.setPwd(sc.next());member = hasMember(member);//找member对象if (member != null) {System.out.println("姓名\t会员卡号\t剩余积分\t开卡日期");System.out.println(member.getName()+"\t"+member.getId()+"\t"+member.getScores()+"\t"+member.getFirstDate());}else {System.out.println("您输入的额会员卡号或密码错误,无法查询积分!");}}
//积分兑换public boolean excScores() {boolean bool = true;//积分兑换是否完成Member member = new Member();System.out.print("请输入您的会员卡号:");member.setId(sc.nextInt());System.out.print("请输入您的会员密码:");member.setPwd(sc.next());member = hasMember(member);if (member != null) {System.out.print("请输入要兑换使用的积分(100积分抵用1元,不足100不能抵用):");int score = sc.nextInt();//对会员想要兑换的积分进行判断if (score<=member.getScores()) {//能兑换member.setScores(member.getScores()-score);System.out.println("您使用积分抵扣了"+score/100+"元");bool = true;}else {System.out.println("积分不够,无法抵扣!");bool = false;}}else {System.out.println("会员卡号或密码错误,无法完成积分兑换!!!!");bool = false;}return bool;}
//修改密码public boolean changePwd() {boolean bool = true;Member member = new Member();System.out.print("请输入您的会员卡号:");member.setId(sc.nextInt());System.out.print("请输入您的会员密码:");member.setPwd(sc.next());member = hasMember(member);// 找到会员对象才可以进行修改if (member != null) {System.out.print("请输入新的密码:");String newPwd;// 设置新的密码boolean bool1 = true;// 新的会员密码格式是否正确while (bool1) {newPwd = sc.next();if (newPwd.length() < 6) {System.out.print("密码长度不能小于6位,请重新输入密码:");continue;} else {member.setPwd(newPwd);bool1 = false;// 新设置的密码格式正确bool = true;// 密码修改成功}}} else {System.out.println("会员卡号或密码错误,无法修改密码。");bool = false;}return bool;}}
--------------------------------------------------------------------------------------------------
3.测试类
//测试类
/** 为某超市开发一个超市会员管理系统,实现会员的管理,具体实现功能如下:1.积分累计 2.积分兑换 3.查询剩余积分 4.修改密码 5.开卡 6.退出*/
public class Test {public static void main(String[] args) {MemberSer memberSer = new MemberSer();memberSer.start();}}