Guest User

Untitled

a guest
Jan 25th, 2020
355
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * A class to model a simple email client. The client is run by a
  3.  * particular user, and sends and retrieves mail via a particular server.
  4.  *
  5.  * Exercise 3.52
  6.  * Add a subject line.
  7.  *
  8.  * @author David J. Barnes and Michael Kölling
  9.  * @version 2016.02.29
  10.  */
  11. public class MailClient
  12. {
  13.     // The server used for sending and receiving.
  14.     private MailServer server;
  15.     // The user running this client.
  16.     private String user;
  17.  
  18.     /**
  19.      * Create a mail client run by user and attached to the given server.
  20.      */
  21.     public MailClient(MailServer server, String user)
  22.     {
  23.         this.server = server;
  24.         this.user = user;
  25.     }
  26.  
  27.     /**
  28.      * Return the next mail item (if any) for this user.
  29.      */
  30.     public MailItem getNextMailItem()
  31.     {
  32.         return server.getNextMailItem(user);
  33.     }
  34.  
  35.     /**
  36.      * Print the next mail item (if any) for this user to the text
  37.      * terminal.
  38.      */
  39.     public void printNextMailItem()
  40.     {
  41.         MailItem item = server.getNextMailItem(user);
  42.         if(item == null) {
  43.             System.out.println("No new mail.");
  44.         }
  45.         else {
  46.             item.print();
  47.         }
  48.     }
  49.  
  50.     /**
  51.      * Send the given message to the given recipient via
  52.      * the attached mail server.
  53.      * @param to The intended recipient.
  54.      * @param message The text of the message to be sent.
  55.      */
  56.     public void sendMailItem(String to, String subject, String message)
  57.     {
  58.         MailItem item = new MailItem(user, to, subject, message);
  59.         server.post(item);
  60.     }
  61. }
  62.  
  63. /**
  64.  * A class to model a simple mail item. The item has sender and recipient
  65.  * addresses and a message string.
  66.  *
  67.  * Exercise 3.54
  68.  * Add a subject line to print out
  69.  *
  70.  * @author David J. Barnes and Michael Kölling
  71.  * @version 2016.02.29
  72.  */
  73. public class MailItem
  74. {
  75.     // The sender of the item.
  76.     private String from;
  77.     // The intended recipient.
  78.     private String to;
  79.     // The text of the message.
  80.     private String message;
  81.     // The subject of the message.
  82.     private String subject;
  83.  
  84.     /**
  85.      * Create a mail item from sender to the given recipient,
  86.      * containing the given message.
  87.      * @param from The sender of this item.
  88.      * @param to The intended recipient of this item.
  89.      * @param message The text of the message to be sent.
  90.      */
  91.     public MailItem(String from, String to, String message, String subject)
  92.     {
  93.         this.from = from;
  94.         this.to = to;
  95.         this.message = message;
  96.         this.subject = subject;
  97.     }
  98.  
  99.     /**
  100.      * @return The sender of this message.
  101.      */
  102.     public String getFrom()
  103.     {
  104.         return from;
  105.     }
  106.  
  107.     /**
  108.      * @return The intended recipient of this message.
  109.      */
  110.     public String getTo()
  111.     {
  112.         return to;
  113.     }
  114.  
  115.     /**
  116.      * @return The text of the message.
  117.      */
  118.     public String getMessage()
  119.     {
  120.         return message;
  121.     }
  122.  
  123.     /**
  124.      * Print this mail message to the text terminal.
  125.      */
  126.     public void print()
  127.     {
  128.         System.out.println("From: " + from);
  129.         System.out.println("To: " + to);
  130.         System.out.println("Subject: " + subject);
  131.         System.out.println("Message: " + message);
  132.     }
  133. }
RAW Paste Data