diff --git a/src/main/java/duke/data/Storage.java b/src/main/java/duke/data/Storage.java index a9a934ac29..6d28e0e009 100644 --- a/src/main/java/duke/data/Storage.java +++ b/src/main/java/duke/data/Storage.java @@ -23,6 +23,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Scanner; +import java.util.Set; /** * Storage handles all the loading and saving of data @@ -402,21 +403,23 @@ public void readStudentListFile(final ArrayList student) { */ public Map> loadPlans() { try { - Map> temp = new HashMap<>(); - File f = new File(".\\src\\main\\java\\duke\\data\\plan.txt"); + Map> temp = new HashMap<>(); if (f.length() == 0) { System.out.println("Plan file is empty. Loading failed."); - return temp; + return null; } else { String intensity = ""; - int planNum = 1; - ArrayList list = new ArrayList<>(); - + int planNum = 0; + int counter = 0; + ArrayList tempList = new ArrayList<>(); + ArrayList countList = new ArrayList<>(); + ArrayList keyList = new ArrayList<>(); while (fileInput.hasNextLine()) { String in = fileInput.nextLine(); + if (in.contains("Intensity")) { String[] line = in.split(": "); if (line[1].equals("high")) { @@ -430,26 +433,46 @@ public Map> loadPlans() { intensity = x.toString(); } } - if (in.contains("Plan")) { - list.clear(); String[] line = in.split(": "); planNum = Integer.parseInt(line[1]); } - if (in.contains(" | ")) { + counter++; String[] line = in.split("\\s*\\|\\s*"); MyTraining ac = new MyTraining(line[0], Integer.parseInt(line[1]), Integer.parseInt(line[2])); - list.add(ac); - } - - if (in.contains("##")) { - temp.put(intensity + planNum, list); + tempList.add(ac); + + final int endOfPlan = 4; + if (line.length == endOfPlan) { + countList.add(counter); + counter = 0; + if (line[endOfPlan - 1].equals("##")) { + String key = intensity + planNum; + keyList.add(key); + } + } } } fileInput.close(); + int a = 0; + for (String s : keyList) { + int size = countList.get(a); + a++; + ArrayList tl = new ArrayList<>(); + for (int x = 0; x < size; x++) { + tl.add(tempList.get(0)); + tempList.remove(0); + } + temp.put(s,tl); + } + Set keys = temp.keySet(); + ArrayList kl = new ArrayList<>(keys); + for (String s : kl) { + System.out.println(s); + } return temp; } } catch (ArrayIndexOutOfBoundsException e) { @@ -483,12 +506,19 @@ public void savePlans(final Map> map, ArrayList p = map.get(s); buffer.write("Plan: " + s.substring(s.length() - 1)); buffer.write("\r\n"); + + int index = 1; for (MyTraining y : p) { buffer.write(y.toFile()); - buffer.write("\r\n"); + if (index != p.size()) { + buffer.write("\r\n"); + } else { + buffer.write("##"); + buffer.write("\r\n"); + } + index++; } - buffer.write("##"); - buffer.write("\r\n"); + p.clear(); } } } diff --git a/src/main/java/duke/data/plan.txt b/src/main/java/duke/data/plan.txt index a74bb0d9bc..03248430fd 100644 --- a/src/main/java/duke/data/plan.txt +++ b/src/main/java/duke/data/plan.txt @@ -1,12 +1,12 @@ Intensity: high Plan: 1 -b | 10 | 10 -## +a | 1 | 1 +wabalabadubdub | 10 | 10 | ## Plan: 2 -a | 10 | 10 -## +b | 2 | 2 | ## Intensity: moderate Plan: 1 -c | 10 | 10 -## +c | 3 | 3 | ## Intensity: relaxed +Plan: 1 +d | 4 | 4 | ## diff --git a/src/main/java/duke/models/MyPlan.java b/src/main/java/duke/models/MyPlan.java index b117d7d129..9a79881653 100644 --- a/src/main/java/duke/models/MyPlan.java +++ b/src/main/java/duke/models/MyPlan.java @@ -10,8 +10,8 @@ import java.util.Map; import java.util.HashSet; import java.util.Set; -import java.util.Collections; import java.util.Comparator; +import java.util.Collections; /** * Loads a training plan from a txt file, create new plan, or edit a plan. @@ -93,16 +93,7 @@ public String createKey(final String intensity, final int num) { public ArrayList keyList() { Set keys = getMap().keySet(); ArrayList kl = new ArrayList<>(keys); - /*Collections.sort(kl, new Comparator() { - public int compare(final String a, final String b) { - return extractInt(a) - extractInt(b); - } - - public int extractInt(final String s) { - String num = s.replaceAll("\\D", ""); - return num.isEmpty() ? 0 : Integer.parseInt(num); - } - });*/ + Collections.sort(kl); return kl; } @@ -118,6 +109,7 @@ public void showPlanList() { for (String s : planList) { String[] num = s.split("(?<=\\D)(?=\\d)"); + if (s.contains("high")) { if (num[1].equals("1")) { index = 1; @@ -398,6 +390,9 @@ public void createPlan(final String intensity) { System.out.println(viewPlan()); cliView.printLine(); inCreation = false; + } else if (input.equals("cancel")) { + clearPlanInList(); + inCreation = false; } else if (input.equals("show")) { if (getList().isEmpty()) { cliView.showNoActivity(); diff --git a/src/main/java/duke/parser/ParserTrainingPlan.java b/src/main/java/duke/parser/ParserTrainingPlan.java index c65a013eca..dbcdfec04c 100644 --- a/src/main/java/duke/parser/ParserTrainingPlan.java +++ b/src/main/java/duke/parser/ParserTrainingPlan.java @@ -71,7 +71,6 @@ public void parseCommand(final String input) { plan.loadPlanToList(key); cliView.printLine(); System.out.println(plan.viewPlan()); - cliView.printLine(); inList = false; } } @@ -103,7 +102,7 @@ public void parseCommand(final String input) { String[] num = key.split("(?<=\\D)(?=\\d)"); try { - plan.editPlan(num[0],key); + plan.editPlan(num[0], key); } catch (IOException e) { System.out.println("IO Problem"); } diff --git a/src/main/java/duke/view/CliView.java b/src/main/java/duke/view/CliView.java index 25a8814d36..a6e5fdad3d 100644 --- a/src/main/java/duke/view/CliView.java +++ b/src/main/java/duke/view/CliView.java @@ -450,7 +450,8 @@ public void showPlanPrompt1() { public void showPlanPrompt2() { System.out.println("Input new activity, finalize " + "the plan(finalize), look at the list(show)" - + ", or edit the positions(switch)."); + + ", edit the positions(switch) or " + + "cancel plan creation(cancel)"); } /**