From 41380c797750abf1e0cf7e6e53354be7b23aaf4f Mon Sep 17 00:00:00 2001 From: yujinyang1998 Date: Thu, 15 Oct 2020 22:44:25 +0800 Subject: [PATCH] v1.0 --- .gitignore | 1 + saves/workout/history.json | 312 +++++++++++++++--- saves/workout/workoutSession/1.json | 1 + saves/workout/workoutSession/3.json | 3 + saves/workout/workoutSession/4.json | 7 + src/main/java/META-INF/MANIFEST.MF | 3 + src/main/java/storage/workout/Storage.java | 14 +- .../workoutsession/WorkoutSession.java | 25 +- .../workoutsession/WorkoutSessionParser.java | 22 +- .../workoutsession/exercise/Exercise.java | 6 +- .../workoutsessionui/WorkoutSessionUI.java | 14 + 11 files changed, 346 insertions(+), 62 deletions(-) create mode 100644 src/main/java/META-INF/MANIFEST.MF diff --git a/.gitignore b/.gitignore index 27d8a0bb95..47b7bc72bb 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ bin/ text-ui-test/EXPECTED-UNIX.TXT data/ +saves/ diff --git a/saves/workout/history.json b/saves/workout/history.json index f5058b1cc2..477c31da98 100644 --- a/saves/workout/history.json +++ b/saves/workout/history.json @@ -8,10 +8,10 @@ "day": 15 }, "time": { - "hour": 20, - "minute": 45, - "second": 45, - "nano": 607542500 + "hour": 21, + "minute": 53, + "second": 53, + "nano": 931518000 } }, "lastEditTime": { @@ -21,10 +21,10 @@ "day": 15 }, "time": { - "hour": 20, - "minute": 45, - "second": 45, - "nano": 607542500 + "hour": 21, + "minute": 53, + "second": 53, + "nano": 932518300 } } }, @@ -37,10 +37,10 @@ "day": 15 }, "time": { - "hour": 20, - "minute": 46, - "second": 21, - "nano": 304254000 + "hour": 21, + "minute": 55, + "second": 45, + "nano": 945448400 } }, "lastEditTime": { @@ -50,10 +50,10 @@ "day": 15 }, "time": { - "hour": 20, - "minute": 46, - "second": 21, - "nano": 304254000 + "hour": 21, + "minute": 55, + "second": 45, + "nano": 946448700 } } }, @@ -66,10 +66,10 @@ "day": 15 }, "time": { - "hour": 20, - "minute": 47, - "second": 46, - "nano": 909094000 + "hour": 21, + "minute": 57, + "second": 36, + "nano": 385192800 } }, "lastEditTime": { @@ -79,10 +79,10 @@ "day": 15 }, "time": { - "hour": 20, - "minute": 47, - "second": 46, - "nano": 909094000 + "hour": 21, + "minute": 57, + "second": 36, + "nano": 385192800 } } }, @@ -95,10 +95,10 @@ "day": 15 }, "time": { - "hour": 20, - "minute": 48, - "second": 13, - "nano": 877009100 + "hour": 22, + "minute": 2, + "second": 30, + "nano": 308990700 } }, "lastEditTime": { @@ -108,10 +108,10 @@ "day": 15 }, "time": { - "hour": 20, - "minute": 48, - "second": 13, - "nano": 877009100 + "hour": 22, + "minute": 2, + "second": 30, + "nano": 308990700 } } }, @@ -124,10 +124,10 @@ "day": 15 }, "time": { - "hour": 20, - "minute": 53, - "second": 17, - "nano": 920083000 + "hour": 22, + "minute": 3, + "second": 25, + "nano": 757600600 } }, "lastEditTime": { @@ -137,10 +137,242 @@ "day": 15 }, "time": { - "hour": 20, - "minute": 53, - "second": 17, - "nano": 920083000 + "hour": 22, + "minute": 3, + "second": 25, + "nano": 757600600 + } + } + }, + { + "filePath": "saves/workout/workoutSession/5.json", + "creationTime": { + "date": { + "year": 2020, + "month": 10, + "day": 15 + }, + "time": { + "hour": 22, + "minute": 6, + "second": 33, + "nano": 707839400 + } + }, + "lastEditTime": { + "date": { + "year": 2020, + "month": 10, + "day": 15 + }, + "time": { + "hour": 22, + "minute": 6, + "second": 33, + "nano": 707839400 + } + } + }, + { + "filePath": "saves/workout/workoutSession/6.json", + "creationTime": { + "date": { + "year": 2020, + "month": 10, + "day": 15 + }, + "time": { + "hour": 22, + "minute": 8, + "second": 28, + "nano": 32634600 + } + }, + "lastEditTime": { + "date": { + "year": 2020, + "month": 10, + "day": 15 + }, + "time": { + "hour": 22, + "minute": 8, + "second": 28, + "nano": 33633700 + } + } + }, + { + "filePath": "saves/workout/workoutSession/7.json", + "creationTime": { + "date": { + "year": 2020, + "month": 10, + "day": 15 + }, + "time": { + "hour": 22, + "minute": 10, + "second": 34, + "nano": 793642500 + } + }, + "lastEditTime": { + "date": { + "year": 2020, + "month": 10, + "day": 15 + }, + "time": { + "hour": 22, + "minute": 10, + "second": 34, + "nano": 794642700 + } + } + }, + { + "filePath": "saves/workout/workoutSession/8.json", + "creationTime": { + "date": { + "year": 2020, + "month": 10, + "day": 15 + }, + "time": { + "hour": 22, + "minute": 11, + "second": 24, + "nano": 863070700 + } + }, + "lastEditTime": { + "date": { + "year": 2020, + "month": 10, + "day": 15 + }, + "time": { + "hour": 22, + "minute": 11, + "second": 24, + "nano": 863070700 + } + } + }, + { + "filePath": "saves/workout/workoutSession/9.json", + "creationTime": { + "date": { + "year": 2020, + "month": 10, + "day": 15 + }, + "time": { + "hour": 22, + "minute": 11, + "second": 54, + "nano": 700742100 + } + }, + "lastEditTime": { + "date": { + "year": 2020, + "month": 10, + "day": 15 + }, + "time": { + "hour": 22, + "minute": 11, + "second": 54, + "nano": 701742900 + } + } + }, + { + "filePath": "saves/workout/workoutSession/10.json", + "creationTime": { + "date": { + "year": 2020, + "month": 10, + "day": 15 + }, + "time": { + "hour": 22, + "minute": 16, + "second": 6, + "nano": 481669100 + } + }, + "lastEditTime": { + "date": { + "year": 2020, + "month": 10, + "day": 15 + }, + "time": { + "hour": 22, + "minute": 16, + "second": 6, + "nano": 481669100 + } + } + }, + { + "filePath": "saves/workout/workoutSession/11.json", + "creationTime": { + "date": { + "year": 2020, + "month": 10, + "day": 15 + }, + "time": { + "hour": 22, + "minute": 19, + "second": 2, + "nano": 502813500 + } + }, + "lastEditTime": { + "date": { + "year": 2020, + "month": 10, + "day": 15 + }, + "time": { + "hour": 22, + "minute": 19, + "second": 2, + "nano": 502813500 + } + } + }, + { + "filePath": "saves/workout/workoutSession/12.json", + "creationTime": { + "date": { + "year": 2020, + "month": 10, + "day": 15 + }, + "time": { + "hour": 22, + "minute": 22, + "second": 48, + "nano": 511306700 + } + }, + "lastEditTime": { + "date": { + "year": 2020, + "month": 10, + "day": 15 + }, + "time": { + "hour": 22, + "minute": 22, + "second": 48, + "nano": 511306700 } } } diff --git a/saves/workout/workoutSession/1.json b/saves/workout/workoutSession/1.json index e69de29bb2..cf1b747360 100644 --- a/saves/workout/workoutSession/1.json +++ b/saves/workout/workoutSession/1.json @@ -0,0 +1 @@ +[{"moveName":"nulldsadsa","repetitions":32,"weight":34}] \ No newline at end of file diff --git a/saves/workout/workoutSession/3.json b/saves/workout/workoutSession/3.json index e69de29bb2..acb2cca46f 100644 --- a/saves/workout/workoutSession/3.json +++ b/saves/workout/workoutSession/3.json @@ -0,0 +1,3 @@ +[ + null +] \ No newline at end of file diff --git a/saves/workout/workoutSession/4.json b/saves/workout/workoutSession/4.json index e69de29bb2..5d59e5bdfa 100644 --- a/saves/workout/workoutSession/4.json +++ b/saves/workout/workoutSession/4.json @@ -0,0 +1,7 @@ +[ + { + "moveName": "nulldasd", + "repetitions": 32, + "weight": 23 + } +] \ No newline at end of file diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..19e86fe56e --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: seedu.duke.Duke + diff --git a/src/main/java/storage/workout/Storage.java b/src/main/java/storage/workout/Storage.java index 23ad349d03..893f2917c7 100644 --- a/src/main/java/storage/workout/Storage.java +++ b/src/main/java/storage/workout/Storage.java @@ -21,13 +21,14 @@ public class Storage { private static final String FILEPATH = "./saves/workout"; //private static ArrayList taskList = new ArrayList<>(); - private static Gson gson; + private static Gson gson = new GsonBuilder().setPrettyPrinting().create(); private static File file = null; /** * Initialise the database with locally stored data. * If the local file is not found. It creates the relevant file and folder. + * * @throws IOException If director or file cannot be created. */ public static void initialise(String filePath) throws IOException { @@ -50,6 +51,7 @@ public static void initialise(String filePath) throws IOException { /** * Write the content in TaskList to a local file. * If the local file is not found. It creates the relevant file and folder. + * * @throws IOException If director or file cannot be created. */ public static void writeToStorage(String filePath, ArrayList taskList) throws IOException { @@ -63,11 +65,15 @@ public static void writeToStorage(String filePath, ArrayList taskList) public static void readFileContents(String filePath, ArrayList taskList) throws FileNotFoundException { File file = new File(filePath); - Type taskListType = new TypeToken>(){}.getType(); - + Type taskListType = new TypeToken>() { + }.getType(); JsonReader reader = new JsonReader(new FileReader(file.getPath())); taskList.clear(); - taskList.addAll(gson.fromJson(reader, taskListType)); + try { + taskList.addAll(gson.fromJson(reader, taskListType)); + } catch (NullPointerException e) { + System.out.printf(""); + } } diff --git a/src/main/java/workout/workoutsession/WorkoutSession.java b/src/main/java/workout/workoutsession/WorkoutSession.java index b2f1aa7015..f01af9e1aa 100644 --- a/src/main/java/workout/workoutsession/WorkoutSession.java +++ b/src/main/java/workout/workoutsession/WorkoutSession.java @@ -11,11 +11,7 @@ public class WorkoutSession { private String filePath = null; private boolean endWorkoutSession = false; - private final ArrayList exercise; - - public WorkoutSession() { - this.exercise = new ArrayList<>(); - } + public ArrayList exercise; public WorkoutSession(String filePath) { this.filePath = filePath; @@ -44,11 +40,13 @@ public void workoutSessionStart() { workoutSessionProcessCommand(); } catch (IOException e) { WorkoutSessionUI.printError(); + } catch (NullPointerException e) { + e.printStackTrace(); } } } - private void workoutSessionProcessCommand() throws IOException { + private void workoutSessionProcessCommand() throws IOException, NullPointerException { Scanner in = new Scanner(System.in); String[] input = WorkoutSessionParser.workoutSessionParser(in.nextLine().trim()); @@ -56,13 +54,18 @@ private void workoutSessionProcessCommand() throws IOException { switch (input[0].toLowerCase()) { case "add": - exercise.add(WorkoutSessionParser.addParser(input)); - Storage.writeToStorage(filePath, exercise); + try { + exercise.add(WorkoutSessionParser.addParser(input)); + Storage.writeToStorage(filePath, exercise); + } catch (NumberFormatException e) { + WorkoutSessionUI.addFormatError(); + } break; case "list": Storage.readFileContents(filePath, exercise); printList(); Storage.writeToStorage(filePath, exercise); + break; case "delete": exercise.remove(WorkoutSessionParser.deleteParser(input)); @@ -73,12 +76,14 @@ private void workoutSessionProcessCommand() throws IOException { Storage.writeToStorage(filePath, exercise); break; default: - + WorkoutSessionUI.inputNotRecognisedError(); } } private void printList() { - + if (exercise.size() <= 0) { + WorkoutSessionUI.emptyListError(); + } for (int i = 0; i < exercise.size(); i++) { System.out.println((i + 1) + ": " + exercise.get(i).toString()); } diff --git a/src/main/java/workout/workoutsession/WorkoutSessionParser.java b/src/main/java/workout/workoutsession/WorkoutSessionParser.java index 940a5ada20..fac655f5bf 100644 --- a/src/main/java/workout/workoutsession/WorkoutSessionParser.java +++ b/src/main/java/workout/workoutsession/WorkoutSessionParser.java @@ -1,6 +1,7 @@ package workout.workoutsession; import workout.workoutsession.exercise.Exercise; +import workout.workoutsession.workoutsessionui.WorkoutSessionUI; public class WorkoutSessionParser { @@ -9,22 +10,33 @@ public static String[] workoutSessionParser(String input) { return input.split(" "); } - public static Exercise addParser(String[] input) { + public static Exercise addParser(String[] input) throws NumberFormatException { + Exercise exercise = null; - String[] returnString = new String[3]; + String[] returnString = new String[4]; + for (int i = 0; i < returnString.length; i++) { + returnString[i] = ""; + } int tracker = 0; for (String s : input) { if (s.compareTo("/n") == 0) { - tracker = 1; - } else if (s.compareTo("/w") == 0) { tracker = 2; + } else if (s.compareTo("/w") == 0) { + tracker = 3; + } else if (s.compareTo("add") == 0) { + tracker = 1; + } else if (tracker == 2 || tracker == 3) { + returnString[tracker] = s; } else { returnString[tracker] += s; } } - return new Exercise(returnString[0],Integer.parseInt(returnString[1]),Integer.parseInt(returnString[2])); + exercise = new Exercise(returnString[1], Integer.parseInt(returnString[2]), Integer.parseInt(returnString[3])); + + return exercise; } + public static int deleteParser(String[] input) { return Integer.parseInt(input[1]); diff --git a/src/main/java/workout/workoutsession/exercise/Exercise.java b/src/main/java/workout/workoutsession/exercise/Exercise.java index 86ed041d48..35666cc3bd 100644 --- a/src/main/java/workout/workoutsession/exercise/Exercise.java +++ b/src/main/java/workout/workoutsession/exercise/Exercise.java @@ -8,9 +8,9 @@ public class Exercise { public Exercise(String moveName, int repetitions, int weight) { - moveName = this.moveName; - repetitions = this.repetitions; - weight = this.weight; + this.moveName = moveName; + this.repetitions = repetitions; + this.weight = weight; } @Override diff --git a/src/main/java/workout/workoutsession/workoutsessionui/WorkoutSessionUI.java b/src/main/java/workout/workoutsession/workoutsessionui/WorkoutSessionUI.java index 11133b73bf..860605d3f4 100644 --- a/src/main/java/workout/workoutsession/workoutsessionui/WorkoutSessionUI.java +++ b/src/main/java/workout/workoutsession/workoutsessionui/WorkoutSessionUI.java @@ -4,4 +4,18 @@ public class WorkoutSessionUI { public static void printError() { System.out.println("There is an error, please restart the session"); } + + public static void addFormatError() { + System.out.println("Wrong format, please enter in the format: \n" + + "add [workout] /n [number of sets] /w " + + "[weight]"); + } + + public static void inputNotRecognisedError() { + System.out.println("Command not recognised."); + } + + public static void emptyListError() { + System.out.println("List is empty. Please enter something."); + } }