Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MyPlan fixed functionalities #280

Merged
2 commits merged into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -452,7 +452,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