客户信息管理系统

1.首先先在同一个包类创建一个Customer类,作为对象

用这个类来封装客户的基本信息,使用private,包括姓名,性别,年龄,电话,邮箱,然后风别用get/set方法来方便后面调用。

2.再在用一个包类创建CustomerList,把上面的Customer作为类型封装数组,同时也要封装一个数作为这个数组的长度,再创建对应的方法,让CustomerView来调用

注意,可以使用构造器,来获得这个数组的长度,因为数组的长度是不能变的,而我们可以通过方法,来在设置主页面哪里,通过这个构造器来获得需要的长度

3.再创建一个CustomerView类,用来显示页面

4.这里要使用一个工具包CMUtility.java类

详细如下:

工具包:

package 系统管理4;

import java.util.Scanner;

/**
 * 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类:

package 系统管理4;

public class Customer {
    //构建客户信息
    private String name;
    private String sex;
    private int age;
    private String tel;
    private String email;
    //构造构造器
    public Customer(String name, String sex, int age, String tel, String email) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.tel = tel;
        this.email = email;
    }
    //设置set/get方法
    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

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

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getEmail() {
        return email;
    }

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

3.CusttomerList类:

package 系统管理4;

import 系统管理4.Customer;

public class CustomerList {
    //封装下面的信息
    private Customer[] customers;
    private int total = 0;

    //进入的时候设置数组长度,在CustomerView中先使用用来设置长度
    public CustomerList(int totalCustomer) {
        customers = new Customer[totalCustomer];
    }

    //设置set/get方法,这里是获得这个数组的长度
    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    //判断添加的是否满了
    public boolean addCustomer(Customer customer) {
        if (total < 0 || total >= customers.length) {//如果total小于0或者大于数组的长度就返回false
            return false;
        }
        customers[total++] = customer;//每添加一次total加一
        return true;
    }

    //修改客户信息
    public boolean replaceCustomer(int index, Customer customer) {
        if (index < 0 | index >= total) {//传入的索引值不能是小于0,或者大于等于数组的长度的
            return false;
        } else {
            customers[index] = customer;//把这个带入的customer赋值给原来传入的索引的位置
            return true;
        }
    }

    //删除客户信息
    public boolean deleteCustomer(int index) {
        if (index < 0 | index >= total) {
            return false;
        } else {
            for (int x = index; x < total - 1; x++) {//使用循环,从删除的位置往上移
                customers[x] = customers[x + 1];
            }
            customers[--total] = null;//因为前面是total++时,最后的total不是最大索引值,total-1才是
            return true;
        }
    }

    //查询全部人信息
    public Customer[] getAllCustomer() {
        return customers;//访问回数组
    }

    //查询一个人的信息
    public Customer getCustomer(int index) {//获得对应的数组,返回值
        if (index < 0 | index >= total) {
            return null;
        }
        return customers[index];
    }
}

CustomerView类:

package 系统管理4;

import 系统管理4.CMUtility;
import 系统管理4.CustomerList;
import 系统管理4.Customer;


public class CustomerView {

    //使用CustomerList中的构造方法,同时封装
    private CustomerList customerList = new CustomerList(100);

    public CustomerView() {//创建初始值
        Customer customer = new Customer("王涛", "男", 23, "13213123", "wt@gmail.com");
        customerList.addCustomer(customer);
    }

    //构造主页面
    public void enterManinMenu() {
        while (true) {
            System.out.println("\n----------客户信息管理软件--------");
            System.out.println(" 1.添加客户");
            System.out.println(" 2.修改客户");
            System.out.println(" 3.删除客户");
            System.out.println(" 4.客户列表");
            System.out.println(" 5.退出\n");
            System.out.print(" 请选择(1-5):");

            char function = CMUtility.readMenuSelection();
            switch (function) {
                case '1':
                    addNewCustomer();
                    break;
                case '2':
                    modifyCustomer();
                    break;
                case '3':
                    deleteCustomer();
                    break;
                case '4':
                    listAllCustomer();
                    break;
                case '5':
                    System.out.println("确认是否退出(Y/N):");
                    char exit = CMUtility.readChar();
                    if (exit == 'Y') {
                        System.out.println("退出成功");
                        return;
                    } else {
                        System.out.println("即将返回主界面");
                        break;
                    }
            }
        }
    }

    //构造添加客户方法
    public void addNewCustomer() {
        System.out.println("------添加客户------");
        //录入数据
        System.out.print("姓名:");
        String name = CMUtility.readString(10);
        System.out.print("性别:");
        String sex = CMUtility.readString(5);
        System.out.print("年龄:");
        int age = CMUtility.readInt();
        System.out.print("电话:");
        String tel = CMUtility.readString(13);
        System.out.print("邮箱:");
        String email = CMUtility.readString(30);
        //创建对象用来保存这次的数据
        Customer customer = new Customer(name, sex, age, tel, email);
        boolean judge = customerList.addCustomer(customer);//带入CustomerList中的add方法判断是否客户信息满了
        if (judge == true) {
            System.out.println("------添加完成------");
        } else {
            System.out.println("------客户已满------");
        }
    }

    //修改客户信息
    public void modifyCustomer() {
        System.out.println("----------------修改客户-----------------");
        Customer custs;
        int number;
        //找到输入编号的数据
        while (true) {
            System.out.println("选择要修改的客户编号(-1退出):");
            number = CMUtility.readInt(3);
            if (number == -1) {
                return;
            } else {
                custs = customerList.getCustomer(number - 1);
            }
            if (custs == null) {
                System.out.println("无法找到该用户");
            } else {
                break;
            }
        }
        //进行修改
        //先用对应的数据类型存得到的值
        System.out.println("姓名(" + custs.getName() + ")");
        String name = CMUtility.readString(10, custs.getName());
        System.out.println("性别(" + custs.getSex() + ")");
        String sex = CMUtility.readString(5, custs.getSex());
        System.out.println("年龄(" + custs.getAge() + ")");
        int age = CMUtility.readInt(custs.getAge());
        System.out.println("电话(" + custs.getTel() + ")");
        String tel = CMUtility.readString(13, custs.getTel());
        System.out.println("邮箱(" + custs.getEmail() + ")");
        String email = CMUtility.readString(30, custs.getEmail());
        //把要修改的内容创建一个新的对象存起来
        Customer newcust = new Customer(name, sex, age, tel, email);//再把值带入这个创建的对象内
        //调用CustomerList中的replaceCustomer方法
        boolean judge = customerList.replaceCustomer(number - 1, newcust);//带入参数,判断是否成功
        if (judge == true) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }
    }

    //删除客户信息
    public void deleteCustomer() {
        System.out.println("------------------删除客户------------------");
        Customer newcust;
        int number;
        while (true) {
            System.out.println("请选择待删除客户编号(-1退出)");
            number = CMUtility.readInt();
            if (number == -1) {//如果输入-1就退出删除功能
                return;
            } else {//如果不是就获得对应输入的值的数组
                newcust = customerList.getCustomer(number - 1);
            }
            if (newcust == null) {//如果使用getCustomer的方法中得到的值是NULL就执行下面
                System.out.println("无法找到该客户");
            } else {
                break;
            }
        }
        System.out.println("确认是否删除(Y/N):");
        char judge = CMUtility.readConfirmSelection();
        if (judge == 'Y') {//判断是否删除
            boolean judge1 = customerList.deleteCustomer(number - 1);//使用CustomerList中的deleteCustomer,判断,这个值是否在数组内
            if (judge1 == true) {//如果是则输出以下
                System.out.println("删除成功");
            } else {//如果不是就输出下面
                System.out.println("删除失败");
            }
        } else if (judge=='N'){//如果是N就执行下面
            System.out.println("已退出删除");
            return;
        }
    }

    //查询全部人的信息
    public void listAllCustomer() {
        System.out.println("------------------------客户列表------------------------");
        int total = customerList.getTotal();
        if (total == 0) {
            System.out.println("没有客户信息");
        } else {
            System.out.println("编号\t姓名\t\t性别\t\t年龄\t\t电话\t\t\t\t邮箱");
            Customer[] customer = customerList.getAllCustomer();//使用CustomerList中的getAllCustomer方法,获得数组
            int number = 1;//设置编号
            for (int x = 0; x < total; x++) {//使用循环打印对应数组的内容
                System.out.println(number + "\t" + customer[x].getName() + "\t\t" + customer[x].getSex() + "\t\t" + customer[x].getAge() + "\t\t" + customer[x].getTel() + "\t\t\t\t" + customer[x].getEmail());
                number++;
            }
        }
    }

    //运行主界面
    public static void main(String[] args) {//运行界面
        CustomerView customerView = new CustomerView();//创建对象
        customerView.enterManinMenu();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值