// The main creates two different email message lists and
// creates messages to put in them.
public static void main(String[] args)
{
// Construct two MsgList objects and put them in variables
// called "inbox" and "Trash". Each should show 5
// messages at a time.
MsgList inbox = new MsgList();
MsgList trash = new MsgList();
// Create 3 Msg objects, one of which should be from
// you to Alyce or Pam, with the subject "testing" and
// the body "This is a test."
Msg msg1 = new Msg("abrady@kzoo.edu", "student@kzoo.edu",
"testing", "This is a test.");
Msg msg2 = new Msg("pcutter@kzoo.edu", "student@kzoo.edu",
"testing again", "This is another test.");
Msg msg3 = new Msg("abrady@kzoo.edu", "student@kzoo.edu",
"still testing", "This is a third test.");
// Add all 3 messages to your inbox. Then
// add them again to have 6 messages total.
inbox.receive(msg1);
inbox.receive(msg2);
inbox.receive(msg3);
inbox.receive(msg1);
inbox.receive(msg2);
inbox.receive(msg3);
// List the messages in your inbox starting with the first.
inbox.listMsgHeaders(0);
// Display the body of the 2nd message.
inbox.displayBody(1);
// List the messages in your inbox starting with the third.
inbox.listMsgHeaders(2);
// Move the 2nd message from your inbox to the Trash.
inbox.move(1, trash);
}
public class Msg
{
private String recipient;
private String sender;
private String subject;
private String body;
public Msg(String to, String from, String subject, String body)
{
this.recipient = to;
this.sender = from;
this.subject = subject;
this.body = body;
}
public String getToField()
{
return this.recipient;
}
public String getFromField()
{
return this.sender;
}
public String getSubject()
{
return this.subject;
}
public String getMsgBody()
{
return this.body;
}
}
public class MsgList
{
private int numToShow;
private ArrayList<Msg> msgList;
public MsgList(int numHeadersToShow)
{
this.numToShow = numHeadersToShow;
this.msgList = new ArrayList<Msg>();
}
public void receive(Msg newMsg)
{
this.msgList.add(newMsg);
}
public void displayBody(int msgNum)
{
System.out.println(this.msgList.get(msgNum).getMsgBody());
// OR:
// Msg whichMsg = this.msgList.get(msgNum);
// String body = whichMsg.getMsgBody();
// System.out.println(body);
}
public void listMsgHeaders(int startingWith)
{
// Determine start and end indices of messages to show.
int startIndex = startingWith;
int endIndex = startingWith + this.numToShow;
for ( int i = startIndex; i < endIndex; i++ )
{
Msg m = msgList.get(i);
System.out.println("To: " + m.getToField());
System.out.println("From: " + m.getFromField());
System.out.println("Subject: " + m.getSubject());
}
}
public void move(int msgNum, MsgList folder)
{
// Add the message to the other folder, then delete from this list.
folder.receive(this.msgList.get(msgNum));
this.msgList.remove(msgNum);
}
}