Java项目——客户管理系统(实现增删改查操作)

 所有功能运行无误(已测试)

2022.3 IDEA

JDK :11.0.16 

运行环境:Mac Book Pro  macOS Ventura 13.2(a) Beta版

如果用的IDEA创建.java文件的时候类直接就被定义出来了(文件名跟类名相同的哦)

首先

1、创建一个CMUtility工具类

代码如下:

import java.util.Scanner;

import java.util.*;

/**
 * ClassName: CMUtility
 * Package: Project02
 * Description:
 *
 * @Author 孟富生
 * @Create 2023/7/19 16:22
 * @Version 1.0
 */


/**
 CMUtility工具类:
 将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
 */
public class CMUtility {
    private static Scanner scanner = new Scanner(System.in);
    /**
     用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’5’中的任意字符,则方法返回。返回值为用户键入字符。
     */
    public static char readMenuSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);
            c = str.charAt(0);
            if (c != '1' && c != '2' &&
                    c != '3' && c != '4' && c != '5') {
                System.out.print("选择错误,请重新输入:");
            } else break;
        }
        return c;
    }
    /**
     从键盘读取一个字符,并将其作为方法的返回值。
     */
    public static char readChar() {
        String str = readKeyBoard(1, false);
        return str.charAt(0);
    }
    /**
     从键盘读取一个字符,并将其作为方法的返回值。
     如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
     */
    public static char readChar(char defaultValue) {
        String str = readKeyBoard(1, true);
        return (str.length() == 0) ? defaultValue : str.charAt(0);
    }
    /**
     从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
     */
    public static int readInt() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(2, false);
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }
    /**
     从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
     如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
     */
    public static int readInt(int defaultValue) {
        int n;
        for (; ; ) {
            String str = readKeyBoard(2, true);
            if (str.equals("")) {
                return defaultValue;
            }

            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }
    /**
     从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
     */
    public static String readString(int limit) {
        return readKeyBoard(limit, false);
    }
    /**
     从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
     如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
     */
    public static String readString(int limit, String defaultValue) {
        String str = readKeyBoard(limit, true);
        return str.equals("")? defaultValue : str;
    }
    /**
     用于确认选择的输入。该方法从键盘读取‘Y’或’N’,并将其作为方法的返回值。
     */
    public static char readConfirmSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false).toUpperCase();
            c = str.charAt(0);
            if (c == 'Y' || c == 'N') {
                break;
            } else {
                System.out.print("选择错误,请重新输入:");
            }
        }
        return c;
    }

    private static String readKeyBoard(int limit, boolean blankReturn) {
        String line = "";

        while (scanner.hasNextLine()) {
            line = scanner.nextLine();
            if (line.length() == 0) {
                if (blankReturn) return line;
                else continue;
            }

            if (line.length() < 1 || line.length() > limit) {
                System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
                continue;
            }
            break;
        }

        return line;
    }
}

 2、创建一个客户类Customer

代码如下:


/**
 * ClassName: Customer
 * Package: Project02
 * Description:
 *  客户类
 * @Author 孟富生
 * @Create 2023/7/19 09:10
 * @Version 1.0
 */
public class Customer {
    private String name;   //姓名
    private char genner;  //性别
    private int age;   //年龄
    private String phone;   //电话
    private String email;  //邮箱

    /**
     * 提供构造器
     */
    //无参构造器
    public Customer() {
    }
    //有参构造器
    public Customer(String name, char genner, int age, String phone, String email) {
        this.name = name;
        this.genner = genner;
        this.age = age;
        this.phone = phone;
        this.email = email;
    }

    /**
     * 提供各属性的get/set方法
     * @return
     */
    public String getName() {
        return name;
    }

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

    public char getGenner() {
        return genner;
    }

    public void setGenner(char genner) {
        this.genner = genner;
    }

    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;
    }
}

3、创建一个对象管理类CustomerList

代码如下: 

/**
 * ClassName: CustomerList
 * Package: Project02
 * Description:
 *
 * CustomerList为Customer对象管理模块,内部使用数组管理若干组Customer对象
 * @Author 孟富生
 * @Create 2023/7/19 09:19
 * @Version 1.0
 */
public class CustomerList {
    //只有当数组满了之后tonal才跟数组长度(customer.length)相等
    private Customer[] customers;  //用于保存客户的数组
    private int total;   //用于记录已保存的客户数量

    /**
     * 用途:构造器,初始化customer数组
     * @param totalCustomer   指定customer数组的最大空间(长度)
     */
    public CustomerList(int totalCustomer) {
        customers = new Customer[totalCustomer];
    }

