⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 directory.java

📁 本通讯录包括六项功能
💻 JAVA
字号:
/**
 * AWT Sample application
 *
 * @author 
 * @version 1.00 06/11/28
 */


/*说明:    本通讯录包括六项功能,分别是添加、修改、删除、显示通讯录列表(即文件中保存的
 *      通讯录内容)、保存(即将通讯录列表保存到文件中)、退出。
 *          运行此程序之初,程序自动读文件,将文件中的通讯录列表读到列表List中,使用者无
 *      须再进行此项操作,直接选择列出的六项功能进行操作就好。程序将通讯者按照他们姓名的
 *      拼音以由小到大的顺序排列。在执行完添加、修改、删除功能以后,使用者需要执行保存功
 *      能,将将链表List保存到文件中。
 *         使用者可以打开通讯所保存在的文件directory.txt进行读写,文件的第一行显示通讯录
 *      中保存的通讯者的个数,从第二行开始显示通讯者信息。
 *         整个程序运行正常,能够实现全部功能,目前没有发现bug。
 *         欢迎使用,提出宝贵意见!
 */
import java.io.*;
import java.util.LinkedList;
import java.util.Stack;

class Node{
   public String Name,Cellphone,Fixedphone,Email;
   public Node(){
       Name = Cellphone = Fixedphone = Email = "";   
    }
}

public class Directory{
   LinkedList list=new LinkedList();
   public void Add(){
      Node adder=new Node();
      Stack stack=new Stack();                                                                                                                                                                           	  
      Node tmp=new Node();  
      int i=0,a;
        try
        {
          System.out.println("Please enter the adder's name:");
          BufferedReader name=new BufferedReader(new InputStreamReader(System.in));
          adder.Name=name.readLine();
          System.out.println("Please enter the adder's Cellphone:");
          BufferedReader cellphone=new BufferedReader(new InputStreamReader(System.in));
          adder.Cellphone=cellphone.readLine();
          System.out.println("Please enter the adder's Fixedphone:");
          BufferedReader fixedphone=new BufferedReader(new InputStreamReader(System.in));
          adder.Fixedphone=fixedphone.readLine();
          System.out.println("Please enter the adder's Email:");
          BufferedReader email=new BufferedReader(new InputStreamReader(System.in));
          adder.Email=email.readLine();
          while(!this.list.isEmpty()&&i==0)
          {
            tmp=(Node)this.list.removeFirst();
            a=adder.Name.compareTo(tmp.Name);
            if(a>0)
              stack.push(tmp);
            else
            {
              list.addFirst(tmp);
              list.addFirst(adder);
              i=1;  	  	 
             }
          }
          if(i==0) 
            stack.push(adder);
          for(;!stack.isEmpty();)
            list.addFirst(stack.pop());   	
          if(this.list.isEmpty())
                list.addLast(adder);
        }catch(IOException e){}

     }


   public void Delete(){
     Node tmp=new Node();
     Stack stack=new Stack();
     int i=0;
     try
     {
       System.out.println("Please enter the name of the person you want to delete:");
       BufferedReader del=new BufferedReader(new InputStreamReader(System.in));
       String delname=del.readLine();
       for(;!this.list.isEmpty()&&i==0;)
       {
         tmp=(Node)this.list.removeFirst();
         if(!delname.equals(tmp.Name)){
             stack.push(tmp);
           }
        else 
        {
          i=1;
          if(!stack.isEmpty()) 
            list.addFirst(stack.pop());
          System.out.println(" ");
          System.out.println("Successful deleting!");      
          System.out.println("Please save the record!");
         }
            
       }
       if(this.list.isEmpty())
         if(i==0)
         {
           System.out.println(" ");
           System.out.println("Can't find the person you entered!");
           System.out.println("Please check the name you entered!");
           for(;!stack.isEmpty();)
             list.addFirst(stack.pop());
          }
         else  
           System.out.println("The directory is empty!");
    }catch(IOException e){}
  }


  public void Modify(){
    Node tmp=new Node();
    Stack stack=new Stack();
    int i=0;
    try
    {
      System.out.println("Please input the name of the person you want to modify:");
      BufferedReader modify=new BufferedReader(new InputStreamReader(System.in));
      String modifyname=modify.readLine();
      while(!this.list.isEmpty()&&i==0)
      {
         tmp=(Node)this.list.removeFirst();
         if(!modifyname.equals(tmp.Name)){
            stack.push(tmp);
            }
         else if(modifyname.equals(tmp.Name))
         {
           System.out.println("Please enter "+modifyname+"'s imformation again:");
           tmp.Name=modifyname;
           System.out.println("Please enter "+modifyname+"'s Cellphone:");
           BufferedReader cellphone=new BufferedReader(new InputStreamReader(System.in));
           tmp.Cellphone=cellphone.readLine();
           System.out.println("Please enter "+modifyname+"'s Fixedphone:");
           BufferedReader fixedphone=new BufferedReader(new InputStreamReader(System.in));
           tmp.Fixedphone=fixedphone.readLine();
           System.out.println("Please enter "+modifyname+"'s Email:");
           BufferedReader email=new BufferedReader(new InputStreamReader(System.in));
           tmp.Email=email.readLine();
           list.addFirst(tmp);
           for(;!stack.isEmpty();)
              list.addFirst(stack.pop());
           System.out.println(" ");
           System.out.println("Successful modifying!");
           System.out.println("Please save the record!");
           i=1;
          }
      }
      if(this.list.isEmpty())
      {
        System.out.println(" ");
        System.out.println("The person donot exist in the directory!");
        System.out.println("Please check the name you entered!");
        for(;!stack.isEmpty();)
          list.addFirst(stack.pop());
      }
    }catch(IOException e){}
  }

