Skip to content

Commit

Permalink
Added CompareCommand.java for compare command
Browse files Browse the repository at this point in the history
  • Loading branch information
yellow-fellow committed Oct 13, 2020
1 parent 6c7d09b commit 4ad6022
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
96 changes: 96 additions & 0 deletions src/main/java/seedu/duke/command/CompareCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package seedu.duke.command;

import seedu.duke.exception.DukeException;
import seedu.duke.ui.Ui;
import seedu.duke.user.User;
import seedu.duke.user.UserList;
import java.util.ArrayList;
import java.util.Arrays;

import seedu.duke.task.Event;

public class CompareCommand extends Command{

public CompareCommand(String input) {
super(input);
}

@Override
public void execute(UserList users, Ui ui, User nowUser) throws DukeException {

ArrayList<Integer> outputArray = new ArrayList<Integer>(Arrays.asList(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23));

if (nowUser != null) {
String[] parsedInputs = input.split(" /", 3);
String targetName = parsedInputs[0].substring(1);
String date = parsedInputs[1];
ArrayList<Event> nowUserTimetable;
ArrayList<Event> targetUserTimetable;

User targetUser = users.getUserByName(targetName); //implement a function to return user from UserList based on name

if (targetUser == null){
throw new DukeException("No such user!");
}

switch (date) {
case "mon":
nowUserTimetable = nowUser.getTimetable().getTimetable("mon");
targetUserTimetable = targetUser.getTimetable().getTimetable("mon");
break;
case "tue":
nowUserTimetable = nowUser.getTimetable().getTimetable("tue");
targetUserTimetable = targetUser.getTimetable().getTimetable("tue");
break;
case "wed":
nowUserTimetable = nowUser.getTimetable().getTimetable("wed");
targetUserTimetable = targetUser.getTimetable().getTimetable("wed");
break;
case "thu":
nowUserTimetable = nowUser.getTimetable().getTimetable("thu");
targetUserTimetable = targetUser.getTimetable().getTimetable("thu");
break;
case "fri":
nowUserTimetable = nowUser.getTimetable().getTimetable("fri");
targetUserTimetable = targetUser.getTimetable().getTimetable("fri");
break;
case "sat":
nowUserTimetable = nowUser.getTimetable().getTimetable("sat");
targetUserTimetable = targetUser.getTimetable().getTimetable("sat");
break;
case "sun":
nowUserTimetable = nowUser.getTimetable().getTimetable("sun");
targetUserTimetable = targetUser.getTimetable().getTimetable("sun");
break;
default:
throw new DukeException("Sorry! I don't know what day you mean :-(");
}

/**
* Compare targetUser and nowUser Timetables (ArrayList<Object>)
* Assuming for each day of the week, timetable ArrayList<Object> contains only [0:23] timeslots
* Loop below returns an outputArray that holds the common available hours in that day
*/
for (Event event : nowUserTimetable){
int temp_x = (Integer.valueOf(Integer.parseInt(event.getTimeStart().substring(0,2))));
int temp_y = (Integer.valueOf(Integer.parseInt(event.getTimeEnd().substring(0,2))));

for (int i = temp_x; i<temp_y+1 ; i++){
outputArray.remove(Integer.valueOf(i));
}
}

for (Event event : targetUserTimetable){
int temp_x = (Integer.valueOf(Integer.parseInt(event.getTimeStart().substring(0,2))));
int temp_y = (Integer.valueOf(Integer.parseInt(event.getTimeEnd().substring(0,2))));

for (int i = temp_x; i<=temp_y ; i++){
outputArray.remove(Integer.valueOf(i));
}
}

ui.printCompare(outputArray);

}
}
}
4 changes: 4 additions & 0 deletions src/main/java/seedu/duke/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import seedu.duke.command.AddCommand;
import seedu.duke.command.ByeCommand;
import seedu.duke.command.ClearCommand;
import seedu.duke.command.CompareCommand;
import seedu.duke.command.Command;
import seedu.duke.command.DeleteCommand;
//import seedu.duke.command.DoneCommand;
Expand All @@ -27,6 +28,7 @@ public class Parser {
private static final String COMMAND_BYE = "bye";
private static final String COMMAND_LOGIN = "login";
private static final String COMMAND_ADD = "add";
private static final String COMMAND_COMPARE = "compare";


/**
Expand Down Expand Up @@ -68,6 +70,8 @@ public static Command parse(String input) throws DukeException {
return new AddCommand(parsedInputs[1]);
case COMMAND_BYE:
return new ByeCommand();
case COMMAND_COMPARE:
return new CompareCommand(parsedInputs[1]);
default:
throw new DukeException("Sorry! I don't know what that means :-(");
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/seedu/duke/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public Task(String description, String location, String timeStart, String timeEn
//this.isDone = false;
}

public String getTimeStart(){
return this.timeStart;
}

public String getTimeEnd() {
return this.timeEnd;
}

/**
* Sets the task type based on 'todo', 'deadline' and 'event'.
*
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/seedu/duke/ui/Ui.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package seedu.duke.ui;

//import seedu.duke.task.Deadline;
import seedu.duke.exception.DukeException;
import seedu.duke.task.Event;
import seedu.duke.task.Task;
Expand Down Expand Up @@ -180,4 +179,14 @@ public void greetUser(User currentUser) {
public void showError(String message) {
System.out.println(MESSAGE_SAD_FACE + " OOPS!!! " + message);
}

/**
* Prints the common available timeslots between 2 timetables
*/
public void printCompare(ArrayList<Integer> outputArray){
System.out.println("Your common timeslots are: \n");
for (int availableTime : outputArray){
System.out.println(availableTime + "HR ");
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/seedu/duke/user/UserList.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ public User getUser(int index) throws DukeException {
}
}

public User getUserByName(String userName){
for (User user : this.users) {
if (user.getName().equals(userName)) {
return user;
}
}
return null;
}

public int getTotalUserCount() {
return users.size();
}
Expand Down

0 comments on commit 4ad6022

Please sign in to comment.