设计电话薄程序 (1)根据下面的描述设计类层次关系。 1)联系人信息有:姓名、电话、备注; 2)家人信息:还包括地址、固定电话; 3)朋友信息:还包括类别(同学、同事、其他朋友); 4)工作联系人信息:还包括公司、部门、职务。 (2)注意重写toString(),返回每类对象的全部信息 (3) 要求在父类中定义display()方法,此方法中将toString()结果输出值控台 (4)提供添加、删除、修改、查看指定成员、查看所有联系人信息等方法 (5)选做:图形用户界面、网络服务端 (6)使用Comparable或Comparator方法对联系人进行排序 (7)定义测试方法测试以上功能。
时间: 2024-02-09 16:08:49 浏览: 92
根据上述要求,可以设计以下类层次关系:
```
Contact(联系人信息类)
- name(姓名)
- phone(电话)
- note(备注)
+ display()(展示信息方法)
+ toString()(重写toString方法)
Family(家人信息类,继承自Contact)
- address(地址)
- homePhone(固定电话)
+ display()(展示信息方法,重写父类方法)
Friend(朋友信息类,继承自Contact)
- category(类别,如同学、同事、其他朋友)
+ display()(展示信息方法,重写父类方法)
WorkContact(工作联系人信息类,继承自Contact)
- company(公司)
- department(部门)
- jobTitle(职务)
+ display()(展示信息方法,重写父类方法)
AddressBook(电话薄类)
- contacts(联系人列表)
+ addContact(contact: Contact)(添加联系人方法)
+ removeContact(contact: Contact)(删除联系人方法)
+ updateContact(contact: Contact)(修改联系人方法)
+ getContactByName(name: String)(查看指定成员方法)
+ getAllContacts()(查看所有联系人信息方法)
+ sortContacts(comparator: Comparator<Contact>)(对联系人进行排序方法)
```
其中,`Contact` 类作为父类,定义了基本的联系人信息和展示信息方法,而 `Family`、`Friend`、`WorkContact` 则分别继承自 `Contact` 并添加了自己特有的信息和展示信息方法。`AddressBook` 类则对联系人信息进行管理,并提供添加、删除、修改、查看指定成员、查看所有联系人信息、对联系人进行排序等方法。
以下是示例代码:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Contact {
protected String name;
protected String phone;
protected String note;
public Contact(String name, String phone, String note) {
this.name = name;
this.phone = phone;
this.note = note;
}
public void display() {
System.out.println(toString());
}
@Override
public String toString() {
return "Name: " + name + ", Phone: " + phone + ", Note: " + note;
}
}
class Family extends Contact {
private String address;
private String homePhone;
public Family(String name, String phone, String note, String address, String homePhone) {
super(name, phone, note);
this.address = address;
this.homePhone = homePhone;
}
@Override
public void display() {
System.out.println(toString());
}
@Override
public String toString() {
return super.toString() + ", Address: " + address + ", Home Phone: " + homePhone;
}
}
class Friend extends Contact {
private String category;
public Friend(String name, String phone, String note, String category) {
super(name, phone, note);
this.category = category;
}
@Override
public void display() {
System.out.println(toString());
}
@Override
public String toString() {
return super.toString() + ", Category: " + category;
}
}
class WorkContact extends Contact {
private String company;
private String department;
private String jobTitle;
public WorkContact(String name, String phone, String note, String company, String department, String jobTitle) {
super(name, phone, note);
this.company = company;
this.department = department;
this.jobTitle = jobTitle;
}
@Override
public void display() {
System.out.println(toString());
}
@Override
public String toString() {
return super.toString() + ", Company: " + company + ", Department: " + department + ", Job Title: " + jobTitle;
}
}
class AddressBook {
private List<Contact> contacts = new ArrayList<>();
public void addContact(Contact contact) {
contacts.add(contact);
}
public void removeContact(Contact contact) {
contacts.remove(contact);
}
public void updateContact(Contact contact) {
int index = contacts.indexOf(contact);
if (index >= 0) {
contacts.set(index, contact);
}
}
public Contact getContactByName(String name) {
for (Contact contact : contacts) {
if (contact.name.equals(name)) {
return contact;
}
}
return null;
}
public void getAllContacts() {
for (Contact contact : contacts) {
contact.display();
}
}
public void sortContacts(Comparator<Contact> comparator) {
Collections.sort(contacts, comparator);
}
}
class Test {
public static void main(String[] args) {
AddressBook addressBook = new AddressBook();
addressBook.addContact(new Family("Tom", "123456", "My brother", "Beijing", "654321"));
addressBook.addContact(new Friend("Jerry", "234567", "My classmate", "Classmate"));
addressBook.addContact(new WorkContact("John", "345678", "My colleague", "ABC Company", "IT Department", "Software Engineer"));
System.out.println("All contacts:");
addressBook.getAllContacts();
System.out.println();
System.out.println("Update contact:");
Contact contact = addressBook.getContactByName("Jerry");
if (contact != null) {
contact.phone = "987654";
addressBook.updateContact(contact);
}
addressBook.getAllContacts();
System.out.println();
System.out.println("Sort contacts by name:");
addressBook.sortContacts(new Comparator<Contact>() {
@Override
public int compare(Contact o1, Contact o2) {
return o1.name.compareTo(o2.name);
}
});
addressBook.getAllContacts();
}
}
```
阅读全文