项目二—— 分类会员管理系统

时间:2022-11-13 20:27:14

自己学习编写的程序,有很多不足之处请各位多多指正,谢谢大家!

系统需求:

1. 项目1中的功能。

2. 新增“会员等级管理”:

      等级体系定义:初级、中级、高级。不同的等级享受不同的折扣额度: 不打折、9折、8折。

3.  新增“高级会员生日祝福功能”:

     根据高级会员的生日,系统在当日的早上8:00,自动发送文字类型的生日祝福。

4. 系统可以根据“姓名”查询并输出他的等级。

5.  系统可以根据“会员编号”计算并输出经过打折以后实际应付的金额。

程序入口:

public class Run {

    public static void main(String[] args) {
        new SendBlessing();
        new Login();
        new MemberSystem();
    }

}

生日祝福:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 根据高级会员的生日,系统在当日的早上8:00,自动发送文字类型的生日祝福。
 * */
public class SendBlessing {
    List<Member> members;
    SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
    public SendBlessing(){
        sending();
    }
    
    public void sending() {
        members = MemberList.getMembers();
//        MemberLevel ML = new MemberLevel();
//        ML = null;
        Iterator<Member> it = members.iterator();
        while(it.hasNext()) {
            Member m = it.next();
            MemberLevel memberlevel = m.getMemberlevel();
            String level = memberlevel.getLevel();
            String birthday = m.getBirthday();
            String id = m.getId();
            String name = m.getName();
            try {
                Date date =sdf.parse(birthday);
                Calendar calendar1 = Calendar.getInstance();
                calendar1.setTime(date);
                Calendar calendar2 = Calendar.getInstance();
                int year1 = calendar1.get(Calendar.YEAR);
                int year2 = calendar2.get(Calendar.YEAR);
                int month1 = calendar1.get(Calendar.MONTH);
                int month2 = calendar2.get(Calendar.MONTH);
                int day1 = calendar1.get(Calendar.DATE);
                int day2 = calendar2.get(Calendar.DATE);
                int hour = calendar2.HOUR_OF_DAY;
                int minute = calendar2.MINUTE;
                int second = calendar2.SECOND;
                if(level.equals("高级") && month1 == month2 && day1 == day2 && hour == 8 && minute == 0 && second == 0){
                    System.out.println("祝会员编号编号:" + id + "会员姓名:" + name + "的会员生日快乐!");
                    }
                }
            catch(Exception e){
                System.out.println("传入数据格式错误");
            }
        }
    }
}

登录程序:

package com.Member_version2;

import java.util.Scanner;

public class Login {
    static Scanner sca = new Scanner(System.in);
    public Login() {
        loading();
    }
    
    public void loading() {
        System.out.println("请输入用户名:");
        String user = sca.next();
        System.out.println("请输入密码:");
        String password = sca.next();
        boolean flag = login(user, password);
        if(flag == true) {
            System.out.println("登录成功!");
        }else {
            System.out.println("登陆失败");
            System.exit(0);
        }
    } 
    
    public boolean login(String user, String password) {
        if(user.equals("admin") && password.equals("admin")) {
//            System.out.println("登录成功!");
            return true;
        }else {
//            System.out.println("登陆失败");
            return false;
        }
        
    }
}

主程序:

