文章目录
- 前言
- 一、主菜单
- 二、步骤
-
- 1.Customer类
- 2.CustomerList类
-
- (1)public CustomerList(int totalCustomer) {}
- (2)public boolean addCustomer(Customer customer) {}
- (3)public boolean replaceCustomer(int index, Customer cust){}
- (4)public boolean deleteCustomer(int index){}
- (5)public Customer[] getAllCustomers() {}
- (6)public Customer getCustomer(int index) {}
- (7)public int getTotal(){}
- 3.CustomerView类
前言
该软件能够实现对客户对象的插入、修改和删除(用数组实现),并能够打印客户明细表。该程序也是写出了ArrayList里面add、remove、set、get等方法的部分实现。
提示:以下是本篇文章正文内容,下面案例可供参考
一、主菜单
-----------------客户信息管理软件-----------------
1 添 加 客 户
2 修 改 客 户
3 删 除 客 户
4 客 户 列 表
5 退 出
请选择(1-5):_
二、步骤
1.Customer类
Customer为实体类,用来封装客户信息
该类封装客户的以下信息:
String name :客户姓名
char gender :性别
int age :年龄
String phone:电话号码
String email :电子邮箱
提供各属性的get/set方法
提供所需的构造器
2.CustomerList类
CustomerList为Customer对象的管理模块,內部使用数组管理一组Customer对象
本类封装以下信息:
Customer[] customers:用来保存客户对象的数组
int total = 0 :记录已保存客户对象的数量
该类至少提供以下构造器和方法:
(1)public CustomerList(int totalCustomer) {}
用途:构造器,用来初始化customers数组
参数:totalCustomer:指定customers数组的最大空间
(2)public boolean addCustomer(Customer customer) {}
用途:将参数customer添加到数组中最后一个客户对象记录之后
参数:customer指定要添加的客户对象
返回:添加成功返回true;false表示数组已满,无法添加
(3)public boolean replaceCustomer(int index, Customer cust){}
用途:用参数customer替换数组中由index指定的对象
参数:customer指定替换的新客户对象 ,index指定所替换对象在数组中的位置,从0开始
返回:替换成功返回true;false表示索引无效,无法替换
(4)public boolean deleteCustomer(int index){}
用途:从数组中删除参数index指定索引位置的客户对象记录
参数: index指定所删除对象在数组中的索引位置,从0开始
返回:删除成功返回true;false表示索引无效,无法删除
(5)public Customer[] getAllCustomers() {}
用途:返回数组中记录的所有客户对象
返回: Customer[] 数组中包含了当前所有客户对象,该数组长度与对象个数相同。
(6)public Customer getCustomer(int index) {}
用途:返回参数index指定索引位置的客户对象记录
参数: index指定所要获取的客户在数组中的索引位置,从0开始
返回:封装了客户信息的Customer对象
(7)public int getTotal(){}
用途:获取已保存客户对象的数量
代码如下(示例):
package day1;
public class CustomerList{
private Customer[] customers;
private int total=0;
public CustomerList(){
}
public CustomerList(int totalCustomer){
customers = new Customer[totalCustomer];
}
public Customer[] getCustomers() {
return customers;
}
public void setCustomers(Customer[] customers) {
this.customers = customers;
}
public void setTotal(int total) {
this.total = total;
}
public boolean addCustomer(Customer customer){
customers[total]=customer;
if (total<customers.length){
total=total+1;
return true;
}else {
return false;
}
}
public boolean replaceCustomer(int index, Customer cust){
if (index<0||index>=total) return false;
customers[index]=cust;
return true;
}
public boolean deleteCustomer(int index){
if (index<0||index>=total) return false;
customers[index-1] = null;
for (int i = index-1; i < total-1; i++) {
customers[i] = customers[i + 1];
}
total=total-1;
return true;
}
public Customer[] getAllCustomers(){
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 null;
return customers[index];
}
public int getTotal(){
return total;
}
}
3.CustomerView类
(1)CustomerView为主模块
负责菜单的显示和处理用户操作
(2)CustomerList customerList = new CustomerList(10)
创建最大包含10个客户对象的CustomerList 对象,供以下各成员方法使用。
(3)public void enterMainMenu()
用途:显示主菜单,响应用户输入,根据用户操作分别调用其他相应的成员方法(如addNewCustomer),以完成客户信息处理。
(3)private void addNewCustomer() 、private void modifyCustomer()、private void deleteCustomer()、private void listAllCustomers()
用途:这四个方法分别完成“添加客户”、“修改客户”、“删除客户”和“客户列表”等各菜单功能。这四个方法仅供enterMainMenu()方法调用。
(3)public static void main(String[] args)
用途:创建CustomerView实例,并调用 enterMainMenu()方法以执行程序。
代码如下(示例):
package day1;
public class CustomerView {
private CustomerList customerList=new CustomerList(10);
public CustomerView(){
Customer cust = new Customer("张三", '男', 30, "010-56253825", "[email protected]");
customerList.addCustomer(cust);
customerList.getAllCustomers();
}
public void enterMainMenu(){
while (true){
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 c=CMUtility.readMenuSelection();
switch (c){
case '1':
addNewCustomer();
break;
case '2':
modifyCustomer();
break;
case '3':
deleteCustomer();
break;
case '4':
listAllCustomers();
break;
case '5':
System.exit(0);
}
}
}
private void addNewCustomer(){
System.out.println("---------------------添加客户---------------------");
System.out.println("姓名:");
String name = CMUtility.readString(5);
System.out.println("性别:");
char gender = CMUtility.readChar();
System.out.println("年龄:");
int age = CMUtility.readInt();
System.out.println("电话:");
String phone = CMUtility.readString(13);
System.out.println("邮箱:");
String email = CMUtility.readString(50);
Customer customer = new Customer(name,gender,age,phone,email);
boolean a = customerList.addCustomer(customer);
if(a==true){
System.out.println(customerList.getTotal());
System.out.println("添加成功!");
}else {
System.out.println("数组已满,无法添加!");
}
}
private void modifyCustomer(){
System.out.println("---------------------修改客户---------------------");
System.out.println("请选择待修改客户编号(-1退出):");
int number = CMUtility.readInt();
if (number==-1){
System.out.println("已退出!");
}else {
int max = customerList.getTotal();
Customer[] c = customerList.getAllCustomers();
String name1=" ";// 客户姓名
char gender1=' ';//性别
int age1=0;//年龄
String phone1=" ";//电话号码
String email1=" ";//电子邮箱
for (int i=0;i<max;i++){
if (number == (i + 1)) {
name1=c[i].getName();
gender1=c[i].getGender();
age1=c[i].getAge();
phone1=c[i].getEmail();
email1=c[i].getPhone();
}
}
System.out.println("姓名(佟刚):<直接回车表示不修改>");
String name = CMUtility.readString(5,name1);
System.out.println("性别(男):");
char gender = CMUtility.readChar(gender1);
System.out.println("年龄(35):");
int age = CMUtility.readInt(age1);
System.out.println("电话(010-56253825):");
String phone = CMUtility.readString(13,phone1);
System.out.println("邮箱([email protected]):");
String email = CMUtility.readString(50,email1);
Customer customer = new Customer(name,gender,age,phone,email);
boolean a = customerList.replaceCustomer(number-1,customer);
if(a==true){
System.out.println(customerList.getTotal());
System.out.println("---------------------修改完成---------------------");
}else {
System.out.println("索引越界,无法添加!");
}
}
}
private void deleteCustomer(){
System.out.println("---------------------删除客户---------------------");
System.out.println("请选择待删除客户编号(-1退出):");
int number = CMUtility.readInt();
boolean a=false;
if (number==-1){
System.out.println("退出!");
}else {
System.out.println("确认是否删除(Y/N):");
char c = CMUtility.readConfirmSelection();
if (c=='y'||c=='Y'){
int max = customerList.getTotal();
for (int i=0;i<max;i++){
if ((i+1)==number){
a= customerList.deleteCustomer(number);
}
}
}
if (a==true){
System.out.println("---------------------删除完成---------------------");
}else {
System.out.println("你输入的客户编号有误!");
}
}
}
private void listAllCustomers(){
System.out.println("---------------------------客户列表---------------------------");
System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t\t\t邮箱");
Customer[] c = customerList.getAllCustomers();
for (int i=0;i<customerList.getTotal();i++){
System.out.println((i+1)+"\t"+c[i].getName()+"\t"+c[i].getGender()+"\t"+c[i].getAge()+"\t"+c[i].getPhone()+"\t"+c[i].getEmail());
//System.out.println(a[i]);
}
}
public static void main(String[] args) {
CustomerView customerView = new CustomerView();
customerView.enterMainMenu();
}
}