2016-09-30 24 views
0

我正在嘗試讀取文件,其中每行是由分隔的條目;例如: bob:[email protected][email protected]使用java地圖搜索名稱和電子郵件並設置

AddressBookApp.java應該允許用戶選擇文件,搜索的方式是通過名稱或電子郵件。

Entry類:

public class Entry 
{ 
    public String keyName; 
    public List<String> emailsList; 


    public void Entry(String name, List<String> emails) 
    { 
     this.keyName = name; 
     this.emailsList = emails; 


     System.out.println("The name is:"+keyName); 
     System.out.println("Emails:" + emailsList +"\n"); 
    } 
} 

AddressBook.java

public class AddressBook 
{ 
    private static Set<Entry> entryList; 
    private static Entry one; 

    public void AddressBook(Entry entry) 
    { 
     entryList = new HashSet<Entry>(); 
     this.one = entry; 

     entryList.add(one); 
    } 

    public void SearchByName(String name) 
    { 
     String serchName = name; 

     for(Entry e: entryList) 
     { 
      // How to extract the name and list of emails of e 
     } 
    } 
} 

AddressBookApp.java

public class AddressBookApp 
{ 
    /** Used to obtain user input. */ 
    private static Scanner input = new Scanner(System.in); 
    private static Entry singleEnt; 
    public static void main(String[] args) 
    { 
     String fileName, entryName; 

     System.out.print("Enter address book filename: "); 
     fileName = input.nextLine(); 

     try 
     { 
      AddressBook addressBook = readAddressBook(fileName); 
      showMenu(addressBook); 
     } 
     catch(IOException e) 
     { 
      System.out.println("Could not read from " + fileName + ": " + e.getMessage()); 
     } 
    } 

    /** 
    * Read the address book file, containing all the names and email addresses. 
    * 
    * @param fileName The name of the address book file. 
    * @return A new AddressBook object containing all the information. 
    * @throws IOException If the file cannot be read. 
    */ 
    private static AddressBook readAddressBook(String fileName) throws IOException 
    { 
     AddressBook addressBook = new AddressBook(); 
     singleEnt = new Entry(); 
     List<String> emails = new ArrayList<String>(); 

     BufferedReader reader = new BufferedReader(new FileReader(fileName)); 
     String line = reader.readLine(); 
     while(line != null) 
     { 
      String[] parts = line.split(":"); 
      for(int i = 1; i < parts.length; i++) 
       emails.add(parts[i]); 

      singleEnt.Entry(parts[0], emails); 
      // Insert your code here to add a new address book entry. 
      // Note: 
      // parts[0] contains the person's name. 
      // parts[1], parts[2], etc. contain the person's email address(es). 
      addressBook.AddressBook(singleEnt); 
      line = reader.readLine(); 
     } 
     reader.close(); 

     return addressBook; 
    } 

    /** 
    * Show the main menu, offering the user options to (1) search entries by 
    * name, (2) search entries by email, or (3) quit. 
    * 
    * @param addressBook The AddressBook object to search. 
    */ 
    private static void showMenu(AddressBook addressBook) 
    { 
     boolean done = false; 
     while(!done) 
     { 
      int option; 
      System.out.println("(1) Search by name, (2) Search by email, (3) Quit"); 

      try 
      { 
       switch(Integer.parseInt(input.nextLine())) 
       { 
        case 1: 
         System.out.print("Enter name: "); 
         String name = input.nextLine(); 

         // Insert your code here to find an entry by name and display it. 
         break; 

        case 2: 
         System.out.print("Enter email address: "); 
         String email = input.nextLine(); 

         // Insert your code here to find an entry by email and display it. 
         break; 

        case 3: 
         done = true; 
         break; 
       } 
      } 
      catch(NumberFormatException e) 
      { 
       // The user entered something non-numerical. 
       System.out.println("Enter a number"); 
      } 
     } 
    } 
} 

回答

相關問題