Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. import java.text.ParseException;
  2. import java.time.LocalDate;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5.  
  6. /**
  7. * LoanRequestQueue class, which deals with system for reserving resources which are currently all in use
  8. * @author John-Mark Strickland, W J Buchanan
  9. */
  10. public class LoanRequestQueue {
  11.  
  12. private static ArrayList<Queue> queues = new ArrayList<>();
  13. private static Inventory inv = new Inventory();
  14.  
  15. /**
  16. * Gets a list of all resources a user has requested
  17. * @param username The username to filter by
  18. * @return list of resource names
  19. */
  20. public static ArrayList<String> requestedItems(String username) {
  21. ArrayList<String> result = new ArrayList<String>();
  22. for (int i = 0; i < queues.size(); i ++) {
  23. Queue current = queues.get(i);
  24. if (current.containsUser(username)) {
  25. result.add(inv.getTitle(current.getResourceIDQueue()));
  26. }
  27. }
  28. return result;
  29. }
  30.  
  31. /**
  32. * Gets a list of all resources a user has reserved (front of queue)
  33. * @param username The username to filter by
  34. * @return list of resource names
  35. */
  36. public static ArrayList<String> reservedItems(String username) {
  37. ArrayList<String> result = new ArrayList<String>();
  38. for (int i = 0; i < queues.size(); i ++) {
  39. Queue current = queues.get(i);
  40. if (((User)current.peek()).getUsername().equals(username)) {
  41. result.add(inv.getTitle(current.getResourceIDQueue()));
  42. }
  43. }
  44. return result;
  45. }
  46.  
  47. /**
  48. * Removes the Front element of the queue
  49. * @param rID Specifies the queue to modify
  50. */
  51. public static void removeHead(String rID) {
  52. for (int i = 0; i < queues.size(); i ++) {
  53. if (rID.equals(queues.get(i).getResourceIDQueue())) {
  54. queues.get(i).dequeue();
  55. }
  56. }
  57. }
  58.  
  59. /**
  60. * Removes an entire queue instance
  61. * @param rID Specifies queue to modify
  62. */
  63. public static void removeQueue(String rID) {
  64. for (int i = 0; i < queues.size(); i ++) {
  65. if (rID.equals(queues.get(i).getResourceIDQueue())) {
  66. queues.remove(i);
  67. }
  68. }
  69. }
  70.  
  71. /**
  72. * Returns boolean of whether a queue exists
  73. * @param rID Specifies queue to search for
  74. * @return true if queue is found, false otherwise
  75. */
  76. public static boolean queueExists(String rID) {
  77. for (int i = 0; i < queues.size(); i ++) {
  78. if (rID.equals(queues.get(i).getResourceIDQueue())) {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84.  
  85. /**
  86. * Returns the queue with a given rID
  87. * @param rID Specifies the queue to return
  88. * @return The queue with the matching rID
  89. */
  90. public static Queue getQueue(String rID) {
  91. for (int i = 0; i < queues.size(); i ++) {
  92. if (rID.equals(queues.get(i).getResourceIDQueue())) {
  93. return queues.get(i);
  94. }
  95. }
  96. return null;
  97. }
  98.  
  99. /**
  100. * Alerts a user that the resource they have requested has been returned
  101. * and is ready to collect.
  102. * @param user The User to be alerted.
  103. * @param resourceID The Resource to which the Alert refers.
  104. */
  105. public static void alertUserReturn(User user, String resourceID){
  106. user.addAlert(new Alert(2, inv.getTitle(resourceID)));
  107. }
  108.  
  109. /**
  110. * Alerts a user that a due date has been set on their resource as another
  111. * user has requested it.
  112. * @param user The User to be alerted.
  113. * @param resourceID The Resource to which the Alert refers.
  114. */
  115. public static void alertResourceNeedsReturing(User user, String resourceID){
  116. user.addAlert(new Alert(1, inv.getTitle(resourceID)));
  117. }
  118.  
  119. /**
  120. * This function try's to either locate the queue if one has been created for a resource
  121. * or will create a new one if one has not been created
  122. * @param resourceID the ID to be checked against other queues
  123. * @param userProfile the profile of the user who requests a resource
  124. */
  125. public static void addResourceRequest(String resourceID, User userProfile) {
  126. if (!queueExists(resourceID)) {
  127. Queue queue = new Queue(resourceID);
  128. queue.enqueue(userProfile);
  129. queues.add(queue);
  130. } else {
  131. for (int i = 0; i < queues.size(); i++) {
  132. if (queues.get(i).getResourceIDQueue() == resourceID) {
  133. queues.get(i).enqueue(userProfile);
  134. }
  135. }
  136. }
  137. System.out.println("(true) Resource Requested: " + resourceID + " | " + userProfile.getUsername());
  138. //calculateDueDate(resourceID, userProfile);
  139. }
  140.  
  141. public static void calculateDueDate(String resourceID, User userProfile) {
  142.  
  143. //sets due date and notifies current loaner
  144. String loanID = LoanRecord.getLID(userProfile.getUsername(), resourceID);
  145. if(LoanRecord.getDueDate(loanID) == null) {
  146. Date date = LoanRecord.getDateSet(loanID);
  147. try {
  148. date = DateHandler.getDateFormat().parse(LocalDate.parse(date.toString()).
  149. plusDays(inv.getDuration(loanID)).toString());
  150. } catch (ParseException e) {
  151. e.printStackTrace();
  152. }
  153. boolean before = true;
  154. try {
  155. before = date.before(DateHandler.getDateFormat().parse(DateHandler.currentDateString()));
  156. } catch (ParseException e) {
  157. e.printStackTrace();
  158. }
  159. if (before) {
  160. LoanRecord.setDueDate(loanID, (LocalDate.parse(DateHandler.currentDateString()).
  161. plusDays(1)).toString());
  162. } else {
  163. LoanRecord.setDueDate(loanID, date.toString());
  164. }
  165. Alert alert = new Alert(1, resourceID);
  166. System.out.println("Alert sent to : " + userProfile.getUsername());
  167. }
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement