簡體   English   中英

如何使收件人的名稱對郵件系統不區分大小寫?

[英]How to make the names of the recipients case-insensitive for a mail system?

我在項目中有以下課程:

  • 郵件服務器
  • MailClient
  • MailItem

我必須修改MailServer,以便它使用HashMap來存儲MailItems而不是ArrayList。 HashMap的鍵必須是收件人的名稱,每個值都必須是ArrayList,其中包含為該收件人存儲的所有MailItems。收件人的名稱必須不區分大小寫,即“ 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);  
    }
}

如果我正確理解了您的問題,那么收件人必須不區分大小寫,並且將它們用作HashMap鍵。

為什么不只對String使用toLowerCase函數,並使用該版本的接收者作為鍵? 這樣,出於查詢目的,“ PAUL”,“ Paul”和“ paul”都變成了“ paul”。

您可以通過在使用它的任何函數的開頭將名稱輸入小寫來應用它。

我只是將所有電子郵件地址都轉換為小寫:

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

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM