第二阶段:项目二--拼电商管理系统

主要实现基础的成员增删改查。

        1、首先构建用户实体类Customer。

        2、构建客户列表类CustomerList。

        3、构建主页面操作实现类CustomerView。

构建Customer类: 

package com.Management;

/**
 * ClassName:Customer
 * Package: com.Management
 * Description:
 *
 * @Author Joan Jia
 * @Create 2024/3/29 14:11
 * @Version 1.0
 */
public class Customer {
    private String name;
    private char gender;
    private int age;
    private String phone;
    private String email;

    public Customer(){

    }
    public Customer(String n,char g,int age,String p,String email) {
        this.name=n;
        this.gender=g;
        this.age=age;
        this.phone=p;
        this.email=email;
    }
    public String getName() {
        return name;
    }

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

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

构建 CustomerList类:

package com.Management;

/**
 * ClassName:CustomerList
 * Package: com.Management
 * Description:内部使用数组管理一组Customer对象
 *
 * @Author Joan Jia
 * @Create 2024/3/29 14:18
 * @Version 1.0
 */
public class CustomerList {
    private Customer[] customers;
    private int total=0;//记录已保存客户对象的数量

    public CustomerList(int totalCustomer) {
        //在创建此类构造器的同时创建客户对象数组
        customers=new Customer[totalCustomer];//totalCustomer记录数组最大容量
    }
    public int getTotal(){
        return total;
    }

    //添加客户
    public boolean addCustomer(Customer customer){
        if(this.getTotal()<customers.length){//如果当前数组未满,返回true
            this.customers[total]=customer;
            this.total++;
            System.out.println("添加成功");
            return true;
        }else //如果当前数组已满,返回false
            return false;
    }

    //修改客户信息,用cust替代索引是index的客户
    public boolean replaceCustomer(int index,Customer cust){
        if(index>=this.getTotal()||index<0||this.getTotal()==0){
            System.out.println("您输入的用户不存在,请重新输入");
            return false;
        }else{
            customers[index]=cust;
            return true;
        }
    }
    //删除客户,删除索引是index的客户
    public boolean deleteCustomer(int index){
        if(index>=this.getTotal()||index<0){
            return false;
        }else{
            for(int i=index;i<this.total;i++){
                Customer temp=customers[index];
                customers[index]=customers[index+1];
                customers[index+1]=temp;
            }
            System.out.println("已成功删除!");
            return true;
        }
    }

    //查询客户列表
    public Customer[] getAllCustomers(){
        return customers;
    }

    //查询索引是index的客户信息
    public Customer getCustomers(int index){
        return customers[index];
    }



    //作单元测试,测试该类中的方法是否正确执行
    public static void main(String[] args) {
        CustomerList customerLists=new CustomerList(5);
        Customer cust1=new Customer();
        Customer cust2=new Customer();
        cust1.setName("甄嬛");
        cust1.setAge(21);
        cust1.setGender('女');
        cust1.setEmail("zijincheng@123.com");
        cust1.setPhone("1254867651");

        cust2.setName("大胖橘");
        cust2.setAge(40);
        cust2.setGender('男');
        cust2.setEmail("yangxindian@123.com");
        cust2.setPhone("256842349");
        //添加客户
        customerLists.addCustomer(cust1);
        customerLists.addCustomer(cust2);
        //修改客户
        customerLists.replaceCustomer(0,cust2);
        //删除客户
        customerLists.deleteCustomer(3);
        Customer[] customers=customerLists.getAllCustomers();
        for(int i=0;i<customerLists.getTotal();i++){
            System.out.println("姓名:"+customers[i].getName()+",年龄:"+customers[i].getAge()+",性别:"+customers[i].getGender()+",电话:"+customers[i].getPhone()+",邮箱:"+customers[i].getEmail());
        }

        customerLists.getCustomers(1);


    }
}

 构建主页面操作实现类CustomerView类:

package com.Management;

/**
 * ClassName:CustomerView
 * Package: com.Management
 * Description:
 *
 * @Author Joan Jia
 * @Create 2024/3/29 15:33
 * @Version 1.0
 */
public class CustomerView {
    public static void main(String[] args) {
        CustomerView p1=new CustomerView();
        p1.enterMainMenu();
    }


    //创建一个最大包含10个客户的对象数组
    CustomerList customerList=new CustomerList(10);

    public void enterMainMenu(){
        boolean flag=true;

        do{
            System.out.println("------------拼电商客户管理系统------------");
            System.out.println();
            System.out.println("1 添加客户");
            System.out.println("2 修改客户");
            System.out.println("3 删除客户");
            System.out.println("4 客户列表");
            System.out.println("5 退\t出");
            System.out.println();
            System.out.println("请选择(1-5):");
            char w=CMUtility.readMenuSelection();//封装的工具类,键入一个数字
            switch(w){
                case '1':
                    this.addNewCustomer();
                    break;
                case '2':
                    this.modifyCustomer();
                    break;
                case '3':
                    this.deleteCustomer();
                    break;
                case '4':
                    this.listAllCustomers();
                    break;
                case '5':
                    flag=false;
                    System.out.println("成功退出!");
                    break;
            }
        }while(flag);


    }

