Java printAllMessages():遍历用户,打印出每个用户名,并打印出与该用户关联的所有电子邮件

Java printAllMessages():遍历用户,打印出每个用户名,并打印出与该用户关联的所有电子邮件,java,Java,向名为printAllMessages的MailServer类添加一个方法,该方法迭代用户,打印出每个用户名,并打印出与该用户关联的所有电子邮件 我不知道如何打印电子邮件的内容,这是在ArrayList表单中 这是我的密码: import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Iterator; import java.util.HashSet; import

向名为
printAllMessages
MailServer
类添加一个方法,该方法迭代用户,打印出每个用户名,并打印出与该用户关联的所有电子邮件

我不知道如何打印电子邮件的内容,这是在
ArrayList
表单中

这是我的密码:

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

/**
 * 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;
    }

    /**
     * Formats the name into lower case.
     * @param who The user to check for.
     */
    private static String formatName(String who)
    {
        if(who.length() > 0)
        {
            return who.toLowerCase();
        }
        return "";
    }

    /**
     * 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 item = it.next();
            if(item.getTo().equals(who)) 
            {
                it.remove();
                return item;
            }
        }
        return null;
    }

    /**
     * Return the specified number of mail items for a user or
     * null if there are none.
     * @param who The user requesting their next item.
     * @param howMany The number of mail items requested.
     * @return The user's specified number of next items.
     */
    public ArrayList<MailItem> getNextMailItems(String who, int howMany)
    {
        ArrayList<MailItem> itemsToReturn = new ArrayList<MailItem>();
        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() && howMany > 0)
        {
            MailItem item = it.next();
            it.remove();
            itemsToReturn.add(item);
            howMany--;
        }
        return itemsToReturn;
    }

    /**
     * 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)
    {
        if(isSpam(item))
        {
            return;
        }
        String who = item.getTo();
        ArrayList<MailItem> mails;
        if(items.containsKey(who))
        {
            mails = items.get(who);
        }
        else
        {
            mails = new ArrayList<MailItem>();
            items.put(who, mails);
        }
        mails.add(item);
    }

    /**
     * Return true if the item is spam; otherwise return false.
     * @param The mail item.
     */
    private boolean isSpam(MailItem item)
    {
        if(item.getSubjectLine().contains("SPAM"))
        {
            return true;
        }
        if (item.getMessage().toLowerCase().contains("viagra"))
        {
            return true;
        }
        return false;
    }

    /**
     * Iterates through the users, prints out each user name, and 
     * prints out all the emails associated with that user.
     */
    public void printAllMessages()
    {
        for(String who : items.keySet())
        {
            ArrayList<MailItem> mails = items.get(who); 
            for(MailItem message : mails)
            {
                System.out.println(who + ": " + mails);
            }

        }
    }
}
import java.util.ArrayList;
/**
 * 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;
    private int howMany;

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

    /**
     * Return the specified number of mail items (if any)
     * for this user.
     */
    public ArrayList<MailItem> getNextMailItems(int howMany)
    {
        return server.getNextMailItems(user, howMany);
    }

    /**
     * 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 subjectline, String message)
    {
        MailItem item = new MailItem(user, to, subjectline, message);
        server.post(item);
    }
}
/**
 * A class to model a simple mail item. The item has sender and recipient
 * addresses and a message string.
 * 
 * @author David J. Barnes and Michael Kölling
 * @version 2011.07.31
 */
public class MailItem
{
    // The sender of the item.
    private String from;
    // The intended recipient.
    private String to;
    // The text of the message.
    private String subjectline;
    // The subject line of the message.
    private String message;

    /**
     * Create a mail item from sender to the given recipient,
     * containing the given message.
     * @param from The sender of this item.
     * @param to The intended recipient of this item.
     * @param message The text of the message to be sent.
     */
    public MailItem(String from, String to, String subjectline, String message)
    {
        this.from = from;
        this.to = to;
        this.subjectline = subjectline;
        this.message = message;
    }

    /**
     * @return The sender of this message.
     */
    public String getFrom()
    {
        return from;
    }

    /**
     * @return The intended recipient of this message.
     */
    public String getTo()
    {
        return to.toLowerCase();
    }

    /**
     * @return The subject line of this message.
     */
    public String getSubjectLine()
    {
        return subjectline;
    }

    /**
     * @return The text of the message.
     */
    public String getMessage()
    {
        return message;
    }

