用java编写详细代码:一个客户信息管理系统,包含个人客户( IndividualCustomer)和企业客户(InstitutionalCustomer)两类信息并储存在某类集合中,主函数 编写向系统中添加客户(addCustomer)的方法,并从键盘输入具体客户的信息(要求 包括个人客户和企业客户)保存在某类集合(具体集合类自己选择)。 (4)编写从系统中输出客户(printCustomer)的方法,应用多态编程输出所有具体客户的信息。
时间: 2024-01-16 12:04:40 浏览: 56
好的,以下是代码:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class CustomerManagementSystem {
public static void main(String[] args) {
ArrayList<Customer> customers = new ArrayList<Customer>();
Scanner scan = new Scanner(System.in);
// 添加客户
addCustomer(customers, scan);
// 输出客户信息
printCustomer(customers);
}
// 添加客户
public static void addCustomer(ArrayList<Customer> customers, Scanner scan) {
System.out.println("请输入客户类型(1:个人客户 2:企业客户):");
int type = scan.nextInt();
Customer customer = null;
if (type == 1) {
customer = new IndividualCustomer();
System.out.println("请输入姓名:");
((IndividualCustomer) customer).setName(scan.next());
System.out.println("请输入性别:");
((IndividualCustomer) customer).setGender(scan.next());
System.out.println("请输入年龄:");
((IndividualCustomer) customer).setAge(scan.nextInt());
System.out.println("请输入电话号码:");
((IndividualCustomer) customer).setPhone(scan.next());
System.out.println("请输入邮箱:");
((IndividualCustomer) customer).setEmail(scan.next());
} else if (type == 2) {
customer = new InstitutionalCustomer();
System.out.println("请输入企业名称:");
((InstitutionalCustomer) customer).setCompanyName(scan.next());
System.out.println("请输入联系人姓名:");
((InstitutionalCustomer) customer).setContactName(scan.next());
System.out.println("请输入联系人电话号码:");
((InstitutionalCustomer) customer).setContactPhone(scan.next());
System.out.println("请输入联系人邮箱:");
((InstitutionalCustomer) customer).setContactEmail(scan.next());
} else {
System.out.println("输入类型有误!");
return;
}
customers.add(customer);
}
// 输出客户信息
public static void printCustomer(ArrayList<Customer> customers) {
for (Customer customer : customers) {
customer.printInfo();
}
}
}
class Customer {
public void printInfo() {}
}
class IndividualCustomer extends Customer {
private String name;
private String gender;
private int age;
private String phone;
private String email;
public void setName(String name) {
this.name = name;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setAge(int age) {
this.age = age;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setEmail(String email) {
this.email = email;
}
public void printInfo() {
System.out.println("客户类型:个人客户");
System.out.println("姓名:" + name);
System.out.println("性别:" + gender);
System.out.println("年龄:" + age);
System.out.println("电话号码:" + phone);
System.out.println("邮箱:" + email);
}
}
class InstitutionalCustomer extends Customer {
private String companyName;
private String contactName;
private String contactPhone;
private String contactEmail;
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public void setContactName(String contactName) {
this.contactName = contactName;
}
public void setContactPhone(String contactPhone) {
this.contactPhone = contactPhone;
}
public void setContactEmail(String contactEmail) {
this.contactEmail = contactEmail;
}
public void printInfo() {
System.out.println("客户类型:企业客户");
System.out.println("企业名称:" + companyName);
System.out.println("联系人姓名:" + contactName);
System.out.println("联系人电话号码:" + contactPhone);
System.out.println("联系人邮箱:" + contactEmail);
}
}
```
这是一个简单的客户信息管理系统,可以添加个人客户和企业客户,并输出客户信息。其中使用了多态,即通过客户对象的printInfo()方法输出不同类型的客户信息。
阅读全文