设计电话薄程序 (1)根据下面的描述设计类层次关系。 1)联系人信息有:姓名、电话、备注; 2)家人信息:还包括地址、固定电话; 3)朋友信息:还包括类别(同学、同事、其他朋友); 4)工作联系人信息:还包括公司、部门、职务。 (2)注意重写toString(),返回每类对象的全部信息 (3) 要求在父类中定义display()方法,此方法中将toString()结果输出值控台 (4)提供添加、删除、修改、查看指定成员、查看所有联系人信息等方法的java程序
时间: 2024-02-06 07:09:53 浏览: 56
小代码,电话簿设计,程序设计周
以下是电话薄程序的Java代码实现:
```
class Contact {
private String name;
private String phone;
private 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 void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public void display() {
System.out.println(toString());
}
@Override
public String toString() {
return "联系人信息:\n姓名:" + name + "\n电话:" + phone + "\n备注:" + remark;
}
}
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 void setAddress(String address) {
this.address = address;
}
public String getHomePhone() {
return homePhone;
}
public void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}
@Override
public String toString() {
return super.toString() + "\n家庭信息:\n地址:" + address + "\n固定电话:" + 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;
}
@Override
public String toString() {
return super.toString() + "\n朋友信息:\n类别:" + 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 void setCompany(String company) {
this.company = company;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
@Override
public String toString() {
return super.toString() + "\n工作信息:\n公司:" + company + "\n部门:" + department + "\n职务:" + position;
}
}
class PhoneBook {
private List<Contact> contacts = new ArrayList<>();
public void addContact(Contact contact) {
contacts.add(contact);
}
public void deleteContact(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 getContactByName(String name) {
for (Contact contact : contacts) {
if (contact.getName().equals(name)) {
return contact;
}
}
return null;
}
public List<Contact> getAllContacts() {
return contacts;
}
}
```
使用示例:
```
public static void main(String[] args) {
PhoneBook phoneBook = new PhoneBook();
Contact contact1 = new Contact("张三", "123456789", "好友");
FamilyContact contact2 = new FamilyContact("李四", "987654321", "亲戚", "北京市朝阳区", "010-12345678");
FriendContact contact3 = new FriendContact("王五", "111111111", "同学", "同学");
WorkContact contact4 = new WorkContact("赵六", "222222222", "客户", "阿里巴巴", "技术部", "开发工程师");
phoneBook.addContact(contact1);
phoneBook.addContact(contact2);
phoneBook.addContact(contact3);
phoneBook.addContact(contact4);
System.out.println("所有联系人信息:");
for (Contact contact : phoneBook.getAllContacts()) {
contact.display();
System.out.println();
}
System.out.println("修改联系人信息:");
phoneBook.modifyContact(contact1, "张三丰", "123456789", "武术家");
contact1.display();
System.out.println("查看指定联系人信息:");
Contact contact = phoneBook.getContactByName("王五");
if (contact != null) {
contact.display();
} else {
System.out.println("未找到该联系人!");
}
System.out.println("删除联系人信息:");
phoneBook.deleteContact(contact2);
for (Contact contactInfo : phoneBook.getAllContacts()) {
contactInfo.display();
System.out.println();
}
}
```
阅读全文