借助集合框架来实现超市会员管理系统,实现以下功能:
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;
}
//-----------------------------------
@Override
public 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 list = new ArrayList();// 建立集合,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());// 存ID
member.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();
}
}