From 20aee5db82933f9fbc6ebf97b550deb4132cf783 Mon Sep 17 00:00:00 2001 From: MrOPPA1 <3105784412@qq.com> Date: Tue, 14 Nov 2023 23:21:33 +0800 Subject: [PATCH] fix bugs Fix the bugs that unable to see the category enum and category parse class. --- .gitignore | 1 - src/main/java/seedu/duke/TipsException.java | 26 +++++++++++++++++++ .../seedu/duke/commands/meal/AddCommand.java | 2 +- .../duke/commands/meal/DeleteCommand.java | 1 + .../seedu/duke/commands/meal/ListCommand.java | 4 +-- .../seedu/duke/commands/meal/MealCommand.java | 2 +- src/main/java/seedu/duke/meal/Category.java | 5 ++++ .../java/seedu/duke/meal/CategoryParser.java | 19 ++++++++++++++ src/main/java/seedu/duke/meal/Meal.java | 22 ++++++++++++++++ 9 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 src/main/java/seedu/duke/TipsException.java create mode 100644 src/main/java/seedu/duke/meal/Category.java create mode 100644 src/main/java/seedu/duke/meal/CategoryParser.java create mode 100644 src/main/java/seedu/duke/meal/Meal.java diff --git a/.gitignore b/.gitignore index 06c819e36..3a354b1ea 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,6 @@ # Gradle build files /.gradle/ /build/ -src/main/resources/docs/ # MacOS custom attributes files created by Finder .DS_Store diff --git a/src/main/java/seedu/duke/TipsException.java b/src/main/java/seedu/duke/TipsException.java new file mode 100644 index 000000000..47b9ac1e0 --- /dev/null +++ b/src/main/java/seedu/duke/TipsException.java @@ -0,0 +1,26 @@ +package seedu.duke; + +/** + * Any excption will be throw in this type, which contains information about + * this exception and the possible solution. + * It's suggested to use toString() to show the content of this exception so + * that the tester can have a clear view of the error reason and the possible + * solution to solve this error. + */ +public class TipsException extends Exception { + public String error, tips; + + /** + * Creating a new Tips Exception. + */ + public TipsException(String error, String tips) { + this.error = error; + this.tips = tips; + } + + @Override + public String toString() { + return "Error: " + error + + "\nTips: " + tips; + } +} diff --git a/src/main/java/seedu/duke/commands/meal/AddCommand.java b/src/main/java/seedu/duke/commands/meal/AddCommand.java index b68879d89..a0ae7f6bb 100644 --- a/src/main/java/seedu/duke/commands/meal/AddCommand.java +++ b/src/main/java/seedu/duke/commands/meal/AddCommand.java @@ -1,7 +1,7 @@ package seedu.duke.commands.meal; import seedu.duke.commands.CommandResult; -import seedu.duke.data.meal.Meal; +import seedu.duke.meal.*; import seedu.duke.data.Date; import java.util.List; diff --git a/src/main/java/seedu/duke/commands/meal/DeleteCommand.java b/src/main/java/seedu/duke/commands/meal/DeleteCommand.java index 8380744b5..3138b76e3 100644 --- a/src/main/java/seedu/duke/commands/meal/DeleteCommand.java +++ b/src/main/java/seedu/duke/commands/meal/DeleteCommand.java @@ -1,6 +1,7 @@ package seedu.duke.commands.meal; import seedu.duke.commands.CommandResult; +import seedu.duke.meal.*; import java.util.List; diff --git a/src/main/java/seedu/duke/commands/meal/ListCommand.java b/src/main/java/seedu/duke/commands/meal/ListCommand.java index 40f037c0f..16ad80688 100644 --- a/src/main/java/seedu/duke/commands/meal/ListCommand.java +++ b/src/main/java/seedu/duke/commands/meal/ListCommand.java @@ -1,9 +1,7 @@ package seedu.duke.commands.meal; import seedu.duke.commands.CommandResult; -import seedu.duke.data.meal.Category; -import seedu.duke.data.meal.CategoryParser; -import seedu.duke.data.meal.Meal; +import seedu.duke.meal.*; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/seedu/duke/commands/meal/MealCommand.java b/src/main/java/seedu/duke/commands/meal/MealCommand.java index dd1535f16..74a4a1c27 100644 --- a/src/main/java/seedu/duke/commands/meal/MealCommand.java +++ b/src/main/java/seedu/duke/commands/meal/MealCommand.java @@ -5,7 +5,7 @@ import seedu.duke.commands.Command; import seedu.duke.commands.CommandResult; -import seedu.duke.data.meal.Meal; +import seedu.duke.meal.*; public class MealCommand extends Command { protected static ArrayList meals; diff --git a/src/main/java/seedu/duke/meal/Category.java b/src/main/java/seedu/duke/meal/Category.java new file mode 100644 index 000000000..39d696272 --- /dev/null +++ b/src/main/java/seedu/duke/meal/Category.java @@ -0,0 +1,5 @@ +package seedu.duke.meal; + +public enum Category { + staple_food, snack, beverage +} diff --git a/src/main/java/seedu/duke/meal/CategoryParser.java b/src/main/java/seedu/duke/meal/CategoryParser.java new file mode 100644 index 000000000..1a54fa6c9 --- /dev/null +++ b/src/main/java/seedu/duke/meal/CategoryParser.java @@ -0,0 +1,19 @@ +package seedu.duke.meal; + +public class CategoryParser { + private static Category[] categories = Category.values(); + + public static Category Parse(String categoryString) throws Exception { + try { + int index = Integer.parseInt(categoryString); + return categories[index]; + } catch (Exception exception1) { + try { + return Category.valueOf(categoryString); + } catch (Exception exception2) { + throw new Exception( + "Unable to parse Category String into Category enum.\nPlease input a number ranging in 0~2 or a valid category type."); + } + } + } +} diff --git a/src/main/java/seedu/duke/meal/Meal.java b/src/main/java/seedu/duke/meal/Meal.java new file mode 100644 index 000000000..e6fc60426 --- /dev/null +++ b/src/main/java/seedu/duke/meal/Meal.java @@ -0,0 +1,22 @@ +package seedu.duke.meal; + +import seedu.duke.data.Date; + +public class Meal { + public String name; + public int calories; + public Date time; + public Category category; + + public Meal(String name, int calories, String category, Date time) throws Exception { + this.name = name; + this.calories = calories; + this.time = time; + this.category = CategoryParser.Parse(category); + } + + @Override + public String toString() { + return name + "(" + calories + " calories, " + category + ", on " + time.toString() + ")"; + } +} \ No newline at end of file