一、软件设计结构
- 对于初学者来说,弄清框架显得尤为重要
- 首先该软件有以下三种模块组成
二、MVC设计模式
模型层:Customer处理数据
控制层:CustomerList处理业务逻辑
视图层:CustomerView显示数据
以下三点建议结合代码理解
1.Customer为实体对象,用于封装客户信息
2.CustomerList为Customer对象的管理模块,内部用数组管理一组Customer对象,并提供相应的添加、修改、删除和遍历的方法,供CustomerView调用
3.CustomerView为主模块,负责菜单的显示和处理用户操作
四个类都在同一包下
三、Customer
package org.atjinzhao.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;
}
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;
}
}
四、CustomerList
package org.atjinzhao.customer;
public class CustomerList {
private Customer[] customers;//客户列表
private int total = 0;//记录已保存顾客数量
//构造器
public CustomerList(int totalCustomer) {
customers = new Customer[totalCustomer];
}
//方法
/**
* 添加客户
* return:true添加成功,false:添加失败
*/
public boolean addCustomer(Customer customer){
if (total < customers.length) {
customers[total] = customer;
total++;
return true;
}else return false;
}
/**
* 修改指定索引位置上的客户信息
* @param index
* @param cust
* @return true:修改成功 false修改失败
*/
public boolean replaceCustomer(int index,Customer cust){
if (index < 0 || index >= total) {
return false;
}else{
customers[index] = cust;
return true;
}
}
/**
* 删除指定索引位置上的客户
* @param index
* @return true删除成功 false删除失败
*/
public boolean deleteCustomer(int index){
if (index >= 0 && index < total) {
for (int i = index; i < total - 1; i++) {
customers[i] = customers[i+1];
}
customers[--total] = null;
return true;
}
return false;
}
/**
* 获取所有客户信息
* @return 数组
*/
public Customer[] getAllCustomers(){
//null的部分不返回
Customer[] custs = new Customer[total];
for (int i = 0; i < total; i++) {
custs[i] = customers[i];
}
return custs;
}
/**
* 获取指定索引位置上的客户
*/
public Customer getCustomer(int index){
if (index >= 0 && index < total) {
return customers[index];
}else return null;
}
/**
* 获取存储客户的数量
*/
public int getTotal() {
return total;
}
/**
* 获取最大能储存客户的数量
*/
public int getCustomer(){
return customers.length;
}
}
五、CustomerView
package org.atjinzhao.customer;
public class CustomerView {
private CustomerList customerList = new CustomerList(10);
public CustomerView() {
Customer cust = new Customer("李明",'男',19,"12349982563","lm@gmail.com");
customerList.addCustomer(cust);
}
public void enterMainMenu(){
//显示主页面
boolean isFlag = true;
while(isFlag){
System.out.println("-----------------客户信息管理软件-----------------");
System.out.println(" 1 添加客户");
System.out.println(" 2 修改客户 ");
System.out.println(" 3 删除客户 ");
System.out.println(" 4 客户列表 ");
System.out.println(" 5 退 出 ");
System.out.println(" 请选择(1-5): ");
char selection = CMUtility.readMenuSelection();
switch (selection) {
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;
}
}
}
}
/**
* 添加客户
*/
public void addNewCustomer(){
System.out.println("-----------------添加客户-----------------");
System.out.print("姓名:");
KX ТV&27FF2&VDFVfVfRТ[Ni{nZ^KyNh`ТfТ7G&7G"&VDW&"'VRТb7G"V""Т&WGW&VfVfSТG'ТvW"'6T7G"Т'&VТF6&W$fDW6WFТ77FV&.i[ZZ^IikZS"Т&WGW&ТKKK[h^NyNZzK.n[nX[nKKikk9^yNX ТV&27FF27G&&VE7G&BТ&WGW&VDW&BRТKKK[h^NyNZzK.n[nX[nKKikk9^yNX ТZh.iJh~KZ^ZzNhnk9^[nKVFVfVfV^KKX ТV&27FF27G&&VE7G&BG&FVfVfRТZy>YyK^*i{nZ^KyNh`Т7G&7G"&VDW&B'VRТ&WGW&G"V""b3c3FVfVfR7G#ТyJKNhyNZ^8.k9^K( ~hn( [n[nX[nKKikk9^yNX ТV&27FF26"&VD6&V7FТ~XNZPТ6"3ТfТ7G&7G"&VDW&RWW$66RТ27G"$BТb3ur3tТ'&VТVRТ77FV&.hIikZS"Т&WGW&Т Т&fFR7FF27G&&VDW&BWGW&Т7G&"#Тv66"4DТ66"DТbТb&WGW&&WGW&ТVR6Т ТbffwCBТ77FV&Z^[KJ~KB.ikZ^( Т6Т'&VТ&WGW&Т ТЧ&SУFcУjX[>KfZKZ~Kzn{;{~z[K{NZIyX[4fZ~Kzn{;{{J.zKK^XNih~zhn{~{XyNyX[>ih~z[ZJ~Z^YZIiJ |