Skip to content

Commit

Permalink
Merge pull request #82 from harryleecp/Harry
Browse files Browse the repository at this point in the history
Add new storage package and class
  • Loading branch information
Khenus authored Oct 14, 2020
2 parents 97603fd + ad182d8 commit 0af853f
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/seedu/duke/PlanNus.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import seedu.duke.globalcommons.App;
import seedu.duke.objects.Person;
import seedu.duke.parser.AppParser;
import seedu.duke.storage.Storage;
import seedu.duke.ui.Ui;

import java.io.FileNotFoundException;
import java.io.IOException;

/**
* Class representing main function of PlanNUS.
*/
Expand Down Expand Up @@ -42,11 +46,18 @@ public PlanNus() {
* Main entry function for PlanNUS.
*/
public void run() {
Storage textFile = new Storage();
assert isStartupSuccessfully == true : "Startup is successful";
if (isStartupSuccessfully) {
showWelcomeMessage();
boolean isExit = false;

try {
textFile.loadTextFile(currentPerson);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

while (!isExit) {
try {
System.out.println(AWAIT_COMMAND);
Expand All @@ -61,6 +72,11 @@ public void run() {
System.out.println(e.getMessage());
}
}
try {
textFile.saveTextFile(currentPerson);
} catch (IOException e) {
System.out.println("There is a problem saving PlanNUS.txt");
}
showExitMessage();
}
}
Expand Down
87 changes: 87 additions & 0 deletions src/main/java/seedu/duke/storage/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package seedu.duke.storage;

import seedu.duke.apps.academicplanner.commons.AddUtils;
import seedu.duke.apps.moduleloader.ModuleLoader;
import seedu.duke.objects.PartialModule;
import seedu.duke.objects.Person;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

public class Storage {
private ModuleLoader allModules;
private AddUtils addUtils;

private static final String FILE_NAME = "PlanNUS.txt";
private static final String INDENT = " ";
private static final String NEWLINE = "\n";

private static final int SEMESTER_WORD_POSITION = 1;
private static final int SEMESTER_INDEX_POSITION = 2;
private static final int MODULE_CODE_POSITION = 0;
private static final int MODULE_GRADE_POSITION = 1;
private static final int MODULE_CREDIT_POSITION = 2;


public void loadTextFile(Person currentPerson) throws FileNotFoundException {
File f = new File("PlanNUS.txt");
Scanner in = new Scanner(f);
addUtils = new AddUtils(allModules, currentPerson);
int currSem = 0;

while (in.hasNextLine()) {
String line = in.nextLine();
String[] lineItems = line.split("\\s+");

if (lineItems[SEMESTER_WORD_POSITION].equals("SEMESTER")) {
currSem = Integer.parseInt(lineItems[SEMESTER_INDEX_POSITION]);
} else {
String currentModuleCode = lineItems[MODULE_CODE_POSITION];
String currentModuleGrade = lineItems[MODULE_GRADE_POSITION];
int currentModuleCredit = Integer.parseInt(lineItems[MODULE_CREDIT_POSITION]);

addUtils.addModuleToUser(currentModuleCode, currSem, currentModuleGrade, currentModuleCredit);
}
}
}

public void saveTextFile(Person currentPerson) throws IOException {
FileWriter fw = new FileWriter(FILE_NAME);
ArrayList<PartialModule> sortedBySem = currentPerson.getModulesList();
sortedBySem.sort(Comparator.comparing(PartialModule::getSemesterIndex));

int currSem = 0;
int newSem;
String textToSave = "";

for (PartialModule item : sortedBySem) {
newSem = item.getSemesterIndex();
if (newSem != currSem) {
currSem = newSem;
textToSave += INDENT + "SEMESTER " + currSem + NEWLINE;
}
int spacingCodeAndGrade = 8 + (8 - item.getModuleCode().length());
int spacingGradeAndCredit = 8 + (8 - item.getGrade().length());
textToSave += item.getModuleCode() + writeSpaces(spacingCodeAndGrade)
+ item.getGrade() + writeSpaces(spacingGradeAndCredit)
+ item.getModuleCredit() + NEWLINE;
}

fw.write(textToSave);
fw.close();
}

public String writeSpaces(int spacing) {
String actualSpaces = "";
while (spacing > 0) {
actualSpaces += " ";
spacing -= 1;
}
return actualSpaces;
}
}

0 comments on commit 0af853f

Please sign in to comment.