用Java实现2.某客户信息管理系统(CustomerInformationSystem)中保存着个人客户( IndividualCustomer)和企业客户(InstitutionalCustomer)两类信息,应用继承或接口编写程序实现以下要求: (1)个人客户。对于这类客户,系统保存了客户标识和基本信息,基本信息包括姓名、住宅电话和email。编写构造方法,并编写showInfo方法显示个人客户的信息。(2)企业客户。对于这类客户,系统除了保存客户标识和企业名称之外,也保存了其联系人的信息。联系人的信息包括姓名、住宅电话、email、办公电话以及职位。编写构造方法,并编写showInfo方法显示企业客户的信息。 (3)编写向系统中添加客户(addCustomer)的方法,并从键盘输入具体客户的信息(要求包括个人客户和企业客户)保存在某类集合(具体集合类自己选择)。 (4)编写从系统中输出客户(printCustomer)的方法,应用多态编程输出所有具体客户的信息。
时间: 2024-03-19 10:45:00 浏览: 21
以下是Java实现的代码,使用了继承和接口:
```java
import java.util.ArrayList;
import java.util.Scanner;
interface Customer {
void showInfo();
}
class IndividualCustomer implements Customer {
private String id;
private String name;
private String phone;
private String email;
public IndividualCustomer(String id, String name, String phone, String email) {
this.id = id;
this.name = name;
this.phone = phone;
this.email = email;
}
public void showInfo() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Phone: " + phone);
System.out.println("Email: " + email);
}
}
class ContactPerson {
private String name;
private String phone;
private String email;
private String officePhone;
private String position;
public ContactPerson(String name, String phone, String email, String officePhone, String position) {
this.name = name;
this.phone = phone;
this.email = email;
this.officePhone = officePhone;
this.position = position;
}
public String getName() {
return name;
}
public void showInfo() {
System.out.println("Name: " + name);
System.out.println("Phone: " + phone);
System.out.println("Email: " + email);
System.out.println("Office Phone: " + officePhone);
System.out.println("Position: " + position);
}
}
class InstitutionalCustomer implements Customer {
private String id;
private String name;
private ContactPerson contactPerson;
public InstitutionalCustomer(String id, String name, ContactPerson contactPerson) {
this.id = id;
this.name = name;
this.contactPerson = contactPerson;
}
public void showInfo() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Contact Person: ");
contactPerson.showInfo();
}
}
class CustomerInformationSystem {
private ArrayList<Customer> customers;
public CustomerInformationSystem() {
customers = new ArrayList<>();
}
public void addCustomer() {
Scanner scanner = new Scanner(System.in);
System.out.println("Add a customer:");
System.out.println("Enter type (Individual or Institutional):");
String type = scanner.nextLine();
System.out.println("Enter ID:");
String id = scanner.nextLine();
System.out.println("Enter name:");
String name = scanner.nextLine();
if (type.equalsIgnoreCase("Individual")) {
System.out.println("Enter phone:");
String phone = scanner.nextLine();
System.out.println("Enter email:");
String email = scanner.nextLine();
IndividualCustomer customer = new IndividualCustomer(id, name, phone, email);
customers.add(customer);
} else if (type.equalsIgnoreCase("Institutional")) {
System.out.println("Enter contact person name:");
String contactPersonName = scanner.nextLine();
System.out.println("Enter contact person phone:");
String contactPersonPhone = scanner.nextLine();
System.out.println("Enter contact person email:");
String contactPersonEmail = scanner.nextLine();
System.out.println("Enter contact person office phone:");
String contactPersonOfficePhone = scanner.nextLine();
System.out.println("Enter contact person position:");
String contactPersonPosition = scanner.nextLine();
ContactPerson contactPerson = new ContactPerson(contactPersonName, contactPersonPhone, contactPersonEmail, contactPersonOfficePhone, contactPersonPosition);
InstitutionalCustomer customer = new InstitutionalCustomer(id, name, contactPerson);
customers.add(customer);
} else {
System.out.println("Invalid type.");
}
}
public void printCustomers() {
System.out.println("All customers:");
for (Customer customer : customers) {
customer.showInfo();
System.out.println();
}
}
}
public class Main {
public static void main(String[] args) {
CustomerInformationSystem system = new CustomerInformationSystem();
system.addCustomer();
system.addCustomer();
system.printCustomers();
}
}
```
运行程序后,会提示输入客户信息,输入完毕后可以查看所有客户的信息。