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();