import java.text.SimpleDateFormat;
/**
 * 会员信息增删改查的入口,以及计算价格的方法入口
 * */
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class MemberSystem {
    static Scanner sca = new Scanner(System.in); 
    static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    MemberManage membermanage;
    MemberLevelManage memberlevelmanage;
    PriceCalculate pricecalculate;
    
    
    public MemberSystem() {
        System.out.println("欢迎来到分类会员管理系统!");
        membermanage = new MemberManage();
        memberlevelmanage = new MemberLevelManage();
        pricecalculate = new PriceCalculate();
        system();
    }
    

    public void system() {
        while(true) {
            menu();
            int n = sca.nextInt();
            switch(n) {
            case 1://添加会员
                add();
                break;
            case 2://显示会员信息:1输出所有会员信息show() 2按会员编号查找会员select1() 3 按姓名查找会员(只输出会员编号和等级)select2()
                do {
                    System.out.println("---1:输出所有会员信息---2:通过会员编号查询会员信息---3:通过姓名查询会员等级---");
                    int n1 = sca.nextInt();
                    switch(n1) {
                    case 1:
                        show();//显示所有会员信息
                        break;
                    case 2:
                        select1();//会员编号查询
                            break;
                    case 3:
                        select2();//会员姓名查询
                        break;
                    default:
                        System.out.println("请输入正确序号");
                        break;
                        }
                    }while(false);
                break;                        
            case 3://修改会员信息
                update();
                break;
            case 4://删除会员
                delete();
                break;
            case 5://支付金额计算
                calculate();
                break;
            case 7410://支付金额计算
                memberLevelManage();
                break;
            case 0:
                System.exit(0);
                break;
            default:
                System.out.println("输入错误,请重新输入");
                break;
            }
        }
    }
    
    public static void menu() {
        System.out.println("请根据提示进行操作");
        System.out.println("---1:添加会员信息---2:查询会员信息---3:修改会员信息---4:删除会员信息---5:计算支付价格---0:退出系统---");
    }
    
    /**
     * 添加会员,会员编号自动生成,注册时间自动获取当前时间
     * */
    public void add() {
        while(true) {
            //信息获取
            System.out.println("添加会员信息中···");
            System.out.println("会员姓名:");
            String name = sca.next();
            System.out.println("会员性别:");
            String sex = sca.next(); 
            System.out.println("会员生日:");
            System.out.println("年份:");
            String year = sca.next();
            System.out.println("月份:");
            String month = sca.next();
            System.out.println("日期:");
            String day = sca.next();
            String birthday = year + "/" + month + "/" + day;  
            System.out.println("会员电话:");
            String phoneNumber = sca.next();
            System.out.println("会员住址:");
            String address = sca.next();
            //会员注册日期获取当前时间
            String date = df.format(new Date());
            //会员编号自动生成
            String id = "M" + String.format("%06d", membermanage.count + 1);
            Member m = new Member(id,name,sex,date,phoneNumber,address,birthday,new MemberLevel());
            //调用add方法添加会员信息
            membermanage.add(m);
            System.out.println("添加成功!");
            break;
        }
    }
    
    /**
     * 显示所有会员信息
     * */
    public void show() {
        System.out.println("打印全部会员信息中···");
        List<Member> members = membermanage.show();
        if (!members.isEmpty()) { // 判断会员信息是否为空
            System.out.println("你要查询的会员信息:");
            Iterator it = members.iterator();
            while(it.hasNext()) {
                Member mem = (Member)it.next();
                System.out.println(mem);
                }
            }else {
            System.out.println("你所查询的会员信息为空!");
        }
    }
    /**
     * 按照会员编号查询信息,输出所有信息
     * */
    public void select1() {
        System.out.println("请输入需要查询的会员编号:");
        String id = sca.next();
        List<Member> members = membermanage.select1(id);
        if(members.isEmpty()) {
            System.out.println("当前无会员信息");
        }else {
            System.out.println("打印会员信息中···");
            System.out.println("编号为" + id +"的会员信息为:");
            System.out.println(members.get(0).toString());//通过会员编号只会查找只有一个会员信息
        }
    }
    /**
     * 按照姓名输出会员编号和会员等级信息,包括会员等级,积分,折扣
     * */
    public void select2() {
        System.out.println("请输入需要查询的会员姓名:");
        String name = sca.next();
        List member_l = membermanage.select2(name);
        if(member_l.equals(null)) {
            System.out.println("当前无会员信息");
        }else {
            System.out.println("打印会员信息中···");
            System.out.println("姓名为" + name +"的会员信息为:");
            System.out.println(member_l);//姓名查询可以查询到多个会员信息
        }
    }
    /**
     * 修改会员信息
     * */
    public void update() {
        System.out.println("请输入需要修改的会员编号:");
        String id = sca.next();
        List<Member> members = membermanage.select1(id);
        if(members.isEmpty()) {
            System.out.println("无此会员编号的会员信息");
        }else {
            System.out.println("请输入你要修改的信息:");
            System.out.println("会员姓名:");
            String name = sca.next();
            System.out.println("会员性别:");
            String sex = sca.next(); 
            System.out.println("会员生日:");
            System.out.println("年份:");
            String year = sca.next();
            System.out.println("月份:");
            String month = sca.next();
            System.out.println("日期:");
            String day = sca.next();
            String birthday = year + "/" + month + "/" + day; 
            System.out.println("会员电话:");
            String phoneNumber = sca.next();
            System.out.println("会员住址:");
            String address = sca.next();
            String id_u = id;
            String date = df.format(new Date());
            Member m = new Member(id_u,name,sex,date,phoneNumber,address,birthday,new MemberLevel());
            System.out.println("会员信息修改中···");
            List<Member> M = membermanage.update(members.get(0), m);
            System.out.println("修改后的会员信息为:");
            System.out.println(M);
        }
    }
    /**
     * 删除会员信息
     * */
    public void delete() {
        System.out.println("请输入需要删除的会员的编号:");
        String id = sca.next();
        List<Member> members = membermanage.select1(id);
        if(members.isEmpty()) {
            System.out.println("无此会员编号的会员信息");
        }else {
            System.out.println("以下是你要删除的会员信息:");
            System.out.println(members);
            System.out.println("确认删除请输入1,取消删除请输出2!");
            int n = sca.nextInt();
            if (n == 1) {
                System.out.println("会员信息删除中···");
                membermanage.delete(members.get(0));
                System.out.println("会员信息删除完毕!");
            }else {
                System.out.println("已取消删除操作!");
            }
            
        }
    }
    /**
     * 计算输入价格计算支付价格
     * 并且将价格加到会员积分上,每消费一元增加1积分
     * */
    public void calculate() {
        System.out.println("---欢迎使用支付价格计算系统---");
        System.out.print("请输入你商品总金额:");
        double price = sca.nextDouble();
        System.out.print("请输入您的会员编号:");
        String id = sca.next();
        double discount = memberlevelmanage.select1(id);
        double discountPrice = pricecalculate.calculate(price, discount);
        System.out.println("价格计算中···");
        System.out.println("商品的折扣后价格是:" + discountPrice);
        Member member = memberlevelmanage.select2(id);
        MemberLevel memberlevel = member.getMemberlevel();
        int integral = memberlevel.getIntegration();
        memberlevelmanage.addIntegral(member, integral, price);
    }
    /**
     * 手动修改会员积分
     * */
    public void memberLevelManage() {
        System.out.println("---隐藏起来的会员等级管理系统---");
        System.out.println("---在这里你可以任意修改会员积分!!!---");
        System.out.println("---请输入会员编号:---");
        String id = sca.next();
        {
            System.out.println("请输入更改后的会员积分:");
            int integral = sca.nextInt();
            Member member = memberlevelmanage.select2(id);
            memberlevelmanage.updateIntegral(member, integral); // 修改会员等级信息
                System.out.println("会员积分修改成功!");
        }
    }
}

