基于JAVA语言 链表数据结构 实现通讯录管理系统
问题描述:
该设计采用菜单作为应用程序的主要界面,用控制语句来改变程序执行的顺序,控制语句是实现结构化程序设计的基础。该设计的任务是利用一个简单实用的菜单,通过菜单单项进行选择,实现和完成通讯录管理中常用的几个不同的功能。
【任务要求】
菜单内容:
(0)通讯录链表的建立
(1)通讯者结点的插入
(2)通讯者结点的查询
(3)通讯者结点的删除
(4)通讯录链表的输出
(5)退出管理系统
设计要求:
使用0-5来选择菜单项,其他输入则不起作用。
实现代码:
import java.util.Scanner;
public class Node {
public class Friend{
public Friend next;
private String Name;
private String Tel;
private String Email;
private String BornAdd;
public Friend(){}
public Friend(String Name,String Tel,String Email,String BornAdd){
this.Name=Name;
this.Tel=Tel;
this.Email=Email;
this.BornAdd=BornAdd;
}
public String getName(){
return Name;
}
public void setName(String Name){
this.Name=Name;
}
public void setTel(String Tel){
this.Tel=Tel;
}
public String getTel(){
return Tel;
}
public void setEmail(String Email){
this.Email=Email;}
public String getEmail(){
return Email;}
public void setBornAdd(String BornAdd){
this.BornAdd=BornAdd;}
public String getBornAdd(){
return BornAdd;}
public void message(String Name,String Tel,String Email,String BornAdd){
this.Name=Name;
this.Tel=Tel;
this.Email=Email;
this.BornAdd=BornAdd;}
public String toString(){
return this.Name+"\t"+this.Tel+"\t"+this.Email+"\t"+this.BornAdd;
}
}
private int size;
private Friend head;
{
this.size=0;
this.head=null;
}
public void insertHead(Friend s) {
if (this.size == 0)
{this.head=s;
}
else{s.next=this.head;
this.head=s;
}
this.size++;
}
public boolean delete(String Name){
Friend front=this.head;
if(Name.equals(front.getName())){
this.head=this.head.next;
this.size--;
return true;
}
while(!Name.equals(front.next.getName())){
front=front.next;
}
if(front.next!=null){
front.next=front.next.next;
this.size--;
return true;
}
return false;
}
public Friend select(String Name){
Friend front=this.head;
if(Name.equals(front.getName())){
Friend s=new Friend(Name,front.getTel(),front.getEmail(),front.getBornAdd());
return s;
}
while (front.next!=null){
front=front.next;
if(Name.equals(front.getName())){
Friend s=new Friend(Name,front.getTel(),front.getEmail(),front.getBornAdd());
return s;
}
}
return null;
}
public void display(){
System.out.println("姓名\t手机号\t邮箱地址\t家庭住址");
Friend front=this.head;
while (front!=null&&front.getName()!=null){
System.out.println(front.toString()) ;
front=front.next;
}
if(this.head==null||this.head.getName()==null){
System.out.println("无相关信息");
}
}
public static void main(String[] args) {
Node sm = new Node();
Scanner in = new Scanner(System.in);
boolean open = true;
while (open) {
printMenu();
System.out.println("请输入菜单序号进行操作");
int menu = -1;
try {
menu = in.nextInt();
} catch (Exception e) {
in = new Scanner(System.in);
}
switch (menu) {
case 0: {
System.out.println("创建成功");
}
break;
case 1: {
System.out.println("请输入姓名");
String Name1 = in.next();
System.out.println("手机号,邮箱地址,家庭地址(空格结束,回车确认插入)");
try {
String Tel = in.next();
String Email = in.next();
String BornAdd = in.next();
Friend news = sm.new Friend(Name1, Tel, Email, BornAdd);
sm.insertHead(news);
} catch (Exception e) {
System.out.println("输入信息有误");
in = new Scanner(System.in);
}
break;
}
case 2: {
System.out.println("请输入要查询联系人的姓名");
String Name2 = in.next();
Friend selectS = sm.select(Name2);
System.out.println("姓名\t手机号\t邮箱地址\t家庭地址");
if (selectS == null) {
} else {
System.out.println(selectS.toString());
}
}
break;
case 3: {
System.out.println("请输入要删除联系人的姓名");
String Name3 = in.next();
if (sm.delete(Name3)) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
sm.display();
}
break;
case 4: {
sm.display();
}
break;
case 5: {
System.out.println("系统已退出");
System.exit(0);
break;
}
default:
System.out.println("输入错误");
}
}
in.close();
}
public static void printMenu(){
System.out.println("通讯录管理系统");
System.out.println("*****************");
System.out.println("* 0----创建链表 *");
System.out.println("* 1----插入联系人 *");
System.out.println("* 2----查找联系人 *");
System.out.println("* 3----删除联系人 *");
System.out.println("* 4----输出通讯录 *");
System.out.println("* 5----退出系统 *");
System.out.println("*****************");
}}