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

Add tests for Add, Edit and Delete commands and optimise code #115

Merged
merged 39 commits into from
Mar 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
84edbcd
v1.1
seanieyap Mar 3, 2019
7715c8d
fix some errors and typos
seanieyap Mar 3, 2019
2b79726
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/main
seanieyap Mar 3, 2019
a59a7db
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/main
seanieyap Mar 10, 2019
e94d3a2
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/main
seanieyap Mar 17, 2019
984e459
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/main
seanieyap Mar 17, 2019
3dc81e8
Refactor entire project, remove all traces of Addressbook
seanieyap Mar 17, 2019
474d1d4
Update developer guide for ExportCommandP (#80)
macchazuki Mar 17, 2019
219cd79
Update documentation for view command (#81)
dingheng4448 Mar 17, 2019
669099d
Remove remaining Addressbook classes
seanieyap Mar 17, 2019
262e12c
Refactor some classes
seanieyap Mar 17, 2019
02322e1
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/main
seanieyap Mar 17, 2019
db66222
fix missing files issue
seanieyap Mar 17, 2019
a73e964
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/main
seanieyap Mar 17, 2019
3eb2fe4
Add test cases for add function, Utils and other code enhancements
seanieyap Mar 18, 2019
7913f99
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/main
seanieyap Mar 18, 2019
348b702
fix codacy issues
seanieyap Mar 18, 2019
461da9e
fix codacy issues
seanieyap Mar 18, 2019
16f2c7e
Add tests for Utils
seanieyap Mar 18, 2019
5add3ce
Update Ui.png
seanieyap Mar 18, 2019
586fd41
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/main
seanieyap Mar 18, 2019
edf9da0
Add diagrams
seanieyap Mar 18, 2019
fca2e67
Update Ui.png
seanieyap Mar 18, 2019
4fb7462
fix issue with Ui.png
seanieyap Mar 18, 2019
30b9285
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/main
seanieyap Mar 18, 2019
1b69c0e
Update Codacy Badge link due to reinitialization.
seanieyap Mar 18, 2019
c6d307d
Enhance ordering of slots printed
seanieyap Mar 19, 2019
9f58e44
Update UserGuide.adoc
seanieyap Mar 19, 2019
cc03c36
Update UserGuide.adoc
seanieyap Mar 19, 2019
0c808b0
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/main
seanieyap Mar 19, 2019
320e5ae
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/main
seanieyap Mar 19, 2019
266f244
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/main
seanieyap Mar 20, 2019
387aa71
Update UserGuide.adoc with feedback from peers
seanieyap Mar 20, 2019
8784d69
Update UserGuide.adoc with nicer tables
seanieyap Mar 20, 2019
1cbf2c6
Update UserGuide.adoc: fix formatting issues
seanieyap Mar 20, 2019
078b3c0
UserGuide.adoc Remove potential Netlify breaking code
seanieyap Mar 21, 2019
76c32b2
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/main
seanieyap Mar 23, 2019
087ccd9
Add tests for Add, Edit and Delete commands and optimise code
seanieyap Mar 23, 2019
d414041
Fix checkstyle error
seanieyap Mar 23, 2019
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
9 changes: 2 additions & 7 deletions src/planmysem/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public class AddCommand extends Command {
+ " n/CS2113T Tutorial d/mon st/08:00 et/09:00 des/Topic: Sequence Diagram t/CS2113T "
+ "t/Tutorial r/normal";

public static final String MESSAGE_SUCCESS_NO_CHANGE = "No slots were added.";
public static final String MESSAGE_SUCCESS = "%1$s Slots added.\n\n%2$s";
public static final String MESSAGE_FAIL_OUT_OF_BOUNDS = "Date specified is out of bounds.";

Expand Down Expand Up @@ -75,12 +74,8 @@ public CommandResult execute() {
}
}

if (dates.size() == 0) {
return new CommandResult(MESSAGE_SUCCESS_NO_CHANGE);
} else {
return new CommandResult(String.format(MESSAGE_SUCCESS, dates.size(),
craftSuccessMessage(days, slot)));
}
return new CommandResult(String.format(MESSAGE_SUCCESS, dates.size(),
craftSuccessMessage(days, slot)));
}

/**
Expand Down
40 changes: 25 additions & 15 deletions src/planmysem/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
import static planmysem.ui.Gui.DISPLAYED_INDEX_OFFSET;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

import javafx.util.Pair;
import planmysem.common.Messages;
import planmysem.data.Planner;
import planmysem.data.semester.ReadOnlyDay;
import planmysem.data.slot.ReadOnlySlot;

/**
* Represents an executable command.
*/
public abstract class Command {
protected Planner planner;
protected List<Pair<LocalDate, ? extends ReadOnlySlot>> relevantSlots;
protected Map<LocalDate, Pair<ReadOnlyDay, ReadOnlySlot>> relevantSlots;
private int targetIndex = -1;

/**
Expand Down Expand Up @@ -46,14 +47,15 @@ protected Command() {
* @param slots used to generate summary
* @return summary message for persons displayed
*/
public static String getMessageForSlotsListShownSummary(List<Pair<LocalDate, ? extends ReadOnlySlot>> slots) {
public static String getMessageForSlotsListShownSummary(Map<LocalDate,
Pair<ReadOnlyDay, ReadOnlySlot>> slots) {
return String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, slots.size());
}

/**
* Supplies the data the command will operate on.
*/
public void setData(Planner planner, List<Pair<LocalDate, ? extends ReadOnlySlot>> slots) {
public void setData(Planner planner, Map<LocalDate, Pair<ReadOnlyDay, ReadOnlySlot>> slots) {
this.planner = planner;
this.relevantSlots = slots;
}
Expand All @@ -62,25 +64,33 @@ public int getTargetIndex() {
return targetIndex;
}

public void setTargetIndex(int targetIndex) {
this.targetIndex = targetIndex;
}
// public void setTargetIndex(int targetIndex) {
// this.targetIndex = targetIndex;
// }


/**
* Extracts the the target Day in the last shown list from the given arguments.
*
* @throws IndexOutOfBoundsException if the target index is out of bounds of the last viewed listing
*/
protected List<Pair<LocalDate, ? extends ReadOnlySlot>> getTargetSlots() throws IndexOutOfBoundsException {
return relevantSlots;
}
// protected Map<LocalDate, Pair<ReadOnlyDay, ReadOnlySlot>> getTargetSlots() throws IndexOutOfBoundsException {
// return relevantSlots;
// }

protected Pair<LocalDate, ? extends ReadOnlySlot> getTargetSlot(int index) throws IndexOutOfBoundsException {
return relevantSlots.get(index);
}
protected Pair<LocalDate, Pair<ReadOnlyDay, ReadOnlySlot>> getTargetSlot() throws IndexOutOfBoundsException {
if (relevantSlots == null || relevantSlots.size() < targetIndex) {
throw new IndexOutOfBoundsException();
}

int count = 0;
for (Map.Entry<LocalDate, Pair<ReadOnlyDay, ReadOnlySlot>> entry : relevantSlots.entrySet()) {
if (count == targetIndex - DISPLAYED_INDEX_OFFSET) {
return new Pair(entry.getKey(), entry.getValue());
}
count++;
}

protected Pair<LocalDate, ? extends ReadOnlySlot> getTargetSlot() throws IndexOutOfBoundsException {
return relevantSlots.get(targetIndex - DISPLAYED_INDEX_OFFSET);
throw new IndexOutOfBoundsException();
}
}
9 changes: 5 additions & 4 deletions src/planmysem/commands/CommandResult.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package planmysem.commands;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javafx.util.Pair;
import planmysem.data.semester.ReadOnlyDay;
import planmysem.data.slot.ReadOnlySlot;

/**
Expand All @@ -20,22 +21,22 @@ public class CommandResult {
/**
* The list of Slots that was produced by the command
*/
private final List<Pair<LocalDate, ? extends ReadOnlySlot>> slots;
private final Map<LocalDate, Pair<ReadOnlyDay, ReadOnlySlot>> slots;

public CommandResult(String feedbackToUser) {
this.feedbackToUser = feedbackToUser;
slots = null;
}

public CommandResult(String feedbackToUser, List<Pair<LocalDate, ? extends ReadOnlySlot>> slots) {
public CommandResult(String feedbackToUser, Map<LocalDate, Pair<ReadOnlyDay, ReadOnlySlot>> slots) {
this.feedbackToUser = feedbackToUser;
this.slots = slots;
}

/**
* Returns list of Slots relevant to the command command result, if any.
*/
public Optional<List<Pair<LocalDate, ? extends ReadOnlySlot>>> getRelevantSlots() {
public Optional<Map<LocalDate, Pair<ReadOnlyDay, ReadOnlySlot>>> getRelevantSlots() {
return Optional.ofNullable(slots);
}

Expand Down
63 changes: 9 additions & 54 deletions src/planmysem/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package planmysem.commands;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
Expand All @@ -11,9 +10,8 @@
import planmysem.common.Messages;
import planmysem.common.Utils;
import planmysem.data.exception.IllegalValueException;
import planmysem.data.semester.Day;
import planmysem.data.semester.ReadOnlyDay;
import planmysem.data.slot.ReadOnlySlot;
import planmysem.data.slot.Slot;
import planmysem.data.tag.Tag;

/**
Expand All @@ -33,11 +31,8 @@ public class DeleteCommand extends Command {
+ "\n\tExample 2: " + COMMAND_WORD
+ " 2";


public static final String MESSAGE_SUCCESS_NO_CHANGE = "No Slots were deleted.\n\n%1$s";
public static final String MESSAGE_SUCCESS = "%1$s Slots deleted.\n\n%2$s\n%3$s";
public static final String MESSAGE_FAIL_ILLEGAL_VALUE = MESSAGE_SUCCESS_NO_CHANGE
+ " Illegal characters were detected.";

private final Set<Tag> tags = new HashSet<>();

Expand All @@ -59,70 +54,30 @@ public DeleteCommand(int index) {

@Override
public CommandResult execute() {
Map<LocalDateTime, ReadOnlySlot> selectedSlots = new TreeMap<>();
Map<LocalDate, Pair<ReadOnlyDay, ReadOnlySlot>> selectedSlots = new TreeMap<>();

if (getTargetIndex() == -1) {
for (Map.Entry<LocalDate, Day> day : planner.getSemester().getDays().entrySet()) {
for (Slot slot : day.getValue().getSlots()) {
if (slot.getTags().containsAll(tags)) {
selectedSlots.put(LocalDateTime.of(day.getKey(), slot.getStartTime()), slot);
}
}
}
selectedSlots.putAll(planner.getSlots(tags));

if (selectedSlots.size() == 0) {
return new CommandResult(String.format(MESSAGE_SUCCESS_NO_CHANGE,
Messages.craftSelectedMessage(tags)));
}
} else {
try {
final Pair<LocalDate, ? extends ReadOnlySlot> target = getTargetSlot();
selectedSlots.put(LocalDateTime.of(target.getKey(),
target.getValue().getStartTime()), target.getValue());

if (!planner.containsSlot(target.getKey(), target.getValue())) {
return new CommandResult(Messages.MESSAGE_SLOT_NOT_IN_PLANNER);
}
final Pair<LocalDate, Pair<ReadOnlyDay, ReadOnlySlot>> target = getTargetSlot();
selectedSlots.put(target.getKey(), target.getValue());
} catch (IndexOutOfBoundsException ie) {
return new CommandResult(Messages.MESSAGE_INVALID_SLOT_DISPLAYED_INDEX);
}
}

// perform deletion of slots from the planner
for (Map.Entry<LocalDateTime, ? extends ReadOnlySlot> slot: selectedSlots.entrySet()) {
planner.getSemester().removeSlot(slot.getKey().toLocalDate(), slot.getValue());
for (Map.Entry<LocalDate, Pair<ReadOnlyDay, ReadOnlySlot>> entry: selectedSlots.entrySet()) {
planner.removeSlot(entry.getKey(), entry.getValue().getValue());
}

return new CommandResult(String.format(MESSAGE_SUCCESS, selectedSlots.size(),
Messages.craftSelectedMessage(tags), craftSuccessMessage(selectedSlots)));
Messages.craftSelectedMessage(tags), Messages.craftSelectedMessage("Deleted Slots:", selectedSlots)));
}

/**
* Craft success message.
*/
private String craftSuccessMessage(Map<LocalDateTime, ReadOnlySlot> selectedSlots) {
StringBuilder sb = new StringBuilder();

sb.append("Deleted Slots: ");
sb.append("\n");

int count = 1;
for (Map.Entry<LocalDateTime, ReadOnlySlot> editedSlot : selectedSlots.entrySet()) {
sb.append(count);
sb.append(".\t");
sb.append(editedSlot.getValue().getName().toString());
sb.append(", ");
sb.append(editedSlot.getKey().toLocalDate().toString());
sb.append(" ");
sb.append(editedSlot.getKey().toLocalTime().toString());
sb.append(", ");
sb.append(planner.getSemester().getDays().get(editedSlot.getKey().toLocalDate()).getType());
sb.append(", ");
sb.append(editedSlot.getKey().getDayOfWeek().toString());
count++;
sb.append("\n");
}

return sb.toString();
}

}
Loading