会员信息管理:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 实现增删改查的功能
 * */
public class MemberManage {
    List<Member> members;
    public int count;
    
    public MemberManage() {
        super();
        count = MemberList.getCount();
    }
    
    /**
     * 添加会员
     * */
    public void add(Member m) {
        members = MemberList.getMembers();
        members.add(m);
        MemberList.setMembers(members);
        count++;
    }
    /**
     * 输出所有会员信息
     * */
    public List<Member> show() {
        members = MemberList.getMembers();
        return members;
    }
    /**
     * 按照会员编号查询信息,输出所有信息
     * */
    public List<Member> select1(String id) {
        members = MemberList.getMembers();
        List<Member> M = new ArrayList<Member>();//存储输出的会员信息
        M.clear();
        Iterator<Member> it = members.iterator();
        while(it.hasNext()) {
            Member m = it.next();
            if(m.getId().equals(id)) {
                M.add(m);
            }
        }
        return M;
        
    }
    /**
     * 按照姓名输出会员编号和会员等级信息,包括会员等级,积分,折扣
     * */
    public List select2(String name) {
        members = MemberList.getMembers();
        List<Member> M = new ArrayList<Member>();//存储输出的会员信息
        List L = new ArrayList<>();
        String S = new String();
        S = null;
        Iterator<Member> it = members.iterator();
        while(it.hasNext()) {
            Member m = it.next();
            if(m.getName().equals(name)) {
                M.add(m);
                L.add("会员编号:" + m.getId() + " 会员姓名:" + " " + m.getName() +m.getMemberlevel());
            }
        }
        return L;
    }
    /**
     * 修改会员信息
     * */
    public List<Member> update(Member M, Member m) {
        members = MemberList.getMembers();
        int index = members.indexOf(M);
        members.set(index, m);
        MemberList.setMembers(members);
        return members;    
    }
    /**
     * 删除会员信息
     * */
    public void delete(Member m) {
        members = MemberList.getMembers();
        Iterator<Member> it = members.iterator();
        while(it.hasNext()) {
            if(it.next().equals(m))
            it.remove();
        }
        MemberList.setMembers(members);
    }
}

会员等级管理:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 1.获取会员等级相应的折扣
 * 2.将价格添加到相应的会员积分上,1积分=一元
 * 3.修改会员等级信息(隐藏)
 * */
