用户类
package shopping;
import java.util.Scanner;
/**
* 文件名: User
* 创建者: 李海金
* 创建时间:2020/8/7 13:31
* 工程名:高级特性二次复习
*/
public class User {
private int id;
private String name;
private String data;
private String passWord;
private int score = 100;
public User(int id, String name, String data, String passWord, int score) {
this.id = id;
this.name = name;
this.data = data;
this.passWord = passWord;
this.score = score;
}
public User() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
Scanner sc = new Scanner(System.in);
while (passWord.length()<6) {
System.out.println("密码长度小于六位,请重新设置");
passWord = sc.next();
}
this.passWord = passWord;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
超市业务类
package shopping;
import java.util.Scanner;
/**
* 文件名: SOPP
* 创建者: 李海金
* 创建时间:2020/8/7 13:38
* 工程名:高级特性二次复习
*/
public class SOPP {
public SOPP(){
Shopping sp = new Shopping();
boolean flag = true;
Scanner sc = new Scanner(System.in);
do {
System.out.println("_____________________欢迎进入会员管理系统_______________________");
System.out.println("1,积分累计 \t2,积分兑换 \t3,查询剩余积分 \t4,修改密码 \t5,开卡 \t6,退出");
System.out.print("请选择:");
switch (sc.nextInt()){
case 1:
sp.moreScore();
break;
case 2:
sp.addScore();
break;
case 3:
sp.score();
break;
case 4:
sp.modify();
break;
case 5:
sp.newUser();
break;
case 6:
flag=false;
System.out.println("感谢使用!");
break;
default:
System.out.println("感谢使用!");
System.exit(0);
}
}while (flag);
}
}
Util工具类
package shopping;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 文件名: Shopping
* 创建者: 李海金
* 创建时间:2020/8/7 13:33
* 工程名:高级特性二次复习
*/
public class Shopping {
Scanner sc = new Scanner(System.in);
Map<String, User> map = new HashMap<>();
//Util工具
public boolean util() {
System.out.println("请输入会员卡号:");
int id = sc.nextInt();
System.out.println("请输入密码:");
String psw = sc.next();
for (User user : map.values()) {
if (user.getId() == id && user.getPassWord().equals(psw)) {
return true;
}
}
return false;
}
// 积分累计
public void moreScore() {
System.out.println("请输入会员卡号:");
int id = sc.nextInt();
System.out.println("请输入密码:");
String psw = sc.next();
for (User user : map.values()) {
if (user.getId() == id && user.getPassWord().equals(psw)) {
System.out.print("请输入您的消费金额(1元兑换1积分):");
user.setScore(user.getScore() + sc.nextInt());
System.out.println("积分添加成功!\n您的积分为:" + user.getScore());
} else {
System.out.println("卡号与密码不匹配!");
break;
}
}
}
// 积分兑换
public void addScore() {
System.out.println("请输入会员卡号:");
int id = sc.nextInt();
System.out.println("请输入密码:");
String psw = sc.next();
for (User user : map.values()) {
if (user.getId() == id && user.getPassWord().equals(psw)) {
System.out.println("您的积分为:" + user.getScore());
System.out.print("请输入需要兑换金额(100积分兑换1元,剩余积分不足100时不可兑换):");
int a = sc.nextInt();
if (a <= user.getScore() / 100) {
user.setScore(user.getScore() - (a * 100));
System.out.println("兑换成功!\n本次兑换金额" + a + "元\n剩余积分为:" + (user.getScore()));
}else {
System.out.println("积分不够了!!!");
}
} else {
System.out.println("卡号与密码不匹配!");
break;
}
}
}
// 查询剩余积分
public void score() {
System.out.println("请输入会员卡号:");
int id = sc.nextInt();
System.out.println("请输入密码:");
String psw = sc.next();
for (User user : map.values()) {
if (user.getId() == id && user.getPassWord().equals(psw)) {
System.out.println("姓名\t\t会员卡号\t\t当前积分\t\t开卡日期");
System.out.println(user.getName() + "\t" + user.getId() + "\t\t" + user.getScore() + "\t\t\t" + user.getData());
} else {
System.out.println("卡号与密码不匹配!");
break;
}
}
}
// 修改密码
public void modify() {
System.out.println("请输入会员卡号:");
int id = sc.nextInt();
System.out.println("请输入旧密码:");
String psw = sc.next();
for (User user : map.values()) {
if (user.getId() == id && user.getPassWord().equals(psw)) {
System.out.println("请输入新密码:");
user.setPassWord(sc.next());
System.out.println("密码修改成功!");
} else {
System.out.println("卡号与密码不匹配!");
break;
}
}
}
// 开卡
public void newUser() {
User user = new User();
Random random = new Random();
int psw = random.nextInt(999998) + 1;
System.out.print("请输入姓名:");
user.setName(sc.next());
System.out.print("请设置您的密码(密码长度不得小于6位):");
user.setPassWord(sc.next());
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
user.setData(simpleDateFormat.format(date));
System.out.println("恭喜您成为本超市的会员,新用户注册赠送100积分,您的会员卡号为:" + psw + ",请牢记用户名和密码");
user.setId(psw);
map.put(user.getName(), user);
}
}
测试类
package shopping;
/**
* 文件名: Main
* 创建者: 李海金
* 创建时间:2020/8/7 13:43
* 工程名:高级特性二次复习
*/
public class Main {
public static void main(String[] args) {
new SOPP();
}
}
运行效果图