    /**
     * Print this mail message to the text terminal.
     */
    public void print()
    {
        System.out.println("From: " + from);
        System.out.println("To: " + to);
        System.out.println("Subject Line: " + subjectline);
        System.out.println("Message: " + message);
    }
}
import java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Iterator;
导入java.util.HashSet;
导入java.util.Set;
/**
*邮件服务器的简单模型。服务器能够接收数据
*邮寄物品进行存储,并根据需要将其交付给客户。
* 
*@作者David J.Barnes和Michael Kölling
*@version 2011.07.31
*/
公共类邮件服务器
{
//用于存储任意数量的邮件项目的存储
//在服务器上。
私有HashMap项;
/**
*构造一个邮件服务器。
*/
公共邮件服务器()
{
items=newhashmap();
}
/**
*返回等待用户的邮件数量。
*@param谁是要检查的用户。
*@return等待的物品数量。
*/
public int howmanimalitems(字符串who)
{
整数计数=0;
for(字符串名称:items.keySet())
{
if(who!=null)
{
who=格式名称(who);
}
国际单项体育联合会(国际单项体育联合会(世卫组织))
{
计数++;
}
}
返回计数;
}
/**
*将名称格式化为小写。
*@param谁是要检查的用户。
*/
私有静态字符串formatName(字符串who)
{
if(who.length()>0)
{
返回who.toLowerCase();
}
返回“”;
}
/**
*为用户返回下一个邮件项目,如果存在,则返回null
*没有。
*@param请求下一个项目的用户。
*@返回用户的下一项。
*/
public MailItem getNextMailItem(字符串who)
{
if(who!=null)
{
who=格式名称(who);
}
ArrayList mails=items.get((who));
如果(邮件==null)
{
返回null;
}
Iterator it=mails.Iterator();
while(it.hasNext())
{
MailItem=it.next();
如果(item.getTo().equals(who))
{
it.remove();
退货项目;
}
}
返回null;
}
/**
*为用户或用户返回指定数量的邮件
*如果没有,则为null。
*@param请求下一个项目的用户。
*@param请求的邮件数量。
*@返回用户指定数量的下一个项目。
*/
公共ArrayList getNextMailItems(字符串who,int howMany)
{
ArrayList itemsToReturn=新建ArrayList();
if(who!=null)
{
who=格式名称(who);
}
ArrayList mails=items.get((who));
如果(邮件==null)
{
返回null;
}
Iterator it=mails.Iterator();
while(it.hasNext()&&howmount>0)
{
MailItem=it.next();
it.remove();
itemsToReturn.add(项目);
有多少——;
}
返回项目StoreTurn;
}
/**
*将给定的邮件项目添加到邮件列表中。
*@param item要存储在服务器上的邮件项目。
*/
公开作废邮件(邮件项目)
{
如果(isSpam(项目))
{
返回;
}
字符串who=item.getTo();
ArrayList邮件;
国际单项体育联合会(国际单项体育联合会(世卫组织))
{
邮件=项目。获取(谁);
}
其他的
{
邮件=新的ArrayList();
物品。放置(谁、邮件);
}
邮件。添加(项目);
}
/**
*如果项目是垃圾邮件,则返回true;否则返回false。
*@param是邮件项目。
*/
私有布尔isSpam(邮件项)
{
如果(item.getSubjectLine()包含(“垃圾邮件”))
{
返回true;
}
如果(item.getMessage().toLowerCase()包含(“伟哥”))
{
返回true;
}
返回false;
}
/**
*遍历用户,打印出每个用户名,然后
*打印出与该用户相关的所有电子邮件。
*/
public void printAllMessages()
{
for(字符串who:items.keySet())
{
ArrayList mails=items.get(who);
用于(邮件项消息:邮件)
{
System.out.println(who+“:”+邮件);
}
}
}
}
导入java.util.ArrayList;
/**
*用于对简单电子邮件客户端建模的类。客户端由一个
*并通过特定服务器发送和检索邮件。
* 
*@作者David J.Barnes和Michael Kölling
*@version 2011.07.31
*/
公共类邮件客户端
{
//用于发送和接收的服务器。
专用邮件服务器;
//运行此客户端的用户。
私有字符串用户;
私人int有多少;
/**
*创建由用户运行并连接到给定服务器的邮件客户端。
*/
公共邮件客户端(邮件服务器,字符串用户)
{
this.server=server;
this.user=用户;
}
/**
*返回此用户的下一个邮件项目(如果有)。
*/
public MailItem getNextMailItem()
{
返回server.getNextMailItem(用户);
}
/**
*返回指定数量的邮件(如果有)
*对于此用户。
*/
公共阵列列表getNextMailItems(整数多少)
{
return server.getNextMailItems(用户,数量);
}
/**
*将此用户的下一个邮件项目(如果有)打印到文本中
*终点站。
*/
public void printNextMailItem()
{
邮件一
Map<String, List<Integer>> map = new HashMap<String, List<Integer>>() {{
    put("a", Arrays.asList(0,1,2,3,4));
    put("b", Arrays.asList(5,6,7,8,9));
}};

    
for (Map.Entry<String, List<Integer>> entry: map.entrySet()) {
    System.out.println(entry.getKey() + " " + entry.getValue().toString());
}
System.out.println(map.get("a").toString());
List<String> list = new ArrayList<String>();
String string = "";
Ideone(String a) { string = a; }

public String getString() {
    return this.string;
}

public static void main (String[] args) throws java.lang.Exception
{
    Ideone idA = new Ideone("a");
    Ideone idB = new Ideone("b");
    
    Map<String,List<Ideone>> map = new HashMap();
    
    map.put("john",new ArrayList<Ideone>(Arrays.asList(idA, idB)));
    
    List<Ideone> stringList = map.get("john");
    
    for (Ideone id: stringList) {
        System.out.println(id.getString());
    }
}