2014-02-07 72 views
0

我在項目中的以下類別:如何使收件人的名稱對郵件系統不區分大小寫?

  • 郵件服務器
  • 的MailClient
  • 的MailItem

我有那麼它使用一個HashMap,而不是存儲 MailItems修改郵件服務器一個ArrayList。 HashMap的鍵必須是收件人的名稱, ,每個值必須是包含爲該收件人存儲的所有MailItems的ArrayList。收件人的名稱必須是不區分大小寫的,即「paul」和「Paul」和「PAUL」都是同一個人。

我不確定如何或從哪裏開始設置郵件系統,收件人的姓名不區分大小寫。將不勝感激任何幫助。謝謝。

下面是我的源代碼:

 import java.util.ArrayList; 
     import java.util.List; 
     import java.util.Iterator; 
     import java.util.HashMap; 

     /** 
* A simple model of a mail server. The server is able to receive 
* mail items for storage, and deliver them to clients on demand. 
* 
* @author David J. Barnes and Michael Kölling 
* @version 2011.07.31 
*/ 
public class MailServer 
{ 
    // Storage for the arbitrary number of mail items to be stored 
    // on the server. 
    private HashMap<String, ArrayList<MailItem>> items; 
    /** 
    * Construct a mail server. 
    */ 
    public MailServer() 
    { 
     items = new HashMap<String, ArrayList<MailItem>>(); 

    } 

    /** 
    * Return how many mail items are waiting for a user. 
    * @param who The user to check for. 
    * @return How many items are waiting. 
    */ 
    public int howManyMailItems(String who) 
    { 
     int count = 0; 
     for(String name : items.keySet()) { 
      if(who != null) { 
       who = formatName(who); 
      } 
      if(items.containsKey(who)) { 
       count ++; 
      } 
     } 
     return count; 
    } 

    /** 
    * Return the next mail item for a user or null if there 
    * are none. 
    * @param who The user requesting their next item. 
    * @return The user's next item. 
    */ 
    public MailItem getNextMailItem(String who) 
    { 
     if(who != null) { 
      who = formatName(who); 
     } 
     ArrayList<MailItem> mails = items.get((who)); 
     if(mails == null) { 
      return null; 
     } 
     Iterator<MailItem> it = mails.iterator(); 
     while(it.hasNext()) { 
      MailItem mail = it.next(); 
      if(mail.getTo().equals(who)) { 
       it.remove(); 
       return mail; 
      } 
     } 
     return null; 

    } 

    /** 
    * Add the given mail item to the message list. 
    * @param item The mail item to be stored on the server. 
    */ 
    public void post(MailItem item) 
    { 
     String who = item.getTo(); 
     if(who != null) { 
      who = formatName(who); 
     } 
     if(!items.containsKey(who)) { 
      items.put(who, new ArrayList<MailItem>()); 
     } 
     items.get(who).add(item); 
    } 

    private static String formatName(String who) { 
     if(who.length() > 0) { 
      return who.toLowerCase(); 
     } 
     return ""; 
    } 
} 
/** 
* A class to model a simple email client. The client is run by a 
* particular user, and sends and retrieves mail via a particular server. 
* 
* @author David J. Barnes and Michael Kölling 
* @version 2011.07.31 
*/ 
public class MailClient 
{ 
    // The server used for sending and receiving. 
    private MailServer server; 
    // The user running this client. 
    private String user; 

    /** 
    * Create a mail client run by user and attached to the given server. 
    */ 
    public MailClient(MailServer server, String user) 
    { 
     this.server = server; 
     this.user = user; 
    } 

    /** 
    * Return the next mail item (if any) for this user. 
    */ 
    public MailItem getNextMailItem() 
    {    
     return server.getNextMailItem(user); 
    } 

    /** 
    * Print the next mail item (if any) for this user to the text 
    * terminal. 
    */ 
    public void printNextMailItem() 
    { 
     MailItem item = server.getNextMailItem(user); 
     if(item == null) { 
      System.out.println("No new mail."); 
     } 
     else { 
      item.print(); 
     } 
    } 

    /** 
    * Send the given message to the given recipient via 
    * the attached mail server. 
    * @param to The intended recipient. 
    * @param message The text of the message to be sent. 
    */ 
    public void sendMailItem(String to, String subject, String message) 
    { 
     MailItem item = new MailItem(user, to, subject, message); 
     server.post(item); 
    } 
} 

回答

0

如果我正確地理解你的問題,收件人必須是不區分大小寫的,他們是被用作在HashMap鍵。

爲什麼不直接在String上使用toLowerCase函數,並將該版本的收件人作爲密鑰?這樣,「PAUL」,「Paul」和「paul」都會變成「paul」,以便查找。

您可以通過在使用它的任何函數的開始處降低名稱輸入來應用此操作。

+0

感謝您的幫助。是的,你知道了。我嘗試使用你的方法,但它仍然無法正常工作。我重新發布了我的修訂代碼。當我使用區分大小寫的輸入創建MailClient對象來向對方發​​送電子郵件時。無法爲收件人顯示郵件。有任何想法嗎? – user3078337

+0

您的更新添加了一個小寫名稱的函數,但隨後拋棄了結果 - 「who'沒有通過傳入進行修改,您必須執行'who = formatName(who)'。作爲另一個說明,最好從原始問題清楚地標記代碼的更新... – Krease

+0

您的更新還沒有使用'howManyMailItems'函數中的格式化名稱 – Krease

0

我只是將所有電子郵件地址爲小寫:

String getEmailAddress(String emailAddress) { 
    if (emailAddress.length() > 0) return emailAddress.toLowerCase(); 
    return "" 
} 

希望這有助於。

+0

謝謝,是的,這確實爲MailServer類工作,但我認爲還有一些代碼的其他問題。 – user3078337

相關問題