【 java 面向对象】基于文本界面的客户信息管理系统

发布于:2023-01-05 ⋅ 阅读:(263) ⋅ 点赞:(0)

📋 个人简介

  • 💖 作者简介:大家好,我是阿牛,全栈领域新星创作者。😜
  • 🎉 支持我:点赞👍+收藏⭐️+留言📝
  • 📣 系列专栏:重走 java 路🍁
  • 💬格言:要成为光,因为有怕黑的人!🔥
    请添加图片描述

前言

对于面向对象这一块的复习,以这个项目为核心复习吧!本项目模拟实现一个基于文本界面的客户信息管理软件,进一步掌握编程技巧和调试技巧,熟悉面向对象编程!

项目涉及知识点

类结的的使用,属性、方法及构造器
对象的创建与使用
类的封装性
声明和使用数组
数组的插入、删除和替换
关键字的使用: this

软件设计结构

在这里插入图片描述
CustomerView 为主模块,负责菜单的显示和处理用户操作。

CustomerList 为 Customer 对象的管理模块,内部用数组管理一组 Customer 对象,并提供相应的添加、修改、删除和遍历方法,供 CustomerView 调用。

Customer 为实体对象,用来封装客户信息。

设计

Customer类的设计

  • Customer 为实体类,用来封装客户信息

  • 该类封装客户的以下信息;
    String name:客户姓名
    char sex : 性别
    int age : 年龄
    String phone :电话号码
    String email :电子邮箱

  • 提供各属性的 get、set 方法

  • 提供所需的构造器

实际上Customer类就是一个javabean !

CustomerList类的设计

  • CustomerList 为 Customer 对象的管理模块,内部使用数组管理一组 Customer 对象

  • 本类封装以下信息:
    Customer [ ] customers :用来保存客户对象的数组
    int total =0 : 记录已保存客户对象的数量

  • 该类至少提供以下构造器和方法:
    public CustomerList ( int totalCustomer )
    public boolean addCustomer ( Customer customer )
    public boolean replaceCustomer ( int index , Customer customer )
    public boolean deleteCustomer ( int index )
    public Customer[] getAllCustomers( )
    public Customer getCustomer ( int index )
    public int getTotal ()

CustomerView类的设计

  • CustomerView 为主模块,负责菜单的显示和处理用户操作
    本类封装以下信息:
    CustomerList customerList = new CustomerList (10);
    创建最大包含10个客户对象的 CustomerList 对象,供以下各成员方法使用。
  • 该类至少提供以下方法:
    public void enterMainMenu()
    private void addNewCustomer ()
    private void modifyCustomer ()
    private void deleteCustomer ()
    private void listAllCustomers ()
    public static void main ( String[] args )

CustomerUtil类

这是一个工具类,将不同的功能封装为方法,就是可以直接通过调用方法使用他的功能,无需考虑功能的具体实现细节!

代码

Customer.java

package myProject;
/*
 * Customer 为实体对象,用来封装客户信息。
 */
public class Customer {
    private String name;  //客户姓名
    private char sex; //性别
    private int age; // 年龄
    private String phone; //电话号码
    private String email; //电子邮箱

    public Customer() {
    }

    public Customer(String name, char sex, int age, String phone, String email) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.phone = phone;
        this.email = email;
    }

    public String getName() {
        return name;
    }

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

    public char getSex() {
        return sex;
    }

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

    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.java

package myProject;
/*
 * CustomerList 为 Customer 对象的管理模块,
 * 内部用数组管理一组 Customer 对象,并提供相应的添加、修改、删除和遍历方法,
 * 供 CustomerView 调用。
 */
public class CustomerList {
    private Customer[] customers;  //用来保存客户对象的数组
    private int total = 0;  //记录已保存客户对象的数量

    // 用来初始化customer数组的构造器,totalCustomer指定数组长度
    public CustomerList(int totalCustomer){
        customers = new Customer[totalCustomer];

    }

