Advertisement
Guest User

Untitled

a guest
Oct 1st, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Iterator;
  4. import java.util.HashMap;
  5.  
  6. /**
  7. * A simple model of a mail server. The server is able to receive
  8. * mail items for storage, and deliver them to clients on demand.
  9. *
  10. * @author David J. Barnes and Michael Kölling
  11. * @version 2011.07.31
  12. */
  13. public class MailServer
  14. {
  15. // Storage for the arbitrary number of mail items to be stored
  16. // on the server.
  17. private HashMap<String, ArrayList<MailItem>> mailServer;
  18.  
  19. /**
  20. * Construct a mail server.
  21. */
  22. public MailServer()
  23. {
  24. mailServer = new HashMap<String, ArrayList<MailItem>>();
  25. }
  26.  
  27. /**
  28. * Create a mailbox for a single user
  29. * @param the name of the new user
  30. * @return 1 if the creation succeeded
  31. * 0 if a mailbox with that same name already exists
  32. */
  33. public int createMailbox(String user)
  34. {
  35. if (mailServer.containsKey(user)) {
  36. return 0;
  37. }
  38. else {
  39. ArrayList<MailItem> mailBox = new ArrayList<MailItem>();
  40. mailServer.put(user, mailBox);
  41. return 1;
  42. }
  43. }
  44.  
  45. /**
  46. * Create mailboxes for users
  47. * @param an array containing the names of the new users
  48. * @return the number of successfully created mailboxes
  49. * this number will be less than the number of
  50. * specified users if some mailboxes already existed
  51. */
  52. public int createMailbox(String[] users)
  53. {
  54. int oldSize = mailServer.size();
  55. for (String u : users) {
  56. ArrayList<MailItem> mailBox = new ArrayList<MailItem>();
  57. mailServer.put(u, mailBox);
  58. }
  59. return mailServer.size() - oldSize;
  60. }
  61.  
  62. /**
  63. * Retrieves the mailbox for a user
  64. *
  65. * @param who the owner of the requested mailbox
  66. * @return the mailbox belonging to who,
  67. * null if it does not exist
  68. */
  69. private ArrayList getMailbox(String who)
  70. {
  71. return mailServer.get(who);
  72. }
  73.  
  74. /**
  75. * Return how many mail items are waiting for a user.
  76. * @param who The user to check for.
  77. * @return How many items are waiting.
  78. */
  79. public int howManyMailItems(String who)
  80. {
  81. return getMailbox(who).size();
  82. }
  83.  
  84. /**
  85. * Return the next mail item for a user or null if there
  86. * are none.
  87. * @param who The user requesting their next item.
  88. * @return The user's next item.
  89. */
  90. public MailItem getNextMailItem(String who)
  91. {
  92. Iterator<MailItem> it = getMailbox(who).iterator();
  93. while(it.hasNext())
  94. {
  95. MailItem item = it.next();
  96. it.remove();
  97. return item;
  98. }
  99. return null;
  100. }
  101.  
  102. private boolean ignore(MailItem item)
  103. {
  104. return (item.getFrom().equals("") || item.getTo().equals(""));
  105. }
  106.  
  107. /**
  108. * Add the given mail item to the message list.
  109. * @param item The mail item to be stored on the server.
  110. */
  111. public void post(MailItem item)
  112. {
  113. if (!ignore(item))
  114. {
  115. for (String rec : item.getTo()) {
  116. if (mailServer.containsKey(rec)) {
  117. getMailbox(rec).add(0, item);
  118. }
  119. }
  120. }
  121. }
  122.  
  123. public int howManyMessages()
  124. {
  125. int nMsg = 0;
  126. for (ArrayList<MailItem> mailBox : mailServer.values()) {
  127. nMsg = nMsg + mailBox.size();
  128. }
  129. return nMsg;
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement