Skip to content

Commit

Permalink
Merge pull request #280 from eujingsen/master
Browse files Browse the repository at this point in the history
MyPlan fixed functionalities
  • Loading branch information
NotTheRealEdmund authored Nov 11, 2019
2 parents 17c78e4 + 28ee46b commit fc71f88
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 37 deletions.
64 changes: 47 additions & 17 deletions src/main/java/duke/data/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -402,21 +403,23 @@ public void readStudentListFile(final ArrayList<Student> student) {
*/
public Map<String, ArrayList<MyTraining>> loadPlans() {
try {
Map<String, ArrayList<MyTraining>> temp = new HashMap<>();

File f = new File(".\\src\\main\\java\\duke\\data\\plan.txt");
Map<String, ArrayList<MyTraining>> 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<MyTraining> list = new ArrayList<>();

int planNum = 0;
int counter = 0;
ArrayList<MyTraining> tempList = new ArrayList<>();
ArrayList<Integer> countList = new ArrayList<>();
ArrayList<String> keyList = new ArrayList<>();
while (fileInput.hasNextLine()) {
String in = fileInput.nextLine();


if (in.contains("Intensity")) {
String[] line = in.split(": ");
if (line[1].equals("high")) {
Expand All @@ -430,26 +433,46 @@ public Map<String, ArrayList<MyTraining>> 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<MyTraining> tl = new ArrayList<>();
for (int x = 0; x < size; x++) {
tl.add(tempList.get(0));
tempList.remove(0);
}
temp.put(s,tl);
}
Set<String> keys = temp.keySet();
ArrayList<String> kl = new ArrayList<>(keys);
for (String s : kl) {
System.out.println(s);
}
return temp;
}
} catch (ArrayIndexOutOfBoundsException e) {
Expand Down Expand Up @@ -483,12 +506,19 @@ public void savePlans(final Map<String, ArrayList<MyTraining>> map,
ArrayList<MyTraining> 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();
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/duke/data/plan.txt
Original file line number Diff line number Diff line change
@@ -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 | ##
17 changes: 6 additions & 11 deletions src/main/java/duke/models/MyPlan.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -93,16 +93,7 @@ public String createKey(final String intensity, final int num) {
public ArrayList<String> keyList() {
Set<String> keys = getMap().keySet();
ArrayList<String> kl = new ArrayList<>(keys);
/*Collections.sort(kl, new Comparator<String>() {
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;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/duke/parser/ParserTrainingPlan.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public void parseCommand(final String input) {
plan.loadPlanToList(key);
cliView.printLine();
System.out.println(plan.viewPlan());
cliView.printLine();
inList = false;
}
}
Expand Down Expand Up @@ -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");
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/duke/view/CliView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)");
}

/**
Expand Down

0 comments on commit fc71f88

Please sign in to comment.