From 14f33be3468daae38f27b0b6df69ae5d67f13de9 Mon Sep 17 00:00:00 2001 From: Clement Foo Date: Tue, 25 Oct 2022 16:01:13 +0800 Subject: [PATCH 1/4] Fix item index, make time Optional, make priority stars Integer --- .../logic/commands/EditItemCommand.java | 4 ++- .../seedu/waddle/logic/parser/ParserUtil.java | 5 +-- .../java/seedu/waddle/model/item/Day.java | 10 +++--- .../java/seedu/waddle/model/item/Item.java | 19 ++++++++--- .../seedu/waddle/model/item/Priority.java | 33 +++++++++---------- .../seedu/waddle/storage/JsonAdaptedItem.java | 14 ++++---- src/main/java/seedu/waddle/ui/ItemCard.java | 20 +++++++---- .../java/seedu/waddle/ui/ItemGroupCard.java | 8 ++--- .../java/seedu/waddle/ui/ItemListPanel.java | 6 ++-- src/main/resources/view/ItemListCard.fxml | 2 ++ 10 files changed, 70 insertions(+), 51 deletions(-) diff --git a/src/main/java/seedu/waddle/logic/commands/EditItemCommand.java b/src/main/java/seedu/waddle/logic/commands/EditItemCommand.java index 40a100cffbf..1592a485a87 100644 --- a/src/main/java/seedu/waddle/logic/commands/EditItemCommand.java +++ b/src/main/java/seedu/waddle/logic/commands/EditItemCommand.java @@ -77,7 +77,9 @@ private static Item createEditedItem(Item itemToEdit, Duration updatedDuration = editItemDescriptor.getDuration().orElse(itemToEdit.getDuration()); Item editedItem = new Item(updatedDescription, updatedPriority, updatedCost, updatedDuration); - editedItem.setStartTime(editItemDescriptor.getStartTime().orElse(itemToEdit.getStartTime())); + if (editItemDescriptor.getStartTime().isPresent()) { + editedItem.setStartTime(editItemDescriptor.getStartTime().get()); + } return editedItem; } diff --git a/src/main/java/seedu/waddle/logic/parser/ParserUtil.java b/src/main/java/seedu/waddle/logic/parser/ParserUtil.java index a2fba43493b..3285c61da87 100644 --- a/src/main/java/seedu/waddle/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/waddle/logic/parser/ParserUtil.java @@ -178,10 +178,11 @@ public static String parseDescription(String description) { public static Priority parsePriority(String priority) throws ParseException { requireNonNull(priority); String trimmedPriority = priority.trim(); - if (!Priority.isValidPriority(trimmedPriority)) { + Integer stars = Integer.parseInt(trimmedPriority); + if (!Priority.isValidPriority(stars)) { throw new ParseException(Priority.MESSAGE_CONSTRAINTS); } - return new Priority(trimmedPriority); + return new Priority(stars); } /** diff --git a/src/main/java/seedu/waddle/model/item/Day.java b/src/main/java/seedu/waddle/model/item/Day.java index 3553e5ff95e..fb46339b5ff 100644 --- a/src/main/java/seedu/waddle/model/item/Day.java +++ b/src/main/java/seedu/waddle/model/item/Day.java @@ -14,7 +14,7 @@ public class Day { private final Comparator startTimeComparator = new Comparator() { @Override public int compare(Item item1, Item item2) { - return item1.getStartTime().compareTo(item2.getStartTime()); + return item1.getStartTime().get().compareTo(item2.getStartTime().get()); } }; private final int dayNumber; @@ -90,11 +90,11 @@ public ArrayList getConflictingItems(Item newItem) { // same start time boolean sameStartTime = item.getStartTime().equals(newItem.getStartTime()); // start time of new item is within the duration of a preceding item - boolean startTimeConflict = newItem.getStartTime().isAfter(item.getStartTime()) - && newItem.getStartTime().isBefore(item.getEndTime()); + boolean startTimeConflict = newItem.getStartTime().get().isAfter(item.getStartTime().get()) + && newItem.getStartTime().get().isBefore(item.getEndTime().get()); // end time of new item eats into a proceeding item - boolean endTimeConflict = newItem.getEndTime().isAfter(item.getStartTime()) - && newItem.getEndTime().isBefore(item.getEndTime()); + boolean endTimeConflict = newItem.getEndTime().get().isAfter(item.getStartTime().get()) + && newItem.getEndTime().get().isBefore(item.getEndTime().get()); if (sameStartTime || startTimeConflict || endTimeConflict) { diff --git a/src/main/java/seedu/waddle/model/item/Item.java b/src/main/java/seedu/waddle/model/item/Item.java index fea49ed0595..4cab713b93a 100644 --- a/src/main/java/seedu/waddle/model/item/Item.java +++ b/src/main/java/seedu/waddle/model/item/Item.java @@ -3,6 +3,9 @@ import static seedu.waddle.commons.util.CollectionUtil.requireAllNonNull; import java.time.LocalTime; +import java.util.Optional; + +import javax.swing.text.html.Option; /** * Represents an item in the itinerary. @@ -12,7 +15,6 @@ public class Item { private Priority priority; private Cost cost; private Duration duration; - private LocalTime startTime; // private Category category; @@ -44,12 +46,19 @@ public Duration getDuration() { return duration; } - public LocalTime getStartTime() { - return this.startTime; + public Optional getStartTime() { + return Optional.ofNullable(this.startTime); } - public LocalTime getEndTime() { - return this.startTime.plusMinutes(this.duration.getDuration()); + public Optional getEndTime() { + return Optional.ofNullable(this.startTime.plusMinutes(this.duration.getDuration())); + } + + public Optional getTimeString() { + if (this.startTime != null && this.duration != null) { + return Optional.of(this.startTime + " - " + getEndTime().get()); + } + return Optional.empty(); } public void resetStartTime() { diff --git a/src/main/java/seedu/waddle/model/item/Priority.java b/src/main/java/seedu/waddle/model/item/Priority.java index cc418337e3f..cc152f8075b 100644 --- a/src/main/java/seedu/waddle/model/item/Priority.java +++ b/src/main/java/seedu/waddle/model/item/Priority.java @@ -12,51 +12,48 @@ public class Priority { public static final String MESSAGE_CONSTRAINTS = "Priority should only contain a number between 1 and 5"; - /* - * The first character of the country must not be a whitespace, - * otherwise " " (a blank string) becomes a valid input. - */ - public static final String VALIDATION_REGEX = "[1-5]"; - - public final String priority; + private final Integer stars; /** * Constructs a {@code Priority}. * - * @param priority A valid priority. + * @param stars A valid priority. */ - public Priority(String priority) { - requireNonNull(priority); - checkArgument(isValidPriority(priority), MESSAGE_CONSTRAINTS); - this.priority = priority; + public Priority(Integer stars) { + requireNonNull(stars); + checkArgument(isValidPriority(stars), MESSAGE_CONSTRAINTS); + this.stars = stars; } /** * Returns true if a given string is a valid priority. */ - public static boolean isValidPriority(String test) { - return test.matches(VALIDATION_REGEX); + public static boolean isValidPriority(Integer test) { + return test > 0 && test <= 5; } + public int getStars() { + return this.stars; + } @Override public String toString() { - return priority; + return this.stars.toString(); } @Override public boolean equals(Object other) { return other == this // short circuit if same object || (other instanceof Priority // instanceof handles nulls - && priority.equals(((Priority) other).priority)); // state check + && this.stars.equals(((Priority) other).getStars())); // state check } @Override public int hashCode() { - return priority.hashCode(); + return this.stars.hashCode(); } public int compareTo(Priority p) { - return this.priority.compareToIgnoreCase(p.priority); + return this.stars.compareTo(p.getStars()); } } diff --git a/src/main/java/seedu/waddle/storage/JsonAdaptedItem.java b/src/main/java/seedu/waddle/storage/JsonAdaptedItem.java index 8284f7781b8..dcbaf7af3b7 100644 --- a/src/main/java/seedu/waddle/storage/JsonAdaptedItem.java +++ b/src/main/java/seedu/waddle/storage/JsonAdaptedItem.java @@ -17,7 +17,7 @@ public class JsonAdaptedItem { public static final String MISSING_FIELD_MESSAGE_FORMAT = "Item's %s field is missing!"; private final String description; - private final String priority; + private final Integer stars; private final String cost; //private final String duration; @@ -26,11 +26,11 @@ public class JsonAdaptedItem { */ @JsonCreator public JsonAdaptedItem(@JsonProperty("description") String description, - @JsonProperty("priority") String priority, + @JsonProperty("priority") Integer stars, @JsonProperty("cost") String cost, @JsonProperty("duration") String duration) { this.description = description; - this.priority = priority; + this.stars = stars; this.cost = cost; //this.duration = duration; } @@ -40,7 +40,7 @@ public JsonAdaptedItem(@JsonProperty("description") String description, */ public JsonAdaptedItem(Item source) { description = source.getDescription(); - priority = source.getPriority().priority; + stars = source.getPriority().getStars(); cost = source.getCost().toString(); //TODO duration and startTime null error //duration = source.getDuration().toString(); @@ -65,15 +65,15 @@ public Item toModelType() throws IllegalValueException { */ final String modelDescription = description; - if (priority == null) { + if (stars == null) { throw new IllegalValueException( String.format(MISSING_FIELD_MESSAGE_FORMAT, Priority.class.getSimpleName())); } - if (!Priority.isValidPriority(priority)) { + if (!Priority.isValidPriority(stars)) { throw new IllegalValueException(Priority.MESSAGE_CONSTRAINTS); } - final Priority modelPriority = new Priority(priority); + final Priority modelPriority = new Priority(stars); final Cost modelCost = new Cost(cost); //final Duration modelDuration = new Duration(duration); diff --git a/src/main/java/seedu/waddle/ui/ItemCard.java b/src/main/java/seedu/waddle/ui/ItemCard.java index ba75330da85..472f413d0d1 100644 --- a/src/main/java/seedu/waddle/ui/ItemCard.java +++ b/src/main/java/seedu/waddle/ui/ItemCard.java @@ -20,21 +20,27 @@ public class ItemCard extends UiPart { @FXML private Label id; // Priority and Category have not yet been implemented - // @FXML - // private Label priority; + @FXML + private Label priority; + @FXML + private Label time; // @FXML // private Label category; /** * Creates a {@code ItemCode} with the given {@code Item} and index to display. */ - public ItemCard(Item item, int displayedIndex) { + public ItemCard(Item item, int dayNumber, int displayedIndex) { super(FXML); this.item = item; - id.setText(displayedIndex + ". "); - description.setText(item.getDescription()); - // priority.setText(item.getPriority()); - // category.setText(item.getCategory()); + if (dayNumber > 0) { + this.id.setText(dayNumber + "." + displayedIndex + " "); + } else { + this.id.setText(displayedIndex + ". "); + } + this.description.setText(item.getDescription()); + this.priority.setText(Integer.toString(item.getPriority().getStars())); + this.time.setText(item.getTimeString().orElseGet(() -> "")); } @Override diff --git a/src/main/java/seedu/waddle/ui/ItemGroupCard.java b/src/main/java/seedu/waddle/ui/ItemGroupCard.java index 83810edd84f..8702e5867a1 100644 --- a/src/main/java/seedu/waddle/ui/ItemGroupCard.java +++ b/src/main/java/seedu/waddle/ui/ItemGroupCard.java @@ -32,15 +32,15 @@ public class ItemGroupCard extends UiPart { /** * Creates a {@code ItineraryCode} with the given {@code Itinerary} and index to display. */ - public ItemGroupCard(ObservableList itemGroup, int displayedIndex) { + public ItemGroupCard(ObservableList itemGroup, int dayNumber) { super(FXML); this.itemGroup = itemGroup; - if (displayedIndex == 0) { + if (dayNumber == 0) { this.id.setText("Wishlist"); } else { - this.id.setText("Day " + displayedIndex); + this.id.setText("Day " + dayNumber); } - this.itemListPanelPlaceholder.getChildren().add(new ItemListPanel(itemGroup).getRoot()); + this.itemListPanelPlaceholder.getChildren().add(new ItemListPanel(itemGroup, dayNumber).getRoot()); this.itemListPanelPlaceholder.setMinHeight(UiSizes.ITEM_LIST_MIN_HEIGHT); } diff --git a/src/main/java/seedu/waddle/ui/ItemListPanel.java b/src/main/java/seedu/waddle/ui/ItemListPanel.java index b4ac1201409..35d3d50127d 100644 --- a/src/main/java/seedu/waddle/ui/ItemListPanel.java +++ b/src/main/java/seedu/waddle/ui/ItemListPanel.java @@ -16,6 +16,7 @@ public class ItemListPanel extends ListPanel { private static final String FXML = "ItemListPanel.fxml"; private final Logger logger = LogsCenter.getLogger(ItemListPanel.class); + private final int dayNumber; @FXML private ListView itemListView; @@ -23,8 +24,9 @@ public class ItemListPanel extends ListPanel { /** * Creates a {@code ItemListPanel} with the given {@code ObservableList}. */ - public ItemListPanel(ObservableList itemList) { + public ItemListPanel(ObservableList itemList, int dayNumber) { super(FXML); + this.dayNumber = dayNumber; itemListView.setItems(itemList); itemListView.setCellFactory(listView -> new ItemListPanel.ItemListViewCell()); itemListView.prefHeightProperty().bind(Bindings.size(itemList).multiply(UiSizes.ITEM_CARD_HEIGHT)); @@ -42,7 +44,7 @@ protected void updateItem(Item item, boolean empty) { setGraphic(null); setText(null); } else { - setGraphic(new ItemCard(item, getIndex() + 1).getRoot()); + setGraphic(new ItemCard(item, dayNumber, getIndex() + 1).getRoot()); } } } diff --git a/src/main/resources/view/ItemListCard.fxml b/src/main/resources/view/ItemListCard.fxml index 95b0b56d77d..be9b93d3d1f 100644 --- a/src/main/resources/view/ItemListCard.fxml +++ b/src/main/resources/view/ItemListCard.fxml @@ -26,6 +26,8 @@