项目二:客户信息管理软件
模拟实现一个基于文本界面的《客户信息管理软件》
进一步掌握编程技巧和调试技巧,熟悉面向对象编程
主要涉及以下知识点:
- 类结构的使用:属性、方法及构造器
- 对象的创建与使用
- 类的封装性
- 声明和使用数组
- 数组的插入、删除和替换
- 关键字的使用:this
该软件能够实现对客户对象的插入、修改和删除(用数组实现),并能够打印客户明细表。
项目采用分级菜单方式。主菜单如下:
/* -----------------客户信息管理软件----------------- 1 添 加 客 户 2 修 改 客 户 3 删 除 客 户 4 客 户 列 表 5 退 出 请选择(1-5): */
需求说明
每个客户的信息被保存在
Customer
对象中。以一个
Customer
类型的数组来记录当前所有的客户。每次“添加客户”(菜单1)后,客户
Customer
对象被添加到数组中。每次“修改客户”(菜单2)后,修改后的客户
Customer
对象替换数组中原对象。每次“删除客户”(菜单3)后,客户
Customer
对象被从数组中清除。执行“客户列表 ”(菜单4)时,将列出数组中所有客户的信息。
"添加客户"的界面及操作过程如下所示:
/* …… 请选择(1-5):1 ---------------------添加客户--------------------- 姓名:佟刚 性别:男 年龄:35 电话:010-56253825 邮箱:tongtong@atguigu.com ---------------------添加完成--------------------- */
"修改客户"的界面及操作过程如下所示:
/* …… 请选择(1-5):2 ---------------------修改客户--------------------- 请选择待修改客户编号(-1退出):1 姓名(佟刚):<直接回车表示不修改> 性别(男): 年龄(35): 电话(010-56253825): 邮箱(tongtong@atguigu.com):tongg@atguigu.com ---------------------修改完成--------------------- */
"删除客户"的界面及操作过程如下所示:
/* …… 请选择(1-5):3 ---------------------删除客户--------------------- 请选择待删除客户编号(-1退出):1 确认是否删除(Y/N):y ---------------------删除完成--------------------- */
"客户列表"的界面及操作过程如下所示:
/* …… 请选择(1-5):4 ---------------------------客户列表--------------------------- 编号 姓名 性别 年龄 电话 邮箱 1 佟刚 男 45 010-56253825 tong@abc.com 2 封捷 女 36 010-56253825 fengjie@ibm.com 3 雷丰阳 男 32 010-56253825 leify@163.com -------------------------客户列表完成------------------------- */
/*
项目包结构
> pojo
> customermanager
> bean
> Customer
> service
> CustomerList
> util
> CMUtility
> view
> CustomerView
*/
package com.lingchen.pojo.customermanager.bean;
/**
* @Author 凌宸
* @Date 2021/10/12
* @Version 1.0
*/
/**
* Customer为实体类,用来封装客户信息
* 该类封装客户的以下信息:
* String name :客户姓名
* char gender :性别
* int age :年龄
* String phone:电话号码
* String email :电子邮箱
* 提供各属性的get/set方法
* 提供所需的构造器(可自行确定)
*/
public class Customer {
String name; // 客户姓名
char gender; // 性别
int age; // 年龄
String phone; // 电话号码
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;
}
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;
}
}
package com.lingchen.pojo.customermanager.service;
/**
* @Author 凌宸
* @Date 2021/10/12
* @Version 1.0
*/
import com.lingchen.pojo.customermanager.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 {
Customer[] customers; // 用来保存客户对象的数组
int total = 0; // 记录已保存客户对象的数量
/**
* @Description 构造器,用来初始化customers数组
* @param totalCustomer:指定customers数组的最大空间
*/
public CustomerList(int totalCustomer){
customers = new Customer[totalCustomer];
}
/** @Description 将参数customer添加到数组中最后一个客户对象记录之后
* @param customer 指定要添加的客户对象
* @return 添加成功返回true;false表示数组已满,无法添加
*/
public boolean addCustomer(Customer customer){
if(total > customers.length){
return false;
}
customers[total ++] = customer;
return true;
}
/**
* @Description 用参数customer替换数组中由index指定的对象
* @param index index指定所替换对象在数组中的位置,从0开始
* @param customer customer指定替换的新客户对象
* @return 替换成功返回true;false表示索引无效,无法替换
*/
public boolean replaceCustomer(int index, Customer customer){
if(index < 0 || index >= total){
return false;
}
customers[index] = customer;
return true;
}
/**
* @Description 从数组中删除参数index指定索引位置的客户对象记录
* @param index index指定所删除对象在数组中的索引位置,从0开始