某客户信息管理系统(CustomerlnformationSystem)中保存着个人客户( IndividualCustomer)和企业客户(InstitutionalCustomer)两类信息,应用继承或接 口编写程序实现以下要求: (1)个人客户。对于这类客户,系统保存了客户标识和基本信息,基本信息包括姓名、住宅电 话和email。编写构造方法,并编写showInfo方法显示个人客户的信息。 (2)企业客户。对于这类客户,系统除了保存客户标识和企业名称之外,也保存了其联系人的 信息。联系人的信息包括姓名、住宅电话、email、办公电话以及职位。编写构造方法,并编写 showInfo方法显示企业客户的信息。 (3)编写向系统中添加客户(addCustomer)的方法,并从键盘输入具体客户的信息(要求 包括个人客户和企业客户)保存在某类集合(具体集合类自己选择)。 (4)编写从系统中输出客户(printCustomer)的方法,应用多态编程输出所有具体客户的 信息。
时间: 2023-06-09 21:03:24 浏览: 113
ChitGPT: 你好,很高兴与您对话。针对您的问题,我可以给出以下解答:
针对这种情况,可以采用继承的方式,定义一个父类Customer,然后派生出IndividualCustomer和InstitutionalCustomer两个子类,这样便可以实现代码的复用。同时,可以在每个类中定义相应的构造方法和showInfo方法,实现不同客户的信息管理。
示例代码如下:
```java
// 父类Customer
public class Customer {
private String id; // 客户标识
// 构造方法
public Customer(String id) {
this.id = id;
}
// showInfo方法
public void showInfo() {
System.out.println("客户标识:" + id);
}
}
// 个人客户类IndividualCustomer
public class IndividualCustomer extends Customer {
private String name; // 姓名
private String homePhone; // 住宅电话
private String email; // 邮箱
// 构造方法
public IndividualCustomer(String id, String name, String homePhone, String email) {
super(id);
this.name = name;
this.homePhone = homePhone;
this.email = email;
}
// showInfo方法,重写父类方法
@Override
public void showInfo() {
super.showInfo();
System.out.println("姓名:" + name);
System.out.println("住宅电话:" + homePhone);
System.out.println("邮箱:" + email);
}
}
// 企业客户类InstitutionalCustomer
public class InstitutionalCustomer extends Customer {
private String name; // 企业名称
private Contact contact; // 联系人信息
// 构造方法
public InstitutionalCustomer(String id, String name, Contact contact) {
super(id);
this.name = name;
this.contact = contact;
}
// showInfo方法,重写父类方法
@Override
public void showInfo() {
super.showInfo();
System.out.println("企业名称:" + name);
System.out.println("联系人姓名:" + contact.getName());
System.out.println("联系人住宅电话:" + contact.getHomePhone());
System.out.println("联系人邮箱:" + contact.getEmail());
System.out.println("联系人办公电话:" + contact.getOfficePhone());
System.out.println("联系人职位:" + contact.getPosition());
}
}
// 联系人类Contact
public class Contact {
private String name; // 姓名
private String homePhone; // 住宅电话
private String email; // 邮箱
private String officePhone; // 办公电话
private String position; // 职位
// 构造方法
public Contact(String name, String homePhone, String email, String officePhone, String position) {
this.name = name;
this.homePhone = homePhone;
this.email = email;
this.officePhone = officePhone;
this.position = position;
}
// getter方法
public String getName() {
return name;
}
public String getHomePhone() {
return homePhone;
}
public String getEmail() {
return email;
}
public String getOfficePhone() {
return officePhone;
}
public String getPosition() {
return position;
}
}
// 测试类
public class Test {
public static void main(String[] args) {
// 创建一个List集合保存客户信息
List<Customer> customers = new ArrayList<>();
// 调用addCustomer方法添加个人客户
addCustomer(customers, new IndividualCustomer("1001", "张三", "123456789", "zhangsan@qq.com"));
// 调用addCustomer方法添加企业客户
addCustomer(customers, new InstitutionalCustomer("2001", "ABC公司", new Contact("李四", "987654321", "lisi@qq.com", "010-12345678", "经理")));
// 调用printCustomer方法打印客户信息
printCustomer(customers);
}
// 添加客户方法
public static void addCustomer(List<Customer> customers, Customer customer) {
customers.add(customer);
}
// 打印客户信息方法
public static void printCustomer(List<Customer> customers) {
for (Customer customer : customers) {
customer.showInfo();
System.out.println();
}
}
}
```
这样,我们便实现了题目要求的所有功能。如果还有任何问题,请随时提出。