1)联系人信息有:姓名、电话、备注;家人信息:还包括地址、固定电话; 朋友信息:还包括类别(同学、同事、其他朋友); (2)工作联系人信息:还包括公司、部门、职务。 (3)注重重写toString(),返回每类对象的全部信息 (4)要求在父亲中定义display()方法,此方法中将toString()结果输出值控台 (5)提供添加、删除、修改、查看指定成员、查看所有联系人信息等方法 (6)选做:图形用户界面、网络服务端 (7)使用Comparable或者Comparator方法对于联系人进行排序 (8)定义测试方法测试以上功能。写出可以运行的java程序
时间: 2023-07-04 20:12:46 浏览: 169
好的,下面是一个简单的实现:
```java
import java.util.*;
abstract class Contact implements Comparable<Contact> {
protected String name;
protected String phone;
protected String remark;
public Contact(String name, String phone, String remark) {
this.name = name;
this.phone = phone;
this.remark = remark;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getRemark() {
return remark;
}
public void setName(String name) {
this.name = name;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setRemark(String remark) {
this.remark = remark;
}
public abstract String toString();
public void display() {
System.out.println(toString());
}
public int compareTo(Contact other) {
return this.name.compareTo(other.name);
}
}
class FamilyContact extends Contact {
private String address;
private String homePhone;
public FamilyContact(String name, String phone, String remark, String address, String homePhone) {
super(name, phone, remark);
this.address = address;
this.homePhone = homePhone;
}
public String getAddress() {
return address;
}
public String getHomePhone() {
return homePhone;
}
public void setAddress(String address) {
this.address = address;
}
public void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}
public String toString() {
return "FamilyContact: " + name + ", " + phone + ", " + remark + ", " + address + ", " + homePhone;
}
}
class FriendContact extends Contact {
private String category;
public FriendContact(String name, String phone, String remark, String category) {
super(name, phone, remark);
this.category = category;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String toString() {
return "FriendContact: " + name + ", " + phone + ", " + remark + ", " + category;
}
}
class WorkContact extends Contact {
private String company;
private String department;
private String position;
public WorkContact(String name, String phone, String remark, String company, String department, String position) {
super(name, phone, remark);
this.company = company;
this.department = department;
this.position = position;
}
public String getCompany() {
return company;
}
public String getDepartment() {
return department;
}
public String getPosition() {
return position;
}
public void setCompany(String company) {
this.company = company;
}
public void setDepartment(String department) {
this.department = department;
}
public void setPosition(String position) {
this.position = position;
}
public String toString() {
return "WorkContact: " + name + ", " + phone + ", " + remark + ", " + company + ", " + department + ", " + position;
}
}
class ContactManager {
private ArrayList<Contact> contacts;
public ContactManager() {
contacts = new ArrayList<Contact>();
}
public void addContact(Contact contact) {
contacts.add(contact);
}
public void removeContact(Contact contact) {
contacts.remove(contact);
}
public void modifyContact(Contact contact, String name, String phone, String remark) {
contact.setName(name);
contact.setPhone(phone);
contact.setRemark(remark);
}
public Contact findContact(String name) {
for (Contact contact : contacts) {
if (contact.getName().equals(name)) {
return contact;
}
}
return null;
}
public void displayAllContacts() {
for (Contact contact : contacts) {
contact.display();
}
}
public void displayContactsSorted() {
Collections.sort(contacts);
displayAllContacts();
}
}
public class ContactTest {
public static void main(String[] args) {
ContactManager manager = new ContactManager();
FamilyContact familyContact = new FamilyContact("John", "1234567890", "brother", "123 Main St", "555-5555");
FriendContact friendContact = new FriendContact("Mary", "2345678901", "classmate", "classmate");
WorkContact workContact = new WorkContact("Tom", "3456789012", "co-worker", "ABC Company", "IT", "programmer");
manager.addContact(familyContact);
manager.addContact(friendContact);
manager.addContact(workContact);
manager.displayAllContacts();
Contact contact = manager.findContact("Mary");
manager.modifyContact(contact, "Maggie", "3456789123", "friend");
manager.displayAllContacts();
manager.removeContact(workContact);
manager.displayContactsSorted();
}
}
```
这个程序实现了联系人管理的基本功能,包括添加、删除、修改、查找、查看所有联系人信息等。同时,还实现了排序功能,可以按照联系人姓名进行排序。
阅读全文