简体   繁体   English

如何使收件人的名称对邮件系统不区分大小写?

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

I have the following classes in the project: 我在项目中有以下课程:

  • MailServer 邮件服务器
  • MailClient MailClient
  • MailItem MailItem

I have to modify the MailServer so that it uses a HashMap to store MailItems instead of an ArrayList. 我必须修改MailServer,以便它使用HashMap来存储MailItems而不是ArrayList。 The keys to the HashMap must be the names of the recipients, and each value must be an ArrayList containing all the MailItems stored for that recipient.The names of the recipients must be case-insensitive, ie “paul” and “Paul” and “PAUL” are all the same person. HashMap的键必须是收件人的名称,每个值都必须是ArrayList,其中包含为该收件人存储的所有MailItems。收件人的名称必须不区分大小写,即“ paul”,“ Paul”和“ PAUL”都是同一个人。

I'm not sure how or where to start for setting up the mail system where the names of the recipients are case insensitive. 我不确定收件人的名称不区分大小写的情况下如何或在何处开始设置邮件系统。 Would appreciate any help. 将不胜感激。 Thanks. 谢谢。

Below is my source code: 下面是我的源代码:

      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);  
    }
}

If I understand your question properly, recipients must be case-insensitive, and they are to be used as keys in a HashMap . 如果我正确理解了您的问题,那么收件人必须不区分大小写,并且将它们用作HashMap键。

Why not just use the toLowerCase function on String, and use that version of the recipient as the key? 为什么不只对String使用toLowerCase函数,并使用该版本的接收者作为键? That way, "PAUL", "Paul", and "paul" all get turned into "paul" for purposes of lookup. 这样,出于查询目的,“ PAUL”,“ Paul”和“ paul”都变成了“ paul”。

You'd apply this by lowercasing the name input at the beginning of any function that uses it. 您可以通过在使用它的任何函数的开头将名称输入小写来应用它。

I would just convert all email-addresses to lower case: 我只是将所有电子邮件地址都转换为小写:

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM