Skip to content

Commit

Permalink
Merge pull request #226 from TehZiHuai/master
Browse files Browse the repository at this point in the history
added robustness for the update function
  • Loading branch information
TehZiHuai authored Nov 9, 2019
2 parents f190f5d + 741bf8e commit 9b6f811
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/main/java/CustomExceptions/RoomShareException.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public class RoomShareException extends Exception {
public static final String EMPTY_INDEX = "\tPlease enter a valid index within the range of the list! Eg. restore 1\n";
private static final String INVALID_LEAVE_DATE_MESSAGE = "\tPlease check your dates for your leave!\n";
private static final String NO_SUCH_SUBTASK = "\tSubtask does not exist!\n";
public static final String ASSIGNEE_SET_TO_EVERYONE = "\tThere might have been an error when setting the assignee\n" +
"\tIt could be an error in your entry of the assignee field\n" +
"\tHowever, if you had intended to set the assignee to 'everyone', then ignore this message\n";

private String message;
/**
Expand Down Expand Up @@ -147,6 +150,10 @@ public RoomShareException(ExceptionType type){
message = NO_SUCH_SUBTASK;
break;

case assigneeSetToEveyone:
message = ASSIGNEE_SET_TO_EVERYONE;
break;

default:
message = ANOMALY_TEXT;
break;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/Enums/ExceptionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public enum ExceptionType {
wrongDateFormat,
negativeTimeAmount,

timeClash,
emptyList,
emptyUser,
outOfBounds,
Expand All @@ -31,5 +30,7 @@ public enum ExceptionType {
invalidInputString,
invalidDateRange,
emptyIndex,
noSubtask, invalidDateError
noSubtask,
invalidDateError,
assigneeSetToEveyone
}
77 changes: 67 additions & 10 deletions src/main/java/Operations/TaskCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,18 @@ public String extractDescription(String input) throws RoomShareException {
* @param input user's input
* @return the priority of the task
*/
public Priority extractPriority(String input) {
public Priority extractPriority(String input) throws RoomShareException{
// check for errors in the raw input for misleading characters
int count = 0;
char[] inputAsChar = input.toCharArray();
for (char c: inputAsChar) {
if (c == '*') {
count++;
}
}
if (count == 1) {
throw new RoomShareException(ExceptionType.invalidInputString);
}
String[] priorityArray = input.split("\\*");
Priority priority;
if (priorityArray.length != 1) {
Expand Down Expand Up @@ -114,17 +125,17 @@ public ArrayList<Date> extractDate(String input) throws RoomShareException {
Date currentDate = new Date();
if (count > 0) {
if (count <= 2) {
String dateInput = dateArray[1].trim();
Date date;
try {
String dateInput = dateArray[1].trim();
Date date;
date = parser.formatDate(dateInput);
if (date.before(currentDate)) {
// the input date is before the current date
throw new RoomShareException(ExceptionType.invalidDateError);
}
dates.add(date);
} catch (ArrayIndexOutOfBoundsException a) {
throw new RoomShareException(ExceptionType.invalidDateError);
throw new RoomShareException(ExceptionType.emptyDate);
}
} else {
String fromInput = dateArray[1].trim();
Expand Down Expand Up @@ -167,6 +178,17 @@ public ArrayList<Date> extractDate(String input) throws RoomShareException {
* @return the name of the assignee
*/
public String extractAssignee(String input) throws RoomShareException{
// check for errors in the raw input for misleading characters
int count = 0;
char[] inputAsChar = input.toCharArray();
for (char c: inputAsChar) {
if (c == '@') {
count++;
}
}
if (count == 1) {
throw new RoomShareException(ExceptionType.invalidInputString);
}
String[] assigneeArray = input.split("@");
String assignee;
if (assigneeArray.length != 1) {
Expand All @@ -186,7 +208,18 @@ public String extractAssignee(String input) throws RoomShareException{
* @param input user's input
* @return the recurrence schedule of the task
*/
public RecurrenceScheduleType extractRecurrence(String input) {
public RecurrenceScheduleType extractRecurrence(String input) throws RoomShareException {
// check for errors in the raw input for misleading characters
int count = 0;
char[] inputAsChar = input.toCharArray();
for (char c: inputAsChar) {
if (c == '%') {
count++;
}
}
if (count == 1) {
throw new RoomShareException(ExceptionType.invalidInputString);
}
String[] recurrenceArray = input.split("%");
RecurrenceScheduleType recurrence;
if (recurrenceArray.length != 1) {
Expand All @@ -210,6 +243,17 @@ public RecurrenceScheduleType extractRecurrence(String input) {
* @return the amount of time and unit of the duration as a Pair<Integer,TimeUnit>
*/
public Pair<Integer, TimeUnit> extractDuration(String input) throws RoomShareException {
// check for errors in the raw input for misleading characters
int count = 0;
char[] inputAsChar = input.toCharArray();
for (char c: inputAsChar) {
if (c == '^') {
count++;
}
}
if (count == 1) {
throw new RoomShareException(ExceptionType.invalidInputString);
}
String[] durationArray = input.split("\\^");
int duration;
TimeUnit unit;
Expand Down Expand Up @@ -439,10 +483,13 @@ public Task create(String input) throws RoomShareException, DuplicateException,
* @param oldTask the task to be updated
*/
public void updateTask(String input, Task oldTask) throws RoomShareException {
boolean isNotUpdated = true;
boolean isSetToEveryone = false;
try {
if (input.contains("(") && input.contains(")")) {
String description = this.extractDescription(input);
oldTask.setDescription(description);
isNotUpdated = false;
}
} catch (RoomShareException e) {
System.out.println(UPDATED_DESCRIPTION_ERROR);
Expand All @@ -457,20 +504,24 @@ public void updateTask(String input, Task oldTask) throws RoomShareException {
oldLeave.setDate(start);
oldLeave.setStartDate(start);
oldLeave.setEndDate(end);
isNotUpdated = false;
} else {
Date date = dates.get(0);
if (oldTask instanceof Leave) {
Leave oldLeave = (Leave)oldTask;
oldLeave.setEndDate(date);
isNotUpdated = false;
} else {
oldTask.setDate(date);
isNotUpdated = false;
}
}
}

if (input.contains("*")) {
Priority priority = this.extractPriority(input);
oldTask.setPriority(priority);
isNotUpdated = false;
}

if (input.contains("@")) {
Expand All @@ -480,11 +531,15 @@ public void updateTask(String input, Task oldTask) throws RoomShareException {
} catch (RoomShareException e) {
assignee = "everyone";
}
if (assignee.equals("everyone")) {
isSetToEveryone = true;
}
oldTask.setAssignee(assignee);
if (oldTask instanceof Leave) {
Leave oldLeave = (Leave) oldTask;
oldLeave.setUser(assignee);
}
isNotUpdated = false;
}

if (input.contains("^") && oldTask instanceof Meeting) {
Expand All @@ -493,20 +548,22 @@ public void updateTask(String input, Task oldTask) throws RoomShareException {
TimeUnit unit = durationAndUnit.getValue();
Meeting oldMeeting = (Meeting) oldTask;
oldMeeting.setDuration(duration,unit);
isNotUpdated = false;
}

if (input.contains("%")) {
RecurrenceScheduleType recurrence = this.extractRecurrence(input);
oldTask.setRecurrenceSchedule(recurrence);
isNotUpdated = false;
}

if(input.contains("uncheck") && !(oldTask instanceof Leave)) {
boolean checked = oldTask.getDone();
oldTask.setDone(!checked);
// check if any field was updated at all
if (isNotUpdated) {
throw new RoomShareException(ExceptionType.invalidInputString);
}

else {
throw new RoomShareException(ExceptionType.invalidInputString);
if (isSetToEveryone) {
throw new RoomShareException(ExceptionType.assigneeSetToEveyone);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/RoomShare.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ public void run() throws RoomShareException, IOException, InterruptedException {
} catch (RoomShareException e) {
ui.showError(e);
}
storage.writeFile(TaskList.currentList(), "data.txt");
storage.writeFile(OverdueList.getOverdueList(), "overdue.txt");
listRoutine.list();
} else {
ui.showTagged(input);
Expand All @@ -425,6 +427,8 @@ public void run() throws RoomShareException, IOException, InterruptedException {
ui.showTaggedPercentage(input);
ProgressBar progressBar = new ProgressBar(doneArray[0], doneArray[1]);
ui.showBar(progressBar.showBar());
storage.writeFile(TaskList.currentList(), "data.txt");
storage.writeFile(OverdueList.getOverdueList(), "overdue.txt");
} catch (RoomShareException e) {
ui.showError(e);
}
Expand All @@ -443,6 +447,8 @@ public void run() throws RoomShareException, IOException, InterruptedException {
} catch (RoomShareException e) {
ui.showError(e);
}
storage.writeFile(TaskList.currentList(), "data.txt");
storage.writeFile(OverdueList.getOverdueList(), "overdue.txt");
listRoutine.list();
break;

Expand Down

0 comments on commit 9b6f811

Please sign in to comment.