直接上代码,个人认为代码逻辑很清楚
发现问题,希望各位随时私聊
package market;
public class Member {
private String name; //姓名
private int cardId; //会员卡号
private String password; //密码
private int score; //积分
private String registerDate; //注册日期
public Member(String name, int cardId, String password, int score, String registerDate) {
this.name = name;
this.cardId = cardId;
this.password = password;
this.score = score;
this.registerDate = registerDate;
}
public Member() {
}
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 getRegisterDate() {
return registerDate;
}
public void setRegisterDate(String registerDate) {
this.registerDate = registerDate;
}
}
package market;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;
public class MemberService {
/* 借助集合框架及实用类实现该系统
1.开卡
2.积分累计
3.积分兑换
4.查询剩余积分
5.修改密码
6.退出*/
//写在外边
Scanner input= new Scanner(System.in);
//需要保存用户的数据信息
ArrayList<Member> list= new ArrayList<Member>();
//项目的启动方法
public void start(){
do{
System.out.println("*******欢迎使用超市会员管理系统*********");
System.out.println("1.开卡 2.积分累计 3.积分兑换 4.查询剩余积分 5.修改密码 6.退出");
System.out.println("----------------------------------------------");
System.out.println("请选择(1-6):");
//使用switch 语句进行匹配
int choose = input.nextInt();
switch (choose){
case 1:
//逻辑
register();
continue;
case 2:
//温馨提示
if(saveScore()){
System.out.println("积分累计成功!!!");
}else{
System.out.println("积分累计失败!!!");
}
continue;
case 3:
if(minusScore()){
System.out.println("积分消费成功!!!");
}else{
System.out.println("积分消费失败!!!");
}
continue;
case 4:
showScore();
continue;
case 5:
if(changePWD()){
System.out.println("密码修改成功");
}else{
System.out.println("密码修改失败");
}
continue;
case 6:
System.out.println("感谢您的使用,欢迎下次使用!!!");
break;
default:
System.out.println("您的输入有误,请重新输入(1-6):");
continue;
}
break;
}while(true);
}
/**
* 开始写方法 第一个 开卡
*
* 给一个用户姓名 给一个密码 系统返回给我们一个卡号 (random)9999999
*/
public void register(){
Member member = new Member();
System.out.println("请输入注册的姓名:");
String name = input.next();
//赋值
member.setName(name);
System.out.println("请输入注册的密码:");
//再输入密码的时候,长度必须大于6位
String password;
//如何实现重复进入
boolean flag=true;
do{
password= input.next();
if(password.length()<6){
System.out.println("您输入的会员密码少于6位,请重新输入:");
flag=false;
}else{
flag=true;
member.setPassword(password);
}
}while(!flag);
//会员卡号
member.setCardId(createId());
//赠送积分
member.setScore(1000);
//开卡日期
Date date = new Date();
String times = new SimpleDateFormat("yyyy-MM-dd").format(date);
member.setRegisterDate(times);
//member这个对象我们确实有值了 但是List呢???
list.add(member);
System.out.println("恭喜你,开卡成功,系统赠送您1000积分,您的会员卡号为:"+member.getCardId());
}
/**
* 2.积分累计
*/
public boolean saveScore(){
Member member = new Member();
System.out.println("请输入你的会员卡号:");
member.setCardId(input.nextInt());
System.out.println("请输入你的会员密码:");
member.setPassword(input.next());
//跟数据库(ArrayList)中的用户进行比较
member = search(member);
//判断
if(member!=null){
System.out.println("请输入你的消费金额(消费1元累计1积分):");
int score = input.nextInt();
//改变分数
member.setScore(member.getScore()+score);
return true;
}else{
System.out.println("你输入的会员卡号或者密码错误,无法累计积分!!!");
}
return false;
}
/**
* 随机生成一个会员卡号
*
*/
public int createId(){
Random random = new Random();
int id = random.nextInt(99999999);
//卡号能重复吗 id 取到值一样 怎么办???
for (Member member : list) {
if(member.getCardId()==id){
id=random.nextInt(99999999);
}
}
return id;
}
/**
* 验证 账户 密码是否存在
*/
public Member search(Member member){
for(int i=0;i<list.size();i++){
if(list.get(i).getCardId()==member.getCardId()
&& list.get(i).getPassword().equalsIgnoreCase(member.getPassword())){
return list.get(i);
}
}
return null;
}
/**
* 3.积分兑换
*/
public boolean minusScore() {
Member member = new Member();
System.out.println("请输入你的会员卡号:");
member.setCardId(input.nextInt());
System.out.println("请输入你的会员密码:");
member.setPassword(input.next());
//跟数据库(ArrayList)中的用户进行比较
member = search(member);
if(member!=null){
System.out.println("请输入你需要兑换的积分(100积分顶1元,不足100积分无法兑换):");
int score = input.nextInt();
if(score<=member.getScore()){
member.setScore(member.getScore()-score);
System.out.println("您的消费金额中使用会员积分抵消了"+score/100*1+"元!!!");
return true;
}else{
System.out.println("抱歉,您的积分不够,无法抵用消费金额!!!");
}
}else{
System.out.println("你输入的会员卡号或者密码错误,无法消费积分!!!");
}
return false;
}
/**
* 4.查询剩余积分
*/
public void showScore(){
Member member = new Member();
System.out.println("请输入你的会员卡号:");
member.setCardId(input.nextInt());
System.out.println("请输入你的会员密码:");
member.setPassword(input.next());
//跟数据库(ArrayList)中的用户进行比较
member = search(member);
if(member!=null){
System.out.println("姓名\t\t\t会员卡号\t\t\t剩余积分\t\t\t开卡日期");
System.out.println(member.getName()+"\t\t\t"+member.getCardId()+"\t\t\t"
+member.getScore()+"\t\t\t"+member.getRegisterDate());
}else{
System.out.println("你输入的会员卡号或者密码错误,无法查看信息!!!");
}
}
/**
* 5.修改密码
*/
public boolean changePWD(){
Member member = new Member();
System.out.println("请输入你的会员卡号:");
member.setCardId(input.nextInt());
System.out.println("请输入你的会员密码:");
member.setPassword(input.next());
//跟数据库(ArrayList)中的用户进行比较
member = search(member);
if(member!=null){
System.out.println("请输入你的新密码:");
String password;
boolean flag=true;
do{
password= input.next();
if(password.length()<6){
System.out.println("您输入的会员密码少于6位,请重新输入:");
flag=false;
}else{
flag=true;
member.setPassword(password);
}
}while(!flag);
return true;
}else{
System.out.println("你输入的会员卡号或者密码错误,无法修改密码!!!");
}
return false;
}
}
package market;
public class Test {
public static void main(String[] args) {
MemberService memberService = new MemberService();
memberService.start();
}
}