《极简Java》学习实践三:超市会员管理系统

《极简Java》学习实践三:超市会员管理系统


问题描述

22.1 本项目为超市会员管理系统,用来对超市的会员进行管理。系统的主要功能是注册会员卡、修改密码、赠送积分、积分记录查询、兑换积分和退出。
程序的执行效果如下:

********欢迎进入奕昊超市会员管理系统********
1. 注册会员卡
2. 修改密码
3. 赠送积分
4. 积分记录查询
5. 兑换积分
6. 退出
***************************************
请选择:1
请输入注册姓名: xh
请输入注册密码: 123456
恭喜,注册会员卡成功,系统赠送您100积分!您的会员卡号为:508216
********欢迎进入奕昊超市会员管理系统********
1. 注册会员卡
2. 修改密码
3. 赠送积分
4. 积分记录查询
5. 兑换积分
6. 退出
***************************************
请选择:2
请输入您的会员卡号: 508216
请输入您的会员卡密码: 123456
请输入新的会员密码: 222222
密码修改成功!
********欢迎进入奕昊超市会员管理系统********
1. 注册会员卡
2. 修改密码
3. 赠送积分
4. 积分记录查询
5. 兑换积分
6. 退出
***************************************
请选择:3
请输入您的会员卡号: 508216
请输入您的会员卡密码: 222222
请输入您此次消费金额(消费1元积1分): 400
400积分已经累积到您的会员卡上!
********欢迎进入奕昊超市会员管理系统********
1. 注册会员卡
2. 修改密码
3. 赠送积分
4. 积分记录查询
5. 兑换积分
6. 退出
***************************************
请选择:4
姓名	会员卡号	密码	总共消费	剩余积分	开卡日期
xh	508216	222222	400	500	2023-09-09
********欢迎进入奕昊超市会员管理系统********
1. 注册会员卡
2. 修改密码
3. 赠送积分
4. 积分记录查询
5. 兑换积分
6. 退出
***************************************
请选择:5
请输入您的会员卡号: 508216
请输入您的会员卡密码: 222222
请输入您需要兑换使用的积分(100积分抵用1元,不足100的积分不做抵用): 400
您的消费金额中使用会员积分抵消4元
********欢迎进入奕昊超市会员管理系统********
1. 注册会员卡
2. 修改密码
3. 赠送积分
4. 积分记录查询
5. 兑换积分
6. 退出
***************************************
请选择:6
感谢您的使用,欢迎下次使用!

要求:用集合保存会员和会员消费信息,使用序列化来持久化会员和会员消费信息,每次系统启动时从磁盘上的文件中读取会员和会员消费信息,使用随机数类Random生成会员卡号,使用SimpleDateFormat类格式化日期。

思路解析

这个超市会员管理系统实现的基本思路,就是用Scanner类读取用户输入,然后System.out.println()输出信息。同时,需要建立一个类:

  • class Customer implements Serializable # 用于生成会员对象,实现Serializable接口即可把对象写入文件。

在main()方法中实例化这个类的对象,然后根据用户输入的信息,记录会员属性,退出时保存到文件。

源代码

为了方便,把所有的类都写在了Main.java中。Main.java文件的全部代码如下:

