Java小项目—客户信息管理软件(一)

实现目标


1. 模拟实现一个基于文本界面的《客户信息管理软件》

2. 进一步掌握编程技巧和调试技巧,熟悉面向对象编程 ,主要涉及以下知识点:

类结构的使用

属性、方法及构造器 对象的创建与使用

类的封装性

声明和使用数组

数组的插入、删除和替换

关键字的使用:this

需求说明


1. 该软件能够实现对客户对象的插入、修改和删除(用数组实现),并能够打印客户明细表。

项目采用分级菜单方式,主菜单如下:

2. 每个客户的信息被保存在Customer对象中,以一个Customer类型的数组来记录当前所有的客户。

1.每次"添加客户"(菜单1)后,客户(Customer)对象被添加到数组中。
界面及操作过程如下所示:
请选择(1-5):1
---------------------添加客户---------------------
姓名:佟刚
性别:男
年龄:35
电话:010-56253825
邮箱:tongtong@atguigu.com
---------------------添加完成---------------------

2.每次"修改客户"(菜单2)后,修改后的客户(Customer)对象替换数组中原对象。
界面及操作过程如下所示:
请选择(1-5):2
---------------------修改客户---------------------
请选择待修改客户编号(-1退出):1
姓名(佟刚):<直接回车表示不修改>
性别(男):
年龄(35):
电话(010-56253825):
邮箱(tongtong@atguigu.com):tongg@atguigu.com
---------------------修改完成---------------------

3.每次"删除客户"(菜单3)后,客户(Customer)对象被从数组中清除。
界面及操作过程如下所示:
请选择(1-5):3
---------------------删除客户---------------------
请选择待删除客户编号(-1退出):1
确认是否删除(Y/N):y
---------------------删除完成---------------------

4.执行"客户列表"(菜单4)时,将列出数组中所有客户的信息。
界面及操作过程如下所示:
请选择(1-5):4
---------------------------客户列表---------------------------
编号  姓名       性别    年龄   电话              邮箱
 1    佟刚       男      45    010-56253825     tong@abc.com
 2    封捷       女      36    010-56253825     fengjie@ibm.com
 3    雷丰阳     男      32    010-56253825     leify@163.com
-------------------------客户列表完成-------------------------

软件设计结构


该软件由以下三个模块组成:

CustomerView为主模块,负责菜单的显示和处理用户操作。

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

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

软件设计


工具类的设计

概述:将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。

工具类中的方法不是本程序的重点,只对程序起到辅助的作用,在此就不作详细介绍了,需要的小伙伴可以点击链接下载使用。

代码链接:工具类、Customer类的代码(为客户信息管理软件项目提供的代码参考)_South.return的博客-优快云博客 

Customer类的设计

Customer为实体顾客类,用来封装客户信息  该类封装客户的以下信息:

String name:客户姓名
char gender:性别
int age:年龄
String phone:电话号码
String email:电子邮箱
提供各属性的get/set方法
提供所需的构造器(可自行确定)

Customer类的设计比较简单,在此也不做详细介绍了,想要查看的代码的小伙伴点击以下链接即可。

代码链接:工具类、Customer类的代码(为客户信息管理软件项目提供的代码参考)_South.return的博客-优快云博客 

CustomerList类的设计

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

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


该类至少提供以下构造器和方法:

用途:构造器,用来初始化customers数组
参数:totalCustomer:指定customers数组的最大空间
public CustomerList(int totalCustomer) 

用途:将参数customer添加到数组中最后一个客户对象记录之后
参数:customer指定要添加的客户对象 
返回:添加成功返回true;false表示数组已满,无法添加
public boolean addCustomer(Customer customer) 

用途:用参数customer替换数组中由index指定的对象
参数:customer指定替换的新客户对象,index指定所替换对象在数组中的位置,从0开始
返回:替换成功返回true;false表示索引无效,无法替换
public boolean replaceCustomer(int index, Customer cust)

用途:从数组中删除参数index指定索引位置的客户对象记录
参数:index指定所删除对象在数组中的索引位置,从0开始
返回:删除成功返回true;false表示索引无效,无法删除
public boolean deleteCustomer(int index)

用途:返回数组中记录的所有客户对象
返回:Customer[] 数组中包含了当前所有客户对象,该数组长度与对象个数相同。
public Customer[] getAllCustomers() 

用途:返回参数index指定索引位置的客户对象记录
参数:index指定所要获取的客户在数组中的索引位置,从0开始
返回:封装了客户信息的Customer对象
public Customer getCustomer(int index) 

用途:返回Customer对象的个数
public int getTotal()

代码实现:

//创建一个CustomerList类实现用于对Customer顾客类的对象进行增删改查操作
public class CustomerList {
    //声明Customer类型数组customers和记录存储的客户的个数的变量
    public Customer[] customers;
    public int total=0;

    //定义无参和有参构造器
    public CustomerList(){ }
    public CustomerList(int totalCustomer){
        this.customers = new Customer[totalCustomer];
    }

    //添加
    public boolean addCustomer(Customer customer){
        if(customers.length==total){
            return false;
        }else {
            customers[total]=customer;
            total++;
            return true;
        }
    }

    //替换
    public boolean replaceCustomer(int index, Customer cust){
        if(index<0 || index>this.total){
            return false;
        }else{
            customers[index]=cust;
            return true;
        }
    }

    //删除
    public boolean deleteCustomer(int index){
        if (index<0 || index>this.total) {
            return false;
        }
        //index++;
        for (int i=0; i<this.total; i++) {
            if (i >= index) {
                customers[i] = customers[i+1];  //往前移
            }
        }
        this.total--;
        return true;
    }

    //返回数组对象
    public Customer[] getAllCustomers(){
        return customers;
    }

    //查询
    public Customer getCustomer(int index){
        if(customers[index]==null){
            return null;
        }else{
            return customers[index];
        }
    }

    //Customer对象的个数
    public int getTotal(){
        return total;
    }
}


        本期的项目实现就介绍到这里,还有一个CustomerView类的设计会在下一篇博客里详细介绍,是本项目的重点,感兴趣的小伙伴可不要错过了,以上代码如有任何问题请联系小编,感谢你的阅读,咱们下篇博客见~

评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

South.return

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值