    /**
     * 用途:将参数customer添加到最后一个客户之后(保证添加到最后的位置)
     * @param customer  指定要添加的客户对象
     * @return  添加成功返回true;false表示数组已满添加失败
     */
    public boolean addCustomer(Customer customer){
        if (total < customers.length){  //客户数量小于数组长度时才能添加
          //  customers[total] = customer;
          //  total++;   或 ++total; 表示添加成功客户增加了一个  此时前加加后加加都行
            //或
            customers[total++] = customer;  //必须后加加,因为后加加是先处理完赋值运算再进行加加
            return true;
        }
        /*
        else {
            return false;
        }
        或
         */
        return false;  //能执行到这一步说明前面的if条件不满足,所以可以不用else进行嵌套
    }

    /**
     * 用途:用参数cust替换数组中由index指定的对象
     * @param index  指定所替换对象在数组中的位置,从0开始
     * @param cust   指定替换的新客户对象
     * @return  替换成功返回true;false表示索引无效,无法替换
     */
    public boolean replaceCustomer(int index,Customer cust){
        if (index >= 0 && index < total){   //不是index <= total的原因:因为index从0开始,所以index永远比客户数小1,故取不了等号
            customers[index] = cust;
            return true;
        }
        return false;
    }

    /**
     * 用途:从数组中删除参数index指定索引位置的客户对象记录
     * @param index  指所删除对象在数组中的索引位置,从0开始
     * @return  删除成功返回true;false表示索引无效,无法删除
     */
    public boolean deleteCustomer(int index){
        if (index < 0 || index >= total){  //这样写避免了在if里套for
            return false;
        }
        //将index位置客户删除之后后面的客户往前移一位而最后的位置此时已经没有对象就赋值为null
        for (int i = index; i < total-1; i++) {  //total-1是照顾到customers [i+1]中的i+1不要越出数组长度
            customers[i] = customers[i + 1];
        }
     //   customers[total-1] = null;
     //   total--;
        //或
        customers[--total] = null;
        return true;
    }

    /**
     * 用途:返回数组中记录所以客户对象
     * @return  custs数组中包含了当前所以客户对象,该数组长度与对象数相同
     */
    public Customer[] getAllCustomers(){
        //错误的
       // return customers;   这返回的是整个数组,假如数组长度是10,只有三个客户,那么索引值2之后的都将返回null,不符合要求
        //正确的是造一个新数组,将非null的,即有客户的位置赋值给新的数组(数组是引用类型所以赋的是地址),
        Customer[] custs = new Customer[total];
        for (int i = 0; i < custs.length; i++) {
            custs[i] = customers[i];
        }
        return custs;
    }

    /**
     * 用途:返回参数index指定索引位置的客户对象记录
     * @param index  指定所要获取的对象客户在数组中的索引位置,从0开始
     * @return  封装了客户信息的Customer对象
     */
    public Customer getCustomer(int index){
        if (index >= 0 && index < total){
            return customers[index];
        }else {
            return null;
        }
    }

    /**
     * 用途:返回客户列表中客户的数量
     * @return total
     */
    public int getTotal(){
        return total;
    }
}

4、创建一个菜单操作类CustomerView

代码如下: 


/**
 * ClassName: CustomerView
 * Package: Project02
 * Description:
 *CustomerView的设计
 * CustomerView为主模块,负责菜单的显示和处理客户操作
 * @Author 孟富生
 * @Create 2023/7/19 11:05
 * @Version 1.0
 */
public class CustomerView {

    private CustomerList customerList = new CustomerList(10);

    public CustomerView(){
        //欲添加几个客户
        //性别是char类型用单引号,age是int类型不用双引号、单引号
        Customer cust = new Customer("王羲之",'男',98,"1394738924","abx@email.com");
        Customer cust1 = new Customer("孙悟空",'男',108,"1894738990","129@email.com");
        Customer cust2 = new Customer("樊梨花",'女',78,"1921738924","2qw@email.com");

        //将创建好的客户加入进去
        customerList.addCustomer(cust);
        customerList.addCustomer(cust1);
        customerList.addCustomer(cust2);
    }



