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++