Skip to content

Commit

Permalink
Merge pull request #285 from eujingsen/master
Browse files Browse the repository at this point in the history
Txt file directory for JAR fixed
  • Loading branch information
NotTheRealEdmund authored Nov 13, 2019
2 parents 8161e5d + cd7c126 commit 149f36b
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 88 deletions.
9 changes: 9 additions & 0 deletions reports/ApacheLogger.log
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<<<<<<< HEAD
2019-11-13 15:07:06.391 DEBUG [Schedule] - Wrong input format for adding to table
2019-11-13 15:07:06.479 DEBUG [DateHandler] - Valid time and format in convertTime
2019-11-13 15:07:06.479 DEBUG [DateHandler] - Valid time and format in convertTime
2019-11-13 15:07:06.479 DEBUG [ParseScheduleTable] - Correct Time
2019-11-13 15:07:06.495 DEBUG [DateHandler] - Valid time and format in convertTime
2019-11-13 15:07:06.495 DEBUG [DateHandler] - Invalid time or format in convertTime
=======
2019-11-12 01:29:32.765 DEBUG [Schedule] - Wrong input format for adding to table
2019-11-12 01:29:32.795 DEBUG [DateHandler] - Valid time and format in convertTime
2019-11-12 01:29:32.796 DEBUG [DateHandler] - Valid time and format in convertTime
2019-11-12 01:29:32.796 DEBUG [ParseScheduleTable] - Correct Time
2019-11-12 01:29:32.805 DEBUG [DateHandler] - Valid time and format in convertTime
2019-11-12 01:29:32.806 DEBUG [DateHandler] - Invalid time or format in convertTime
>>>>>>> upstream/master
Binary file modified reports/app_20191111.log.gz
Binary file not shown.
131 changes: 64 additions & 67 deletions src/main/java/duke/data/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,79 +403,78 @@ public void readStudentListFile(final ArrayList<Student> student) {
*/
public Map<String, ArrayList<MyTraining>> loadPlans() {
try {
File f = new File(".\\src\\main\\java\\duke\\data\\plan.txt");
File f = new File(filePath);
Map<String, ArrayList<MyTraining>> temp = new HashMap<>();
ArrayList<String> temp2 = new ArrayList<>();

if (f.length() == 0) {
System.out.println("Plan file is empty. Loading failed.");
savePlans(temp, temp2);
return null;
} else {
String intensity = "";
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")) {
MyPlan.Intensity x = MyPlan.Intensity.high;
intensity = x.toString();
} else if (line[1].equals("moderate")) {
MyPlan.Intensity x = MyPlan.Intensity.moderate;
intensity = x.toString();
} else if (line[1].equals("relaxed")) {
MyPlan.Intensity x = MyPlan.Intensity.relaxed;
intensity = x.toString();
}
}
if (in.contains("Plan")) {
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]));
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);
}
}
}
String intensity = "";
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")) {
MyPlan.Intensity x = MyPlan.Intensity.high;
intensity = x.toString();
} else if (line[1].equals("moderate")) {
MyPlan.Intensity x = MyPlan.Intensity.moderate;
intensity = x.toString();
} else if (line[1].equals("relaxed")) {
MyPlan.Intensity x = MyPlan.Intensity.relaxed;
intensity = x.toString();
}
}
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);
if (in.contains("Plan")) {
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]));
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);
}
}
temp.put(s,tl);
}
Set<String> keys = temp.keySet();
ArrayList<String> kl = new ArrayList<>(keys);
for (String s : kl) {
System.out.println(s);
}
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);
}
return temp;
temp.put(s,tl);
}
} catch (ArrayIndexOutOfBoundsException e) {
Set<String> keys = temp.keySet();
ArrayList<String> kl = new ArrayList<>(keys);
for (String s : kl) {
System.out.println(s);
}
return temp;
} catch (ArrayIndexOutOfBoundsException | IOException e) {
return null;
}
}
Expand All @@ -489,13 +488,11 @@ public Map<String, ArrayList<MyTraining>> loadPlans() {
*/
public void savePlans(final Map<String, ArrayList<MyTraining>> map,
final ArrayList<String> keys) throws IOException {
PrintWriter clear = new PrintWriter(
".\\src\\main\\java\\duke\\data\\plan.txt");
PrintWriter clear = new PrintWriter(filePath);
clear.close();

BufferedWriter buffer = new BufferedWriter(
new FileWriter(".\\src\\main\\java\\duke\\data\\plan.txt",
true));
new FileWriter(filePath, true));

for (int i = 1; i <= MyPlan.Intensity.values().length; i++) {
MyPlan.Intensity x = MyPlan.Intensity.valueOf(i);
Expand Down
40 changes: 22 additions & 18 deletions src/main/java/duke/models/MyPlan.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,15 @@ public String createKey(final String intensity, final int num) {
* @return the arraylist of keys, sorted by intensity and plan number
*/
public ArrayList<String> keyList() {
Set<String> keys = getMap().keySet();
ArrayList<String> kl = new ArrayList<>(keys);
Collections.sort(kl);
ArrayList<String> kl = new ArrayList<>();
try {
Set<String> keys = getMap().keySet();
kl = new ArrayList<>(keys);
Collections.sort(kl);
return kl;
} catch (NullPointerException e) {
System.out.println("No plan in map");
}
return kl;
}

Expand Down Expand Up @@ -278,15 +284,13 @@ public String viewPlan() {
*/
public void editPlan(final String intensity,
final String key) throws IOException {
cliView.printLine();
System.out.println(viewPlan());
cliView.printLine();
while (true) {
cliView.printLine();
cliView.showEditPlanPrompt1();
if (sc.hasNextLine()) {
String input = sc.nextLine();
if (input.equals("switch")) {
cliView.printLine();
cliView.showEditPlanPrompt2();
cliView.printLine();
String[] pos = sc.nextLine().split(" ");
Expand All @@ -312,21 +316,18 @@ public void editPlan(final String intensity,
cliView.showActivityAdded();
System.out.println(" "
+ getList().get(lastAdded).toString());
cliView.printLine();
} else if (input.equals("show")) {
if (getList().isEmpty()) {
cliView.showNoActivity();
} else {
cliView.printLine();
cliView.showViewPlan(viewPlan());
cliView.printLine();
}
} else if (input.equals("finalize")) {
saveToMap(getList(), intensity, key);
cliView.printLine();
cliView.showEditPlanSuccessful();
System.out.println(viewPlan());
cliView.printLine();
break;
}
}
Expand Down Expand Up @@ -356,14 +357,19 @@ private void saveToMap(final ArrayList<MyTraining> newList,
final String key) throws IOException {
if (key.equals("0")) {
int planNum = 1;
Set<String> keys = getMap().keySet();
for (String k : keys) {
if (k.contains(intensity)) {
planNum++;
try {
Set<String> keys = getMap().keySet();
for (String k : keys) {
if (k.contains(intensity)) {
planNum++;
}
}
} catch (NullPointerException e) {
System.out.println("");
} finally {
String k = createKey(intensity, planNum);
getMap().put(k, newList);
}
String k = createKey(intensity, planNum);
getMap().put(k, newList);
} else {
getMap().put(key, newList);
}
Expand All @@ -382,10 +388,10 @@ public void createPlan(final String intensity) {
boolean inCreation = true;
if (Intensity.contains(intensity)) {
while (inCreation) {
cliView.printLine();
if (sc.hasNextLine()) {
String input = sc.nextLine();
if (input.equals("finalize")) {
cliView.printLine();
cliView.showPlanCreated();
System.out.println(viewPlan());
cliView.printLine();
Expand Down Expand Up @@ -418,7 +424,6 @@ public void createPlan(final String intensity) {
switchPos(Integer.parseInt(pos[0]),
Integer.parseInt(pos[1]));
cliView.showPlanPrompt2();
cliView.printLine();
} else {
System.out.println("Input correct "
+ "position numbers.");
Expand All @@ -442,7 +447,6 @@ public void createPlan(final String intensity) {
System.out.println(" "
+ getList().get(lastAdded).toString());
cliView.showPlanPrompt2();
cliView.printLine();
}
}
}
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/duke/parser/ParserTrainingPlan.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package duke.parser;

import duke.models.MyPlan;
import duke.models.MyTraining;
import duke.view.CliView;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import duke.data.Storage;

Expand All @@ -25,14 +25,29 @@ public class ParserTrainingPlan implements IParser {
*/
private MyPlan plan;

/**
* Location of save file for plan.
*/
private String userDir = System.getProperty("user.dir");
/**
* Filepath for the plan text file.
*/
private String path = userDir + "/plan.txt";

/**
* Constructor for ParserTrainingPlan.
* @throws IOException IO
*/
public ParserTrainingPlan() throws IOException {
cliView = new CliView();
File file = new File(path);
if (!file.exists()) {
FileWriter fileWriter = new FileWriter(path);
fileWriter.flush();
fileWriter.close();
}
plan = new MyPlan(new Storage(
".\\src\\main\\java\\duke\\data\\plan.txt").loadPlans());
path).loadPlans());
sc = new Scanner(System.in);
}

Expand Down

0 comments on commit 149f36b

Please sign in to comment.