    //主页面添加客户流程
    private void addNewCustomer(){
        Customer customer=new Customer();//每次调用该方法都会新创建一个客户对象。
        System.out.println("------------添加客户------------");
        System.out.print("姓名:");
        String name=CMUtility.readString(10);
        customer.setName(name);//给客户姓名赋值
        System.out.print("性别:");
        char gender=CMUtility.readChar();
        customer.setGender(gender);
        System.out.print("年龄:");
        int age=CMUtility.readInt();
        customer.setAge(age);
        System.out.print("电话:");
        String phone=CMUtility.readString(11);
        customer.setPhone(phone);
        System.out.print("邮箱:");
        String email=CMUtility.readString(30);
        customer.setEmail(email);
        customerList.addCustomer(customer);//将新创建的对象加入客户列表。

    }
    private void modifyCustomer(){
        System.out.println("------------修改客户------------");
        System.out.println("请选择待修改客户编号(-1退出):");//使用return返回一个空值
        int num=CMUtility.readInt();
        if(num==-1){//如果输入的是-1,则退出,返回一个空值。
            return;
        }else{
            Customer customer=customerList.getCustomers(num);//获取编号是num的客户对象。
            System.out.print("姓名("+customer.getName()+"):");
            String name = CMUtility.readString(4, customer.getName());
            System.out.print("性别("+customer.getGender()+"):");
            char gender = CMUtility.readChar(customer.getGender());
            System.out.print("年龄("+customer.getAge()+"):");
            int age = CMUtility.readInt(customer.getAge());
            System.out.print("电话("+customer.getPhone()+"):");
            String phone = CMUtility.readString(15, customer.getPhone());
            System.out.print("邮箱("+customer.getEmail()+"):");
            String email = CMUtility.readString(15, customer.getEmail());

            Customer cust=new Customer(name,gender,age,phone,email);
            boolean flag=customerList.replaceCustomer(num,cust);//将新修改的替换掉原来的。
            if(flag){
                System.out.println("已成功修改!");
            }else{
                System.out.println("修改失败!");
            }

        }
            /*
            Customer customer=customerList.getCustomers(num);//获取编号是num的客户对象。

            System.out.print("姓名("+customer.getName()+"):");
            String name=CMUtility.readString(10);
            if(name.equals("\n")){
                //这里第二个参数如果name是回车符则不修改,返回值和getName一样.
                String name1=CMUtility.readString(10, customer.getName());
                customer.setName(name1);
            }else//如果不是回车符,则修改成输入的名字
                customer.setName(name);

            System.out.print("性别("+customer.getGender()+"):");
            char gen=CMUtility.readChar();
            if(gen=='\n'){
                //这里第二个参数如果gen是回车符则不修改,返回值和getName一样.
                char gender=CMUtility.readChar(customer.getGender());
                customer.setGender(gender);
            }else//如果不是回车符,则修改成输入的名字
                customer.setGender(gen);


            System.out.print("年龄("+customer.getAge()+"):");
            int age=CMUtility.readInt();
            char c=(char)age;
            if(c=='\n'){
                //这里第二个参数如果gen是回车符则不修改,返回值和getName一样.
                int age1=CMUtility.readInt(customer.getAge());
                customer.setAge(age1);
            }else//如果不是回车符,则修改成输入的年龄
                customer.setAge(age);


            System.out.print("电话("+customer.getPhone()+"):");
            String phone=CMUtility.readString(11);
            if(phone.equals("\n")){
                String phone1=CMUtility.readString(11,customer.getPhone());
                customer.setPhone(phone1);
            }else{
                customer.setPhone(phone);
            }

            System.out.print("邮箱("+customer.getEmail()+"):");
            String email0=CMUtility.readString(30);
            if(email0.equals("\n")){
                String email1=CMUtility.readString(30,customer.getEmail());
                customer.setEmail(email1);
            }else{
                customer.setEmail(email0);
            }
            System.out.println("------------修改完成------------");
        }

             */

    }

    //查看客户列表
    public void listAllCustomers(){
        System.out.println("------------客户列表------------");
        System.out.println("编号\t姓名\t性别\t年龄\t电话\t邮箱");
        Customer[] customers=customerList.getAllCustomers();
        for(int i=0;i<customerList.getTotal();i++){
            System.out.println(i+"\t\t"+customers[i].getName()+"\t\t"+customers[i].getGender()+"\t\t"+customers[i].getAge()+"\t\t"+customers[i].getPhone()+"\t\t"+customers[i].getEmail());
        }
    }

    //删除客户
    public void deleteCustomer(){
        System.out.println("------------删除客户------------");
        System.out.println("请选择待删除的客户编号(-1退出)");
        int num=CMUtility.readInt();
        if(num==-1){//如果输入的是-1,则退出,返回一个空值。
            return;
        }else{
            System.out.print("确认是否删除(Y/N):");
            char yn = CMUtility.readConfirmSelection();
            if (yn == 'N')
                return;
            boolean flag=customerList.deleteCustomer(num);
            if(flag){
                System.out.println("删除成功!");
            }else{
                System.out.println("您输入有误,删除失败!");
            }
        }
    }

}

 注意点:

 1、在实现输入-1就退出操作,是在方法中直接写return;例如:

2、在 CustomerView类中编写客户修改方法时,不知道为啥注释的那块是错误的.........

要充分理解CMUtility类中的readString(a,b)方法:

        并且注意在CustomerList类中,定义了replaceCustomer方法实现修改后的用户替换。

3、对于Customer类中的构造器 ,可以根据情况适当构造有参构造器,这样比如在本例中的客户修改过程,使用有参构造器创建了实例对象,更方便。否则还要一个一个set方法赋值。

        已上传到Github:https://github.com/201917421/Customer_Management.git      

  Q:当软件退出时,所有客户信息便丢失了!运用什么手段可以将这些信息长久保存,以便在下一次运行软件时继续使用? 

 待解决...........咋整?

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值