    /**
     * 进入主界面的方法
     */
    public void enterMainMenu(){
        boolean bo = true;
        do{
            System.out.println("===============拼电商客户管理系统===============");
            System.out.println("\n\t\t\t\t1、添加客户");
            System.out.println("\t\t\t\t2、修改客户");
            System.out.println("\t\t\t\t3、删除客户");
            System.out.println("\t\t\t\t4、客户列表");
            System.out.println("\t\t\t\t5、退    出");
            System.out.println("\n\t\t\t\t请选择1-5:");
            char key = CMUtility.readMenuSelection();
            switch (key){
                //因为是char类型,所以要带上单引号
                case '1':
                    addNewCustomer();
                    break;
                case '2':
                    modifyCustomer();
                    break;
                case '3':
                    deleteCustomer();
                    break;
                case '4':
                    listAllCustomer();
                    break;
                case '5':
                    System.out.print("确认是否退出(Y/N):");
                    char isExit = CMUtility.readConfirmSelection();
                    if (isExit == 'Y'){  //Y必须大写,因为已经自动转换为大写
                        bo = false;
                    }
                    break;
            }

        }while (bo);

    }

    /**
     * 添加客户
     */
    private void addNewCustomer(){
        System.out.println("---------------添加客户---------------");
        System.out.print("姓名:");
        String name = CMUtility.readString(4);
        System.out.print("性别:");
        char gender = CMUtility.readChar();
        System.out.print("年龄:");
        int age = CMUtility.readInt();
        System.out.print("电话:");
        String phone = CMUtility.readString(15);
        System.out.print("邮箱:");
        String email = CMUtility.readString(15);

        Customer cust = new Customer(name,gender,age,phone,email);
        boolean flag = customerList.addCustomer(cust);
        if(flag){
            System.out.println("---------------添加完成---------------");
        }else {
            System.out.println("---------------客户已满,添加失败!---------------");
        }
    }

    /**
     * 修改客户
     */
    private void modifyCustomer(){
        System.out.println("---------------修改客户---------------");
        int index = 0;
        Customer cust = null;
        for(;;){
            System.out.print("请选择待修改客户编号(-1退出):");
            index = CMUtility.readInt();
            if(index == -1){
                return;
            }

            cust = customerList.getCustomer(index-1);
            if(cust == null){
                System.out.println("无法找到指定客户!");
            }else   //可以没有{}
                break;
        }
        System.out.print("姓名(" + cust.getName() + "):");
        String name = CMUtility.readString(4, cust.getName());

        System.out.print("性别(" + cust.getGenner() + "):");
        char gender = CMUtility.readChar(cust.getGenner());

        System.out.print("年龄(" + cust.getAge() + "):");
        int age = CMUtility.readInt(cust.getAge());

        System.out.print("电话(" + cust.getPhone() + "):");
        String phone = CMUtility.readString(15, cust.getPhone());

        System.out.print("邮箱(" + cust.getEmail() + "):");
        String email = CMUtility.readString(15, cust.getEmail());

        cust = new Customer(name, gender, age, phone, email);

        boolean flag = customerList.replaceCustomer(index-1, cust);
        if (flag) {
            System.out
                    .println("---------------------修改完成---------------------");
        } else {
            System.out.println("----------无法找到指定客户,修改失败--------------");
        }
    }

    /**
     * 删除客户
     */
    private void deleteCustomer(){
        System.out.println("---------------删除客户---------------");
        int index = 0;
        Customer cust = null;
        for (;;) {
            System.out.print("请选择待删除客户编号(-1退出):");
            index = CMUtility.readInt();
            if (index == -1) {
                return;
            }

            cust = customerList.getCustomer(index - 1);
            if (cust == null) {
                System.out.println("无法找到指定客户!");
            } else
                break;
        }

        System.out.print("确认是否删除(Y/N):");
        char yn = CMUtility.readConfirmSelection();
        if (yn == 'N')
            return;

        boolean flag = customerList.deleteCustomer(index - 1);
        if (flag) {
            System.out
                    .println("---------------------删除完成---------------------");
        } else {
            System.out.println("----------无法找到指定客户,删除失败--------------");
        }

    }

    /**
     * 客户列表
     */
    private void listAllCustomer(){
        System.out.println("---------------客户列表---------------");
        Customer[] custs = customerList.getAllCustomers();
        if (custs.length == 0) {
            System.out.println("没有客户记录!");
        } else {
            System.out.println("编号   姓名   性别   年龄\t 电话\t\t\t 邮箱");
            for (int i = 0; i < custs.length; i++) {
            System.out.println(i + 1 + "\t" + custs[i].getName()
                    + "\t" + custs[i].getGenner() + "\t" + custs[i].getAge()
                    + "\t\t" + custs[i].getPhone() + "\t" + custs[i].getEmail());
            }
        }

        System.out.println("-------------------------客户列表完成-------------------------");
    }

    public static void main(String[] args) {
        CustomerView view1 = new CustomerView();
        view1.enterMainMenu();
    }
}

创建完以上的四个类之后就可以运行CustomerView这个(类)文件

运行界面截图如下:

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值