public class MemberLevelManage {
    List<Member> members;
    public MemberLevelManage() {
        super();
    }
    /**
     * 按照会员编号查找会员,并返回此会员的折扣信息
     * */
    public double select1(String id) {
        members = MemberList.getMembers();
        MemberLevel ML = new MemberLevel();
        ML = null;
        Iterator<Member> it = members.iterator();
        while(it.hasNext()) {
            Member m = it.next();
            if(m.getId().equals(id)) {
                ML= m.getMemberlevel();
            }        
        }
        double discount = ML.getDiscount();
        return discount;
    }
    /**
     * 通过会员编号查询并返回会员信息
     * */
    public Member select2(String id) {
        members = MemberList.getMembers();
        Iterator<Member> it = members.iterator();
        while (it.hasNext()) {
            Member member = it.next();
            if (member.getId().equals(id)) {
                MemberList.setMembers(members);
                return member;
            }
        }
        return null;
    }
    /**
     * 通过修改会员积分修改会员等级,折扣
     * */
    public void updateIntegral(Member member,int integral) {
        double discount;
        String level;
        if (integral < 2000) {
            discount = 1;
            level = "初级";
        } else if (2000 <= integral && integral < 5000) {
            discount = 0.9;
            level = "中级";
        } else {
            discount = 0.8;
            level = "高级";
        }
        members = MemberList.getMembers();
        int index = members.indexOf(member);
        member.setMemberlevel(new MemberLevel(level, discount, integral));
        members.set(index, member);
    }
    /**
     * 将价格添加到会员积分上
     * */
    public void addIntegral(Member member, int integral, double price) {
        double discount;
        String level;
        integral += (int)price;
        if (integral < 2000) {
            discount = 1;
            level = "初级";
        } else if (2000 <= integral && integral < 5000) {
            discount = 0.9;
            level = "中级";
        } else {
            discount = 0.8;
            level = "高级";
        }
        members = MemberList.getMembers();
        int index = members.indexOf(member);
        member.setMemberlevel(new MemberLevel(level, discount, integral));
        members.set(index, member);
    }
}

价格管理:

/**
 * 计算商品折扣后价格
 * */
public class PriceCalculate {
    
    public PriceCalculate() {
        super();
    }
    
    public double calculate(double price, double discount) {
        double discountPrice = price * discount;
        return discountPrice;
    }
}

会员信息:

/**
 * 1. 会员(Member)基础信息包括:会员编号、姓名、性别、注册时间、电话号码、地址、生日。
 * 
 * 
 * */
public class Member {
    private String id;//会员编号
    private String name;//姓名
    private String sex;//性别
    private String date;//日期
    private String phoneNumber;//联系电话
    private String address;//地址
    private String birthday;//生日
    private MemberLevel memberlevel;//等级信息
    
    /**
     * 此处需不需要赋初值呢?
     * */
    public Member(String id, String name, String sex, String date, String phoneNumber, 
            String address, String birthday,MemberLevel memberlevel) {
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.date = date;
        this.phoneNumber = phoneNumber;
        this.address = address;
        this.birthday = birthday;
        this.memberlevel = memberlevel;
    }
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        boolean p = true;
        while(p) {
            if(sex.equals("男") || sex.equals("女")) {
                this.sex = sex;
                p = false;
            }else {
                System.out.println("请输入“男”或“女”");
                p = false;
            }
        }
        return;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
    public MemberLevel getMemberlevel() {
        return memberlevel;
    }
    public void setMemberlevel(MemberLevel memberlevel) {
        this.memberlevel = memberlevel;
    }

    @Override
    public String toString() {
        return "会员编号=" + id + ", 姓名=" + name + ", 性别=" + sex + ", 注册日期=" + date + ", 联系电话="
                + phoneNumber + ", 地址=" + address + ", 生日=" + birthday  + ", " + memberlevel.toString();
    }
}

会员等级信息:

/**
 * 会员积分,折扣,等级
 * */
public class MemberLevel {
    private String level;//会员等级
    private double discount;//折扣
    private int integration; //积分    
    
    public MemberLevel() {
        super();
        this.level = "初级";
        this.discount = 1.0;
        this.integration = 0;
    }
    
    
    public MemberLevel(String level, double discount, int integration) {
        super();
        this.level = level;
        this.discount = discount;
        this.integration = integration;
    }


    public String getLevel() {
        return level;
    }
    public void setLevel(String level) {
        if(integration >= 0 || integration<= 2000) {
            this.level = "初级";
        }
        if(integration > 2000 || integration<= 5000) {
            this.level = "中级";
        }
        if(integration > 10000) {
            this.level = "高级";
        }
    }
    public double getDiscount() {
        return discount;
    }
    public void setDiscount(double discount) {
        if(level.equals("初级")) {
            this.discount = 1.0;
        }
        if(level.equals("中级")) {
            this.discount = 0.9;
        }
        if(level.equals("高级")) {
            this.discount = 0.8;
        }
    }
    public int getIntegration() {
        return integration;
    }
    public void setIntegration(int integration) {
        this.integration = integration;
    }

    @Override
    public String toString() {
        return "会员等级=" + level + ", 折扣=" + discount + ", 会员积分=" + integration;
    }
    
}

数组信息:

import java.util.ArrayList;
/**
 * 集合类,存储数据
 * */
import java.util.List;

public class MemberList {
    private static List<Member> members = new ArrayList<Member>();//将Member对象放入集合中存储
    private static int count;//标记集合中对象的数量
    
    
    public static List<Member> getMembers() {
        return members;
    }
    public static void setMembers(List<Member> members) {
        MemberList.members = members;
    }
    public static int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
}