用java来实现以下内容:通讯录管理系统 1.通过java模拟链表来实现 2.实现来简单的增删改查 3.实现了通过文件来保存通讯录的信息 4.实现自定义异常来提示错误(就是想用以下自定义异常) 5.可以通过姓名和电话号码的字串来查找 6.保证了电话号码的唯一性

时间: 2024-03-17 22:45:30 浏览: 15
好的,我可以帮您解答这个问题。通讯录管理系统可以用Java语言通过链表来实现,以下是代码示例: 首先,定义一个联系人类 ContactPerson,包含姓名和电话号码两个属性: ```java public class ContactPerson { private String name; private String phoneNumber; public ContactPerson(String name, String phoneNumber) { this.name = name; this.phoneNumber = phoneNumber; } // getter and setter methods public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } } ``` 接下来,定义一个链表节点类 ListNode,包含一个 ContactPerson 对象和下一个节点的引用: ```java public class ListNode { private ContactPerson contactPerson; private ListNode next; public ListNode(ContactPerson contactPerson) { this.contactPerson = contactPerson; } // getter and setter methods public ContactPerson getContactPerson() { return contactPerson; } public void setContactPerson(ContactPerson contactPerson) { this.contactPerson = contactPerson; } public ListNode getNext() { return next; } public void setNext(ListNode next) { this.next = next; } } ``` 然后,定义一个链表类 LinkedList,实现增删改查的方法: ```java public class LinkedList { private ListNode head; public void addContactPerson(ContactPerson contactPerson) { if (head == null) { head = new ListNode(contactPerson); } else { ListNode current = head; while (current.getNext() != null) { current = current.getNext(); } current.setNext(new ListNode(contactPerson)); } } public void removeContactPerson(String name) throws ContactPersonNotFoundException { if (head == null) { throw new ContactPersonNotFoundException("通讯录为空,无法删除联系人!"); } if (head.getContactPerson().getName().equals(name)) { head = head.getNext(); } else { ListNode current = head; while (current.getNext() != null && !current.getNext().getContactPerson().getName().equals(name)) { current = current.getNext(); } if (current.getNext() == null) { throw new ContactPersonNotFoundException("联系人不存在,无法删除!"); } current.setNext(current.getNext().getNext()); } } public void updateContactPerson(ContactPerson contactPerson) throws ContactPersonNotFoundException { if (head == null) { throw new ContactPersonNotFoundException("通讯录为空,无法更新联系人!"); } ListNode current = head; while (current != null) { if (current.getContactPerson().getName().equals(contactPerson.getName())) { current.setContactPerson(contactPerson); return; } current = current.getNext(); } throw new ContactPersonNotFoundException("联系人不存在,无法更新!"); } public ContactPerson findContactPersonByName(String name) throws ContactPersonNotFoundException { if (head == null) { throw new ContactPersonNotFoundException("通讯录为空,无法查找联系人!"); } ListNode current = head; while (current != null) { if (current.getContactPerson().getName().contains(name)) { return current.getContactPerson(); } current = current.getNext(); } throw new ContactPersonNotFoundException("联系人不存在,无法查找!"); } public ContactPerson findContactPersonByPhoneNumber(String phoneNumber) throws ContactPersonNotFoundException { if (head == null) { throw new ContactPersonNotFoundException("通讯录为空,无法查找联系人!"); } ListNode current = head; while (current != null) { if (current.getContactPerson().getPhoneNumber().equals(phoneNumber)) { return current.getContactPerson(); } current = current.getNext(); } throw new ContactPersonNotFoundException("联系人不存在,无法查找!"); } } ``` 其中,ContactPersonNotFoundException 是自定义的异常类,用于提示错误信息: ```java public class ContactPersonNotFoundException extends Exception { public ContactPersonNotFoundException(String message) { super(message); } } ``` 最后,我们可以通过文件来保存通讯录的信息。例如,将通讯录保存到 contacts.txt 文件中: ```java public void saveToFile(String fileName) throws IOException { File file = new File(fileName); if (!file.exists()) { file.createNewFile(); } BufferedWriter out = new BufferedWriter(new FileWriter(file)); ListNode current = head; while (current != null) { out.write(current.getContactPerson().getName() + "," + current.getContactPerson().getPhoneNumber()); out.newLine(); current = current.getNext(); } out.flush(); out.close(); } ``` 以上就是用Java模拟链表来实现通讯录管理系统的示例代码。

相关推荐

最新推荐

recommend-type

C++双向链表实现简单通讯录

主要为大家详细介绍了C++双向链表实现简单通讯录,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C语言实现食堂就餐管理系统(带链表)

主要为大家详细介绍了C语言实现食堂就餐管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

基于C语言实现个人通讯录管理系统

之前利用C语言完成了一个关于个人通讯录管理系统的课题,主要是关于联系人的添加、查找、删除、修改、输出以及文件的写入与读出,还有一个甜点功能—模拟通话,它的实现原理也很容易理解,文章末尾会介绍到。...
recommend-type

课设报告-“贪吃蛇_C语言_链表实现”-CSDN~.docx

课设报告-“贪吃蛇_C语言_链表实现~_课设”中 兑现将课设报告另发一篇博客~
recommend-type

多项式相加 java链表实现

用java链表实现了两个多项式的相加、相乘,最后得到最终结果以链表形式输出
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。