思路分析
1、会员类Member
(1)姓名 name:String
(2)卡号 cardId:int
(3)密码 password:String
(4)积分 score:int
(5)开卡日期 registDate:String
2、会员管理类
(1)所有会员(卡)--集合:List ArrayList<Member>
(2)程序整体流程搭建的方法
循环
分支
(3)验证会员是否存在的方法
集合的遍历,集合元素的查找
(4)系统随机生成会员卡号的方法
Random类
A:开卡
输入会员信息(会员卡密码的验证--String类相应方法-循环)
姓名、密码
卡号:系统随机生成会员卡号的方法
积分:系统默认100
开卡日期:日期时间操作(Date类、SimpleDateFormat类)
将注册成功的会员添加进集合(ArrayList add())
B:修改密码
验证会员是否存在
会员对象密码属性重新赋值
C:积分累计
验证会员是否存在
会员对象积分属性重新赋值
D:积分兑换
验证会员是否存在
会员积分兑换过程(根据相应规则)
E:查询剩余积分
验证会员是否存在
集合查找相应会员,显示信息(遍历)
F:退出文章来源:https://www.toymoban.com/news/detail-466124.html
3、测试类
会员管理类的调用
文章来源地址https://www.toymoban.com/news/detail-466124.html
会员类
package cn.bdqn.superMarket;
public class Member {
//姓名、会员卡号、密码、积分、开卡日期
private String name;
private int CarID;
private String password;
private int score;
private String registDate;
public Member() {
}
public Member(String name, int carID, String password, int score, String registDate) {
this.name = name;
CarID = carID;
this.password = password;
this.score = score;
this.registDate = registDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCarID() {
return CarID;
}
public void setCarID(int carID) {
CarID = carID;
}
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 getRegistDate() {
return registDate;
}
public void setRegistDate(String registDate) {
this.registDate = registDate;
}
}
业务类
package cn.bdqn.superMarket;
import java.text.SimpleDateFormat;
import java.util.*;
public class MemberDao {
List<Member> members=new ArrayList<>();
Scanner sc=new Scanner(System.in);
//首页菜单
public void menu()
{
System.out.println("欢迎进入超市会员管理系统");
System.out.println("==========================================");
System.out.println("1.积分累计");
System.out.println("2.积分兑换");
System.out.println("3.查询剩余积分");
System.out.println("4.修改密码");
System.out.println("5.开卡");
System.out.println("6.退出");
System.out.println("==========================================");
System.out.println("请选择你需要的服务:");
}
public void star()
{
int num=-1;
do {
menu();
int chooseNum=sc.nextInt();
switch(chooseNum)
{
case 1:
if(saveScore())
{
System.out.println("积分累积成功");
}else{
System.out.println("积分累计失败");
}
break;
case 2:
if(minusScore())
{
System.out.println("积分查询成功");
}else{
System.out.println("积分查询失败");
}
break;
case 3://查询积分
checkScore();
break;
case 4://修改密码
change();
break;
case 5://开卡
register();
break;
case 6:
System.out.println("感谢您的是使用,欢迎下次光临");
break;
default:
break;
}
if(chooseNum<1||chooseNum>5)
{
break;
}else{
//提示输入0返回
System.out.println("输入0返回主菜单:");
num=sc.nextInt();
}
}while(num==0);
}
public boolean saveScore()//积分
{
boolean flag=true;
System.out.println("请输入会员卡号:");
int ID=sc.nextInt();
System.out.println("请输入密码:");
String psw=sc.next();
Member member=haveMember(ID,psw);
if(member!=null)
{
System.out.println("请输入您此次消费金额(1元积1分):");
int score=sc.nextInt();
member.setScore(member.getScore()+score);
flag= true;
}else {
System.out.println("对不起,该会员卡号不存在,无法积分");
flag= false;
}
return flag;
}
public boolean minusScore()//积分兑换
{
boolean flag=true;
System.out.println("请输入会员卡号:");
int ID=sc.nextInt();
System.out.println("请输入密码:");
String psw=sc.next();
Member member=haveMember(ID,psw);
if(member!=null)
{
System.out.println("请输入您此次兑换积分的额度:");
int score=sc.nextInt();
if(score<member.getScore())
{
member.setScore(member.getScore()-score);
System.out.println("您此次兑换和:"+score/100*0.1+"元");
}else{
System.out.println("积分不足无法兑换");
}
flag= true;
}else {
System.out.println("对不起,该会员卡号不存在,无法兑换积分");
flag= false;
}
return flag;
}
public void checkScore()//积分查询
{
boolean flag=true;
System.out.println("请输入会员卡号:");
int ID=sc.nextInt();
System.out.println("请输入密码:");
String psw=sc.next();
Member member=haveMember(ID,psw);
if(member!=null)
{
System.out.println("会员卡号"+"\t"+"姓名"+"\t"+"剩余积分"+"\t"+"开卡日期");
System.out.println(member.getCarID()+"\t"+member.getName()+"\t"+member.getScore()+"\t"+member.getRegistDate());
}else{
System.out.println("对不起!该会员卡号不存在,无法查询积分");
}
}
public void change()//修改密码
{
boolean flag=true;
System.out.println("请输入会员卡号:");
int ID=sc.nextInt();
System.out.println("请输入密码:");
String psw=sc.next();
Member member=haveMember(ID,psw);
if(member!=null)
{
System.out.println("请输入新密码:");
String pass=sc.next();
member.setPassword(pass);
System.out.println("密码修改成功-");
}
}
//开卡
public void register()
{
Member member = new Member();
System.out.println("请输入注册姓名:");
String name=sc.next();
member.setName(name);
//随机生成卡号
int ID=creatID();
member.setCarID(ID);
System.out.println("请输入您的密码:");
String pwd;
//保证密码设置正确
boolean falg=true;
do {
pwd=sc.next();
if(pwd.length()<6)
{
System.out.println("密码不能少于6位,请重新输入");
falg=false;
}else{
falg=true;
member.setPassword(pwd);
}
}while(!falg);
//默认开卡赠送100积分
member.setScore(100);
//开卡日期
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM=dd HH:mm:ss");
String sdate= sdf.format(date);
member.setRegistDate(sdate);
System.out.println("恭喜开卡成功,新用户赠送100积分,您的卡号为:"+ID);
members.add(member);
}
//随机生成卡号
public int creatID()
{
Random random = new Random();
int id=random.nextInt(999999);
for (Member member :members) {
if(member.getCarID()==id)
{
id=random.nextInt(999999);
}
}
return id;
}
//查询当前会员是否存在,根据给的卡号和密码查找
public Member haveMember(int cardID,String psw)
{
for (Member member :members) {
if(member.getCarID()==cardID&&member.getPassword().equals(psw))
{
return member;
}
}
return null;
}
}
测试类
package cn.bdqn.superMarket;
public class Test {
public static void main(String[] args) {
MemberDao man = new MemberDao();
man.star();
}
}
到了这里,关于JAVA小项目之超市会员管理系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!