Customer类的代码
package com.pro01.Customer;
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;
}
//有参构造
//初始化会有name,gender,age,phone,email属性赋值。
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 Customer() {
}
}
Customer类设置为属性供CustomerList方法调用
CustomerList类的代码
package com.pro01.CustomerList;
import com.pro01.Customer.Customer;
public class CustomerList {
private Customer[] customers;// 用来保存对象的数组
private int total = 0;// 用来记录客户的个数
/**
* 传进一个有参构造来new一个Customer[]的对象,也就是当在CustomerView里面new
* CustomerList 通过CustomerList 的构造器new Customer[] 并传进去所存客户的人数
* @param
* @return
*/
public CustomerList(int totalCustomer) {
customers = new Customer[totalCustomer];
}
/**
* 添加客户
* 写一个有返回一个布尔类型的方法,并带有一个形参Customer customer,这是当在
* CustomerView中new Customer的时候将当时的对象传给addCustomer方法中的形参
*
* @param customer
* @return true 添加成功 ; false 添加失败
*/
public boolean addCustomer(Customer customer) {
//如果所存客户中的数大于customers.length说明客户端所存客户已满则无法继续添加则返
//一个false
if (total > customers.length) {
return false;
}
//如果第一个判断没进去说明客户端所存客户未满则可以继续添加客户信息
//那么就将所传的Customer参数赋给customers[total],并且人数增加则total需自加1。
//注:后++
//添加成功后返回true
customers[total++] = customer;
return true;
}
/**
* 修改客户
* 写一个有返回一个布尔类型的方法,并带有一个形参Customer cust和一个索引的index、
*
* @param index
* @param customer
* @return true修改成功 ; false 修改失败
*/
public boolean repalceCustomer(int index, Customer cust) {
//判断所传索引是否存在
if (index < 0 || index >= total) {
return false;
}
//如果存在则将新修改的参数cust赋给customers[index]返回true
customers[index] = cust;
return true;
}
/**
* 删除客户
* 写一个有返回一个布尔类型的方法,并带有一个索引的index、
* @param index
* @return true删除成功 ; false 删除失败
*/
public boolean deleteCustomer(int index) {
//判断所传索引是否存在
if (index < 0 || index >= total) {
return false;
}
//如果存在则进行删除,删除则是用你所想删的位置的索引,用他的下一个索引的值替换掉
//前一个索引的值然后让最后一个索引所在的位置设为null。而total为人数删除则需要--
for (int i = index; i < total - 1; i++) {
customers[i] = customers[i + 1];
}
// customers[total-1] = null;
// total--;
customers[--total] = null;
return true;
}
/**
* 获取所有客户信息
*
* @return
*/
public Customer[] getAllCustomers() {
//创建了一个新的数组用遍历将已存的数组赋值过来
Customer[] cust = new Customer[total];
for (int i = 0; i < total; i++) {
cust[i] = customers[i];
}
//则返回一个新的数组cust。
return cust;
}
/**
* 获取索引位置上的客户
*
* @param index
* @return 如果不符合就返回null
*/
public Customer getCustomer(int index) {
//判断所传索引是否存在
if (index < 0 || index >= total) {
return null;
}
//如果存在就返回一个Customer的customers[index]里面的地址
return customers[index];
}
/**
* 获取客户的数量
* @return
*/
public int getTotal() {
return total;
}
}