设计电话薄程序 (1)根据下面的描述设计类层次关系。 1)联系人信息有:姓名、电话、备注; 2)家人信息:还包括地址、固定电话; 3)朋友信息:还包括类别(同学、同事、其他朋友); 4)工作联系人信息:还包括公司、部门、职务。 (2)注意重写toString(),返回每类对象的全部信息 (3) 要求在父类中定义display()方法,此方法中将toString()结果输出值控台 (4)提供添加、删除、修改、查看指定成员、查看所有联系人信息等方法的可运行的Java程序
时间: 2024-02-06 19:09:55 浏览: 57
以下是对应的Java程序:
```
import java.util.ArrayList;
import java.util.Scanner;
// 联系人类
class 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 void display() {
System.out.println(toString());
}
// 重写toString()方法
@Override
public String toString() {
return "姓名:" + name + ",电话:" + phone + ",备注:" + remark;
}
}
// 家人类
class Family extends Contact {
private String address;
private String homePhone;
// 构造函数
public Family(String name, String phone, String remark, String address, String homePhone) {
super(name, phone, remark);
this.address = address;
this.homePhone = homePhone;
}
// 显示联系人信息
@Override
public void display() {
System.out.println(toString());
}
// 重写toString()方法
@Override
public String toString() {
return super.toString() + ",地址:" + address + ",固定电话:" + homePhone;
}
}
// 朋友类
class Friend extends Contact {
private String category;
// 构造函数
public Friend(String name, String phone, String remark, String category) {
super(name, phone, remark);
this.category = category;
}
// 显示联系人信息
@Override
public void display() {
System.out.println(toString());
}
// 重写toString()方法
@Override
public String toString() {
return super.toString() + ",类别:" + 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;
}
// 显示联系人信息
@Override
public void display() {
System.out.println(toString());
}
// 重写toString()方法
@Override
public String toString() {
return super.toString() + ",公司:" + company + ",部门:" + department + ",职务:" + position;
}
}
// 电话薄类
class PhoneBook {
private ArrayList<Contact> contacts;
// 构造函数
public PhoneBook() {
contacts = new ArrayList<Contact>();
}
// 添加联系人
public void addContact(Contact contact) {
contacts.add(contact);
System.out.println("添加成功!");
}
// 删除联系人
public void deleteContact(int index) {
if (index >= 0 && index < contacts.size()) {
contacts.remove(index);
System.out.println("删除成功!");
} else {
System.out.println("删除失败:输入的索引不合法!");
}
}
// 修改联系人
public void modifyContact(int index, Contact newContact) {
if (index >= 0 && index < contacts.size()) {
contacts.set(index, newContact);
System.out.println("修改成功!");
} else {
System.out.println("修改失败:输入的索引不合法!");
}
}
// 查看指定成员
public void viewContact(int index) {
if (index >= 0 && index < contacts.size()) {
contacts.get(index).display();
} else {
System.out.println("查看失败:输入的索引不合法!");
}
}
// 查看所有联系人信息
public void viewAllContacts() {
if (contacts.size() > 0) {
for (int i = 0; i < contacts.size(); i++) {
System.out.print(i + 1 + ". ");
contacts.get(i).display();
}
} else {
System.out.println("电话薄为空!");
}
}
}
// 主程序
public class PhoneBookMain {
public static void main(String[] args) {
PhoneBook phoneBook = new PhoneBook();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 添加联系人");
System.out.println("2. 删除联系人");
System.out.println("3. 修改联系人");
System.out.println("4. 查看指定成员");
System.out.println("5. 查看所有联系人信息");
System.out.println("6. 退出程序");
int choice = scanner.nextInt();
switch (choice) {
case 1: // 添加联系人
System.out.println("请选择联系人类型:");
System.out.println("1. 家人");
System.out.println("2. 朋友");
System.out.println("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();
Family family = new Family(name, phone, remark, address, homePhone);
phoneBook.addContact(family);
} else if (type == 2) { // 添加朋友
System.out.println("请输入类别:");
String category = scanner.next();
Friend friend = new Friend(name, phone, remark, category);
phoneBook.addContact(friend);
} 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("请输入要删除的联系人的索引:");
int deleteIndex = scanner.nextInt();
phoneBook.deleteContact(deleteIndex - 1);
break;
case 3: // 修改联系人
System.out.println("请输入要修改的联系人的索引:");
int modifyIndex = scanner.nextInt();
phoneBook.viewContact(modifyIndex - 1);
System.out.println("请选择修改后的联系人类型:");
System.out.println("1. 家人");
System.out.println("2. 朋友");
System.out.println("3. 工作联系人");
int modifyType = scanner.nextInt();
System.out.println("请输入姓名:");
String modifyName = scanner.next();
System.out.println("请输入电话:");
String modifyPhone = scanner.next();
System.out.println("请输入备注:");
String modifyRemark = scanner.next();
if (modifyType == 1) { // 修改家人
System.out.println("请输入地址:");
String modifyAddress = scanner.next();
System.out.println("请输入固定电话:");
String modifyHomePhone = scanner.next();
Family modifyFamily = new Family(modifyName, modifyPhone, modifyRemark, modifyAddress, modifyHomePhone);
phoneBook.modifyContact(modifyIndex - 1, modifyFamily);
} else if (modifyType == 2) { // 修改朋友
System.out.println("请输入类别:");
String modifyCategory = scanner.next();
Friend modifyFriend = new Friend(modifyName, modifyPhone, modifyRemark, modifyCategory);
phoneBook.modifyContact(modifyIndex - 1, modifyFriend);
} else if (modifyType == 3) { // 修改工作联系人
System.out.println("请输入公司:");
String modifyCompany = scanner.next();
System.out.println("请输入部门:");
String modifyDepartment = scanner.next();
System.out.println("请输入职务:");
String modifyPosition = scanner.next();
WorkContact modifyWorkContact = new WorkContact(modifyName, modifyPhone, modifyRemark, modifyCompany, modifyDepartment, modifyPosition);
phoneBook.modifyContact(modifyIndex - 1, modifyWorkContact);
}
break;
case 4: // 查看指定成员
System.out.println("请输入要查看的联系人的索引:");
int viewIndex = scanner.nextInt();
phoneBook.viewContact(viewIndex - 1);
break;
case 5: // 查看所有联系人信息
phoneBook.viewAllContacts();
break;
case 6: // 退出程序
scanner.close();
System.exit(0);
default:
System.out.println("输入的操作不合法,请重新输入!");
break;
}
System.out.println();
}
}
}
```
阅读全文