    // 将指定的客户添加到数组中
    public boolean addCustomer(Customer customer){
        if (total >= customers.length){
            return false;
        }
        customers[total] = customer;
        total++;
        return true;
    }

    // 修改指定索引位置上的客户信息,customer为修改后的新对象
    public boolean replaceCustomer(int index,Customer customer){
        if(index<0 || index>=total){
            return false;
        }
        customers[index] = customer;
        return true;
    }

    // 删除指定索引位置上的客户
    public boolean deleteCustomer(int index){
        if(index<0 || index>=total){
            return false;
        }

        for(int i = index;i < total-1;i++){
            customers[i] = customers[i+1];
        }
        customers[total-1]=null;
        total--;
        return true;
    }

    // 获取所有客户信息
    public Customer[] getAllCustomers(){
        return this.customers;
    }

    // 按照索引查找客户
    public Customer getCustomer(int index){
        if(index<0 || index>=total){
            return null;
        }
        return this.customers[index];
    }

    // 获取客户的数量
    public int getTotal(){
        return this.total;
    }
}

CustomerView.java(项目入口文件)

package myProject;
/*
 * CustomerView 为主模块,负责菜单的显示和处理用户操作。
 */
public class CustomerView {
    private  CustomerList customerList = new CustomerList(10);

    // 显示客户信息管理界面
    public void enterMainMenu(){
        boolean flag = true;
        while (flag){
            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 menu = CustomerUtil.readMenuSelect();
            switch (menu){
                case '1': addNewCustomer(); break;
                case '2': modifyCustomer(); break;
                case '3': deleteCustomer(); break;
                case '4': listAllCustomers(); break;
                case '5':
                    System.out.println("确认是否退出(Y/N): ");
                    char isExit = CustomerUtil.readConfirmSelect();
                    if(isExit == 'Y'){
                        flag = false;
                    }
            }
        }
    }


    // 添加客户操作
    private void addNewCustomer(){
        System.out.println("------------------添加客户-----------------");
        System.out.print("姓名:");
        String name = CustomerUtil.readString(10);
        System.out.print("性别:");
        char sex = CustomerUtil.readChar();
        System.out.print("年龄:");
        int age = CustomerUtil.readInt();
        System.out.print("电话:");
        String phone = CustomerUtil.readString(13);
        System.out.print("邮箱:");
        String email = CustomerUtil.readString(30);
        // 将上述数据封装到对象
        Customer customer = new Customer(name,sex,age,phone,email);

       boolean isSuccess =  customerList.addCustomer(customer);
       if(!isSuccess){
           System.out.println("-----------------客户目录已满,添加失败!------------------");
       }else{
           System.out.println("-----------------添加完成------------------");
       }
    }


    // 修改客户信息操作
    private void modifyCustomer(){
        System.out.println("------------------修改客户-----------------");
        Customer cust;
        int number;
        while(true){
            System.out.println("请选择待修改客户编号(-1退出):");
            number = CustomerUtil.readInt();
            if (number == -1){
                return;
            }
            cust = customerList.getCustomer(number - 1);
            if(cust == null){
                System.out.println("无法找到指定客户!");
            }else{
                break;
            }
        }
        // 修改客户信息
        System.out.print("姓名(" + cust.getName() + "): ");
        String name = CustomerUtil.readString(10,cust.getName());
        System.out.print("性别(" + cust.getSex() + "): ");
        char sex = CustomerUtil.readChar(cust.getSex());
        System.out.print("年龄(" + cust.getAge() + "): ");
        int age = CustomerUtil.readInt(cust.getAge());
        System.out.print("电话(" + cust.getPhone() + "): ");
        String phone = CustomerUtil.readString(13,cust.getPhone());
        System.out.print("邮箱(" + cust.getEmail() + "): ");
        String email = CustomerUtil.readString(30,cust.getEmail());

        Customer newCust = new Customer(name, sex, age, phone, email);
        boolean isReplaced = customerList.replaceCustomer(number-1,newCust);
        if(isReplaced){
            System.out.println("------------------修改成功-----------------");
        }else{
            System.out.println("------------------修改失败!-----------------");
        }
    }

