From 8aada2999eaf7e9ed51868ed2408def2491d34a4 Mon Sep 17 00:00:00 2001 From: ciaoosuuu <> Date: Sun, 23 Oct 2022 00:37:59 +0800 Subject: [PATCH] Add itinerary methods --- .../waddle/commons/core/index/MultiIndex.java | 8 ++++ .../java/seedu/waddle/model/item/Day.java | 4 ++ .../waddle/model/itinerary/Itinerary.java | 44 ++++++++++++++++--- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/main/java/seedu/waddle/commons/core/index/MultiIndex.java b/src/main/java/seedu/waddle/commons/core/index/MultiIndex.java index 68ee3ffbdd8..41895fd382c 100644 --- a/src/main/java/seedu/waddle/commons/core/index/MultiIndex.java +++ b/src/main/java/seedu/waddle/commons/core/index/MultiIndex.java @@ -42,6 +42,14 @@ public Index getIndex(int pos) { return indices.get(pos - 1); } + public Index getDayIndex() { + return getIndex(1); + } + + public Index getTaskIndex() { + return getIndex(2); + } + private boolean isValidPos(int pos) { diff --git a/src/main/java/seedu/waddle/model/item/Day.java b/src/main/java/seedu/waddle/model/item/Day.java index e0cec4235aa..5699ae24049 100644 --- a/src/main/java/seedu/waddle/model/item/Day.java +++ b/src/main/java/seedu/waddle/model/item/Day.java @@ -61,6 +61,10 @@ public Item removeItem(Index index) { return removedItem; } + public Item getItem(Index index) { + return this.itemList.get(index.getZeroBased()); + } + /** * Deletes the day. Resets the startTime field of all items in this day. * diff --git a/src/main/java/seedu/waddle/model/itinerary/Itinerary.java b/src/main/java/seedu/waddle/model/itinerary/Itinerary.java index ad2fd9cfa68..b954df1f982 100644 --- a/src/main/java/seedu/waddle/model/itinerary/Itinerary.java +++ b/src/main/java/seedu/waddle/model/itinerary/Itinerary.java @@ -7,6 +7,8 @@ import java.util.List; import java.util.Objects; +import seedu.waddle.commons.core.index.MultiIndex; +import seedu.waddle.logic.commands.exceptions.CommandException; import seedu.waddle.model.item.Day; import seedu.waddle.model.item.Item; import seedu.waddle.model.item.UniqueItemList; @@ -124,6 +126,22 @@ public void setItem(Item target, Item editedItem) { unscheduledItemList.setItem(target, editedItem); } + public void setItem(Item target, Item editedItem, MultiIndex index) throws CommandException { + if (index.getDayIndex() == null) { + this.unscheduledItemList.setItem(target, editedItem); + sortUnscheduledItemList(); + } else { + Day day = this.days.get(index.getDayIndex().getZeroBased()); + day.removeItem(index.getTaskIndex()); + try { + day.addItem(editedItem); + } catch (CommandException e) { + day.addItem(target); + throw e; + } + } + } + public int getItemSize() { return this.unscheduledItemList.getSize(); } @@ -136,15 +154,26 @@ private void sortUnscheduledItemList() { this.unscheduledItemList.sort(priorityComparator); } - /* public void unplanItem(MultiIndex index) { - Day day = this.days.get(index.getDayIndex()); + /** + * Unplan an item. + * @param index A multiIndex to locate the day and index of task within the day + */ + public void unplanItem(MultiIndex index) { + Day day = this.days.get(index.getDayIndex().getZeroBased()); Item unplannedItem = day.removeItem(index.getTaskIndex()); addItem(unplannedItem); - this.unscheduledItemList.sort(priorityComparator); + sortUnscheduledItemList(); this.budget.updateSpending(-unplannedItem.getCost().getValue()); } - public void planItem(int itemIndex, int dayIndex, int startTime) { + /** + * Plan an item. + * @param itemIndex Index of item in unscheduled list. + * @param dayIndex Day to include this item. + * @param startTime startTime of the item. + * @throws CommandException When adding item to specific day leads to conflict in time. + */ + public void planItem(int itemIndex, int dayIndex, int startTime) throws CommandException { Item item = this.unscheduledItemList.get(itemIndex); Day day = this.days.get(dayIndex); day.addItem(item); @@ -154,12 +183,13 @@ public void planItem(int itemIndex, int dayIndex, int startTime) { public Item getItem(MultiIndex index) { if (index.getDayIndex() == null) { - return this.unscheduledItemList.get(index.getTaskIndex()); + return this.unscheduledItemList.get(index.getTaskIndex().getZeroBased()); } else { - Day day = this.days.get(index.getDayIndex()); + Day day = this.days.get(index.getDayIndex().getZeroBased()); return day.getItem(index.getTaskIndex()); } - } */ + } + /** * Returns true if both itineraries have the same identity and data fields. * This defines a stronger notion of equality between two itineraries.