用java编写详细代码一个客户信息管理系统,包含个人客户( IndividualCustomer)和企业客户(InstitutionalCustomer)两类信息并储存在某类集合中,编写向系统中添加客户(addCustomer)的方法,并从键盘输入具体客户的信息(要求 包括个人客户和企业客户)保存在某类集合(具体集合类自己选择)。 (4)编写从系统中输出客户(printCustomer)的方法,应用多态编程输出所有具体客户的信息
时间: 2024-01-16 19:04:26 浏览: 26
好的,这是您的问题:用java编写详细代码一个客户信息管理系统,包含个人客户( IndividualCustomer)和企业客户(InstitutionalCustomer)两类信息并储存在某类集合中,编写向系统中添加客户(addCustomer)的方法,并从键盘输入具体客户的信息(要求 包括个人客户和企业客户)保存在某类集合(具体集合类自己选择)。 (4)编写从系统中输出客户(printCustomer)的方法,应用多态编程输出所有具体客户的信息。
以下是代码实现:
```
import java.util.ArrayList;
import java.util.Scanner;
abstract class Customer {
private String name;
private String address;
private String telephoneNumber;
public Customer(String name, String address, String telephoneNumber) {
this.name = name;
this.address = address;
this.telephoneNumber = telephoneNumber;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getTelephoneNumber() {
return telephoneNumber;
}
public abstract String getType();
public abstract void printDetails();
}
class IndividualCustomer extends Customer {
private int age;
public IndividualCustomer(String name, String address, String telephoneNumber, int age) {
super(name, address, telephoneNumber);
this.age = age;
}
public int getAge() {
return age;
}
public String getType() {
return "Individual";
}
public void printDetails() {
System.out.println("Name: " + getName());
System.out.println("Address: " + getAddress());
System.out.println("Telephone Number: " + getTelephoneNumber());
System.out.println("Age: " + age);
}
}
class InstitutionalCustomer extends Customer {
private String contactPerson;
public InstitutionalCustomer(String name, String address, String telephoneNumber, String contactPerson) {
super(name, address, telephoneNumber);
this.contactPerson = contactPerson;
}
public String getContactPerson() {
return contactPerson;
}
public String getType() {
return "Institutional";
}
public void printDetails() {
System.out.println("Name: " + getName());
System.out.println("Address: " + getAddress());
System.out.println("Telephone Number: " + getTelephoneNumber());
System.out.println("Contact Person: " + contactPerson);
}
}
public class CustomerManagementSystem {
private ArrayList<Customer> customers;
public CustomerManagementSystem() {
customers = new ArrayList<Customer>();
}
public void addCustomer() {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the type of customer (I for individual, C for institutional):");
String type = scanner.nextLine();
System.out.println("Please enter the name of the customer:");
String name = scanner.nextLine();
System.out.println("Please enter the address of the customer:");
String address = scanner.nextLine();
System.out.println("Please enter the telephone number of the customer:");
String telephoneNumber = scanner.nextLine();
if (type.equals("I")) {
System.out.println("Please enter the age of the customer:");
int age = scanner.nextInt();
customers.add(new IndividualCustomer(name, address, telephoneNumber, age));
} else if (type.equals("C")) {
System.out.println("Please enter the contact person of the customer:");
String contactPerson = scanner.nextLine();
customers.add(new InstitutionalCustomer(name, address, telephoneNumber, contactPerson));
} else {
System.out.println("Invalid type of customer.");
}
}
public void printCustomers() {
for (Customer customer : customers) {
System.out.println(customer.getType() + " customer:");
customer.printDetails();
System.out.println();
}
}
}
```
这个系统包含了两个类:`Customer` 和 `CustomerManagementSystem`。`Customer` 是一个抽象类,包含了客户的基本信息和一个抽象方法 `printDetails()`,用于输出客户的详细信息。`IndividualCustomer` 和 `InstitutionalCustomer` 继承了 `Customer`,并实现了 `printDetails()` 方法。`CustomerManagementSystem` 类用于管理客户信息,包括添加客户和输出客户信息。
在添加客户时,用户需要输入客户的类型(个人或企业)、姓名、地址、电话号码和其他特定于类型的信息(如年龄或联系人)。然后,根据类型创建相应的 `Customer` 对象,并将其添加到 `customers` 列表中。
在输出客户信息时,`printCustomers()` 方法遍历 `customers` 列表,并调用每个客户的 `printDetails()` 方法。由于 `IndividualCustomer` 和 `InstitutionalCustomer` 都是 `Customer` 类型的对象,因此在调用 `printDetails()` 方法时,会自动调用相应类型的 `printDetails()` 方法。
希望这个代码可以帮到你,如果有任何疑问,请随时问我。