Home  >  Article  >  Java  >  Steps and implementation methods of implementing supermarket membership management system in Java

Steps and implementation methods of implementing supermarket membership management system in Java

WBOY
WBOYforward
2023-05-07 13:43:082021browse

Requirements:Use the collection framework and practical classes to implement the system

1. Points accumulation
2. Points redemption
3. Query remaining points
4. Modify password
5, open card
6, exit

Execution result:

Card activation, points accumulation part:

Steps and implementation methods of implementing supermarket membership management system in Java

Redeem points and check the remaining points:

Steps and implementation methods of implementing supermarket membership management system in Java

Change the password and use the new password to check the remaining points:

Steps and implementation methods of implementing supermarket membership management system in Java

Exit part:

Steps and implementation methods of implementing supermarket membership management system in Java

Implementation ideas:

1. Create member user class:

  • Username, password, membership card number (randomly generated), registration date, points

2. Create supermarket business class:

  • Menu display

  • Business selection method of points deposit and withdrawal, points redemption method, points inquiry method, password modification method, card opening method

  • Determine whether there is a query element method in the collection (since the code in this method appears in other methods, it will be extracted and listed as a method)

3. Test class

Source code:

Member user class

package cn.zyq.Aug0203;

/**
 * 会员用户类
 * @author admin
 *
 */
public class Member {
    //姓名
    private String name;
    //密码
    private String pwd;
    //会员卡号
    private String id;
    //注册日期
    private String registData;
    //积分
    private int score;
    
    public Member() {
    }
    
    public Member(String name, String pwd, String id, String registData, int score) {
        super();
        this.name = name;
        this.pwd = pwd;
        this.id = id;
        this.registData = registData;
        this.score = score;
    }

    public String getName() {
        return name;
    }

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

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getId() {
        return id;
    }

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

    public String getRegistData() {
        return registData;
    }

    public void setRegistData(String registData) {
        this.registData = registData;
    }

    public int getScore() {
        return score;
    }

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

Supermarket business class

package cn.zyq.Aug0203;

/**
 * 超市业务类
 */
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Business {
    Scanner sc = new Scanner(System.in);
    List list = new ArrayList();
    
    /**
     * 用户可选择菜单
     */
    public void init() {
        System.out.println("\n--------------------欢迎进入会员管理系统--------------------\n");
        System.out.println("1.积分累计      2.积分兑换      3.查询剩余积分      4.修改密码      5.开卡            6.退出");
        System.out.println("\n-------------------------------------------------------");
        System.out.println();
        System.out.print("请选择您要进行的操作:");
        choose(sc.nextInt());
    }
    
    /**
     * 用户选择的业务
     *  @param num
     */
    public void choose(int num) {
        switch (num) {
        case 1:
            saveScore();
            break;
        case 2:
            useScore();
            break;
        case 3:
            search();
            break;
        case 4:
            updatePwd();
            break;
        case 5:
            regist();
            break;
        case 6:
            System.out.println("欢迎下次光临!");
            System.exit(0);
            break;
        }
        init();
    }
    
    /**
     * 积分积累
     */
    public void saveScore() {
        
        Member m = check();
        if(m!=null) {
            System.out.print("请输入您消费的金额(一元一积分):");
            int score = sc.nextInt();
            m.setScore(m.getScore()+score);
            System.out.println("积分增加成功,目前您的积分为:"+m.getScore());
            System.out.println("积分累计成功!");
        }else {
            System.out.println("积分累计失败,您输入的信息有误!");
        }
    }

    
    /**
     * 积分兑换
     */
    public void useScore() {
        
        Member m = check();
        if(m!=null) {
            System.out.print("请输入您需要兑换使用的积分(100积分抵用1元,不足100的积分不做抵用):");
            int score = sc.nextInt();
            if(m.getScore()>=100 && score>=100 && score<=m.getScore()) {
                m.setScore(m.getScore()-score);
                System.out.println("您本次消费抵用金额为:"+score/100);
                System.out.println("兑换积分成功!");
            }else {
                System.out.println("兑换积分失败,账户积分不足或需要兑换积分大于剩余积分!");
            }
        }else {
            System.out.println("账号信息不匹配,无法兑换积分!");
        }
    }
    
    /**
     * 查询剩余积分
     */
    public void search() {
        
        Member m = check();
        if(m!=null) {
            System.out.println("姓名\t会员卡号\t剩余积分\t开卡日期");
            System.out.println(m.getName()+"\t"+m.getId()+"\t"+m.getScore()+"\t"+m.getRegistData());
        }else {
            System.out.println("输入的账号信息不匹配!");
        }
    }
    
    /**
     * 修改密码
     */
    public void updatePwd() {
        
        Member m = check();
        if(m!=null) {
            System.out.print("请输入您的新密码:");
            String pwd = sc.next();
            
            //重新设置密码
            m.setPwd(pwd);
            System.out.println("密码修改成功!");
        }else {
            System.out.println("输入的账号信息不匹配,无法进行此业务!");
        }
    }
    
    
    /**
     * 积分兑换
     */
    public void regist() {
        System.out.print("欢迎使用本超市会员卡,请输入您的姓名:");
        String name = sc.next();
        System.out.print("请设置您的密码(要求密码长度大于6):");
        String pwd = sc.next();
        //判断密码是否合法
        boolean flag = false;
        while(!flag) {
            if(pwd.length()<6) {
                flag = false;
                System.out.print("密码长度小于6位,请重新输入密码:");
                pwd = sc.next();
            }
            else {
                flag = true;
            }
        }
        
        //生成一个八位数的随机会员卡号
        Random random = new Random();
        int rand = random.nextInt(999999);
        String id = String.valueOf(rand);
        //判断会员卡是否已存在
        for(Member m:list) {
            if(m.getId()==id) {
                rand = random.nextInt(99999999);
                id = String.valueOf(rand);
            }
        }
        
        //注册日期
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String registData = dateFormat.format(date);
        
        //开卡送积分100;
        int score = 100;
        //将用户记录添加到列表
        list.add(new Member(name, pwd, id, registData, score));
        System.out.println("恭喜你成为本超市会员,系统赠送您100积分,您的会员卡号为:"+id+",请牢记卡号和密码!");
        
    }
    
    /**
     * 信息检测,list中是否存有指定用户信息
     */
    public Member check() {
        System.out.print("请输入您的会员卡号:");
        String id = sc.next();
        System.out.print("请输入您的密码:");
        String pwd = sc.next();
        for(Member m:list) {
            if(m.getId().equals(id) && m.getPwd().equals(pwd)) {
                return m;
            }
        }
        return null;
    }
}

Test class

package cn.zyq.Aug0203;

/**
 * 测试类
 * @author admin
 *
 */
public class Test {
    public static void main(String[] args) {
        Business business = new Business();
        business.init();
    }
}

The above is the detailed content of Steps and implementation methods of implementing supermarket membership management system in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete