用java写一个超级详细的超市积分管理系统-带注释

文章介绍了一个超市积分管理系统的详细设计,包括用户注册、积分赠送、积分兑换、积分查询等功能,并提供了核心功能的Java代码实现,如用户注册和积分赠送的示例。系统基于Java语言,使用Springboot和Mybatis等技术栈,数据库可选MySQL或Oracle等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.系统简介: 此超市积分管理系统用于记录顾客在超市中购物所获得的积分。系统主要包括以下功能:顾客注册,积分赠送,积分兑换,积分查询,积分排名,积分兑换券管理等。

2.系统设计: a. 首先需要设计数据库,包括顾客信息表、商品表、积分日志表、兑换券表等。 b. 实现用户注册功能,包括保存用户信息,并为用户分配一个唯一的id。 c. 积分赠送功能:管理员可以为用户赠送积分,系统需要记录赠送时间、赠送数量、赠送人等信息。 d. 积分兑换功能:用户可以使用积分兑换奖品或兑换券,系统需要记录兑换时间、兑换商品、购买数量、消耗积分数等信息。 e. 积分查询功能:顾客可以通过输入id查询个人积分,管理员可以查询所有顾客的积分情况。 f. 积分排名功能:系统可以根据顾客积分排序,并展示前N名顾客的积分与排名情况。 g. 积分兑换券管理功能:管理员可以添加、删除、查询兑换券信息,顾客可以查看并使用兑换券。

3.技术实现: a. 数据库选择:可以使用MySQL或者Oracle等关系型数据库,也可以使用NoSQL数据库如MongoDB等。 b. 后端技术:使用Java语言,可以选择Spring boot作为后端框架,Mybatis或者Hibernate作为ORM框架。 c. 前端技术:使用HTML、CSS、JavaScript、Jquery等前端技术进行页面设计。

以上是此超市积分管理系统的概述及功能设计

下面是超市积分管理系统的Java代码实现。

