Skip to content

Commit

Permalink
Merge pull request #161 from coderhuang559/master
Browse files Browse the repository at this point in the history
Fix add time bug
  • Loading branch information
Kailash201 authored Nov 3, 2023
2 parents bfb38a7 + 714cd7b commit 885d380
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ public DeletePersonTimeCommand(Name personName, ArrayList<TimeInterval> timeInte
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
String status;
if (model.hasPerson(personName)) {
model.deleteTimeFromPerson(personName, timeIntervalsToDelete);
status = model.deleteTimeFromPerson(personName, timeIntervalsToDelete);
} else {
throw new CommandException("Person does not exists");
throw new CommandException("Person does not exist \n");
}
return new CommandResult(String.format(MESSAGE_DELETE_TIME_SUCCESS, personName.fullName));
return new CommandResult(String.format(MESSAGE_DELETE_TIME_SUCCESS + status, personName.fullName));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import java.util.stream.Stream;

import seedu.address.logic.commands.AddGroupMeetingTimeCommand;
import seedu.address.logic.commands.AddTimeCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.TimeInterval;
import seedu.address.model.group.Group;
import seedu.address.model.person.Name;

/**
* Parses input arguments and creates a new AddGroupFreeTimeCommand object
Expand All @@ -23,19 +25,16 @@ public class AddGroupMeetingTimeCommandParser implements Parser<AddGroupMeetingT
*/
public AddGroupMeetingTimeCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_GROUPTAG, PREFIX_FREETIME, PREFIX_ENDINTERVAL);
ArgumentTokenizer.tokenize(args, PREFIX_GROUPTAG, PREFIX_FREETIME);

//find a way to separate error msg when ";" is missing
if (!arePrefixesPresent(argMultimap, PREFIX_GROUPTAG, PREFIX_FREETIME)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddGroupMeetingTimeCommand.MESSAGE_USAGE));
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_GROUPTAG);
Group group = ParserUtil.parseSingleGroup(argMultimap.getValue(PREFIX_GROUPTAG).get());
TimeInterval firstInterval = ParserUtil.parseEachInterval(argMultimap.getValue(PREFIX_FREETIME).get());
ArrayList<TimeInterval> timeInterval = ParserUtil.parseInterval(argMultimap.getAllValues(PREFIX_ENDINTERVAL));
timeInterval.add(0, firstInterval);
ArrayList<TimeInterval> timeInterval = ParserUtil.parseInterval(argMultimap.getAllValues(PREFIX_FREETIME));

