Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.ParseException;
- import java.time.LocalDate;
- import java.util.ArrayList;
- import java.util.Date;
- /**
- * LoanRequestQueue class, which deals with system for reserving resources which are currently all in use
- * @author John-Mark Strickland, W J Buchanan
- */
- public class LoanRequestQueue {
- private static ArrayList<Queue> queues = new ArrayList<>();
- private static Inventory inv = new Inventory();
- /**
- * Gets a list of all resources a user has requested
- * @param username The username to filter by
- * @return list of resource names
- */
- public static ArrayList<String> requestedItems(String username) {
- ArrayList<String> result = new ArrayList<String>();
- for (int i = 0; i < queues.size(); i ++) {
- Queue current = queues.get(i);
- if (current.containsUser(username)) {
- result.add(inv.getTitle(current.getResourceIDQueue()));
- }
- }
- return result;
- }
- /**
- * Gets a list of all resources a user has reserved (front of queue)
- * @param username The username to filter by
- * @return list of resource names
- */
- public static ArrayList<String> reservedItems(String username) {
- ArrayList<String> result = new ArrayList<String>();
- for (int i = 0; i < queues.size(); i ++) {
- Queue current = queues.get(i);
- if (((User)current.peek()).getUsername().equals(username)) {
- result.add(inv.getTitle(current.getResourceIDQueue()));
- }
- }
- return result;
- }
- /**
- * Removes the Front element of the queue
- * @param rID Specifies the queue to modify
- */
- public static void removeHead(String rID) {
- for (int i = 0; i < queues.size(); i ++) {
- if (rID.equals(queues.get(i).getResourceIDQueue())) {
- queues.get(i).dequeue();
- }
- }
- }
- /**
- * Removes an entire queue instance
- * @param rID Specifies queue to modify
- */
- public static void removeQueue(String rID) {
- for (int i = 0; i < queues.size(); i ++) {
- if (rID.equals(queues.get(i).getResourceIDQueue())) {
- queues.remove(i);
- }
- }
- }
- /**
- * Returns boolean of whether a queue exists
- * @param rID Specifies queue to search for
- * @return true if queue is found, false otherwise
- */
- public static boolean queueExists(String rID) {
- for (int i = 0; i < queues.size(); i ++) {
- if (rID.equals(queues.get(i).getResourceIDQueue())) {
- return true;
- }
- }
- return false;
- }
- /**
- * Returns the queue with a given rID
- * @param rID Specifies the queue to return
- * @return The queue with the matching rID
- */
- public static Queue getQueue(String rID) {
- for (int i = 0; i < queues.size(); i ++) {
- if (rID.equals(queues.get(i).getResourceIDQueue())) {
- return queues.get(i);
- }
- }
- return null;
- }
- /**
- * Alerts a user that the resource they have requested has been returned
- * and is ready to collect.
- * @param user The User to be alerted.
- * @param resourceID The Resource to which the Alert refers.
- */
- public static void alertUserReturn(User user, String resourceID){
- user.addAlert(new Alert(2, inv.getTitle(resourceID)));
- }
- /**
- * Alerts a user that a due date has been set on their resource as another
- * user has requested it.
- * @param user The User to be alerted.
- * @param resourceID The Resource to which the Alert refers.
- */
- public static void alertResourceNeedsReturing(User user, String resourceID){
- user.addAlert(new Alert(1, inv.getTitle(resourceID)));
- }
- /**
- * This function try's to either locate the queue if one has been created for a resource
- * or will create a new one if one has not been created
- * @param resourceID the ID to be checked against other queues
- * @param userProfile the profile of the user who requests a resource
- */
- public static void addResourceRequest(String resourceID, User userProfile) {
- if (!queueExists(resourceID)) {
- Queue queue = new Queue(resourceID);
- queue.enqueue(userProfile);
- queues.add(queue);
- } else {
- for (int i = 0; i < queues.size(); i++) {
- if (queues.get(i).getResourceIDQueue() == resourceID) {
- queues.get(i).enqueue(userProfile);
- }
- }
- }
- System.out.println("(true) Resource Requested: " + resourceID + " | " + userProfile.getUsername());
- //calculateDueDate(resourceID, userProfile);
- }
- public static void calculateDueDate(String resourceID, User userProfile) {
- //sets due date and notifies current loaner
- String loanID = LoanRecord.getLID(userProfile.getUsername(), resourceID);
- if(LoanRecord.getDueDate(loanID) == null) {
- Date date = LoanRecord.getDateSet(loanID);
- try {
- date = DateHandler.getDateFormat().parse(LocalDate.parse(date.toString()).
- plusDays(inv.getDuration(loanID)).toString());
- } catch (ParseException e) {
- e.printStackTrace();
- }
- boolean before = true;
- try {
- before = date.before(DateHandler.getDateFormat().parse(DateHandler.currentDateString()));
- } catch (ParseException e) {
- e.printStackTrace();
- }
- if (before) {
- LoanRecord.setDueDate(loanID, (LocalDate.parse(DateHandler.currentDateString()).
- plusDays(1)).toString());
- } else {
- LoanRecord.setDueDate(loanID, date.toString());
- }
- Alert alert = new Alert(1, resourceID);
- System.out.println("Alert sent to : " + userProfile.getUsername());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement