Stage 3: In the third file Mailbox.java, create a class named Mailbox that model
ID: 3723056 • Letter: S
Question
Stage 3: In the third file Mailbox.java, create a class named Mailbox that models a mailbox that contains a list of email messages. It should contain the following:
• Private instance variables messages and numOfMessages
Hint: the type for messages should be ArrayList <Message>
• public Mailbox() A default constructor to initialize two instance variables
• public void addMessage(Message m) Add a new message into the list messages, and update the numOfMessages
• public Message getMessage(int i) Return the ith message from the list
• public int getNumberOfMessages() Return the number of messages in mailbox
• public void removeMessage(int i) Remove the ith message from the list
• public void printAllMessages() Print out all messages in the mailbox
Stage 4: In the fourth file MailboxTester.java, you will need to do the followings:
1. Create an object of Mailbox type.
2. Read all eight messages into Mailbox object from the file of “messages1.txt”. The values for each line will be used to initialize each instance variable of the Message object, and then added into the Mailbox object. Important: You must read the values in from the file using Scanner object, you are not allowed to initialize the objects manually.
3. Print out all messages using printAllMessages() method. Your output should look like Figure 2
4. Remove message at position 0 (index) using removeMessage(), and repeat it six times.
5. Print out all messages in the mailbox object using printAllMessages() method.
Your output should look like Figure 3.
Optbiars Message, list in the mailbox #fter loading from file Hessage 1 From: Eric A Dially o: achary_B_Howard This is test nessage 1 Mesaage 2 From: Brett V Bohorque:z To: Narcus R_HcPartland Ih18 1s_test_nessage_2 Message 3 rom: Aron_I_Brewster To: Patrick C Nauman This 1s_test nessage 3 Message 4 Fram: Joshua-J_Dennis To: Jacob_G_Neff This is test nessage 4 Hessage5 From: Stephen N Gentzler To: Kevin A_Orlowski Th1a_is_test neasage 5 Message 6 rom: Adam_ Janes D Geosits To: Oscar J_Phillips Th1s 1s-test nessage.6 Message 7 Fram: Rudolf C_Gouker To: Andy_R_Sayer Thisis_test nessage 7 Mesaage8 From: Zachary B_Howard To: Cra19 Smith This_is_test nessage 8Explanation / Answer
Hi... Please check below java code.
Mailbox.java
import java.util.List;
public class Mailbox {
private List<Message> all;
private int numOfMessages;
public List<Message> getAll() {
return all;
}
public void setAll(List<Message> all) {
this.all = all;
}
public int getNumOfMessages() {
return numOfMessages;
}
public void setNumOfMessages(int numOfMessages) {
this.numOfMessages = numOfMessages;
}
public Mailbox(List<Message> all, int numOfMessages) {
super();
this.all = all;
this.numOfMessages = numOfMessages;
}
public Message getMessage(int i){
return all.get(i-1);
}
public void removeMessages(int i){
all.remove(i-1);
}
public void printAllMessages(){
for(Message m:all){
System.out.println(m.getMessage());
}
}
}
Message.java
public class Message {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Message(String message) {
super();
this.message = message;
}
}
MailboxTester.java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MailboxTester {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner sc= new Scanner(System.in);
BufferedReader br = new BufferedReader(new FileReader("c:/Santhosh/abc.txt"));
String line = "";
StringBuffer sb = new StringBuffer();
int i=0;
List<Message> ls = new ArrayList<Message>();
Mailbox mb = null;
while( (line = br.readLine())!=null){
if(i==0){
sb.append(line+" ");
}else if(line.startsWith("Message")){
String ab = sb.toString();
Message m = new Message(ab);
ls.add(m);
//System.out.println("Hellooo");
sb = new StringBuffer();
sb.append(line+" ");
//System.out.println(sb);
}else{
sb.append(line+" ");
}
i++;
}
if(line==null){
String ab = sb.toString();
Message m = new Message(ab);
ls.add(m);
}
mb = new Mailbox(ls, ls.size());
mb.printAllMessages();
}
}
Text file:
Message 1
From: Chilly
To: Red chilly
This is text message 1
Message 2
From: Chilly
To: Red chilly mkster
This is text message 2
Output:
Message 1
From: Chilly
To: Red chilly
This is text message 1
Message 2From: Chilly
To: Red chilly mkster
This is text message 2
Please test the code and let me know any issues. Thank you. All the best.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.