Java - 通過數據結構實現簡易通訊錄
版權聲明:本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
本文鏈接:https://blog.csdn.net/weixin_45766022/article/details/106202857
Java - 通過數據結構實現簡易通訊錄
僅供學習參考
編程軟件:eclipse2017版
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.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();
}
}
————————————————
版權聲明:本文為CSDN博主「我寧願相信那是夢」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_45766022/article/details/106202857
沈陽治療外陰白斑哪里好:http://www.wybbsdyy.com/