CustomerView类的实现:
public class CustomerView {
public static void main(String[] args) {
CustomerView custView = new CustomerView();
custView.enterMenu();
}
//属性
private CustomerList customerList = new CustomerList(10);//实例化一个CustomerList对象,数组长度为10
//数组长度是CustomerList的属性customers
//构造器
public CustomerView() {
//在实例化时初始化一个Customer对象添加到CustomerList(customerList)中
Customer cust = new Customer("Jony", '男', 18, "18238899465", "Google@163.com");
customerList.addCustomer(cust);
}
//方法
/**
* 程序界面进入方法
*/
public void enterMenu() {
boolean isFlag=true;
while(isFlag) {
System.out.println("\n****************客户信息管理软件****************");
System.out.println(" 1 添加客户");
System.out.println(" 2 修改客户");
System.out.println(" 3 删除客户");
System.out.println(" 4 客户列表");
System.out.println(" 5 退 出\n");
System.out.println(" 请选择:(1-5)");
char menu = CMUtility.readMenuSelection();
switch(menu) {
case '1':
addNewCustomer();
break;
case '2':
modifyCustomer();
break;
case '3':
deleteCustomer();
break;
case '4':
listAllCustomer();
break;
case '5':
System.out.println("是否确实退出(Y/N):");
char isExit = CMUtility.readConfirmSelection();
if(isExit == 'Y') {
System.out.println("欢迎使用客户信息管理软件!");
isFlag = false;
}
}
// isFlag=false;
}
}
/**
* 添加客户
*/
public void addNewCustomer() {
System.out.println("****************************添加客户****************************");
System.out.print("姓名:");
String name = CMUtility.readString(10);
System.out.print("性别:");
char gender = CMUtility.readChar();
System.out.print("年龄:");
int age = CMUtility.readInt();
System.out.print("电话:");
String phone = CMUtility.readString(13);
System.out.print("邮箱:");
String email = CMUtility.readString(20);
//将添加的信息实例化给对象cust
Customer cust = new Customer(name, gender, age, phone, email);
//调用CustomerList的addCustomer()方法
customerList.addCustomer(cust);
}
/**
* 修改用户
*/
public void modifyCustomer() {
System.out.println("****************************修改客户****************************");
Customer cust;
int number;
//如果选择的数字不正确实现重新选择
for(;;) {
System.out.print("请选择修改客户编号(-1退出):");
number = CMUtility.readInt();
if (number == -1) {
return;//退出该功能
}
//得到该索引上的客户对象
cust = customerList.getCustomer(number - 1);
if(cust == null) {
return;
}else {
break;//退出循环执行修改代码
}
}
//修改客户信息
System.out.print("姓名(" + cust.getName() + "):");
String name = CMUtility.readString(10, cust.getName());
System.out.print("姓别(" + cust.getGender() + "):");
char gender = CMUtility.readChar(cust.getGender());
System.out.print("年龄(" + cust.getAge() + "):");
int age = CMUtility.readInt(cust.getAge());
System.out.print("电话(" + cust.getPhone() + "):");
String phone = CMUtility.readString(13, cust.getName());
System.out.print("邮箱(" + cust.getEmail() + "):");
String email = CMUtility.readString(30,cust.getEmail());
//将修改的信息实例化给一个新的Customer对象
Customer newCust = new Customer(name, gender, age, phone, email);
//调用CustomerList的replaceCustomer()方法
boolean isModify = customerList.replaceCustomer(number - 1, newCust);
if(isModify) {
System.out.println("********************************************修改成功********************************************");
}else {
System.out.println("********************************************修改失败********************************************");
}
}
/**
* 删除用户
*/
public void deleteCustomer() {
System.out.println("********************************************删除客户********************************************");
int number;
for(;;) {
System.out.print("选择待删除客户的编号(-1退出):");
number = CMUtility.readInt();
if(number == -1) {
return;
}
//将选择的数组索引首地址赋值给cust,判断是否为null
Customer cust = customerList.getCustomer(number - 1);
if(cust == null) {
System.out.println("无法找到该用户!");
}else {
// customerList.deletCustomer(number - 1);
break;
}
}
System.out.println("是否确认删除(Y/N):");
char isDelete = CMUtility.readConfirmSelection();
if (isDelete == 'Y') {
//调用CustomerList的deletCustomer()方法
customerList.deletCustomer(number - 1);
}
}
/**
* 客户列表
*/
public void listAllCustomer() {
System.out.println("*************************客户列表********************************************");
//调用CustomerList的getTotal()方法查看是否客户数量为零
int total = customerList.getTotal();
if(total == 0) {
System.out.println("没有客户信息");
}else {
System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
//调用CustomerList的getAllCustomers()方法,赋值非新的数组custs,为了输出的时候可以减少输入长度
Customer[] custs = customerList.getAllCustomers();
for(int i = 0;i < custs.length;i++) {
Customer cust = custs[i];
System.out.println((i+1)+"\t"+ cust.getName()+"\t"+cust.getGender()+"\t"+cust.getAge()
+"\t"+cust.getPhone()+"\t"+cust.getEmail());
}
}
System.out.println("***********************客户列表完成********************************************");
}
}
该客户信息管理软件实现完成。