Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2223S1#82 from ciaoosuuu/change-itine…
Browse files Browse the repository at this point in the history
…rary

Edit commands
  • Loading branch information
ciaoosuuu authored Oct 22, 2022
2 parents 6d2ed66 + 3dbca41 commit f0e9288
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 47 deletions.
10 changes: 10 additions & 0 deletions src/main/java/seedu/waddle/commons/core/index/MultiIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public void appendOneBasedIndex(int index) {
indices.add(Index.fromOneBased(index));
}

public void addIndex(Index index) {
indices.add(index);
}

/**
* Removes indices more than or equal to specified position.
*
Expand All @@ -43,10 +47,16 @@ public Index getIndex(int pos) {
}

public Index getDayIndex() {
if (this.indices.size() == 1) {
return null;
}
return getIndex(1);
}

public Index getTaskIndex() {
if (this.indices.size() == 1) {
return getIndex(1);
}
return getIndex(2);
}

Expand Down
23 changes: 17 additions & 6 deletions src/main/java/seedu/waddle/logic/commands/DeleteItemCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import static java.util.Objects.requireNonNull;

import seedu.waddle.commons.core.Messages;
import seedu.waddle.commons.core.index.Index;
import seedu.waddle.commons.core.index.MultiIndex;
import seedu.waddle.logic.StageManager;
import seedu.waddle.logic.commands.exceptions.CommandException;
import seedu.waddle.model.Model;
import seedu.waddle.model.item.Day;
import seedu.waddle.model.item.Item;
import seedu.waddle.model.itinerary.Itinerary;

Expand All @@ -26,12 +27,12 @@ public class DeleteItemCommand extends Command {

public static final String MESSAGE_DELETE_ITINERARY_SUCCESS = "Deleted item: %1$s";

private final Index targetIndex;
private final MultiIndex targetIndex;

/**
* Creates an AddItemCommand to add the specified {@code Item}
*/
public DeleteItemCommand(Index targetIndex) {
public DeleteItemCommand(MultiIndex targetIndex) {
this.targetIndex = targetIndex;
}

Expand All @@ -51,10 +52,20 @@ public CommandResult execute(Model model) throws CommandException {
*/
Itinerary itinerary = stageManager.getSelectedItinerary();

if (targetIndex.getZeroBased() >= itinerary.getItemSize()) {
throw new CommandException(Messages.MESSAGE_INVALID_ITEM_DISPLAYED_INDEX);
if (targetIndex.getDayIndex() == null) {
if (targetIndex.getTaskIndex().getZeroBased() >= itinerary.getItemSize()) {
throw new CommandException(Messages.MESSAGE_INVALID_ITEM_DISPLAYED_INDEX);
}
} else {
if (targetIndex.getDayIndex().getZeroBased() >= itinerary.getDuration().getValue()) {
throw new CommandException(Messages.MESSAGE_INVALID_ITEM_DISPLAYED_INDEX);
}
Day day = itinerary.getDays().get(targetIndex.getDayIndex().getZeroBased());
if (targetIndex.getTaskIndex().getZeroBased() >= day.getItemSize()) {
throw new CommandException(Messages.MESSAGE_INVALID_ITEM_DISPLAYED_INDEX);
}
}
Item itemToDelete = itinerary.removeItem(targetIndex.getZeroBased());
Item itemToDelete = itinerary.removeItem(targetIndex);
return new CommandResult(String.format(MESSAGE_DELETE_ITINERARY_SUCCESS, itemToDelete));
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/seedu/waddle/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ private static Itinerary createEditedItinerary(Itinerary itineraryToEdit,
People updatedPeople = editItineraryDescriptor.getPeople().orElse(itineraryToEdit.getPeople());
Budget updatedBudget = editItineraryDescriptor.getBudget().orElse(itineraryToEdit.getBudget());

return new Itinerary(updatedName, updatedCountry, updatedStartDate, updatedDuration,
Itinerary editedItinerary = new Itinerary(updatedName, updatedCountry, updatedStartDate, updatedDuration,
updatedPeople, updatedBudget);
editedItinerary.setSpending(itineraryToEdit.getBudget());
editedItinerary.setDays(itineraryToEdit.getDays());
return editedItinerary;
}

@Override
Expand Down
68 changes: 46 additions & 22 deletions src/main/java/seedu/waddle/logic/commands/EditItemCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
import static seedu.waddle.logic.parser.CliSyntax.PREFIX_PRIORITY;
import static seedu.waddle.logic.parser.CliSyntax.PREFIX_START_TIME;

import java.time.LocalTime;
import java.util.Optional;

import seedu.waddle.commons.core.Messages;
import seedu.waddle.commons.core.index.Index;
import seedu.waddle.commons.core.index.MultiIndex;
import seedu.waddle.commons.util.CollectionUtil;
import seedu.waddle.logic.StageManager;
import seedu.waddle.logic.commands.exceptions.CommandException;
import seedu.waddle.model.Model;
import seedu.waddle.model.item.Cost;
import seedu.waddle.model.item.Day;
import seedu.waddle.model.item.Duration;
import seedu.waddle.model.item.Item;
import seedu.waddle.model.item.Priority;
import seedu.waddle.model.item.StartTime;
import seedu.waddle.model.itinerary.Itinerary;

/**
Expand All @@ -45,18 +46,20 @@ public class EditItemCommand extends Command {
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_DUPLICATE_ITEM = "This item already exists.";

private final Index index;
public static final String MESSAGE_EDIT_START_TIME = "Editing start time of unscheduled item is not allowed";

private final MultiIndex multiIndex;
private final EditItemDescriptor editItemDescriptor;

/**
* @param index of the item in the unique item list to edit
* @param multiIndex of the item in the unique item list to edit
* @param editItemDescriptor details to edit the item with
*/
public EditItemCommand(Index index, EditItemDescriptor editItemDescriptor) {
requireNonNull(index);
public EditItemCommand(MultiIndex multiIndex, EditItemDescriptor editItemDescriptor) {
requireNonNull(multiIndex);
requireNonNull(editItemDescriptor);

this.index = index;
this.multiIndex = multiIndex;
this.editItemDescriptor = new EditItemDescriptor(editItemDescriptor);
}

Expand All @@ -70,8 +73,13 @@ private static Item createEditedItem(Item itemToEdit,

String updatedDescription = editItemDescriptor.getDescription().orElse(itemToEdit.getDescription());
Priority updatedPriority = editItemDescriptor.getPriority().orElse(itemToEdit.getPriority());
Cost updatedCost = editItemDescriptor.getCost().orElse(itemToEdit.getCost());
Duration updatedDuration = editItemDescriptor.getDuration().orElse(itemToEdit.getDuration());

Item editedItem = new Item(updatedDescription, updatedPriority, updatedCost, updatedDuration);
editedItem.setStartTime(editItemDescriptor.getStartTime().orElse(itemToEdit.getStartTime()));

return new Item(updatedDescription, updatedPriority, itemToEdit.getCost(), itemToEdit.getDuration());
return editedItem;
}

@Override
Expand All @@ -80,19 +88,35 @@ public CommandResult execute(Model model) throws CommandException {
StageManager stageManager = StageManager.getInstance();
Itinerary itinerary = stageManager.getSelectedItinerary();

if (index.getZeroBased() >= itinerary.getItemSize()) {
throw new CommandException(Messages.MESSAGE_INVALID_ITEM_DISPLAYED_INDEX);
}
if (multiIndex.getDayIndex() == null) {
if (multiIndex.getTaskIndex().getZeroBased() >= itinerary.getItemSize()) {
throw new CommandException(Messages.MESSAGE_INVALID_ITEM_DISPLAYED_INDEX);
}
Item itemToEdit = itinerary.getItem(multiIndex);
Item editedItem = createEditedItem(itemToEdit, editItemDescriptor);
if (!itemToEdit.isSameItem(editedItem) && itinerary.hasItem(editedItem)) {
throw new CommandException(MESSAGE_DUPLICATE_ITEM);
}
itinerary.setItem(itemToEdit, editedItem, multiIndex);

Item itemToEdit = itinerary.getItemList().get(index.getZeroBased());
Item editedItem = createEditedItem(itemToEdit, editItemDescriptor);
return new CommandResult(String.format(MESSAGE_EDIT_ITEM_SUCCESS, editedItem));
} else {
if (multiIndex.getDayIndex().getZeroBased() >= itinerary.getDuration().getValue()) {
throw new CommandException(Messages.MESSAGE_INVALID_ITEM_DISPLAYED_INDEX);
}
Day day = itinerary.getDays().get(multiIndex.getDayIndex().getZeroBased());
if (multiIndex.getTaskIndex().getZeroBased() >= day.getItemSize()) {
throw new CommandException(Messages.MESSAGE_INVALID_ITEM_DISPLAYED_INDEX);
}
Item itemToEdit = itinerary.getItem(multiIndex);
Item editedItem = createEditedItem(itemToEdit, editItemDescriptor);
if (!itemToEdit.isSameItem(editedItem) && day.hasItem(editedItem)) {
throw new CommandException(MESSAGE_DUPLICATE_ITEM);
}
itinerary.setItem(itemToEdit, editedItem, multiIndex);

if (!itemToEdit.isSameItem(editedItem) && itinerary.hasItem(editedItem)) {
throw new CommandException(MESSAGE_DUPLICATE_ITEM);
return new CommandResult(String.format(MESSAGE_EDIT_ITEM_SUCCESS, editedItem));
}

itinerary.setItem(itemToEdit, editedItem);
return new CommandResult(String.format(MESSAGE_EDIT_ITEM_SUCCESS, editedItem));
}

@Override
Expand All @@ -109,7 +133,7 @@ public boolean equals(Object other) {

// state check
EditItemCommand e = (EditItemCommand) other;
return index.equals(e.index)
return multiIndex.equals(e.multiIndex)
&& editItemDescriptor.equals(e.editItemDescriptor);
}

Expand All @@ -122,7 +146,7 @@ public static class EditItemDescriptor {
private Priority priority;
private Cost cost;
private Duration duration;
private StartTime startTime;
private LocalTime startTime;

public EditItemDescriptor() {
}
Expand Down Expand Up @@ -178,11 +202,11 @@ public void setDuration(Duration duration) {
this.duration = duration;
}

public Optional<StartTime> getStartTime() {
public Optional<LocalTime> getStartTime() {
return Optional.ofNullable(startTime);
}

public void setStartTime(StartTime startTime) {
public void setStartTime(LocalTime startTime) {
this.startTime = startTime;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static seedu.waddle.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.waddle.commons.core.index.Index;
import seedu.waddle.commons.core.index.MultiIndex;
import seedu.waddle.logic.commands.DeleteItemCommand;
import seedu.waddle.logic.parser.exceptions.ParseException;

Expand All @@ -19,7 +19,7 @@ public class DeleteItemCommandParser implements Parser<DeleteItemCommand> {
*/
public DeleteItemCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
MultiIndex index = ParserUtil.parseMultiIndex(args);
return new DeleteItemCommand(index);
} catch (ParseException pe) {
throw new ParseException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static seedu.waddle.logic.parser.CliSyntax.PREFIX_PRIORITY;
import static seedu.waddle.logic.parser.CliSyntax.PREFIX_START_TIME;

import seedu.waddle.commons.core.index.Index;
import seedu.waddle.commons.core.index.MultiIndex;
import seedu.waddle.logic.commands.EditItemCommand;
import seedu.waddle.logic.parser.exceptions.ParseException;

Expand All @@ -28,10 +28,10 @@ public EditItemCommand parse(String args) throws ParseException {
ArgumentTokenizer.tokenize(args, PREFIX_DESCRIPTION, PREFIX_PRIORITY,
PREFIX_COST, PREFIX_DURATION, PREFIX_START_TIME);

Index index;
MultiIndex multiIndex;

try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
multiIndex = ParserUtil.parseMultiIndex(argMultimap.getPreamble());
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
EditItemCommand.MESSAGE_USAGE), pe);
Expand Down Expand Up @@ -59,6 +59,9 @@ public EditItemCommand parse(String args) throws ParseException {
}

if (argMultimap.getValue(PREFIX_START_TIME).isPresent()) {
if (multiIndex.getDayIndex() == null) {
throw new ParseException(EditItemCommand.MESSAGE_EDIT_START_TIME);
}
editItemDescriptor.setStartTime(
ParserUtil.parseStartTime(argMultimap.getValue(PREFIX_START_TIME).get()));
}
Expand All @@ -67,7 +70,7 @@ public EditItemCommand parse(String args) throws ParseException {
throw new ParseException(EditItemCommand.MESSAGE_NOT_EDITED);
}

return new EditItemCommand(index, editItemDescriptor);
return new EditItemCommand(multiIndex, editItemDescriptor);
}

}
33 changes: 28 additions & 5 deletions src/main/java/seedu/waddle/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import static java.util.Objects.requireNonNull;

import java.time.LocalTime;
import java.time.format.DateTimeParseException;

import seedu.waddle.commons.core.index.Index;
import seedu.waddle.commons.core.index.MultiIndex;
import seedu.waddle.commons.util.StringUtil;
import seedu.waddle.logic.parser.exceptions.ParseException;
import seedu.waddle.model.item.Cost;
import seedu.waddle.model.item.Duration;
import seedu.waddle.model.item.Priority;
import seedu.waddle.model.item.StartTime;
import seedu.waddle.model.itinerary.Budget;
import seedu.waddle.model.itinerary.Country;
import seedu.waddle.model.itinerary.Date;
Expand Down Expand Up @@ -37,6 +40,23 @@ public static Index parseIndex(String oneBasedIndex) throws ParseException {
return Index.fromOneBased(Integer.parseInt(trimmedIndex));
}

/**
* Parses {@code oneBasedMultiIndex} into an {@code MultiIndex} and returns it. Leading and trailing whitespaces
* will be trimmed.
* @throws ParseException if the specified MultiIndex is invalid (not non-zero unsigned integer).
*/
public static MultiIndex parseMultiIndex(String oneBasedMultiIndex) throws ParseException {
String trimmedMultiIndex = oneBasedMultiIndex.trim();
String[] oneBasedIndexList = trimmedMultiIndex.split(".", 2);
MultiIndex multiIndex = new MultiIndex();
for (String oneBasedIndex : oneBasedIndexList) {
Index index = parseIndex(oneBasedIndex);
multiIndex.addIndex(index);
}
return multiIndex;
}


/**
* Parses a {@code String name} into a {@code Name}.
* Leading and trailing whitespaces will be trimmed.
Expand Down Expand Up @@ -196,13 +216,16 @@ public static Duration parseDuration(String duration) throws ParseException {
*
* @throws ParseException if the given {@code startTime} is invalid.
*/
public static StartTime parseStartTime(String startTime) throws ParseException {
public static LocalTime parseStartTime(String startTime) throws ParseException {
requireNonNull(startTime);
String trimmedStartTime = startTime.trim();
if (!StartTime.isValidStartTime(trimmedStartTime)) {
throw new ParseException(StartTime.MESSAGE_CONSTRAINTS);
LocalTime time;
try {
time = LocalTime.parse(startTime);
} catch (DateTimeParseException e) {
throw new ParseException("Start time should be written in HH:MM:SS format. For example, 10:15 or 10:15:30");
}
return new StartTime(startTime);
return time;
}

}
8 changes: 8 additions & 0 deletions src/main/java/seedu/waddle/model/item/Day.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,12 @@ public ArrayList<Item> getConflictingItems(Item newItem) {
}
return conflictingItems;
}

public int getItemSize() {
return itemList.getSize();
}

public boolean hasItem(Item item) {
return this.itemList.contains(item);
}
}
8 changes: 8 additions & 0 deletions src/main/java/seedu/waddle/model/itinerary/Budget.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public float calculateLeftOverBudget() {
return this.initialBudget - this.spending;
}

public float getSpending() {
return this.spending;
}

public void setSpending(float amt) {
this.spending = amt;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
Expand Down
Loading

0 comments on commit f0e9288

Please sign in to comment.