search
HomeJavajavaTutorialSteps and implementation methods of implementing supermarket membership management system in Java

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<Member> list = new ArrayList<Member>();
    
    /**
     * 用户可选择菜单
     */
    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:亿速云. If there is any infringement, please contact admin@php.cn delete
How does platform independence benefit enterprise-level Java applications?How does platform independence benefit enterprise-level Java applications?May 03, 2025 am 12:23 AM

Java is widely used in enterprise-level applications because of its platform independence. 1) Platform independence is implemented through Java virtual machine (JVM), so that the code can run on any platform that supports Java. 2) It simplifies cross-platform deployment and development processes, providing greater flexibility and scalability. 3) However, it is necessary to pay attention to performance differences and third-party library compatibility and adopt best practices such as using pure Java code and cross-platform testing.

What role does Java play in the development of IoT (Internet of Things) devices, considering platform independence?What role does Java play in the development of IoT (Internet of Things) devices, considering platform independence?May 03, 2025 am 12:22 AM

JavaplaysasignificantroleinIoTduetoitsplatformindependence.1)Itallowscodetobewrittenonceandrunonvariousdevices.2)Java'secosystemprovidesusefullibrariesforIoT.3)ItssecurityfeaturesenhanceIoTsystemsafety.However,developersmustaddressmemoryandstartuptim

Describe a scenario where you encountered a platform-specific issue in Java and how you resolved it.Describe a scenario where you encountered a platform-specific issue in Java and how you resolved it.May 03, 2025 am 12:21 AM

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

What are the benefits of Java's platform independence for developers?What are the benefits of Java's platform independence for developers?May 03, 2025 am 12:15 AM

Java'splatformindependenceissignificantbecauseitallowsdeveloperstowritecodeonceandrunitonanyplatformwithaJVM.This"writeonce,runanywhere"(WORA)approachoffers:1)Cross-platformcompatibility,enablingdeploymentacrossdifferentOSwithoutissues;2)Re

What are the advantages of using Java for web applications that need to run on different servers?What are the advantages of using Java for web applications that need to run on different servers?May 03, 2025 am 12:13 AM

Java is suitable for developing cross-server web applications. 1) Java's "write once, run everywhere" philosophy makes its code run on any platform that supports JVM. 2) Java has a rich ecosystem, including tools such as Spring and Hibernate, to simplify the development process. 3) Java performs excellently in performance and security, providing efficient memory management and strong security guarantees.

How does the JVM contribute to Java's 'write once, run anywhere' (WORA) capability?How does the JVM contribute to Java's 'write once, run anywhere' (WORA) capability?May 02, 2025 am 12:25 AM

JVM implements the WORA features of Java through bytecode interpretation, platform-independent APIs and dynamic class loading: 1. Bytecode is interpreted as machine code to ensure cross-platform operation; 2. Standard API abstract operating system differences; 3. Classes are loaded dynamically at runtime to ensure consistency.

How do newer versions of Java address platform-specific issues?How do newer versions of Java address platform-specific issues?May 02, 2025 am 12:18 AM

The latest version of Java effectively solves platform-specific problems through JVM optimization, standard library improvements and third-party library support. 1) JVM optimization, such as Java11's ZGC improves garbage collection performance. 2) Standard library improvements, such as Java9's module system reducing platform-related problems. 3) Third-party libraries provide platform-optimized versions, such as OpenCV.

Explain the process of bytecode verification performed by the JVM.Explain the process of bytecode verification performed by the JVM.May 02, 2025 am 12:18 AM

The JVM's bytecode verification process includes four key steps: 1) Check whether the class file format complies with the specifications, 2) Verify the validity and correctness of the bytecode instructions, 3) Perform data flow analysis to ensure type safety, and 4) Balancing the thoroughness and performance of verification. Through these steps, the JVM ensures that only secure, correct bytecode is executed, thereby protecting the integrity and security of the program.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.