1.用户注册功能:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class UserRegister {
    // 用户信息表
    private static Map<Integer, User> userMap = new HashMap<>();
    // 用户id
    private static int userId = 1;

    /**
     * 注册功能
     */
    public static void register() {
        System.out.println("请输入用户名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        User user = new User(userId, name, 0);
        userMap.put(userId, user);
        // 分配id
        userId++;
        System.out.println("注册成功,您的用户id为:" + (userId - 1));
    }
}

/**
 * 用户类
 */
class User {
    // 用户id
    private int id;
    // 用户名
    private String name;
    // 用户积分
    private int score;

    public User(int id, String name, int score) {
        this.id = id;
        this.name = name;
        this.score = score;
    }

    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 int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

2.积分赠送功能:

import java.time.LocalDateTime;
import java.util.Scanner;

public class ScoreGiving {
    // 积分日志表
    private static ScoreLog[] scoreLogArr = new ScoreLog[20];
    // 日志id
    private static int logId = 1;

    /**
     * 积分赠送功能
     */
    public static void giving() {
        System.out.println("请输入大礼包发放时间(例:2022-01-01 12:12:12):");
        Scanner scanner = new Scanner(System.in);
        String timeStr = scanner.nextLine();
        LocalDateTime time = LocalDateTime.parse(timeStr);
        System.out.println("请输入赠送积分数:");
        int score = scanner.nextInt();
        System.out.println("请输入赠送人姓名:");
        scanner.nextLine();
        String giver = scanner.nextLine();

        System.out.println("请选择接收人:");
        int id = UserQuery.queryAllUser();
        User user = UserQuery.queryUserById(id);

        // 保存积分日志
        ScoreLog scoreLog = new ScoreLog(logId, time, giver, user.getName(), score);
        scoreLogArr[logId - 1] = scoreLog;
        logId++;
        // 更新用户积分
        int newScore = user.getScore() + score;
        user.setScore(newScore);
        System.out.println("赠送成功,积分数为:" + score);
    }
}

/**
 * 积分日志类
 */
class ScoreLog {
    // 日志id
    private int id;
    // 日志时间
    private LocalDateTime time;
    // 赠送人
    private String giver;
    // 接收人
    private String receiver;
    // 赠送积分数
    private int score;

    public ScoreLog(int id, LocalDateTime time, String giver, String receiver, int score) {
        this.id = id;
        this.time = time;
        this.giver = giver;
        this.receiver = receiver;
        this.score = score;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public LocalDateTime getTime() {
        return time;
    }

    public void setTime(LocalDateTime time) {
        this.time = time;
    }

    public String getGiver() {
        return giver;
    }

    public void setGiver(String giver) {
        this.giver = giver;
    }

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

以上是用户注册和积分赠送功能的代码实现。

3.积分兑换功能:

import java.time.LocalDateTime;
import java.util.Scanner;

public class ScoreExchange {
    // 兑换券表
    private static Coupon[] couponArr = new Coupon[10];
    // 兑换券id
    private static int couponId = 1;

    /**
     * 积分兑换功能
     */
    public static void exchange() {
        System.out.println("请输入需兑换商品名称:");
        Scanner scanner = new Scanner(System.in);
        String goodsName = scanner.nextLine();
        System.out.println("请输入兑换商品数量:");
        int number = scanner.nextInt();

        // 计算总积分数
        int totalScore = GoodsQuery.queryScoreByName(goodsName) * number;
        // 获取要兑换的用户
        System.out.println("请选择用户:");
        int id = UserQuery.queryAllUser();
        User user = UserQuery.queryUserById(id);
        // 判断积分是否足够
        if (user.getScore() < totalScore) {
            System.out.println("抱歉,您的积分不足!");
            return;
        }
        // 扣除积分
        int newScore = user.getScore() - totalScore;
        user.setScore(newScore);
        System.out.println("扣除积分:" + totalScore);

        // 保存积分日志
        LocalDateTime time = LocalDateTime.now();
        ScoreLog scoreLog = new ScoreLog(ScoreGiving.getLogId(), time, "系统", user.getName(), -totalScore);
        ScoreGiving.getLogArr()[ScoreGiving.getLogId() - 1] = scoreLog;
        ScoreGiving.setLogId(ScoreGiving.getLogId() + 1);

        // 保存兑换券
        int couponScore = 0;
        if (totalScore >= 500) {
            couponScore = 100;
        } else if (totalScore >= 300) {
            couponScore = 50;
        } else {
            couponScore = 20;
        }
        Coupon coupon = new Coupon(couponId, user.getName(), goodsName, number, couponScore);
        couponArr[couponId - 1] = coupon;
        couponId++;
        System.out.println("兑换成功,兑换券面额为:" + couponScore);

    }
}

/**
 * 兑换券类
 */
class Coupon {
    // 兑换券id
    private int id;
    // 用户名
    private String name;
    // 商品名称
    private String goodsName;
    // 商品数量
    private int number;
    // 兑换券面额
    private int score;

    public Coupon(int id, String name, String goodsName, int number, int score) {
        this.id = id;
        this.name = name;
        this.goodsName = goodsName;
        this.number = number;
        this.score = score;
    }

    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 getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

4.积分查询功能:

import java.util.Scanner;

public class ScoreQuery {
    /**
     * 查询个人积分
     */
    public static void queryMyScore() {
        System.out.println("请输入用户id:");
        Scanner scanner = new Scanner(System.in);
        int id = scanner.nextInt();
        User user = UserQuery.queryUserById(id);
        if (user == null) {
            System.out.println("未找到该用户!");
            return;
        }
        System.out.println("用户:" + user.getName() + ",积分为:" + user.getScore());
    }

    /**
     * 查询所有用户积分
     */
    public static void queryAllScore() {
        System.out.println("用户id\t用户名\t积分");
        for (Map.Entry<Integer, User> entry : UserRegister.getUserMap().entrySet()) {
            User user = entry.getValue();
            System.out.println(user.getId() + "\t" + user.getName() + "\t" + user.getScore());
        }
    }
}

以上是积分兑换和积分查询功能的代码实现。

5.积分排名功能:

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

public class ScoreRank {
    /**
     * 积分排名
     *
     * @param num 显示排名的数量
     */
    public static void rank(int num) {
        // 对用户排序(按照积分降序)
        Map<Integer, User> scoreMap = UserRegister.getUserMap();
        Map<Integer, User> sortedMap = new TreeMap<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return scoreMap.get(o2).getScore() - scoreMap.get(o1).getScore();
            }
        });
        sortedMap.putAll(scoreMap);
        // 显示排名
        System.out.println("用户id\t\t用户名\t\t积分\t\t排名");
        int count = 0;
        int lastScore = Integer.MAX_VALUE;
        int rank = 1;
        for (Map.Entry<Integer, User> entry : sortedMap.entrySet()) {
            User user = entry.getValue();
            if (user.getScore() != lastScore) {
                rank = count + 1;
                lastScore = user.getScore();
            }
            System.out.println(user.getId() + "\t\t" + user.getName() + "\t\t" + user.getScore() + "\t\t" + rank);
            count++;
            if (count == num) {
                break;
            }
        }
    }
}

6.积分兑换券管理功能:

import java.util.Scanner;

public class CouponManager {
    /**
     * 添加兑换券
     */
    public static void addCoupon() {
        System.out.println("请输入需要添加的兑换券面额:");
        Scanner scanner = new Scanner(System.in);
        int score = scanner.nextInt();
        System.out.println("请输入此兑换券所需积分数:");
        int needScore = scanner.nextInt();
        Coupon coupon = new Coupon(ScoreExchange.getCouponId(), null, null, 0, score);
        ScoreExchange.getCouponArr()[ScoreExchange.getCouponId() - 1] = coupon;
        ScoreExchange.setCouponId(ScoreExchange.getCouponId() + 1);
        System.out.println("添加成功!");
    }

    /**
     * 删除兑换券
     *
     * @param id 兑换券id
     */
    public static void deleteCoupon(int id) {
        System.out.println("是否确认删除此兑换券?(Y/N)");
        Scanner scanner = new Scanner(System.in);
        String choice = scanner.nextLine();
        if (choice.equals("Y")) {
            ScoreExchange.getCouponArr()[id - 1] = null;
            System.out.println("删除成功!");
        }
    }

    /**
     * 查询所有兑换券信息
     */
    public static void queryAllCoupon() {
        System.out.println("兑换券id\t\t兑换人\t\t商品名称\t\t商品数量\t\t兑换券面额");
        for (Coupon coupon : ScoreExchange.getCouponArr()) {
            if (coupon == null) {
                continue;
            }
            System.out.println(coupon.getId() + "\t\t" + coupon.getName() + "\t\t" + coupon.getGoodsName() + "\t\t" + coupon.getNumber() + "\t\t" + coupon.getScore());
        }
    }
}

以上是积分排名和积分兑换券管理功能的代码实现。整个超市积分管理系统的代码实现就包括上述部分。

 

补充部分1.商品信息查询

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class GoodsQuery {
    // 商品信息表
    private static List<Goods> goodsList = new ArrayList<>();

    static {
        Goods g1 = new Goods("牛奶", 2, 100);
        goodsList.add(g1);
        Goods g2 = new Goods("鸡蛋", 1, 50);
        goodsList.add(g2);
        Goods g3 = new Goods("面包", 3, 80);
        goodsList.add(g3);
    }

    /**
     * 根据商品名称查询积分
     *
     * @param name 商品名称
     * @return 积分
     */
    public static int queryScoreByName(String name) {
        for (Goods goods : goodsList) {
            if (goods.getName().equals(name)) {
                return goods.getScore();
            }
        }
        System.out.println("未找到该商品信息!");
        return 0;
    }

}

/**
 * 商品类
 */
class Goods {
    // 商品名称
    private String name;
    // 商品价格
    private int price;
    // 商品积分
    private int score;

    public Goods(String name, int price, int score) {
        this.name = name;
        this.price = price;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乌托邦物联

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值