主模块CustomerView的详细代码
package myProject02.ui;
import myProject02.service.CustomerList;
import myProject02.util.CMUtility;
import myProject02.bean.*;
/**
*
* @Description 主模块,负责菜单的显示和处理用户操作
* @author ZTop Email:ztop.zhou@foxmail.com
* @version
* @data 2021年6月17日上午10:23:45
*
*/
public class CustomerView {
private CustomerList customerList = new CustomerList(10);
/**
* 初始化第一个用户信息
*/
public CustomerView(){
Customer initialCust = new Customer("张三", '男', 30, "18012341234", "163@163");
customerList.addCustomer(initialCust);
}
/**
* 显示主菜单,响应用户输入,根据用户操作分别调用其他相应的成员方法
* @Description
*/
public void enterMainMenu(){
boolean isFlag = true;
while(isFlag){
System.out.println("\n-----------------客户信息管理软件-----------------\n");
System.out.println(" 1 添 加 客 户");
System.out.println(" 2 修 改 客 户");
System.out.println(" 3 删 除 客 户");
System.out.println(" 4 客 户 列 表");
System.out.println(" 5 退 出\n");
System.out.print(" 请选择(1-5):");
char menu = CMUtility.readMenuSelection();
switch(menu){
case '1':
addNewCustomer();
break;
case '2':
modifyCustomer();
break;
case '3':
deleteCustomer();
break;
case '4':
listAllCustomers();
break;
case '5':
System.out.print("确认是否退出(Y/N):");
char isExit = CMUtility.readConfirmSelection();
if(isExit == 'Y'){
isFlag = false;
}
}
}
}
/**
* 添加客户
* @Description
*/
private void addNewCustomer(){
System.out.println("---------------------添加客户---------------------");
System.out.print("姓名:");
String name = CMUtility.readString(50);
System.out.print("性别:");
char gender = CMUtility.readChar();
System.out.print("年龄:");
int age = CMUtility.readInt();
System.out.print("电话:");
String phone = CMUtility.readString(50);
System.out.print("邮箱:");
String email = CMUtility.readString(50);
//添加新用户
Customer cust = new Customer(name, gender, age, phone, email);
boolean flag = customerList.addCustomer(cust);
if(flag)
System.out.println("---------------------添加完成---------------------");
else
System.out.println("---------------------添加失败---------------------");
}
/**
* 修改客户
* @Description
*/
private void modifyCustomer(){
System.out.println("---------------------修改客户---------------------");
int total = customerList.getTotal();
// 若客户列表为空的情况
if(total == 0){
System.out.println("客户列表为空!请添加客户!");
return;
}
// 若客户列表不为空的情况
System.out.print("请选择待修改客户编号(-1退出):");
for(;;){
int modifyIndex = CMUtility.readInt();
if(modifyIndex == -1)
return;
else{
//判断输入是否在合理范围,如果不是就重新输入
if(modifyIndex <= 0 || modifyIndex > total){
System.out.println("无法找到指定客户!");
System.out.print("请选择待修改客户编号(-1退出):");
continue;
}
// 加入需要修改的信息
Customer cust = customerList.getCustomer(modifyIndex - 1);//减1操作
System.out.print("姓名(" + cust.getName() + "):");
String custModifyName = CMUtility.readString(10, cust.getName());
System.out.print("性别(" + cust.getGender() + "):");
char custModifyGender = CMUtility.readChar(cust.getGender());
System.out.print("年龄(" + cust.getAge() + "):");
int custModifyAge = CMUtility.readInt(cust.getAge());
System.out.print("电话(" + cust.getPhone() + "):");
String custModifyPhone = CMUtility.readString(20, cust.getPhone());
System.out.print("邮箱(" + cust.getName() + "):");
String custModifyEmail = CMUtility.readString(50, cust.getEmail());
Customer custModify = new Customer(custModifyName, custModifyGender, custModifyAge, custModifyPhone, custModifyEmail);
customerList.replaceCustomer(modifyIndex - 1, custModify);//不要忘了减1操作
System.out.println("---------------------修改完成---------------------");
return;
}
// System.out.println("修改客户");
}
}
/**
* 删除客户
* @Description
*/
private void deleteCustomer(){
System.out.println("---------------------删除客户---------------------");
int total = customerList.getTotal();
// 若客户列表为空的情况
if(total == 0){
System.out.println("客户列表为空!请添加客户!");
return;
}
// 若客户列表不为空的情况
System.out.print("请选择待删除客户编号(-1退出):");
int isDelete = CMUtility.readInt();
if(isDelete == -1)
return;
else{
for(;;){
boolean flag = customerList.deleteCustomer(isDelete - 1);
if(flag == false){
System.out.println("无法找到指定客户!");
System.out.print("请选择待修改客户编号(-1退出):");
isDelete = CMUtility.readInt();
continue;
}
//确认是否删除
System.out.print("确认是否删除(Y/N):");
for(;;){
char isDeleteTrue = CMUtility.readChar();
if(isDeleteTrue == 'N' || isDeleteTrue == 'n')
return;//直接推出到主界面
else if(isDeleteTrue == 'Y' || isDeleteTrue == 'y'){
customerList.deleteCustomer(isDelete - 1);
System.out.println("---------------------删除成功---------------------");
return;
}
else
System.out.print("请按要求重新输入:");
}
}
}
}
/**
* 展示客户列表
* @Description
*/
private void listAllCustomers(){
System.out.println("---------------------------客户列表---------------------------");
System.out.println("编号\t姓名\t性别\t年龄\t\t电话\t\t邮箱");
int total = customerList.getTotal();
if(total == 0)
System.out.println("没有客户信息!");
for(int i = 1; i <= total; i++){
System.out.println( i + "\t"
+ customerList.getCustomer(i-1).getName() + '\t'
+ customerList.getCustomer(i-1).getGender() + '\t'
+ customerList.getCustomer(i-1).getAge() + '\t'
+ customerList.getCustomer(i-1).getPhone() + '\t' + '\t'
+ customerList.getCustomer(i-1).getEmail());
}
System.out.println("-------------------------客户列表完成-------------------------");
return;
}
public static void main(String[] srgs){
CustomerView view = new CustomerView();
view.enterMainMenu();
}
}