用java编写详细代码:一个客户信息管理系统,包含个人客户( IndividualCustomer)和企业客户(InstitutionalCustomer)两类信息并储存在某类集合中,主函数 编写向系统中添加客户(addCustomer)的方法,并从键盘输入具体客户的信息(要求 包括个人客户和企业客户)保存在某类集合(具体集合类自己选择)。 (4)编写从系统中输出客户(printCustomer)的方法,应用多态编程输出所有具体客户的信息。
时间: 2024-01-16 21:04:26 浏览: 36
好的,以下是Java代码:
```java
import java.util.ArrayList;
import java.util.Scanner;
class Customer {
private String name;
private String address;
private String phoneNumber;
private String email;
// 构造方法
public Customer(String name, String address, String phoneNumber, String email) {
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
this.email = email;
}
// getter 和 setter 方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
// toString 方法
public String toString() {
return "Name: " + name + "\nAddress: " + address + "\nPhone Number: " + phoneNumber + "\nEmail: " + email;
}
}
class IndividualCustomer extends Customer {
private String idNumber;
// 构造方法
public IndividualCustomer(String name, String address, String phoneNumber, String email, String idNumber) {
super(name, address, phoneNumber, email);
this.idNumber = idNumber;
}
// getter 和 setter 方法
public String getIdNumber() {
return idNumber;
}
public void setIdNumber(String idNumber) {
this.idNumber = idNumber;
}
// toString 方法
public String toString() {
return super.toString() + "\nID Number: " + idNumber;
}
}
class InstitutionalCustomer extends Customer {
private String taxIdNumber;
// 构造方法
public InstitutionalCustomer(String name, String address, String phoneNumber, String email, String taxIdNumber) {
super(name, address, phoneNumber, email);
this.taxIdNumber = taxIdNumber;
}
// getter 和 setter 方法
public String getTaxIdNumber() {
return taxIdNumber;
}
public void setTaxIdNumber(String taxIdNumber) {
this.taxIdNumber = taxIdNumber;
}
// toString 方法
public String toString() {
return super.toString() + "\nTax ID Number: " + taxIdNumber;
}
}
public class CustomerManagementSystem {
private ArrayList<Customer> customers = new ArrayList<Customer>();
// 添加客户
public void addCustomer(Customer customer) {
customers.add(customer);
}
// 输出客户信息
public void printCustomers() {
for (Customer customer : customers) {
System.out.println(customer.toString());
System.out.println();
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
CustomerManagementSystem system = new CustomerManagementSystem();
System.out.println("Enter customer information:");
// 添加个人客户
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Address: ");
String address = scanner.nextLine();
System.out.print("Phone Number: ");
String phoneNumber = scanner.nextLine();
System.out.print("Email: ");
String email = scanner.nextLine();
System.out.print("ID Number: ");
String idNumber = scanner.nextLine();
IndividualCustomer individualCustomer = new IndividualCustomer(name, address, phoneNumber, email, idNumber);
system.addCustomer(individualCustomer);
// 添加企业客户
System.out.print("Name: ");
name = scanner.nextLine();
System.out.print("Address: ");
address = scanner.nextLine();
System.out.print("Phone Number: ");
phoneNumber = scanner.nextLine();
System.out.print("Email: ");
email = scanner.nextLine();
System.out.print("Tax ID Number: ");
String taxIdNumber = scanner.nextLine();
InstitutionalCustomer institutionalCustomer = new InstitutionalCustomer(name, address, phoneNumber, email, taxIdNumber);
system.addCustomer(institutionalCustomer);
// 输出客户信息
system.printCustomers();
}
}
```
这个客户信息管理系统使用了面向对象的编程思想,将客户抽象出来成为一个类,然后派生出个人客户和企业客户两个子类,通过多态来统一处理不同类型的客户。在主函数中,通过输入来创建客户对象,并将其添加到集合中,最后输出所有客户的信息。
阅读全文