Menu

Project Codes Codes Class Mailitem Class Model Simple Mail Item Item Sender Recipient Addr Q43907435

These are the project codes,

Codes for class MailItem

/**
* A class to model a simple mail item. The item has sender andrecipient
* addresses and a message string.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.02.29
*/
public class MailItem
{
    // The sender of the item.
    private String from;
    // The intended recipient.
    private String to;
    // The text of the message.
    private String message;

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

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

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

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

    /**
     * Print this mail message to the textterminal.
     */
    public void print()
    {
       System.out.println(“From: ” + from);
        System.out.println(“To:” + to);
       System.out.println(“Message: ” + message);
    }
}

Codes for class MailClient

/**
* A class to model a simple email client. The client is run bya
* particular user, and sends and retrieves mail via a particularserver.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.02.29
*/
public class MailClient
{
    // The server used for sending andreceiving.
    private MailServer server;
    // The user running this client.
    private String user;

    /**
     * Create a mail client run by user andattached to the given server.
     */
    public MailClient(MailServer server, Stringuser)
    {
        this.server =server;
        this.user = user;
    }

    /**
     * Return the next mail item (if any) forthis user.
     */
    public MailItem getNextMailItem()
    {
        returnserver.getNextMailItem(user);
    }

    /**
     * Print the next mail item (if any) forthis 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 givenrecipient via
     * the attached mail server.
     * @param to The intended recipient.
     * @param message The text of the messageto be sent.
     */
    public void sendMailItem(String to, Stringmessage)
    {
        MailItem item = newMailItem(user, to, message);
        server.post(item);
    }
}

Codes for class MailServer

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;

/**
* A simple model of a mail server. The server is able toreceive
* mail items for storage, and deliver them to clients ondemand.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.02.29
*/
public class MailServer
{
    // Storage for the arbitrary number of mailitems to be stored
    // on the server.
    private List<MailItem> items;

    /**
     * Construct a mail server.
     */
    public MailServer()
    {
        items = newArrayList<>();
    }

    /**
     * Return how many mail items are waitingfor a user.
     * @param who The user to check for.
     * @return How many items arewaiting.
     */
    public int howManyMailItems(String who)
    {
        int count = 0;
        for(MailItem item :items) {
           if(item.getTo().equals(who)) {
               count++;
           }
        }
        return count;
    }

    /**
     * Return the next mail item for a user ornull if there
     * are none.
     * @param who The user requesting theirnext item.
     * @return The user’s next item.
     */
    public MailItem getNextMailItem(Stringwho)
    {
        Iterator<MailItem>it = items.iterator();
        while(it.hasNext()){
           MailItem item = it.next();
           if(item.getTo().equals(who)) {
               it.remove();
               return item;
           }
        }
        return null;
    }

    /**
     * Add the given mail item to the messagelist.
     * @param item The mail item to be storedon the server.
     */
    public void post(MailItem item)
    {
        items.add(item);
    }
}

These are the three class and below is the question

Q2. Mail system (70%). Improve the mail system project to enable to send email to users on another mail server. The project c

Q2. Mail system (70%). Improve the mail system project to enable to send email to users on another mail server. The project code “mail-system” can be found in the chapter 3 in the course project folder. • Verify that the original project is unable to send emails between servers. Open this project in Blue), create two mail servers and also create two users on server 1, and a third user on server 2. Send two messages from user 1 to user 2 (on server 1) and user 3 (on server 2), respectively. Check whether user 2 and user 3 can receive the message. You’ll find user 2 can receive the message, but user 3 can’t. Read through the source code and find out why. Write your explanation. • Improve the project to enable sending emails between users on different servers. Copy the project files folder to a new folder named mail-system-v2 and program based on this folder. Hints: In order to send emails across servers, you’ll also need to send a copy of the message to the mail server of the receiver. One way to achieve this is to also post the message item to the destination mail server in the method send Mailltem in the class MailClient. The signature of the method send Mailltem should be changed to “public void send Mailltem(MailClient to. String message)”, that is when your send a message, choose the mail client object instead of the String of user name. Also, in the class MailClient, add two getters (getServer and getUser) to return mail server object and user name respectively. In class MailServer, change the type of the parameters who in methods howManyMailItems and howManyMailItems. Lastly, in class Mailltem, change the type of members from and to from String to MailClient. Modify print method to be able to print reasonable information. • Repeat the similar testing procedure in step 1), see how it works. Show transcribed image text Q2. Mail system (70%). Improve the mail system project to enable to send email to users on another mail server. The project code “mail-system” can be found in the chapter 3 in the course project folder. • Verify that the original project is unable to send emails between servers. Open this project in Blue), create two mail servers and also create two users on server 1, and a third user on server 2. Send two messages from user 1 to user 2 (on server 1) and user 3 (on server 2), respectively. Check whether user 2 and user 3 can receive the message. You’ll find user 2 can receive the message, but user 3 can’t. Read through the source code and find out why. Write your explanation. • Improve the project to enable sending emails between users on different servers. Copy the project files folder to a new folder named mail-system-v2 and program based on this folder. Hints: In order to send emails across servers, you’ll also need to send a copy of the message to the mail server of the receiver. One way to achieve this is to also post the message item to the destination mail server in the method send Mailltem in the class MailClient. The signature of the method send Mailltem should be changed to “public void send Mailltem(MailClient to. String message)”, that is when your send a message, choose the mail client object instead of the String of user name. Also, in the class MailClient, add two getters (getServer and getUser) to return mail server object and user name respectively. In class MailServer, change the type of the parameters who in methods howManyMailItems and howManyMailItems. Lastly, in class Mailltem, change the type of members from and to from String to MailClient. Modify print method to be able to print reasonable information. • Repeat the similar testing procedure in step 1), see how it works.

Expert Answer


Answer to These are the project codes, Codes for class MailItem /** * A class to model a simple mail item. The item has sender and…

OR