Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs in logic classes #247

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 13 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,5 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(e.getMessage());
}
}

}
32 changes: 12 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,5 @@ public String listEvents(List<DatedEvent> events) {
}
return result;
}

}
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,18 +74,17 @@ 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) {
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) {
Expand All @@ -106,7 +105,7 @@ public CommandResult execute(Model model) throws CommandException {
case "module":
if (index == null) {
userSchedule.deleteModule(eventName);
return new CommandResult("Module" + eventName
return new CommandResult("Module '" + eventName
+ "'' deleted from your calendar!", false, false, true, false);
} else {
friend = model.getFilteredPersonList().get(index.getZeroBased());
Expand All @@ -122,9 +121,6 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException("Invalid event type!\n"
+ "Event type must either be 'cca' or 'module'!\n");
}
} catch (Exception e) {
throw new CommandException(e.getMessage());
}
}

/**
Expand All @@ -139,4 +135,5 @@ public String listEvents(List<DatedEvent> events) {
}
return result;
}

}
23 changes: 10 additions & 13 deletions src/main/java/seedu/address/logic/parser/AddEventCommandParser.java
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 {
throw new ParseException(String.format("Invalid index!" + "\n"
+ "Index can only be 'user' or a positive integer! \n"));
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EVENTNAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EVENTTYPE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_REMINDER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SCHEDULE;

import java.util.stream.Stream;

import seedu.address.logic.commands.AddEventCommand;
import seedu.address.logic.commands.AddScheduleCommand;
import seedu.address.logic.parser.exceptions.ParseException;

Expand All @@ -33,30 +33,26 @@ public AddScheduleCommand parse(String args) throws ParseException {
+ AddScheduleCommand.MESSAGE_USAGE));
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_EVENTNAME, PREFIX_SCHEDULE,
PREFIX_REMINDER);
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_EVENTNAME, PREFIX_SCHEDULE, PREFIX_EVENTTYPE);

String indexString;

String eventName = argMultimap.getValue(PREFIX_EVENTNAME).get().toLowerCase();
String eventType = argMultimap.getValue(PREFIX_EVENTTYPE).get().toLowerCase();
String schedule = argMultimap.getValue(PREFIX_SCHEDULE).get();

try {
indexString = argMultimap.getPreamble().toLowerCase();
} catch (Exception pe) {
throw new ParseException(String.format("Please input an index!" + "\n"
+ "Message Usage:\n" + AddScheduleCommand.MESSAGE_USAGE));
}
indexString = argMultimap.getPreamble().toLowerCase();

if (indexString.equals("user")) {
return new AddScheduleCommand(eventName, eventType, schedule);
} else if (Integer.parseInt(indexString) > 0) {
return new AddScheduleCommand(eventName, eventType,
ParserUtil.parseIndex(indexString), schedule);
} else {
throw new ParseException("Invalid index!\n"
+ "Index can only be 'user' or a 'positive integer!' \n");
try {
Integer.parseInt(indexString);
return new AddScheduleCommand(eventName, eventType, ParserUtil.parseIndex(indexString), schedule);
} catch (NumberFormatException e) {
throw new ParseException(String.format("Invalid index!" + "\n" +
"Index can only be 'user' or a positive integer! \n"));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,20 @@ public RemoveEventCommand parse(String args) throws ParseException {
+ RemoveEventCommand.MESSAGE_USAGE));
}

if (!arePrefixesUnique(argMultimap, PREFIX_EVENTNAME)) {
throw new ParseException(String.format("You can only have 1 of each prefix!\n"
+ RemoveEventCommand.MESSAGE_USAGE));
}

try {
String indexString = argMultimap.getPreamble().toLowerCase();
String eventName = argMultimap.getValue(PREFIX_EVENTNAME).get().toLowerCase();
if (indexString.equals("user")) {
return new RemoveEventCommand(eventName, null);
} else if (Integer.parseInt(indexString) > 0) {
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_EVENTNAME);

String indexString = argMultimap.getPreamble().toLowerCase();
String eventName = argMultimap.getValue(PREFIX_EVENTNAME).get().toLowerCase();
if (indexString.equals("user")) {
return new RemoveEventCommand(eventName, null);
} else {
try {
Integer.parseInt(indexString);
return new RemoveEventCommand(eventName, ParserUtil.parseIndex(indexString));
} else {
throw new ParseException("Invalid index!\n"
+ "Index must either be 'user' or a positive integer!\n");
} 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(MESSAGE_INVALID_COMMAND_FORMAT, RemoveEventCommand.MESSAGE_USAGE), pe);
}
}

Expand All @@ -61,12 +56,4 @@ private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Pre
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}

/**
* Returns true if there are duplicate prefixes
* @param argumentMultimap
* @param prefixes
*/
private static boolean arePrefixesUnique(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getAllValues(prefix).size() == 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,21 @@ public RemoveScheduleCommand parse(String args) throws ParseException {
+ RemoveScheduleCommand.MESSAGE_USAGE));
}

if (!arePrefixesUnique(argMultimap, PREFIX_EVENTNAME, PREFIX_EVENTTYPE)) {
throw new ParseException(String.format("You can only have 1 of each prefix!\n"
+ RemoveScheduleCommand.MESSAGE_USAGE));
}

try {
String indexString = argMultimap.getPreamble().toLowerCase();
String eventName = argMultimap.getValue(PREFIX_EVENTNAME).get().toLowerCase();
String eventType = argMultimap.getValue(PREFIX_EVENTTYPE).get().toLowerCase();
if (indexString.equals("user")) {
return new RemoveScheduleCommand(eventName, eventType, null);
} else if (Integer.parseInt(indexString) > 0) {
return new RemoveScheduleCommand(eventName, eventType,
ParserUtil.parseIndex(indexString));
} else {
throw new ParseException("Invalid index!\n"
+ "Index must either be 'user' or a positive integer!\n");
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_EVENTNAME, PREFIX_EVENTTYPE);

String indexString = argMultimap.getPreamble().toLowerCase();
String eventName = argMultimap.getValue(PREFIX_EVENTNAME).get().toLowerCase();
String eventType = argMultimap.getValue(PREFIX_EVENTTYPE).get().toLowerCase();
if (indexString.equals("user")) {
return new RemoveScheduleCommand(eventName, eventType, null);
} else {
try {
Integer.parseInt(indexString);
return new RemoveScheduleCommand(eventName, eventType, ParserUtil.parseIndex(indexString));
} 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(MESSAGE_INVALID_COMMAND_FORMAT, RemoveScheduleCommand.MESSAGE_USAGE), pe);
}
}

Expand All @@ -64,12 +58,4 @@ private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Pre
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}

/**
* Returns true if there are duplicate prefixes
* @param argumentMultimap
* @param prefixes
*/
private static boolean arePrefixesUnique(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getAllValues(prefix).size() == 1);
}
}
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/person/Birthday.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
public class Birthday {
public static final String MESSAGE_CONSTRAINTS =
"Birthday should be in the format of YYYY-MM-DD OR YYYY-M-D and should be a valid date.";
"Birthday should be in the format of YYYY-MM-DD and should be a valid date.";
public static final String VALIDATION_REGEX =
"^\\d{4}-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$";
public final String date;
Expand Down
Loading
Loading