Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to figure out how to print all the messages in the mailServer. Can a

ID: 3552162 • Letter: I

Question

I am trying to figure out how to print all the messages in the mailServer. Can anyone help me.

I keep getting an error on the secon keySet() I have bolded. the error message says

cannot find symbol - method keySet().

At the end I would like it to print out the recipiant and then the message associated with that person


public void printAllMessages()

{

for (String who : mailMap.keySet()) {

ArrayList<MailItem> internalList = mailMap.get(who);

  

for (String message : internalList.keySet()) {

internailList.addAll();

}

  

System.out.println(internalList);

}

}


Here is the whole code:


import java.util.ArrayList;

import java.util.List;

import java.util.Iterator;

import java.util.HashMap;

import java.util.HashSet;


/**

* 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>> mailMap;


/**

* Construct a mail server.

*/

public MailServer()

{

mailMap = 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;


if(mailMap.containsKey(who)) {

ArrayList<MailItem> items = mailMap.get(who);

count = items.size();

// count = mailMap.get(who).size();

}


return count;

}


public ArrayList<MailItem> getNextMailItems(String who, int howMany)

{

ArrayList<MailItem> itemsToReturn = new ArrayList<MailItem>();


if (mailMap.containsKey(who)) {

ArrayList<MailItem> internalList = mailMap.get(who);

Iterator<MailItem> it = internalList.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> internalList;


if (mailMap.containsKey(who))

{

internalList = mailMap.get(who);

}

else{

internalList = new ArrayList<MailItem>();

mailMap.put(who, internalList);

}


internalList.add(item);

}


/**

* return true if the item is spam; otherwise return false

*/

private boolean isSpam(MailItem item)

{

if (item.getMessage().contains("viagra")) {

return true;

}

if (item.getSubject().contains("SPAM")) {

return true;

}

if (item.getMessage().contains("Viagra")) {

return true;

}

return false;

}


/**

* print all messages method

*/

public void printAllMessages()

{

for (String who : mailMap.keySet()) {

ArrayList<MailItem> internalList = mailMap.get(who);

internalList = new ArrayList<>();


for (int i = 0; i < mailMap.size(); i++) {

//internalList.add(mailMap.get(i));

System.out.println(MailItem.getTo() + MailItem.getFrom() +

MailItem.getSubject() + MailItem.getMessage());

}

  

}

}


}

Explanation / Answer

Modified Function

Error was that you were using keyset() for arraylist object whereas there is no keyset() for arraylist object

Two alternatives for what you need


Alternative 1

public void printAllMessages()

{

for (String who : mailMap.keySet()) {

ArrayList<MailItem> internalList = mailMap.get(who);

//internalList = new ArrayList<>();


for (int i = 0; i < internalList.size(); i++) {

   MailItem m = internalList.get(i);

System.out.println("TO: "+m.getTo() +", FROM: "+ m.getFrom() +", SUBJECT: "+ m.getSubject() +", MESSAGE: "+ m.getMessage());

}

  

}

}


Alternative 2

public void printAllMessages()

{

for (String who : mailMap.keySet()) {

ArrayList<MailItem> internalList = mailMap.get(who);

//internalList = new ArrayList<>();


for (int i = 0; i < internalList.size(); i++) {

MailItem m = internalList.get(i);

m.print();

}

  

}

}


MailServer.java

import java.util.ArrayList;

import java.util.List;

import java.util.Iterator;

import java.util.HashMap;

import java.util.HashSet;


/**

* 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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote