package cn.kgc.demo02.test;
// 业务类,操作系统
import java.text.SimpleDateFormat;
import java.util.*;
public class MemberDao {
// 需要用户录入
Scanner input=new Scanner(System.in);
// 需要创建集合保存Member对象
List<Member> memberList=new ArrayList<>();
// 系统开始提示语
public void headStart(){
System.out.println("===================================欢迎使用===================================");
System.out.println("1会员开卡 2积分累积 3积分兑换 4积分查询 5修改密码 6退出系统");
System.out.println("==============================================================================");
System.out.print("请操作:");
}
// 系统控制
public void start(){
do {
headStart();
int choose = input.nextInt();
switch (choose){
// 1 开卡
case 1:
register();
break;
// 2 积分累积
case 2:
if(saveScore()){
System.out.println("积分累积成功");
}else {
System.out.println("积分累积失败");
}
break;
// 3 积分兑换
case 3:
if(minuScore()){
System.out.println("积分兑换成功");
}else {
System.out.println("积分兑换失败");
}
break;
// 4 积分查询
case 4:
showScore();
break;
// 5 修改密码
case 5:
if(changePwd()){
System.out.println("密码修改成功");
}else {
System.out.println("密码修改失败");
}
break;
// 6 退出
case 6:
System.out.println("退出成功!欢迎下次光临!");
System.exit(0);
// 输入错误
default:
System.out.println("抱歉,您的输入有误!请输入1-6的整数");
break;
}
}while (true);
}
// 1 开卡
public void register(){
// 获取会员名字,密码
Member member = new Member();
System.out.print("请输入姓名:");
member.setName(input.next());
// 设开关
boolean flag=true;
// 判断密码长度
do {
System.out.print("请输入密码:");
String mm = input.next();
if (mm.length()<6){
System.out.println("抱歉,密码长度不可小于6位数,请重试");
flag=false;
}else{
member.setPassword(mm);
flag=true;
}
}while (!flag);
// 随机生成卡号
int id = cardId();
member.setCardId(id);
// 开卡送积分100
member.setScore(100);
// 获取开卡时间
Date todayDate = new Date();
// Date ---> String格式
SimpleDateFormat today = new SimpleDateFormat("yyyy-MM-dd");
String date = today.format(todayDate);
member.setCreatTime(date);
// 输出开卡信息
System.out.println("恭喜,开卡成功!您的卡号为:"+member.getCardId());
memberList.add(member);
}
// 1.1 随机生成卡号
public int cardId(){
// 随机数类
Random r = new Random();
int id = r.nextInt(99999999);
// 判断集合中卡号是否已存在
for (Member m:memberList){
if (id==m.getCardId()){
id=r.nextInt();
}
}
return id;
}
//2 积分累积
public boolean saveScore(){
// 输入卡号密码
Member member = new Member();
System.out.print("请输入您的卡号:");
member.setCardId(input.nextInt());
System.out.print("请输入您的密码:");
member.setPassword(input.next());
// 判断卡号密码是否正确
if((member=hasMember(member))!=null){
System.out.print("请输入你的消费金额(消费1元送1分):");
int buy = input.nextInt();
member.setScore(member.getScore()+buy);
return true;
}else {
System.out.println("抱歉,您输入的卡号或密码有误,请重试!");
}
return false;
}
//3 积分兑换
public boolean minuScore(){
// 输入卡号密码
Member member = new Member();
System.out.print("请输入您的卡号:");
member.setCardId(input.nextInt());
System.out.print("请输入您的密码:");
member.setPassword(input.next());
// 判断卡号密码是否正确
if((member=hasMember(member))!=null){
System.out.print("请输入你要兑换的积分(100积分抵用0.1元):");
int use = input.nextInt();
if (use<=member.getScore()){
member.setScore(member.getScore()-use);
System.out.println("兑换成功,本次消费抵扣"+use/100*0.1+"元");
return true;
}else {
System.out.println("抱歉,积分不足!");
}
}else {
System.out.println("抱歉,您输入的卡号或密码有误,请重试!");
}
return false;
}
//4 积分查询
public void showScore(){
// 输入卡号密码
Member member = new Member();
System.out.print("请输入您的卡号:");
member.setCardId(input.nextInt());
System.out.print("请输入您的密码:");
member.setPassword(input.next());
// 判断卡号密码是否正确
if((member=hasMember(member))!=null){
System.out.println("姓名"+" "+"会员卡号"+" "+"剩余积分"+" "+"开卡时间");
System.out.println(member.getName()+" "+member.getCardId()+" "+member.getScore()+" "+member.getCreatTime());
}else {
System.out.println("抱歉,您输入的卡号或密码有误,请重试!");
}
}
// 4.1 判断卡号密码是否正确
public Member hasMember(Member member){
if (member.getPassword()!=null) {
for (int i = 0; i < memberList.size(); i++) {
if (member.getCardId() == memberList.get(i).getCardId() && member.getPassword().equalsIgnoreCase(memberList.get(i).getPassword())) {
return memberList.get(i);
}
}
}
return null;
}
//5 修改密码
public boolean changePwd(){
// 输入卡号密码
Member member = new Member();
System.out.print("请输入您的卡号:");
member.setCardId(input.nextInt());
System.out.print("请输入您的密码:");
member.setPassword(input.next());
// 判断是否正确
if((member=hasMember(member))!=null){
System.out.print("请输入新密码:");
member.setPassword(input.next());
System.out.println("修改成功,新密码为:"+member.getPassword());
return true;
}else {
System.out.println("抱歉,您输入的卡号或密码有误!请重试");
}
return false;
}
}