forked from nus-cs2103-AY1920S1/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.
[Complete] Import Interviewer's Data from .csv file (#67)
* Import from excel function * Adds implementation for ExcelReader * debug importExcel classes * Check style for Import branch * Reads csv file correctly * Finished up import interviewers feature * Integrated with Schedule class and removed ScheduleStub * Handles import exceptions * Corrected Styling * Adds test for Import commands * Corrects styling for Import Tests * Rename ExcelReader to CsvReader
- Loading branch information
1 parent
53ad58f
commit 624f005
Showing
12 changed files
with
193 additions
and
83 deletions.
There are no files selected for viewing
52 changes: 36 additions & 16 deletions
52
src/main/java/seedu/address/logic/commands/ImportCommand.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 |
---|---|---|
@@ -1,43 +1,63 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
|
||
import seedu.address.model.Model; | ||
import seedu.address.model.util.ExcelReader; | ||
import seedu.address.model.Schedule; | ||
import seedu.address.model.util.CsvReader; | ||
|
||
|
||
/** | ||
* Import excel file containing interviewee's information. | ||
* Import csv file containing interviewer's/ interviewers's information. | ||
*/ | ||
public class ImportCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "import"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Import excel file containing " | ||
+ "interviewee's information.\n" | ||
+ "Example: " + COMMAND_WORD + "<excelFilePath>"; | ||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Import .csv file containing " | ||
+ "interviewer or interviewee's information.\n" | ||
+ "Example: " + COMMAND_WORD + " interviewer " + "<csvFilePath>"; | ||
|
||
public static final String SHOWING_MESSAGE = "Data imported successfully."; | ||
public static final String SUCCESS_MESSAGE = "Data imported successfully."; | ||
public static final String MESSAGE_NOT_IMPLEMENTED_YET = "Command not implemented yet"; | ||
public static final String INCORRECT_FORMAT = "Data is in incorrect format. Please refer to the " | ||
+ "User Guide for the supported format"; | ||
public static final String FILE_DOES_NOT_EXIST = "Target file does not exist. Please ensure that " | ||
+ "the file path is correct."; | ||
|
||
private String filePath; | ||
private String type; | ||
|
||
public ImportCommand(String filePath) { | ||
this.filePath = filePath; | ||
public ImportCommand(String args) { | ||
String[] strings = args.split(" "); | ||
this.type = strings[0]; | ||
this.filePath = strings[1]; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) { | ||
String result = SHOWING_MESSAGE; | ||
try { | ||
ExcelReader excelReader = new ExcelReader(filePath); | ||
result = excelReader.translate(); | ||
|
||
try { | ||
if (this.type.equals("interviewer")) { | ||
ArrayList<Schedule> schedules; | ||
CsvReader csvReader = new CsvReader(filePath); | ||
schedules = csvReader.read(); | ||
model.setSchedulesList(schedules); | ||
return new CommandResult(SUCCESS_MESSAGE, false, false); | ||
} else if (this.type.equals("interviewee")) { | ||
return new CommandResult(MESSAGE_NOT_IMPLEMENTED_YET, false, false); | ||
} else { | ||
return new CommandResult(MESSAGE_USAGE, false, false); | ||
} | ||
} catch (FileNotFoundException e) { | ||
return new CommandResult(FILE_DOES_NOT_EXIST, false, false); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
result = "File is in wrong format"; | ||
return new CommandResult("Failed", false, false); | ||
} catch (ArrayIndexOutOfBoundsException e) { | ||
return new CommandResult(INCORRECT_FORMAT, false, false); | ||
} | ||
|
||
return new CommandResult(result, false, false); | ||
|
||
} | ||
} |
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
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,75 @@ | ||
package seedu.address.model.util; | ||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
|
||
import seedu.address.model.Schedule; | ||
|
||
/** | ||
* Helper class to read .csv files (Comma separated values). | ||
*/ | ||
public class CsvReader { | ||
private String filePath; | ||
|
||
/** | ||
* Constructor for CsvReader object to read from excel. | ||
* @param filePath | ||
*/ | ||
public CsvReader(String filePath) { | ||
this.filePath = filePath; | ||
} | ||
|
||
/** | ||
* Reads from excel and returns the corresponding string. | ||
* @return String | ||
* @throws IOException if input file is not found. | ||
*/ | ||
public ArrayList<Schedule> read() throws IOException { | ||
|
||
BufferedReader csvReader = new BufferedReader(new FileReader(filePath)); | ||
String firstLine = csvReader.readLine(); | ||
int numberOfDays = getValue(firstLine.split(",")[0]); | ||
int numberOfColumns = getValue(firstLine.split(",")[1]) + 1; | ||
ArrayList<Schedule> schedules = new ArrayList<>(); | ||
csvReader.readLine(); //removes next line | ||
for (int i = 0; i < numberOfDays; i++) { | ||
LinkedList<LinkedList<String>> table = new LinkedList<>(); | ||
String row; | ||
boolean firstEncounter = true; | ||
while ((row = csvReader.readLine()) != null) { | ||
String[] data = row.split(",", -1); | ||
if (data[0].equals("")) { | ||
if (firstEncounter) { | ||
firstEncounter = false; | ||
} else { | ||
break; | ||
} | ||
} else if (numberOfColumns != 0) { | ||
LinkedList<String> dataRow = new LinkedList<>(); | ||
for (int j = 0; j < numberOfColumns; j++) { | ||
String element = data[j]; | ||
dataRow.add(element); | ||
} | ||
table.add(dataRow); | ||
} | ||
} | ||
String date = table.getFirst().getFirst(); | ||
schedules.add(new Schedule(date, table)); | ||
} | ||
csvReader.close(); | ||
return schedules; | ||
} | ||
|
||
private static int getValue(String element) { | ||
String[] strings = element.split("= "); | ||
return Integer.parseInt(strings[1]); | ||
} | ||
|
||
private boolean fileExists() { | ||
File file = new File(this.filePath); | ||
return file.exists(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,27 @@ | ||
Days = 3,Interviewers = 10,,,,,,,,, | ||
,,,,,,,,,, | ||
,,,,,,,,,, | ||
9/10/2019 (Wed),Deparment A - Person A,Deparment A - Person B,Deparment B - Person C,Deparment B - Person D,Department B - Person E,Deparment C - Person F,Deparment C - Person G,Deparment D - Person H,Deparment E - Person I,Deparment E - Person J | ||
6:00pm - 6:30pm,1,1,,,,,,,, | ||
6:30pm - 7:00pm,0,1,,,,,,,, | ||
7:00pm - 7:30 pm,,,,,,,,,, | ||
7:30pm - 8:00 pm,,,,,,,,,, | ||
8:00pm - 8:30 pm,,,,,,,,,, | ||
8:30pm - 9:00pm,,,,,,,,,, | ||
,,,,,,,,,, | ||
,,,,,,,,,, | ||
10/10/2019 (Thur),Deparment A - Person A,Deparment A - Person B,Deparment B - Person C,Deparment B - Person D,Department B - Person E,Deparment C - Person F,Deparment C - Person G,Deparment D - Person H,Deparment E - Person I,Deparment E - Person J | ||
6:00pm - 6:30pm,,,,,,,,,, | ||
6:30pm - 7:00pm,,,,,,,,,, | ||
7:00pm - 7:30 pm,,,,,,,,,, | ||
7:30pm - 8:00 pm,,,,,,,,,, | ||
8:00pm - 8:30 pm,,,,,,,,,, | ||
8:30pm - 9:00pm,,,,,,,,,, | ||
,,,,,,,,,, | ||
,,,,,,,,,, | ||
11/10/2019 (Fri),Deparment A - Person A,Deparment A - Person B,Deparment B - Person C,Deparment B - Person D,Department B - Person E,Deparment C - Person F,Deparment C - Person G,Deparment D - Person H,Deparment E - Person I,Deparment E - Person J | ||
6:00pm - 6:30pm,,,,,,,,,, | ||
6:30pm - 7:00pm,,,,,,,,,, | ||
7:00pm - 7:30 pm,,,,,,,,,, | ||
7:30pm - 8:00 pm,,,,,,,,,, | ||
8:00pm - 8:30 pm,,,,,,,,,, |
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
32 changes: 32 additions & 0 deletions
32
src/test/java/seedu/address/logic/commands/ImportCommandTest.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,32 @@ | ||
package seedu.address.logic.commands; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.model.Model; | ||
import seedu.address.model.ModelManager; | ||
|
||
|
||
public class ImportCommandTest { | ||
private static final String SUCCESS_FILE_PATH = "src/test/data/ImportsTest/test.csv"; | ||
private static final String INVALID_FILE_PATH = "invalid/file/path"; | ||
private static final String INTERVIEWER = "interviewer"; | ||
private Model model = new ModelManager(); | ||
|
||
@Test | ||
public void interviewerImportCommandSuccess() { | ||
ImportCommand importCommand = new ImportCommand(INTERVIEWER + " " + SUCCESS_FILE_PATH); | ||
CommandResult expectedCommandResult = new CommandResult(ImportCommand.SUCCESS_MESSAGE, false, false); | ||
Model expectedModel = model; | ||
assertCommandSuccess(importCommand, model, expectedCommandResult, expectedModel); | ||
} | ||
|
||
@Test | ||
public void interviewerImportCommandFailure() { | ||
ImportCommand importCommand = new ImportCommand(INTERVIEWER + " " + INVALID_FILE_PATH); | ||
CommandResult expectedCommandResult = new CommandResult(ImportCommand.FILE_DOES_NOT_EXIST, false, false); | ||
assertEquals(importCommand.execute(model), expectedCommandResult); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/test/java/seedu/address/logic/parser/ImportCommandParserTest.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,17 @@ | ||
package seedu.address.logic.parser; | ||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.logic.commands.ImportCommand; | ||
|
||
|
||
|
||
public class ImportCommandParserTest { | ||
private ImportCommandParser parser = new ImportCommandParser(); | ||
@Test | ||
public void parse_emptyArg_throwsParseException() { | ||
assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, ImportCommand.MESSAGE_USAGE)); | ||
} | ||
} |