java实现简易通讯录_通过数据结构实现简易通讯录

AddressBookTest

是测试类

package MyADB;import java.util.InputMismatchException;

import java.util.Scanner;class InstructionsMistake extends Exception {

public InstructionsMistake(String mo) {

super(mo);

public class AddressBookTest {

public static void main(String[] args) throws InstructionsMistake{

MyAddressBook AdB = new MyAddressBook();

Scanner rb = new Scanner(System.in);

String name = new String();

String cell = new String();

boolean isNum = false;

int co = 0;

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.

退出通讯录程序

");

do {

System.out.print("\n********

请输入你所要操作的代码

:");

try {

co = rb.nextInt();

} catch (InputMismatchException e) {

throw new InstructionsMistake("

输入的操作代码有误

");

}

if (co == 1) {

System.out.print("

请输入新的联系人姓名

:");

name = rb.next();

System.out.print("

请输入新的联系人手机号码

:");

cell = rb.next();

//

运用正则表达式对手机号码的输入进行规范

isNum = cell.matches("^1[3|5|7|8]\\d{9}$");

while (!isNum) {

System.out.print("

输入的手机号码有误,请重新输入

:");

cell = rb.next();

isNum = cell.matches("^1[3|5|7|8]\\d{9}$");

}

AdB.addAdB(name, cell);

System.out.println("

联系人

" + name + "

成功录入

");

} else if (co == 2) {

System.out.print("

请输入所查询的联系人姓名

:");

name = rb.next();

String str = AdB.searchAdB(name);

if (str == null) {

System.out.println("

找不到

" + name + "

联系人

");

} else {

System.out.println("

查找成功

");

System.out.println("

该联系人的手机号码为

:" + str);

}

} else if (co == 3) {

System.out.print("

请输入要更改的联系人姓名

:");

name = rb.next();

String str = AdB.searchAdB(name);

if (str == null) {

System.out.println("

找不到

" + name + "

联系人

");

} else {

System.out.println("1/

更改联系人的姓名

");

System.out.println("2/

更改联系人的手机号码

");

System.out.print("

请输入操作代码

:");

int cot = rb.nextInt();

if (cot == 1) {

System.out.print("

请输入该联系人的新姓名

:");

String toName = rb.next();

toName = AdB.ChangeAdBName(name,toName);

System.out.println("

该联系人姓名成功更改为

:" + toName);

} else if (cot == 2) {

System.out.print("

请输入该联系人的新手机号码

:");

String toCell = rb.next();

isNum = toCell.matches("^1[3|5|7|8]\\d{9}$");

while (!isNum) {

System.out.print("

输入的手机号码有误,请重新输入

:");

toCell = rb.next();

isNum = toCell.matches("^1[3|5|7|8]\\d{9}$");

}

toCell = AdB.ChangeAdBCell(name,toCell);

System.out.println("

该联系人手机号码成功更改为

:" + toCell)

} else if (co == 4) {

System.out.print("

输入要删除的联系人姓名

:");

name = rb.next();

AdB.deleteAdB(name);

} else if (co == 5) {

System.out.println(AdB);

} else if (co == 6){

break;

}

} while (co != 6);

System.out.println("********

成功退出通讯录程序

********");

MyAddressBook

package MyADB;

//

双向

public class MyAddressBook {//

通讯录

protected Node first;//

第一个联系人

(

通讯录的管理工具

)

protected Node last;//

最后一个联系人

protected int size = 0;//

联系人的个数

//

通讯录中的单个联系人

protected class Node {//

联系人

(

内部类

)

Node prev;//

上一个联系人

Node next;//

下一个联系人

public String name;//

姓名

public String cell;//

手机号码

public Node(String name, String call) {

this.name = name;

this.cell = call;

//

尾插法

public void addAdB(String name, String call) {

Node node = new Node(name, call);//

新建一个联系人

if (size == 0) {

this.first = node;

this.last = node;

} else {

//

把新增联系人作为之前最后的联系人的下一个

this.last.next = node;

//

把最后一个联系人作为新增联系人的上一个联系人

node.prev = this.last;

//

把新增联系人作为通讯录的最后一个

this.last = node;

}size++;

}//

查找联系人

public String searchAdB(String name) {

if (size == 0) {

System.out.println("

通讯录为空

");

return null;

}Node current = this.first;

for (int i = 0; i < size; i++) {

if (!current.name.equals(name)) {

if (current.next == null) {

//

找不到返回空

return null;

current = current.next;

//

找到后返回该联系人的手机号码

return current.cell;

}//

返回联系人自身

public Node retuName(String name) {

if (size == 0) {

System.out.println("

通讯录为空

");

return null;

}Node current = this.first;

for (int i = 0; i < size; i++) {

if (!current.name.equals(name)) {

current = current.next;

return current;

//

更改联系人姓名

public String ChangeAdBName(String name, String toName) {

Node current = retuName(name);

current.name = toName;

return current.name;

}//

更改联系人手机号码

public String ChangeAdBCell(String name, String toCell) {

Node current = retuName(name);

current.cell = toCell;

return current.cell;

}//

删除指定联系人

public void deleteAdB(String name) {

if (size == 0) {

System.out.println("

通讯录为空

");

return;

}//

找到被删除的联系人

Node current = this.first;

for (int i = 0; i < size; i++) {

if (!current.name.equals(name)) {

if (current.next == null) {

System.out.println("

找不到

" + name + "

联系人

");

return;

current = current.next;

//

进行删除操作

if (current == first) {//

删除通讯录中顶部的一个联系人

this.first = current.next;

this.first.prev = null;

} else if (current == last) {//

删除通讯录中最底部的一个联系人

this.last = current.prev;//

将该联系人的上一个联系人作为通讯录的最后一个联系人

this.last.next = null;//

最后一个联系人对下一个联系人引用为空

} else

//

将该联系人的下一个联系人作为该联系人的上一个联系人的

next

current.next英镑符号https://www.gendan5.com/currency/GBP.html

current.prev.next = current.next;

//

将该联系人的上一个联系人作为该联系人的下一个联系人的

prev

current.next.prev = current.prev;

}size--;

System.out.println("

已将

" + name + "

移除通讯录

");

}public String toString() {

if (size == 0) {

return "

通讯录为空

";

}//

拼接字符串

StringBuilder sbBuilder = new StringBuilder(size * 2 + 1);

Node current = this.first;

int counet = 0;

while (current != null) {

sbBuilder.append("

联系人姓名为

:" + current.name + "\n");

sbBuilder.append("

该联系人手机号码为

:" + current.cell + "\n");

if (counet != size - 1) {

sbBuilder.append("\n");

counet++;

}current = current.next;

}return sbBuilder.toString();

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值