Skip to content

Commit

Permalink
Merge pull request #247 from teozhengyang/v1.4-bugfix-3
Browse files Browse the repository at this point in the history
Fix bugs in logic classes
  • Loading branch information
kristayeo authored Nov 7, 2023
2 parents 3bf1915 + 00b4d89 commit 394e56d
Show file tree
Hide file tree
Showing 25 changed files with 311 additions and 316 deletions.
24 changes: 23 additions & 1 deletion src/main/java/seedu/address/logic/commands/AddEventCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class AddEventCommand extends Command {
+ "h/2020-03-02 1400 1600 "
+ "r/y \n"
+ "Note: "
+ "If you are adding a dated event, then index should be the index of "
+ "Index should be the index of "
+ "the friend you are adding the dated event to or 'user' "
+ "if you would like to add the event to yourself \n";

Expand Down Expand Up @@ -111,4 +111,26 @@ public CommandResult execute(Model model) throws CommandException {
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (!(o instanceof AddEventCommand)) {
return false;
}

AddEventCommand other = (AddEventCommand) o;
if (index == null && other.index != null) {
return false;
} else if (index != null && other.index == null) {
return false;
} else {
return eventName.equals(other.eventName)
&& schedule.equals(other.schedule)
&& reminder.equals(other.reminder);
}
}

}
46 changes: 34 additions & 12 deletions src/main/java/seedu/address/logic/commands/AddScheduleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public class AddScheduleCommand extends Command {
+ "Example: " + COMMAND_WORD
+ " 1"
+ " type/cca"
+ " en/CS2103T Lecture\n"
+ " en/Basketball\n"
+ " h/Monday 1400 1600\n"
+ "NOTE: If you want to add an event to yourself, use index user\n"
+ "NOTE: If you want to add a cca/module to yourself, use index user\n"
+ "Example: " + COMMAND_WORD
+ " user"
+ " type/cca"
+ " en/CS2103T Lecture"
+ " en/Basketball"
+ " h/Monday 1400 1600";

public static final String MESSAGE_SUCCESS = "New event added: ";
Expand All @@ -45,10 +45,10 @@ public class AddScheduleCommand extends Command {

/**
* Constructs an AddScheduleCommand object with the specified event name, index, schedule and event type.
* @param eventName
* @param eventType
* @param index
* @param schedule
* @param eventName The name of the event.
* @param eventType The type of the event.
* @param index The index of the friend to add the event to.
* @param schedule The schedule of the event.
*/
public AddScheduleCommand(String eventName, String eventType, Index index, String schedule) {
this.eventName = eventName;
Expand All @@ -59,9 +59,9 @@ public AddScheduleCommand(String eventName, String eventType, Index index, Strin

/**
* Constructs an AddScheduleCommand object with the specified event name, schedule and event type.
* @param eventName
* @param eventType
* @param schedule
* @param eventName The name of the event.
* @param eventType The type of the event.
* @param schedule The schedule of the event.
*/
public AddScheduleCommand(String eventName, String eventType, String schedule) {
this(eventName, eventType, null, schedule);
Expand Down Expand Up @@ -108,8 +108,8 @@ public CommandResult execute(Model model) throws CommandException {
+ friend.getName(), false, false, true, false);
case "module":
// If the event type is module, add the event to the friend's module schedule
Schedule friendScedule = friend.getSchedule();
friendScedule.addModule(eventName + " " + schedule);
friendSchedule = friend.getSchedule();
friendSchedule.addModule(eventName + " " + schedule);
return new CommandResult(MESSAGE_SUCCESS
+ "\nModule:\n"
+ eventName
Expand All @@ -126,4 +126,26 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(e.getMessage());
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (!(o instanceof AddScheduleCommand)) {
return false;
}

AddScheduleCommand other = (AddScheduleCommand) o;
if (index == null && other.index != null) {
return false;
} else if (index != null && other.index == null) {
return false;
} else {
return eventName.equals(other.eventName)
&& eventType.equals(other.eventType)
&& schedule.equals(other.schedule);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.timetable.FreeTime;
import seedu.address.model.person.timetable.Schedule;
Expand Down Expand Up @@ -161,26 +160,6 @@ public StringBuilder createCommonFreeTimeMessage(ArrayList<Person> overlappingCo
}
}

/**
* Returns a List of common free times with specified contact.
*
* @param model {@code Model} which the command should operate on.
* @param contactName Name of contact to find common free time with.
* @return List of common free times with specified contact.
* @throws CommandException if an error occurs during command execution.
*/
public List<FreeTime> getCommonFreeTimeWith(Model model, Name contactName) throws CommandException {
Person friend = model.getPersonWithName(contactName);
Schedule friendSchedule = friend.getSchedule();
Schedule userSchedule = model.getUser().getSchedule();
List<FreeTime> commonFreeTime = userSchedule.getThisWeeksFreeTimesWith(friendSchedule);
if (commonFreeTime.isEmpty()) {
throw new CommandException(createNoOverlapFriendMessage(friend));
} else {
return commonFreeTime;
}
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
52 changes: 32 additions & 20 deletions src/main/java/seedu/address/logic/commands/RemoveEventCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,20 @@ public CommandResult execute(Model model) throws CommandException {
Person friend;

Schedule userSchedule = model.getUser().getSchedule();
try {
if (index == null) {
userSchedule.deleteDatedEvent(eventName);
return new CommandResult("Dated Event '"
+ eventName
if (index == null) {
userSchedule.deleteDatedEvent(eventName);
return new CommandResult("Dated Event '" + eventName
+ "' deleted from your calendar!", false, false, true, false);
} else {
List<Person> lastShownList = model.getFilteredPersonList();
if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX + "\n"
} else {
List<Person> lastShownList = model.getFilteredPersonList();
if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX + "\n"
+ "Index can be max " + lastShownList.size() + "!");
}
friend = model.getFilteredPersonList().get(index.getZeroBased());
friend.getSchedule().deleteDatedEvent(eventName);
return new CommandResult("Dated Event '"
+ eventName
+ "' deleted from "
+ friend.getName().toString()
+ "'s calendar!", false, false, true, false);
}
} catch (Exception e) {
throw new CommandException("Event " + eventName + " does not exist!\n"
+ "Please check that you have entered the correct event name!\n");
friend = model.getFilteredPersonList().get(index.getZeroBased());
friend.getSchedule().deleteDatedEvent(eventName);
return new CommandResult("Dated Event '" + eventName + "' deleted from "
+ friend.getName().toString() + "'s calendar!", false, false, true, false);
}
}

Expand All @@ -103,4 +94,25 @@ public String listEvents(List<DatedEvent> events) {
}
return result;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (!(o instanceof RemoveEventCommand)) {
return false;
}

RemoveEventCommand other = (RemoveEventCommand) o;
if (index == null && other.index != null) {
return false;
} else if (index != null && other.index == null) {
return false;
} else {
return eventName.equals(other.eventName);
}
}

}
101 changes: 56 additions & 45 deletions src/main/java/seedu/address/logic/commands/RemoveScheduleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public class RemoveScheduleCommand extends Command {
+ "en/EVENT_NAME\n"
+ "Example: " + COMMAND_WORD
+ " 1"
+ " type/dated"
+ " en/CS2103T Lecture\n"
+ " type/cca"
+ " en/Basketball\n"
+ "NOTE: If you want to remove an event from yourself, use index user\n"
+ "Example: " + COMMAND_WORD
+ " user"
+ " type/dated"
+ " en/CS2103T Lecture";
+ " type/cca"
+ " en/Basketball";

private final String eventName;
private final String eventType;
Expand Down Expand Up @@ -74,56 +74,45 @@ public CommandResult execute(Model model) throws CommandException {
Person friend;

Schedule userSchedule = model.getUser().getSchedule();
try {
if (this.index == null) {
friend = model.getUser();
} else {
List<Person> lastShownList = model.getFilteredPersonList();
if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX + "\n"
if (this.index == null) {
friend = model.getUser();
} else {
List<Person> lastShownList = model.getFilteredPersonList();
if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX + "\n"
+ "Index can be max " + lastShownList.size() + "!");
}
friend = model.getFilteredPersonList().get(index.getZeroBased());
}
switch (eventType) {
// If the event is a cca event, remove it from the specified contact's calendar.
case "cca":
if (index == null) {
userSchedule.deleteCca(eventName);
return new CommandResult("CCA '"
+ eventName
friend = model.getFilteredPersonList().get(index.getZeroBased());
}

switch (eventType) {
case "cca":
if (index == null) {
userSchedule.deleteCca(eventName);
return new CommandResult("CCA '" + eventName
+ "'' deleted from your calendar!", false, false, true, false);
} else {
friend = model.getFilteredPersonList().get(index.getZeroBased());
friend.getSchedule().deleteCca(eventName);
return new CommandResult("CCA '"
+ eventName
+ "'' deleted from "
} else {
friend = model.getFilteredPersonList().get(index.getZeroBased());
friend.getSchedule().deleteCca(eventName);
return new CommandResult("CCA '" + eventName + "'' deleted from "
+ friend.getName().toString()
+ "'s calendar!", false, false, true, false);
}
// If the event is a module event, remove it from the specified contact's calendar.
case "module":
if (index == null) {
userSchedule.deleteModule(eventName);
return new CommandResult("Module" + eventName
}
case "module":
if (index == null) {
userSchedule.deleteModule(eventName);
return new CommandResult("Module '" + eventName
+ "'' deleted from your calendar!", false, false, true, false);
} else {
friend = model.getFilteredPersonList().get(index.getZeroBased());
friend.getSchedule().deleteModule(eventName);
return new CommandResult("Module '"
+ eventName
+ "'' deleted from "
} else {
friend = model.getFilteredPersonList().get(index.getZeroBased());
friend.getSchedule().deleteModule(eventName);
return new CommandResult("Module '" + eventName + "'' deleted from "
+ friend.getName().toString()
+ "'s calendar!", false, false, true, false);
}
// If the event type is invalid, throw an exception.
default:
throw new CommandException("Invalid event type!\n"
+ "Event type must either be 'cca' or 'module'!\n");
}
} catch (Exception e) {
throw new CommandException(e.getMessage());
default:
throw new CommandException("Invalid event type!\n"
+ "Event type must either be 'cca' or 'module'!\n");
}
}

Expand All @@ -139,4 +128,26 @@ public String listEvents(List<DatedEvent> events) {
}
return result;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (!(o instanceof RemoveScheduleCommand)) {
return false;
}

RemoveScheduleCommand other = (RemoveScheduleCommand) o;
if (index == null && other.index != null) {
return false;
} else if (index != null && other.index == null) {
return false;
} else {
return eventName.equals(other.eventName)
&& eventType.equals(other.eventType);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,17 @@ public AddEventCommand parse(String args) throws ParseException {
String schedule = argMultimap.getValue(PREFIX_SCHEDULE).get();
String reminder = argMultimap.getValue(PREFIX_REMINDER).get();

try {
indexString = argMultimap.getPreamble().toLowerCase();
if (indexString.equals("user")) {
return new AddEventCommand(eventName, schedule, reminder);
} else if (Integer.parseInt(indexString) > 0) {
return new AddEventCommand(eventName, ParserUtil.parseIndex(indexString),
schedule, reminder);
} else {
indexString = argMultimap.getPreamble().toLowerCase();
if (indexString.equals("user")) {
return new AddEventCommand(eventName, schedule, reminder);
} else {
try {
Integer.parseInt(indexString);
return new AddEventCommand(eventName, ParserUtil.parseIndex(indexString), schedule, reminder);
} catch (NumberFormatException e) {
throw new ParseException(String.format("Invalid index!" + "\n"
+ "Index can only be 'user' or a positive integer! \n"));
}
} catch (Exception pe) {
throw new ParseException(String.format("Please input an index!" + "\n"
+ "Message Usage:\n" + AddEventCommand.MESSAGE_USAGE));
}
}

Expand Down
Loading

0 comments on commit 394e56d

Please sign in to comment.