    // 删除客户操作
    private void deleteCustomer(){
        System.out.println("------------------删除客户-----------------");
        int number;
        while(true){
            System.out.println("请选择待修改客户编号(-1退出):");
            number = CustomerUtil.readInt();
            if (number == -1){
                return;
            }
            Customer cust = customerList.getCustomer(number - 1);
            if(cust == null){
                System.out.println("无法找到指定客户!");
            }else{
                break;
            }
        }

        System.out.println("确认是否删除(Y/N): ");
        char isDelete = CustomerUtil.readConfirmSelect();
        if(isDelete == 'Y'){
            boolean isSuccess = customerList.deleteCustomer(number-1);
            if(isSuccess){
                System.out.println("------------------删除成功-----------------");
            }else{
                System.out.println("------------------删除失败-----------------");
            }
        }
    }


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

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


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

    }
}

CustomerUtil.java

package myProject;

import java.util.Scanner;

/*
 * 将不同的功能封装为方法,就是可以直接通过调用方法使用他的功能,无需考虑功能的具体实现细节
 */
public class CustomerUtil {
    private static Scanner scanner = new Scanner(System.in);

    /*
    1、HasNext和HasNextLine会要求用户在控制台输入字符,然后回车,把字符存储到Scanner对象中,不会赋值到变量中,可以用于判断输入的字符是否符合规则要求。
    HasNext会以空格为结束标志,空格后的数字会抛弃掉。
    HasNextLine会以Enter为结束标志

    2、Next和NextLine是直接从Scanner中获取HasNext和HasNextLine存储起来的值给到变量中。如果前面没有HasNext或者HashNextLine获取值,也可以自己获取用户在控制台中输入的字符。

    */

    //让用户一直输入,直到用户输入符合条件的字符串才退出循环。
    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.println("输入长度(不能大于" + limit + ")错误,请重新输入:");
                continue;
            }
            break;
        }
        return line;
    }

    // 用于界面菜单的选择,该方法读取键盘
    public static char readMenuSelect(){
        char c;
        while(true){
            String str = readKeyBoard(1,false);
            c = str.charAt(0);
            if(c != '1' && c != '2' && c != '3' && c != '4' && c != '5'){
                System.out.println("选择错误,请重新输入:");
            }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;
        while(true){
            String str = readKeyBoard(2,false);
            try{
                n = Integer.parseInt(str);
                break;
            }catch (NumberFormatException e){
                System.out.println("数字输入错误,请重新输入:");
            }
        }
        return  n;
    }

    // 从键盘读取一个长度不过2的整数并返回,如果不输入字符直接回车,以defaultValue作为返回值
    public static int readInt(int defaultValue){
        int n;
        while(true){
            String str = readKeyBoard(2,true);
            if(str.equals("")){
                return defaultValue;
            }
            try{
                n = Integer.parseInt(str);
                break;
            }catch (NumberFormatException e){
                System.out.println("数字输入错,请重新输入:");
            }
        }
        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 readConfirmSelect(){
        char c;
        while(true){
            String str = readKeyBoard(1,false).toUpperCase();
            c = str.charAt(0);
            if(c == 'Y' || c == 'N'){
                break;
            }else{
                System.out.println("选择错误,请重新输入:");
            }
        }
        return c;
    }

}

运行截图

添加
在这里插入图片描述
查询
在这里插入图片描述

修改
在这里插入图片描述
删除
在这里插入图片描述

结语

如果你觉得博主写的还不错的话,可以关注一下当前专栏,博主会更完这个系列的哦!也欢迎订阅博主的其他好的专栏。

🏰系列专栏
👉软磨 css
👉硬泡 javascript
👉flask框架快速入门

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到