使用java某客户信息管理系统(CustomerlnformationSystem)中保存着个人客户( IndividualCustomer)和企业客户(InstitutionalCustomer)两类信息,应用继承或接 口编写程序实现以下要求: (1)个人客户。对于这类客户,系统保存了客户标识和基本信息,基本信息包括姓名、住宅电 话和email。编写构造方法,并编写showInfo方法显示个人客户的信息。 (2)企业客户。对于这类客户,系统除了保存客户标识和企业名称之外,也保存了其联系人的 信息。联系人的信息包括姓名、住宅电话、email、办公电话以及职位。编写构造方法,并编写 showInfo方法显示企业客户的信息。 (3)编写向系统中添加客户(addCustomer)的方法,并从键盘输入具体客户的信息(要求 包括个人客户和企业客户)保存在某类集合(具体集合类自己选择)。 (4)编写从系统中输出客户(printCustomer)的方法,应用多态编程输出所有具体客户的 信息。
时间: 2024-02-26 11:58:03 浏览: 24
好的,根据您的要求,我来给您解答这个问题。
首先,我们需要定义一个Customer类,用于保存所有客户的一般信息,例如客户标识。
```java
public class Customer {
private int customerId;
public Customer(int customerId) {
this.customerId = customerId;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public void showInfo() {
System.out.println("Customer ID: " + customerId);
}
}
```
接下来,我们需要定义一个IndividualCustomer类,用于保存个人客户的信息,并继承自Customer类。
```java
public class IndividualCustomer extends Customer {
private String name;
private String residentialPhone;
private String email;
public IndividualCustomer(int customerId, String name, String residentialPhone, String email) {
super(customerId);
this.name = name;
this.residentialPhone = residentialPhone;
this.email = email;
}
public void showInfo() {
super.showInfo();
System.out.println("Name: " + name);
System.out.println("Residential Phone: " + residentialPhone);
System.out.println("Email: " + email);
}
}
```
然后,我们需要定义一个InstitutionalCustomer类,用于保存企业客户的信息,并同样继承自Customer类。
```java
public class InstitutionalCustomer extends Customer {
private String name;
private String contactName;
private String residentialPhone;
private String email;
private String officePhone;
private String position;
public InstitutionalCustomer(int customerId, String name, String contactName, String residentialPhone, String email, String officePhone, String position) {
super(customerId);
this.name = name;
this.contactName = contactName;
this.residentialPhone = residentialPhone;
this.email = email;
this.officePhone = officePhone;
this.position = position;
}
public void showInfo() {
super.showInfo();
System.out.println("Name: " + name);
System.out.println("Contact Name: " + contactName);
System.out.println("Residential Phone: " + residentialPhone);
System.out.println("Email: " + email);
System.out.println("Office Phone: " + officePhone);
System.out.println("Position: " + position);
}
}
```
接下来,我们需要定义一个CustomerInformationSystem类,用于添加客户并输出客户信息。
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CustomerInformationSystem {
private List<Customer> customerList;
public CustomerInformationSystem() {
customerList = new ArrayList<>();
}
public void addCustomer() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter customer type (1 for IndividualCustomer, 2 for InstitutionalCustomer): ");
int type = scanner.nextInt();
System.out.println("Enter customer ID: ");
int customerId = scanner.nextInt();
System.out.println("Enter customer name: ");
String name = scanner.next();
System.out.println("Enter customer residential phone: ");
String residentialPhone = scanner.next();
System.out.println("Enter customer email: ");
String email = scanner.next();
if (type == 1) {
IndividualCustomer individualCustomer = new IndividualCustomer(customerId, name, residentialPhone, email);
customerList.add(individualCustomer);
} else if (type == 2) {
System.out.println("Enter customer contact name: ");
String contactName = scanner.next();
System.out.println("Enter customer office phone: ");
String officePhone = scanner.next();
System.out.println("Enter customer position: ");
String position = scanner.next();
InstitutionalCustomer institutionalCustomer = new InstitutionalCustomer(customerId, name, contactName, residentialPhone, email, officePhone, position);
customerList.add(institutionalCustomer);
}
}
public void printCustomer() {
for (Customer customer : customerList) {
customer.showInfo();
}
}
}
```
最后,我们可以在main方法中测试这个系统。
```java
public class Main {
public static void main(String[] args) {
CustomerInformationSystem customerInformationSystem = new CustomerInformationSystem();
customerInformationSystem.addCustomer();
customerInformationSystem.addCustomer();
customerInformationSystem.printCustomer();
}
}
```
这个系统可以根据用户输入的类型和具体信息来添加个人客户和企业客户,并且可以输出所有客户的信息。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)