当前位置 博文首页 > aaa_hao的博客:Java项目二(案例):客户信息管理软件

    aaa_hao的博客:Java项目二(案例):客户信息管理软件

    作者:[db:作者] 时间:2021-08-24 16:14

    Java项目二(案例):客户信息管理软件

    项目概述

    软件功能

    记录客户的个人信息,并能够打印客户列表。

    软件说明

    每个客户的信息被保存在Customer对象中。
    以一个Customer类型的数组来记录当前所有的客户。
    每次“添加客户”(菜单1)后,客户(Customer)对象被添加到数组中。
    每次“修改客户”(菜单2)后,修改后的客户(Customer)对象替换数组中原对象。
    每次“删除客户”(菜单3)后,客户(Customer)对象被从数组中清除。
    执行“客户列表 ”(菜单4)时,将列出数组中所有客户的信息。

    涉及Java知识点

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

    程序代码示例

    程序共有四个类文件,分别保存在四个包下,分别是:
    com.kaho.java.bean ----- Customer.java
    com.kaho.java.service ----- CustomerList.java
    com.kaho.java.util ----- CMUtility.java
    com.kaho.java.ui ----- CustomerView

    详情请看代码注释

    Customer类

    package com.kaho.java.bean;
    /**
     Customer为实体类,用来封装客户信息
     该类封装客户的以下信息:
    String name :客户姓名
    char gender :性别
    int age :年龄
    String phone:电话号码
    String email :电子邮箱
     提供各属性的get/set方法
     提供所需的构造器(可自行确定)
    
     这是一个JavaBean类型文件
     这个类中定义的方法用于对具体的Customer对象内部的个人信息(属性)进行操作
     */
    public class Customer {
        //属性
        private String name;   //客户姓名
        private char gender;   //性别
        private int age;       //年龄
        private String phone;  //电话号码
        private String email;  //电子邮箱
    
        //构造器
        public Customer(){
    
        }
        public Customer(String name,char gender,int age,String phone,String email){
            this.name = name;
            this.gender = gender;
            this.age = age;
            this.phone = phone;
            this.email = email;
        }
    
        //getter、setter方法
        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;
        }
    
        //用于返回客户所有个人信息的方法
        public String getDetails(){
            return name + "\t\t" + gender + "\t\t" + age + "\t\t" + phone + "\t\t" + email;
        }
    
    }
    
    

    CustomerList类

    package com.kaho.java.service;
    
    import com.kaho.java.bean.Customer;  //该类用到了bean包下的Customer类
    
    /**
    CustomerList为Customer对象的管理模块,内部使用一维数组管理一组Customer对象
    本类封装以下信息:
    Customer[] customers:用来保存客户对象的数组
    int total = 0 :记录已保存客户对象的数量
    该类至少提供以下构造器和方法:
    public CustomerList(int totalCustomer)
    public boolean addCustomer(Customer customer)
    public boolean replaceCustomer(int index, Customer cust)
    public boolean deleteCustomer(int index)
    public Customer[] getAllCustomers()
    public Customer getCustomer(int index)
    public int getTotal()
    
     这个类中定义的各个方法用于对客户对象整体进行操作而不对具体对象的内部细节(即个人信息属性)进行操作
    
     */
    public class CustomerList {
        private Customer[] customers;     //声明一个Customer类型的customers数组(未初始化)来保存多个客户"对象"
        private int total = 0;            //用来记录已保存客户对象的数量
    
        /**
        用途:构造器,用来初始化customers数组
        参数:totalCustomer:指定customers数组的最大空间
         */
        public CustomerList(int totalCustomer){
            customers = new Customer[totalCustomer];      //初始化数组,预先设定数组的总长度
        }
    
        //方法(增、删、改、查等)
        /**
        用途:将参数customer添加到数组中最后一个客户对象记录之后
        参数:customer指定要添加的客户对象
        返回:添加成功返回true;false表示数组已满,无法添加
         */
        public boolean addCustomer(Customer customer){
            if (total >= customers.length) return false;
            //total初始值为0,而后每添加一个客户对象会对total进行一次自增
            customers[total] = customer;
            total++;
            return true;
        }
    
        /**
        用途:用参数cust(Customer类型)替换数组中由索引index指定的对象
        参数:cust指定替换的新客户对象
    		 index指定所替换对象在数组中的位置,从0开始
        返回:替换成功返回true;false表示索引无效,无法替换
         */
        public boolean replaceCustomer(int index, Customer cust){
            if (index > total - 1 || index < 0) return false;
            customers[index] = cust;  //将customers数组在索引index的元素赋为一个新的客户对象cust
            return true;
        }
    
        /**
        用途:从数组中删除参数index指定索引位置的客户对象记录
        参数: index指定所删除对象在数组中的索引位置,从0开始
        返回:删除成功返回true;false表示索引无效,无法删除
         */
        public boolean deleteCustomer(int index){
            if (index > total - 1 || index < 0) return false;
            for (int i = index;i < total - 1;i++){
                customers[i] = customers[i + 1];    //类似于顺序表的删除,从前往后,将后一位数组元素赋给前一位
            }
            customers[total - 1] = null; //将新数组已赋值部分(0 ~ total-1)的最后一个多出来的重复元素抹去,设置为空指针(引用型数据类型)
            total--;        //删除一个客户对象,数组已赋值部分长度减一
            return true;
        }
    
        /**
        用途:返回数组中记录的所有客户对象
        返回: Customer[] 数组中包含了当前所有客户对象,该数组长度与对象个数相同。
         注意:不能直接return customers,因为实际上customers数组的长度是初始化时的totalCustomer(即 10),而非已赋了值的前 total个
         */
        public Customer[] getAllCustomers(){
            Customer[] cust = new Customer[total];
            for (int i = 0;i < total;i++){
                cust[i] = customers[i];     //这里是将对象的地址赋给新数组,两个数组保存的都是这几个已记录的客户对象
            }
            return cust;
        }
    
        /**
        用途:返回参数index指定索引位置的客户对象记录
        参数: index指定所要获取的客户在数组中的索引位置,从0开始
        返回:封装了客户信息的Customer对象,索引不合法则返回null
         */
        public Customer getCustomer(int index) {
            if (index < 0 || index >= total) return null;
            return customers[index];
        }
    
        /**
        用途:获取当前已记录的客户数量
        返回:当前customers[]数组的长度
         */
        public int getTotal(){
            return total;
        }
    }
    
    

    Utility类

    package com.kaho.java.util;
    
    import java.util.*;   //导入util包
    
    /**
     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);       //charAt()返回字符串中指定索引的字符,这里读取键盘输入的一串字符串中的第一个字符
                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);          //charAt()返回字符串中指定索引的字符
        }
    
        /**
         从键盘读取一个字符,并将其作为方法的返回值。
         如果用户不输入字符而直接回车,方法将以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