Skip to content

Commit

Permalink
Merge c7b22d8 into ea35c50
Browse files Browse the repository at this point in the history
  • Loading branch information
souless94 committed Oct 28, 2018
2 parents ea35c50 + c7b22d8 commit 714360c
Show file tree
Hide file tree
Showing 29 changed files with 1,033 additions and 160 deletions.
13 changes: 10 additions & 3 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ public class Messages {
public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_INVALID_TIMETABLE_FORMAT = "format can only be horizontal or vertical";
public static final String MESSAGE_INVALID_DAY = "Days are in full name, there is no such day in a week";
public static final String MESSAGE_INVALID_TIMING = "timings are in 24h format and is from 0800 to 2300";
public static final String MESSAGE_INVALID_DAY_AND_TIMING =
MESSAGE_INVALID_DAY + "\n" + MESSAGE_INVALID_TIMING;
public static final String MESSAGE_INVALID_FILE = "timetable can only be in csv";

public static final String MESSAGE_NO_MATCH_TO_EXISTING_GROUP = "There is no match to an existing group.";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_GROUPS_LISTED_OVERVIEW = "%1$d groups listed!";
public static final String MESSAGE_TIMETABLE_NOT_FOUND = "timetable to be added is not found";
public static final String MESSAGE_NOT_UNIQUE_PREFIX_INPUT = "can only enter 1 input per prefix";
public static final String MESSAGE_IS_FILE_DIRECTORY = "timetable cannot be a file directory(folder)";



public static final String INVALID_TIMETABLE_FORMAT = "format can only be horizontal or vertical";
public static final String INVALID_DAY = "Days are in full name, there is no such day in a week";
public static final String INVALID_TIMING = "timings are in 24h format and is from 0800 to 2300";
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.core.Messages.MESSAGE_TIMETABLE_NOT_FOUND;
import static seedu.address.logic.commands.EditTimetableCommand.createUpdatedPerson;
import static seedu.address.logic.parser.CliSyntax.PREFIX_FILE_LOCATION;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.io.File;
Expand All @@ -19,15 +19,35 @@
*/
public class AddTimetableCommand extends Command {


public static final String COMMAND_WORD = "add_timetable";
public static final String MESSAGE_USAGE =
COMMAND_WORD + ": adds timetable to the person identified "
+ "by the index number used in the displayed person list."
+ " \n"
+ "Parameters : INDEX (must be a positive integer) "
+ "Example: " + COMMAND_WORD + " 1 ";
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ "[" + PREFIX_FILE_LOCATION + "FILE_LOCATION] "
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_FILE_LOCATION
+ "C:/Users/wen kai/Downloads/y4s1/cs2103/project2/data/timetable/495011161 timetable";

public static final String MESSAGE_ADD_TIMETABLE_SUCCESS = "timetable added successfully: %1$s";
public static final String MESSAGE_TIMETABLE_NOT_FOUND = "timetable to be added is not found";
public static final String MESSAGE_INVALID_TIMETABLE_SIZE =
"timetable to be added is wrong: \n"
+ "if format is horizontal,timetable should have in total rows: 8 , columns : 17 \n"
+ "if format is vertical,timetable should have in total rows: 17 , columns : 8 \n";

private static final String timings = "correctTimings : \n"
+ "0800,0900,1000,1100 \n"
+ "1200,1300,1400,1500,1600 \n"
+ "1700,1800,1900,2000,2100,2200,2300 \n";
private static final String days = "correctDays: {Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday";

public static final String MESSAGE_ADD_TIMETABLE_SUCCESS = "timetable added successfully";
public static final String MESSAGE_INVALID_TIMETABLE = "timetable to be added is wrong: \n"
+ "does not have correct timings or/and days present in first column or/and first row in the csv file \n"
+ timings + "\n"
+ days + "\n";


private final Index index;
Expand All @@ -50,23 +70,40 @@ public CommandResult execute(Model model, CommandHistory history) throws Command

Person personToEdit = CommandUtil.retrievePersonFromIndex(model, index);
String filePath;
if (newFilePath == null) {
if ("default".equals(newFilePath)) {
filePath = personToEdit.getStoredLocation();
} else {
filePath = newFilePath.replace("\\", "/");
}
File toRead = new File(filePath);
if (toRead.exists()) {
boolean doesFileExists = new File(filePath).exists();
if (doesFileExists) {
Timetable timetable = new Timetable(filePath, personToEdit.getFormat(),
personToEdit.getTimetable().getTimetableDataString(), 2, null, null, null);
if (!timetable.isValid()) {
if (!timetable.isCorrectSize()) {
throw new CommandException(MESSAGE_INVALID_TIMETABLE_SIZE);
}
if (!timetable.hasCorrectRowsAndColumns()) {
throw new CommandException(MESSAGE_INVALID_TIMETABLE);
}
}
Person updatedPerson = createUpdatedPerson(personToEdit, timetable, filePath);
model.update(personToEdit, updatedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
model.commitAddressBook();
return new CommandResult(String.format(MESSAGE_ADD_TIMETABLE_SUCCESS, updatedPerson));
return new CommandResult(
String.format(MESSAGE_ADD_TIMETABLE_SUCCESS, updatedPerson.getStoredLocation()));
} else {
throw new CommandException(MESSAGE_TIMETABLE_NOT_FOUND);
}
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof AddTimetableCommand // instanceof handles nulls
&& index.equals(((AddTimetableCommand) other).index))
&& newFilePath.equals(((AddTimetableCommand) other).newFilePath);
}
}

10 changes: 7 additions & 3 deletions src/main/java/seedu/address/logic/commands/CommandUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import seedu.address.model.person.Person;

//@@author Happytreat

/**
* Contains helper methods for commands.
*/
Expand All @@ -39,7 +40,8 @@ public static Group retrieveGroupFromName(Model model, Name groupName) throws Co
/**
* Retrieves the person with index in displayed Group list
*/
public static Person retrievePersonFromIndex (Model model, Index targetIndex) throws CommandException {
public static Person retrievePersonFromIndex(Model model, Index targetIndex)
throws CommandException {
List<Person> lastShownList = model.getFilteredPersonList();

if (targetIndex.getZeroBased() >= lastShownList.size()) {
Expand All @@ -53,7 +55,8 @@ public static Person retrievePersonFromIndex (Model model, Index targetIndex) th
* Delete {@code groupToDelete} from {@code personToUpdate} group list and updates model of new
* {@code personToUpdate}
*/
public static void updatePersonDeleteGroupFromGroupList (Model model, Group groupToDelete, Person personToUpdate) {
public static void updatePersonDeleteGroupFromGroupList(Model model, Group groupToDelete,
Person personToUpdate) {
List<Group> editedGroupList = new ArrayList<>(personToUpdate.getGroups());
editedGroupList.remove(groupToDelete);
Person newPerson = personToUpdate;
Expand All @@ -64,7 +67,8 @@ public static void updatePersonDeleteGroupFromGroupList (Model model, Group grou
/**
* Adds {@code target} to model if there it has not been added before.
*/
public static void addTargetToModelIfNoDuplicates (Model model, Entity target) throws CommandException {
public static void addTargetToModelIfNoDuplicates(Model model, Entity target)
throws CommandException {
if (model.has(target)) {
if (target instanceof Person) {
throw new CommandException(AddCommand.MESSAGE_DUPLICATE_PERSON);
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/seedu/address/logic/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import seedu.address.model.person.Person;

/**
* Deletes a person identified using it's displayed index from the address book
* and delete it as member from all groups that it is in.
* Deletes a person identified using it's displayed index from the address book and delete it as
* member from all groups that it is in.
*/
public class DeleteCommand extends Command {

Expand All @@ -43,10 +43,7 @@ public CommandResult execute(Model model, CommandHistory history) throws Command

Person personToDelete = CommandUtil.retrievePersonFromIndex(model, targetIndex);

File timetableToBeDeleted = new File(
personToDelete.getStoredLocation()
+ "/"
+ personToDelete.getName().toString() + " timetable" + ".csv");
File timetableToBeDeleted = new File(personToDelete.getStoredLocation());
if (timetableToBeDeleted.exists()) {
timetableToBeDeleted.delete();
}
Expand All @@ -65,20 +62,18 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
}

/**
*
* @param groupToBeEdited
* @param personToDelete
* @return updated group that has {@code personToDelete} deleted from its member list
* @throws CommandException
*/
public Group deleteMemberFromGroup(Group groupToBeEdited, Person personToDelete) throws CommandException {
public Group deleteMemberFromGroup(Group groupToBeEdited, Person personToDelete)
throws CommandException {
try {
UniqueList<Person> newGroupMembers = new UniqueList<>();
newGroupMembers.setElements(groupToBeEdited.getGroupMembers());

newGroupMembers.remove(personToDelete);

return new Group(groupToBeEdited.getName(), groupToBeEdited.getDescription(), newGroupMembers);
return new Group(groupToBeEdited.getName(), groupToBeEdited.getDescription(),
newGroupMembers);
} catch (NotFoundException e) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class DeleteTimetableCommand extends Command {

public static final String COMMAND_WORD = "delete_timetable";
public static final String MESSAGE_USAGE =
COMMAND_WORD + ": delete timetable and adds a default timetable to the person identified"
COMMAND_WORD + ": delete timetable from stored location and adds a default timetable to the person identified"
+ "by the index number used in the displayed person list."
+ " \n"
+ "Parameters : INDEX (must be a positive integer) "
Expand Down Expand Up @@ -77,4 +77,11 @@ private Person createPersonWithNewTimetable(Person personToEdit) {
return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags,
"default", "default", "default");
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof DeleteTimetableCommand // instanceof handles nulls
&& index.equals(((DeleteTimetableCommand) other).index));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,10 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
model.commitAddressBook();
return new CommandResult(String.format(MESSAGE_DOWNLOAD_TIMETABLE_SUCCESS));
}
@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof DownloadTimetableCommand // instanceof handles nulls
&& index.equals(((DownloadTimetableCommand) other).index));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class EditTimetableCommand extends Command {
public static final String MESSAGE_USAGE =
COMMAND_WORD + ": edit timetable for the person identified "
+ "by the index number used in the displayed person list."
+ "if no details is entered, make the details blank "
+ "for the timeslot in the timetable"
+ " \n"
+ "Parameters : INDEX (must be a positive integer) "
+ "[" + PREFIX_DAY + "DAY] "
Expand All @@ -37,7 +39,7 @@ public class EditTimetableCommand extends Command {
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_DAY + "Wednesday "
+ PREFIX_TIMING + "1700 "
+ PREFIX_DETAILS + "do cs2103";
+ "[" + PREFIX_DETAILS + "]" + "do cs2103";

public static final String MESSAGE_EDIT_TIMETABLE_SUCCESS = "timetable edited successfully";

Expand Down Expand Up @@ -93,4 +95,14 @@ public static Person createUpdatedPerson(Person personToEdit, Timetable timetabl
return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags,
format, storedLocation, timetableString);
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof EditTimetableCommand // instanceof handles nulls
&& index.equals(((EditTimetableCommand) other).index))
&& day.equals(((EditTimetableCommand) other).day)
&& timing.equals(((EditTimetableCommand) other).timing)
&& details.equals(((EditTimetableCommand) other).details);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public AddTimetableCommand parse(String args) throws ParseException {
ArgumentTokenizer
.tokenize(args, PREFIX_FILE_LOCATION);
Index index;
String newFilePath = null;
String newFilePath = "default";
try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
} catch (ParseException pe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public EditTimetableCommand parse(String args) throws ParseException {
.isOnlyOnePrefix(PREFIX_TIMING)) {
throw new ParseException(String.format(MESSAGE_NOT_UNIQUE_PREFIX_INPUT));
}

ParserUtil.checkBothDayAndTiming(argMultimap.getValue(PREFIX_DAY).get(),
argMultimap.getValue(PREFIX_TIMING).get());
String day = ParserUtil.parseDay(argMultimap.getValue(PREFIX_DAY).get());
String timing = ParserUtil.parseTiming(argMultimap.getValue(PREFIX_TIMING).get());
String details = " ";
Expand Down
58 changes: 47 additions & 11 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package seedu.address.logic.parser;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_FILE;
import static seedu.address.commons.core.Messages.MESSAGE_IS_FILE_DIRECTORY;
import static seedu.address.commons.core.Messages.MESSAGE_TIMETABLE_NOT_FOUND;

import java.io.File;

Expand Down Expand Up @@ -158,11 +161,22 @@ public static String parsePassword(String password) throws ParseException {
*/
public static String parseLocation(String location) throws ParseException {
requireNonNull(location);
boolean doesFileExists = new File(location).exists();
if (!doesFileExists) {
throw new ParseException(Messages.MESSAGE_TIMETABLE_NOT_FOUND);
String trimmedLocation = location.trim();
String fileExtension = trimmedLocation.substring(trimmedLocation.length() - 4);
if (".csv".equals(fileExtension)) {
File timetable = new File(trimmedLocation);
boolean doesFileExists = timetable.exists();
if (timetable.isDirectory()) {
throw new ParseException(MESSAGE_IS_FILE_DIRECTORY);
}
if (!doesFileExists) {
throw new ParseException(MESSAGE_TIMETABLE_NOT_FOUND);
}
} else {
throw new ParseException(MESSAGE_INVALID_FILE);
}
return location;

return trimmedLocation;
}

/**
Expand All @@ -187,7 +201,7 @@ public static String parseFormat(String format) throws ParseException {
if ("horizontal".equals(format) || "vertical".equals(format)) {
return format;
} else {
throw new ParseException(Messages.INVALID_TIMETABLE_FORMAT);
throw new ParseException(Messages.MESSAGE_INVALID_TIMETABLE_FORMAT);
}
}

Expand All @@ -199,12 +213,13 @@ public static String parseFormat(String format) throws ParseException {
*/
public static String parseDay(String day) throws ParseException {
requireNonNull(day);
String trimmedDay = day.trim();
String[] validDays = new TimetableData("horizontal", null, "default", 1, null, null, null)
.getDaysInLowerCase();
if (ArrayUtils.contains(validDays, day.toLowerCase())) {
return day;
if (ArrayUtils.contains(validDays, trimmedDay.toLowerCase())) {
return trimmedDay;
} else {
throw new ParseException(Messages.INVALID_DAY);
throw new ParseException(Messages.MESSAGE_INVALID_DAY);
}
}

Expand All @@ -216,12 +231,13 @@ public static String parseDay(String day) throws ParseException {
*/
public static String parseTiming(String timing) throws ParseException {
requireNonNull(timing);
String trimmedTiming = timing.trim();
String[] validTiming = new TimetableData("horizontal", null, "default", 1, null, null, null)
.getTimings();
if (ArrayUtils.contains(validTiming, timing)) {
return timing;
if (ArrayUtils.contains(validTiming, trimmedTiming)) {
return trimmedTiming;
} else {
throw new ParseException(Messages.INVALID_TIMING);
throw new ParseException(Messages.MESSAGE_INVALID_TIMING);
}
}

Expand All @@ -236,4 +252,24 @@ public static String parseDetails(String details) {
return details;
}

/**
* Parses {@code String timing,String day} Leading and trailing whitespaces will be trimmed.
* checks if timings are in 24h format and is from 0800 to 2300 and checks if day is any of the
* days in a week.
*
* @throws ParseException if the given {@code timing} is invalid.
*/
public static void checkBothDayAndTiming(String day, String timing) throws ParseException {

String trimmedDay = day.trim();
String[] validDays = new TimetableData("horizontal", null, "default", 1, null, null, null)
.getDaysInLowerCase();
String trimmedTiming = timing.trim();
String[] validTiming = new TimetableData("horizontal", null, "default", 1, null, null, null)
.getTimings();
if (!ArrayUtils.contains(validDays, trimmedDay.toLowerCase())
&& !ArrayUtils.contains(validTiming, trimmedTiming)) {
throw new ParseException(Messages.MESSAGE_INVALID_DAY_AND_TIMING);
}
}
}
Loading

0 comments on commit 714360c

Please sign in to comment.