if (TimeInterval.isTimeIntervalOverlap(timeInterval)) {
throw new ParseException(TimeInterval.MESSAGE_CONSTRAINTS_OVERLAP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
public class AddTimeCommandParser implements Parser<AddTimeCommand> {

@Override
public AddTimeCommand parse(String userInput) throws ParseException {
public AddTimeCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(userInput, PREFIX_NAME, PREFIX_FREETIME);
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_FREETIME);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_FREETIME)
|| !argMultimap.getPreamble().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.*;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUPTAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUPREMARK;

import seedu.address.logic.commands.GroupRemarkCommand;
import seedu.address.logic.parser.exceptions.ParseException;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public interface Model {

String addTimeToPerson(Name toAddPerson, ArrayList<TimeInterval> toAddTime) throws CommandException;
TimeIntervalList getTimeFromPerson(Name personName) throws CommandException;
void deleteTimeFromPerson(Name personName, ArrayList<TimeInterval> toDeleteTime) throws CommandException;
String deleteTimeFromPerson(Name personName, ArrayList<TimeInterval> toDeleteTime) throws CommandException;

//=========== Group functions ============================================================================

Expand Down Expand Up @@ -133,7 +133,7 @@ public interface Model {

String addTimeToGroup(Group toAdd, ArrayList<TimeInterval> toAddTime) throws CommandException;

void deleteTimeFromGroup(Group group, ArrayList<TimeInterval> toDeleteTime) throws CommandException;
String deleteTimeFromGroup(Group group, ArrayList<TimeInterval> toDeleteTime) throws CommandException;

TimeIntervalList getTimeFromGroup(Group group) throws CommandException;

Expand Down
14 changes: 8 additions & 6 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,17 @@ public String addTimeToPerson(Name toAddPerson, ArrayList<TimeInterval> toAddTim
* @throws CommandException if time does not exist
*/
@Override
public void deleteTimeFromPerson(Name personName,
public String deleteTimeFromPerson(Name personName,
ArrayList<TimeInterval> toDeleteTime) throws CommandException {
requireNonNull(personName);
Person person = addressBook.getPerson(personName.fullName);
try {
person.deleteFreeTime(toDeleteTime);
String status = person.deleteFreeTime(toDeleteTime);
forceUpdateList();
return status;
} catch (CommandException e) {
throw new CommandException(e.getMessage());
}
forceUpdateList();
}

public TimeIntervalList getTimeFromPerson(Name personName) throws CommandException {
Expand Down Expand Up @@ -256,17 +257,18 @@ public String addTimeToGroup(Group toAdd, ArrayList<TimeInterval> toAddTime) thr
* @throws CommandException if time does not exist
*/
@Override
public void deleteTimeFromGroup(Group group,
public String deleteTimeFromGroup(Group group,
ArrayList<TimeInterval> toDeleteTime) throws CommandException {
requireNonNull(group);
Group groupToDeleteTime = addressBook.getGroup(group.getGroupName());
try {
groupToDeleteTime.deleteTime(toDeleteTime);
String status = groupToDeleteTime.deleteTime(toDeleteTime);
forceUpdateList();
return status;
} catch (CommandException e) {
forceUpdateList();
throw new CommandException(e.getMessage());
}
forceUpdateList();
}

public Group addGroupRemark(String groupName, GroupRemark groupRemark) throws CommandException {
Expand Down
53 changes: 34 additions & 19 deletions src/main/java/seedu/address/model/TimeIntervalList.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,32 @@ public Stream<TimeInterval> toStream() {
}

public String addTime(ArrayList<TimeInterval> timeIntervals) throws CommandException{
boolean timeAdded = false;
boolean isPass = false;
boolean isFail = false;
StringBuilder errorCompilation = new StringBuilder();
StringBuilder passMessage = new StringBuilder();
errorCompilation.append("There is a clash in these input timings with your existing timing:\n");
for (TimeInterval interval : timeIntervals) {
if (isTimeIntervalOverlap(interval)) {
isFail = true;
errorCompilation.append(interval + "\n");
} else {
if (!timeAdded) {
errorCompilation.append("The other times have been added:\n");
if (!isPass) {
passMessage.append("These times have been added:\n");
}
timeAdded = true;
isPass = true;
internalList.add(interval);
errorCompilation.append(interval.toString());
passMessage.append(interval.toString() + "\n");
}
}

if ((timeAdded && errorCompilation.length() > 67) || (!timeAdded && errorCompilation.length() > 35)) {
if (isFail && isPass) {
return errorCompilation.append(passMessage).toString();
} else if (isFail) {
return errorCompilation.toString();
} else {
return passMessage.toString();
}
throw new CommandException("Something went wrong at addtime in timeintervallist class");
}

public void addAll(TimeIntervalList timeIntervalList) {
Expand All @@ -57,23 +63,32 @@ public void addAll(TimeIntervalList timeIntervalList) {
}
}

public void deleteTime(ArrayList<TimeInterval> timeIntervals) throws CommandException {
boolean timeDeleted = false;
public String deleteTime(ArrayList<TimeInterval> timeIntervals) throws CommandException {
boolean isPass = false;
boolean isFail = false;
StringBuilder errorCompilation = new StringBuilder();
StringBuilder passMessage = new StringBuilder();
errorCompilation.append("These times are not in the list:\n");
for (TimeInterval time : timeIntervals) {
if (!internalList.contains(time)) {
errorCompilation.append(time + "\n");
for (TimeInterval interval : timeIntervals) {
if (!internalList.contains(interval)) {
isFail = true;
errorCompilation.append(interval + "\n");
} else {
internalList.remove(time);
timeDeleted = true;
if (!isPass) {
passMessage.append("These times have been added:\n");
}
isPass = true;
internalList.remove(interval);
passMessage.append(interval.toString() + "\n");
}
}
if (timeDeleted) {
errorCompilation.append("The other times have been deleted\n");
}
if ((timeDeleted && errorCompilation.length() > 87) || (!timeDeleted && errorCompilation.length() > 53)) {
throw new CommandException(errorCompilation.toString());

if (isFail && isPass) {
return errorCompilation.append(passMessage).toString();
} else if (isFail) {
return errorCompilation.toString();
} else {
return passMessage.toString();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/group/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public TimeIntervalList getTime() {
return this.timeIntervalList;
}

public void deleteTime(ArrayList<TimeInterval> toDeleteTime) throws CommandException { this.timeIntervalList.deleteTime(toDeleteTime);}
public String deleteTime(ArrayList<TimeInterval> toDeleteTime) throws CommandException { return this.timeIntervalList.deleteTime(toDeleteTime);}

@Override
public boolean equals(Object group) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ public String addFreeTime(ArrayList<TimeInterval> toAddFreeTime) throws CommandE
return this.timeIntervalList.addTime(toAddFreeTime);
}

public void deleteFreeTime(ArrayList<TimeInterval> toAddFreeTime) throws CommandException {
this.timeIntervalList.deleteTime(toAddFreeTime);
public String deleteFreeTime(ArrayList<TimeInterval> toAddFreeTime) throws CommandException {
return this.timeIntervalList.deleteTime(toAddFreeTime);
}

public boolean hasFreeTime(TimeInterval freeTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public TimeIntervalList getTimeFromPerson(Name personName) throws CommandExcepti
}

@Override
public void deleteTimeFromPerson(Name personName, ArrayList<TimeInterval> listOfTimesToDelete) throws CommandException {
public String deleteTimeFromPerson(Name personName, ArrayList<TimeInterval> listOfTimesToDelete) throws CommandException {
throw new AssertionError("This method should not be called.");
}

Expand All @@ -225,7 +225,7 @@ public String addTimeToGroup(Group toAdd, ArrayList<TimeInterval> toAddTime) thr
}

@Override
public void deleteTimeFromGroup(Group group,
public String deleteTimeFromGroup(Group group,
ArrayList<TimeInterval> toDeleteTime) throws CommandException {
throw new AssertionError("This method should not be called.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public TimeIntervalList getTimeFromPerson(Name personName) throws CommandExcepti
}

@Override
public void deleteTimeFromPerson(Name personName, ArrayList<TimeInterval> listOfTimesToDelete) throws CommandException {
public String deleteTimeFromPerson(Name personName, ArrayList<TimeInterval> listOfTimesToDelete) throws CommandException {
throw new AssertionError("This method should not be called.");
}

Expand All @@ -225,7 +225,7 @@ public String addTimeToGroup(Group toAdd, ArrayList<TimeInterval> toAddTime) thr
}

@Override
public void deleteTimeFromGroup(Group group,
public String deleteTimeFromGroup(Group group,
ArrayList<TimeInterval> toDeleteTime) throws CommandException {
throw new AssertionError("This method should not be called.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public TimeIntervalList getTimeFromPerson(Name personName) throws CommandExcepti
}

@Override
public void deleteTimeFromPerson(Name personName, ArrayList<TimeInterval> listOfTimesToDelete) throws CommandException {
public String deleteTimeFromPerson(Name personName, ArrayList<TimeInterval> listOfTimesToDelete) throws CommandException {
throw new AssertionError("This method should not be called.");
}

Expand All @@ -246,7 +246,7 @@ public String addTimeToGroup(Group toAdd, ArrayList<TimeInterval> toAddTime) thr
}

@Override
public void deleteTimeFromGroup(Group group,
public String deleteTimeFromGroup(Group group,
ArrayList<TimeInterval> toDeleteTime) throws CommandException {
throw new AssertionError("This method should not be called.");
}
Expand Down Expand Up @@ -288,12 +288,12 @@ public boolean hasTime(TimeInterval timeInterval) {
}

@Override
public void deleteTimeFromGroup(Group group,
public String deleteTimeFromGroup(Group group,
ArrayList<TimeInterval> toDeleteTime) throws CommandException {
requireNonNull(group);
Group groupInModel = this.group;
try {
groupInModel.deleteTime(toDeleteTime);
return groupInModel.deleteTime(toDeleteTime);
} catch (CommandException e) {
throw new CommandException(e.getMessage());
}
Expand Down Expand Up @@ -323,12 +323,12 @@ public boolean hasTime(TimeInterval timeInterval) {
}

@Override
public void deleteTimeFromGroup(Group group,
public String deleteTimeFromGroup(Group group,
ArrayList<TimeInterval> toDeleteTime) throws CommandException {
requireNonNull(group);
Group groupInModel = this.group;
try {
groupInModel.deleteTime(toDeleteTime);
return groupInModel.deleteTime(toDeleteTime);
} catch (CommandException e) {
throw new CommandException(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public TimeIntervalList getTimeFromPerson(Name personName) throws CommandExcepti
}

@Override
public void deleteTimeFromPerson(Name personName, ArrayList<TimeInterval> listOfTimesToDelete) throws CommandException {
public String deleteTimeFromPerson(Name personName, ArrayList<TimeInterval> listOfTimesToDelete) throws CommandException {
throw new AssertionError("This method should not be called.");
}

Expand All @@ -240,7 +240,7 @@ public String addTimeToGroup(Group toAdd, ArrayList<TimeInterval> toAddTime) thr
}

@Override
public void deleteTimeFromGroup(Group group,
public String deleteTimeFromGroup(Group group,
ArrayList<TimeInterval> toDeleteTime) throws CommandException {
throw new AssertionError("This method should not be called.");
}
Expand Down Expand Up @@ -283,12 +283,12 @@ public boolean hasTime(TimeInterval timeInterval) {
}

@Override
public void deleteTimeFromPerson(Name personName,
public String deleteTimeFromPerson(Name personName,
ArrayList<TimeInterval> toDeleteTime) throws CommandException {
requireNonNull(personName);
Person person = this.person;
try {
person.deleteFreeTime(toDeleteTime);
return person.deleteFreeTime(toDeleteTime);
} catch (CommandException e) {
throw new CommandException(e.getMessage());
}
Expand Down Expand Up @@ -319,12 +319,12 @@ public boolean hasTime(TimeInterval timeInterval) {
}

@Override
public void deleteTimeFromPerson(Name personName,
public String deleteTimeFromPerson(Name personName,
ArrayList<TimeInterval> toDeleteTime) throws CommandException {
requireNonNull(personName);
Person person = this.person;
try {
person.deleteFreeTime(toDeleteTime);
return person.deleteFreeTime(toDeleteTime);
} catch (CommandException e) {
throw new CommandException(e.getMessage());
}
Expand Down

0 comments on commit 885d380

Please sign in to comment.