package cn.minimal.ComputerManage;
import java.io.*;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Main {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Scanner scanner = new Scanner(System.in);
        Customer customer;
        File file = new File("customer.object");
        if (!file.exists()) {
            customer = new Customer();
        }
        else {
            FileInputStream input = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(input);
            customer = (Customer)ois.readObject();
            ois.close();
        }
        Random generator = new Random();
        while (true) {
            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.print("请选择:");
            int selection = scanner.nextInt();
            if (selection < 1 || selection > 6) {
                System.out.println("选择输入错误!");
                continue;
            }
            if (selection == 1) {
                System.out.print("请输入注册姓名: ");
                String name = scanner.next();
                System.out.print("请输入注册密码: ");
                String password = scanner.next();
                int cardID = generator.nextInt(1000000);
                customer.openCard(name, password, cardID);
                System.out.println("恭喜,注册会员卡成功,系统赠送您100积分!您的会员卡号为:" + cardID);
            } else if (selection == 2) {
                System.out.print("请输入您的会员卡号: ");
                int inputCardID = scanner.nextInt();
                if (inputCardID != customer.cardID) {
                    System.out.println("您输入的卡号错误!");
                    continue;
                }
                System.out.print("请输入您的会员卡密码: ");
                String inputPassword = scanner.next();
                if (!inputPassword.equals(customer.password)) {
                    System.out.println("您输入的卡密码错误!");
                    continue;
                }
                System.out.print("请输入新的会员密码: ");
                customer.password = scanner.next();
                System.out.println("密码修改成功!");
            } else if (selection == 3) {
                System.out.print("请输入您的会员卡号: ");
                int inputCardID = scanner.nextInt();
                if (inputCardID != customer.cardID) {
                    System.out.println("您输入的卡号错误!");
                    continue;
                }
                System.out.print("请输入您的会员卡密码: ");
                String inputPassword = scanner.next();
                if (!inputPassword.equals(customer.password)) {
                    System.out.println("您输入的卡密码错误!");
                    continue;
                }
                System.out.print("请输入您此次消费金额(消费1元积1分): ");
                int consumption = scanner.nextInt();
                customer.score += consumption;
                customer.totalConsumption += consumption;
                System.out.println(consumption + "积分已经累积到您的会员卡上!");
            } else if (selection == 4) {
                DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
                String dStr = format2.format(customer.cardOpenDate);
                System.out.println("姓名\t会员卡号\t密码\t总共消费\t剩余积分\t开卡日期");
                System.out.println(customer.name + "\t" + customer.cardID + "\t" + customer.password + "\t" + customer.totalConsumption + "\t" + customer.score + "\t" + dStr);
            } else if (selection == 5) {
                System.out.print("请输入您的会员卡号: ");
                int inputCardID = scanner.nextInt();
                if (inputCardID != customer.cardID) {
                    System.out.println("您输入的卡号错误!");
                    continue;
                }
                System.out.print("请输入您的会员卡密码: ");
                String inputPassword = scanner.next();
                if (!inputPassword.equals(customer.password)) {
                    System.out.println("您输入的卡密码错误!");
                    continue;
                }
                System.out.print("请输入您需要兑换使用的积分(100积分抵用1元,不足100的积分不做抵用): ");
                int inputScore = scanner.nextInt();
                if (inputScore > customer.score) {
                    System.out.println("兑换积分输入错误!");
                    continue;
                }
                int deduction = inputScore / 100;
                System.out.println("您的消费金额中使用会员积分抵消" + deduction + "元");
                customer.score -= inputScore;
            } else {
                System.out.print("感谢您的使用,欢迎下次使用!");
                FileOutputStream fos = new FileOutputStream("customer.object");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(customer);
                oos.close();
                break;
            }
        }
    }
}
class Customer implements Serializable {
    String name = "";
    String password = "";
    int cardID = 0;
    int score = 0;
    Date cardOpenDate = new Date();
    int totalConsumption = 0;
    public Customer() {
    }
    public void openCard(String name, String password, int cardID) {
        this.name = name;
        this.password = password;
        this.cardID = cardID;
        this.score = 100;
        this.cardOpenDate = new Date();
        this.totalConsumption = 0;
    }
}

在IDE中运行代码

找到Main.java文件中main子程序左边的绿色箭头,点击即可运行代码,如图所示
《极简Java》学习实践三:超市会员管理系统

参考资料

  1. 夏昊 编著. 极简Java. 中国水利水电出版社. 2021.6
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

下唐人

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

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

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

打赏作者

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

抵扣说明:

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

余额充值