Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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.
- *
- * Exercise 3.52
- * Add a subject line.
- *
- * @author David J. Barnes and Michael Kölling
- * @version 2016.02.29
- */
- public class MailClient
- {
- // The server used for sending and receiving.
- private MailServer server;
- // The user running this client.
- private String user;
- /**
- * 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);
- }
- /**
- * 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 subject, String message)
- {
- MailItem item = new MailItem(user, to, subject, message);
- server.post(item);
- }
- }
- /**
- * A class to model a simple mail item. The item has sender and recipient
- * addresses and a message string.
- *
- * Exercise 3.54
- * Add a subject line to print out
- *
- * @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;
- // The subject of the message.
- private String subject;
- /**
- * 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 message, String subject)
- {
- this.from = from;
- this.to = to;
- this.message = message;
- this.subject = subject;
- }
- /**
- * @return The sender of this message.
- */
- public String getFrom()
- {
- return from;
- }
- /**
- * @return The intended recipient of this message.
- */
- public String getTo()
- {
- return to;
- }
- /**
- * @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: " + subject);
- System.out.println("Message: " + message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement