本周根据之前学习的一些知识,仿照视频中的内容,完成了一个客户信息管理系统。(未连接数据库,只支持单次使用)
1 功能展示
1.1 添加用户与客户列表
1.2 修改用户
1.3 删除用户
1.4 退出
2 类的设计
项目中主要设计三个类。
Customer为客户的实体,用来封装客户的信息。
CustomerList为Customer对象的管理类,内部是通过一个对象数组对其中的Customer对象进行管理。
CustomerView负责视图部分,调用CustomerList来进行增删改查的操作。
CMUtility为工具类,主要负责读取一些信息。
3 源码
Customer.java:
package com.wuzec.bean;
/**
* Customer为实体对象,用来封装客户信息
* @author wuzec
*
*/
public class Customer {
private String name; //姓名
private char gender; //性别
private int age; //年龄
private String phone; //电话号码
private String email; //电子邮箱
//get、set方法
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 Customer() {
}
public Customer(String name, char gender, int age, String phone, String email) {
super();
this.name = name;
this.gender = gender;
this.age = age;
this.phone = phone;
this.email = email;
}
}
CustomerList.java:
package com.wuzec.service;
import com.wuzec.bean.Customer;
/**
* CustomerList为对象Customer对象的管理模块
* 内部用数组管理一组Customer对象,并提供相应的增删改、遍历方法
* 供CustomerView调用
* @author wuzec
*
*/
public class CustomerList {
private Customer[] customers; //用来保存客户对象数组
private int total = 0; //记录已经保存客户对象的数量
/**
* 用来初始化customers数组的构造器
* @param totalCustomer:指定数组的长度
*/
public CustomerList(int totalCustomer) {
customers = new Customer[totalCustomer];
}
/**
* 将指定客户添加到数组中
* @param customer
* @return true:添加成功 false:添加失败
*/
public boolean addCustomer(Customer customer) {
if(total > customers.length) {
return false;
}
//customers[total] = customer;
//total++;
//或者
customers[total++] = customer;
return true;
}
/**
* 修改指定位置的索引信息
* @param index
* @param cust
* @return true:修改成功 false:修改失败
*/
public boolean replaceCustomer(int index, Customer cust) {
if(index < 0 || index >= total) {
return false;
}
customers[index] = cust;
return true;
}
/**
* 删除指定位置上的用户
* @param index
* @return
*/
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];