


Steps 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:
Redeem points and check the remaining points:
Change the password and use the new password to check the remaining points:
Exit part:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
