目的:理解类的抽象与继承,体会方法重写和重写体现的多态性;综合运用面向对象的编程思想,对下面的问题进行分析,使用Java实现并进行测试。 内容:设计电话薄程序 (1)根据下面的描述设计类层次关系。 1)联系人信息有:姓名、电话、备注; 2)家人信息:还包括地址、固定电话; 3)朋友信息:还包括类别(同学、同事、其他朋友); 4)工作联系人信息:还包括公司、部门、职务。 (2)注意重写toString(),返回每类对象的全部信息 (3) 要求在父类中定义display()方法,此方法中将toString()结果输出值控台 (4)提供添加、删除、修改、查看指定成员、查看所有联系人信息等方法 (5)选做:图形用户界面、网络服务端 (6)使用Comparable或Comparator方法对联系人进行排序 能运行的java程序
时间: 2023-12-19 12:05:05 浏览: 62
Java面向对象(高级)- 抽象类与抽象方法(或abstract关键字)
以下是一个能够实现上述要求的Java程序,其中涉及到类的抽象与继承、方法重写和重载、多态性、排序等面向对象编程的概念:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
abstract 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 abstract String getInfo();
public void display() {
System.out.println(getInfo());
}
public String toString() {
return getInfo();
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getRemark() {
return 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 getInfo() {
return "家人 - " + getName() + " " + getPhone() + " " + getRemark() + " " + 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 getInfo() {
return "朋友 - " + getName() + " " + getPhone() + " " + getRemark() + " " + 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 getInfo() {
return "工作联系人 - " + getName() + " " + getPhone() + " " + getRemark() + " " + company + " " + department + " " + position;
}
}
class PhoneBook {
private ArrayList<Contact> contacts;
public PhoneBook() {
contacts = new ArrayList<Contact>();
}
public void addContact(Contact contact) {
contacts.add(contact);
}
public void removeContact(Contact contact) {
contacts.remove(contact);
}
public void modifyContact(Contact oldContact, Contact newContact) {
int index = contacts.indexOf(oldContact);
contacts.set(index, newContact);
}
public Contact searchContact(String name) {
for (Contact contact : contacts) {
if (contact.getName().equals(name)) {
return contact;
}
}
return null;
}
public void displayContacts() {
for (Contact contact : contacts) {
contact.display();
}
}
public void sortContacts() {
Collections.sort(contacts, new Comparator<Contact>() {
public int compare(Contact c1, Contact c2) {
return c1.getName().compareToIgnoreCase(c2.getName());
}
});
}
}
public class Main {
public static void main(String[] args) {
PhoneBook phoneBook = new PhoneBook();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入要进行的操作:1.添加联系人 2.删除联系人 3.修改联系人 4.查找联系人 5.显示所有联系人 6.排序联系人");
int option = scanner.nextInt();
switch (option) {
case 1:
System.out.println("请输入联系人类型:1.家人 2.朋友 3.工作联系人");
int type = scanner.nextInt();
System.out.println("请输入姓名:");
String name = scanner.next();
System.out.println("请输入电话:");
String phone = scanner.next();
System.out.println("请输入备注:");
String remark = scanner.next();
if (type == 1) {
System.out.println("请输入地址:");
String address = scanner.next();
System.out.println("请输入固定电话:");
String homePhone = scanner.next();
FamilyContact familyContact = new FamilyContact(name, phone, remark, address, homePhone);
phoneBook.addContact(familyContact);
} else if (type == 2) {
System.out.println("请输入类别:");
String category = scanner.next();
FriendContact friendContact = new FriendContact(name, phone, remark, category);
phoneBook.addContact(friendContact);
} else if (type == 3) {
System.out.println("请输入公司:");
String company = scanner.next();
System.out.println("请输入部门:");
String department = scanner.next();
System.out.println("请输入职务:");
String position = scanner.next();
WorkContact workContact = new WorkContact(name, phone, remark, company, department, position);
phoneBook.addContact(workContact);
}
break;
case 2:
System.out.println("请输入要删除的联系人姓名:");
String nameToRemove = scanner.next();
Contact contactToRemove = phoneBook.searchContact(nameToRemove);
if (contactToRemove != null) {
phoneBook.removeContact(contactToRemove);
System.out.println("删除成功!");
} else {
System.out.println("联系人不存在!");
}
break;
case 3:
System.out.println("请输入要修改的联系人姓名:");
String nameToModify = scanner.next();
Contact contactToModify = phoneBook.searchContact(nameToModify);
if (contactToModify != null) {
System.out.println("请输入新的联系人信息:");
int typeToModify = 0;
if (contactToModify instanceof FamilyContact) {
typeToModify = 1;
} else if (contactToModify instanceof FriendContact) {
typeToModify = 2;
} else if (contactToModify instanceof WorkContact) {
typeToModify = 3;
}
System.out.println("请输入姓名:");
String newName = scanner.next();
System.out.println("请输入电话:");
String newPhone = scanner.next();
System.out.println("请输入备注:");
String newRemark = scanner.next();
if (typeToModify == 1) {
System.out.println("请输入地址:");
String newAddress = scanner.next();
System.out.println("请输入固定电话:");
String newHomePhone = scanner.next();
FamilyContact newFamilyContact = new FamilyContact(newName, newPhone, newRemark, newAddress, newHomePhone);
phoneBook.modifyContact(contactToModify, newFamilyContact);
} else if (typeToModify == 2) {
System.out.println("请输入类别:");
String newCategory = scanner.next();
FriendContact newFriendContact = new FriendContact(newName, newPhone, newRemark, newCategory);
phoneBook.modifyContact(contactToModify, newFriendContact);
} else if (typeToModify == 3) {
System.out.println("请输入公司:");
String newCompany = scanner.next();
System.out.println("请输入部门:");
String newDepartment = scanner.next();
System.out.println("请输入职务:");
String newPosition = scanner.next();
WorkContact newWorkContact = new WorkContact(newName, newPhone, newRemark, newCompany, newDepartment, newPosition);
phoneBook.modifyContact(contactToModify, newWorkContact);
}
System.out.println("修改成功!");
} else {
System.out.println("联系人不存在!");
}
break;
case 4:
System.out.println("请输入要查找的联系人姓名:");
String nameToSearch = scanner.next();
Contact contactToSearch = phoneBook.searchContact(nameToSearch);
if (contactToSearch != null) {
contactToSearch.display();
} else {
System.out.println("联系人不存在!");
}
break;
case 5:
phoneBook.displayContacts();
break;
case 6:
phoneBook.sortContacts();
System.out.println("排序成功!");
break;
default:
System.out.println("输入错误,请重新输入!");
break;
}
}
}
}
```
这个程序实现了添加、删除、修改、查找和显示所有联系人信息的功能,并提供了一个排序联系人的选项。在程序运行时,用户可以根据提示输入相应的操作,然后程序会根据用户输入的内容执行相应的操作。
阅读全文