  public void ReadFromFile(String directory)throws IOException{
    try{    
         BufferedReader file=new BufferedReader(new FileReader("directory.txt"));
         int num=Integer.parseInt((String) file.readLine());
         if(num==0)
           this.list.clear();
         else
         {
           for(int i=1;i<=num;i++)
           {     
             Node person=new Node();
             int pos[]={0,0,0};
             String info=file.readLine();
             pos[0]=info.indexOf(" ");
             for(int j=1;j<3;j++)
             {
               int a=info.indexOf(" ",pos[j-1]+1);
               pos[j]=a;
             }
             person.Name=info.substring(0,pos[0]);
             person.Cellphone = info.substring(pos[0]+1, pos[1]);
             person.Fixedphone=info.substring(pos[1]+1 , pos[2]);
             person.Email=info.substring(pos[2]+1);
             this.list.addLast(person);
           }
         }
        file.close();
      }catch(IOException e){}
  }


  public void Save(String directory)throws IOException{
    Stack stack=new Stack();
    try{
         PrintWriter out=new PrintWriter(new FileWriter("directory.txt"));
         out.println(this.list.size());
         if(this.list.isEmpty())
         {
           System.out.println("The directory is empty!");
         }
         while(!this.list.isEmpty())
         {
           Node tmp=(Node)this.list.removeFirst();
           stack.push(tmp);
           out.println(tmp.Name+" "+tmp.Cellphone+" "+tmp.Fixedphone+" "+tmp.Email);
         }
         for(;!stack.isEmpty();)
           list.addFirst(stack.pop());
         out.close();
      }catch(IOException e){}
  } 


  public void Display(){
    int i;
    Node tmp=new Node();
    if(this.list.isEmpty())
    {
      System.out.println("The directory is empty!");
    }
    else
    {
      for(i=0;i<list.size();i++)
      {
      tmp=(Node)this.list.get(i);
      System.out.println(tmp.Name+" "+tmp.Cellphone+" "+tmp.Fixedphone+" "+tmp.Email);	
      }
    }
  }


  public static void main(String[] args)throws IOException{
    int option;
    int i=1;
    Directory List=new Directory();
    try
    {
      List.ReadFromFile("directory.txt");
      System.out.println("                   Welcome!                       ");
      System.out.println("         The directory has been opened!           ");
      while(i==1)
      {
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println(" 1.Add 2.Delete 3.Modify 4.Save 5.display 6.Exit");
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println("Please enter an option:");
        BufferedReader io=new BufferedReader(new InputStreamReader(System.in));
        option=Integer.valueOf((String)io.readLine()).intValue();
        switch(option)
        {
          case 1:{
                   System.out.println(" ");
                   System.out.println("***********************************************");
                   List.Add();
                   System.out.println(" ");
                   System.out.println("Successful adding!");
                   System.out.println("Please save the record!");
                   System.out.println("***********************************************");
                   System.out.println(" ");
                   System.out.println("You can continue using!");
                   break;
                 }
          case 2:{
                   System.out.println(" ");
                   System.out.println("***********************************************");
                   List.Delete();    
                   System.out.println("***********************************************");
                   System.out.println(" ");
                   System.out.println("You could continue using!");
                   break;
                 }
          case 3:{
                   System.out.println(" ");
                   System.out.println("***********************************************");
                   List.Modify();
                   System.out.println("***********************************************");
                   System.out.println(" ");
                   System.out.println("You could continue using!");
                   break;
                 }
          case 4:{
                   System.out.println(" ");
                   System.out.println("***********************************************");
                   List.Save("directory.txt");
                   System.out.println("Successful saving!");
                   System.out.println("***********************************************");
                   System.out.println(" ");
                   System.out.println("You could continue using!");
                   break;
                 }
          case 5:{
                   System.out.println(" ");
                   System.out.println("***********************************************");
                   System.out.println("The directory list:");
                   List.Display();
                   System.out.println("***********************************************");
                   System.out.println(" ");
                   System.out.println("You could continue using!");
                   break;
                 }
          case 6:{
                   System.out.println(" ");
                   System.out.println("***********************************************");
                   System.out.println("Thanks for using!");
                   System.out.println("***********************************************");
                   System.out.println(" ");
                   i=0;
                   break;
                 }
        }
      }
    }catch(IOException e){}
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -