forked from nus-cs2103-AY1920S2/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
217 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package igrad.csvwriter; | ||
|
||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import igrad.model.module.Module; | ||
|
||
/** | ||
* Writes the data stored as a human-readable CSV file. | ||
*/ | ||
public class CsvWriter { | ||
|
||
private static final String fileName = "study_plan.csv"; | ||
private FileWriter csvWriter; | ||
private List<Module> sortedList; | ||
|
||
public CsvWriter(List<Module> sortedList) throws IOException { | ||
csvWriter = new FileWriter(fileName); | ||
|
||
this.sortedList = sortedList; | ||
} | ||
|
||
/** | ||
* Writes to CSV | ||
*/ | ||
public void write() throws IOException { | ||
writeHeaders(); | ||
writeBody(); | ||
closeWriter(); | ||
} | ||
|
||
private void closeWriter() throws IOException { | ||
csvWriter.flush(); | ||
csvWriter.close(); | ||
} | ||
|
||
private void appendNewLine() throws IOException { | ||
csvWriter.append("\n"); | ||
} | ||
|
||
private void append(String text) throws IOException { | ||
csvWriter.append(text); | ||
csvWriter.append(","); | ||
} | ||
|
||
/** | ||
* Writes each module as a line. Separates modules taken in different semesters | ||
* by a new line. | ||
*/ | ||
private void writeBody() throws IOException { | ||
|
||
for (int i = 0; i < sortedList.size(); i++) { | ||
Module module = sortedList.get(i); | ||
|
||
append(module.getSemester().toString()); | ||
append(module.getModuleCode().toString()); | ||
append(module.getTitle().toString()); | ||
append(module.getCredits().toString()); | ||
appendNewLine(); | ||
|
||
if (i < sortedList.size() - 1) { | ||
Module nextModule = sortedList.get(i + 1); | ||
if (!nextModule.getSemester().equals(module.getSemester())) { | ||
appendNewLine(); | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
/** | ||
* Writes the headers of the CSV file. | ||
*/ | ||
private void writeHeaders() throws IOException { | ||
|
||
String[] headers = { | ||
"Semester", | ||
"Module Code", | ||
"Module Title", | ||
"MCs" | ||
}; | ||
|
||
for (String header : headers) { | ||
append(header); | ||
} | ||
|
||
appendNewLine(); | ||
|
||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/igrad/csvwriter/exceptions/InvalidDataException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package igrad.csvwriter.exceptions; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Signals that one or more of the required fields are not available | ||
*/ | ||
public class InvalidDataException extends IOException { | ||
|
||
public InvalidDataException(String msg) { | ||
super(msg); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package igrad.logic.commands; | ||
|
||
import java.io.IOException; | ||
|
||
import igrad.csvwriter.CsvWriter; | ||
import igrad.logic.commands.exceptions.CommandException; | ||
import igrad.model.Model; | ||
import igrad.model.module.sorters.SortBySemester; | ||
|
||
/** | ||
* Format full help instructions for every command for display. | ||
*/ | ||
public class ExportCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "export"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Exports data to a CSV file.\n" | ||
+ "Example: " + COMMAND_WORD; | ||
|
||
public static final String SHOWING_EXPORT_MESSAGE = "I've exported your data to a CSV file." | ||
+ " You can find it in the same folder as this app's executable!"; | ||
public static final String EXPORT_ERROR_MESSAGE = "Unable to export data to CSV file." | ||
+ " Please ensure that you do not have the file <study_plan.csv> open and " | ||
+ "each module is tagged to a semester."; | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
|
||
try { | ||
CsvWriter csvWriter = new CsvWriter(model.getSortedModuleList(new SortBySemester())); | ||
csvWriter.write(); | ||
} catch (IOException e) { | ||
throw new CommandException(EXPORT_ERROR_MESSAGE); | ||
} | ||
|
||
return new CommandResult(SHOWING_EXPORT_MESSAGE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/igrad/model/module/sorters/SortBySemester.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package igrad.model.module.sorters; | ||
|
||
import java.util.Comparator; | ||
|
||
import igrad.model.module.Module; | ||
|
||
|
||
/** | ||
* Sorter for modules by semester | ||
*/ | ||
public class SortBySemester implements Comparator<Module> { | ||
|
||
/** | ||
* Compares the digits in the semester a module is tagged with. | ||
* Results in an ascending sort. | ||
*/ | ||
public int compare(Module m1, Module m2) { | ||
int s1 = extractDigits(m1.getSemester().toString()); | ||
int s2 = extractDigits(m2.getSemester().toString()); | ||
|
||
return s1 - s2; | ||
} | ||
|
||
/** | ||
* Extracts digits from a string | ||
*/ | ||
private int extractDigits(String str) { | ||
return Integer.parseInt(str.replaceAll("\\D+", "")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Semester,Module Code,Module Title,MCs, | ||
Y1S2,CS2040,Data,32, | ||
|
||
Y2S2,CS2030,Prog,4, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CS2103TCS2106MA2101CS2102 |