Skip to content

Commit

Permalink
Separate column titles from Schedule's observable list (#73)
Browse files Browse the repository at this point in the history
* Separate column titles from Schedule's observable list

* Separate column titles from Schedule's observable list

* Fix failing tests

* Fix travis issue
  • Loading branch information
ChrisKheng committed Oct 21, 2019
1 parent 35bab8d commit dae47ae
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 67 deletions.
29 changes: 29 additions & 0 deletions src/main/java/seedu/address/commons/util/Pair.java
@@ -0,0 +1,29 @@
package seedu.address.commons.util;

/**
* Encapsulates a pair, with a head and a tail.
* @param <H> head of the pair.
* @param <T> tail of the pair.
*/
public class Pair<H, T> {
private H head;
private T tail;

public Pair(H head, T tail) {
this.head = head;
this.tail = tail;
}

public H getHead() {
return head;
}

public T getTail() {
return tail;
}

@Override
public String toString() {
return String.format("(%s, %s)", head, tail);
}
}
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/logic/Logic.java
Expand Up @@ -50,6 +50,9 @@ public interface Logic {
/** Returns a list of @code{ObservableList} objects, each representing a Schedule table*/
List<ObservableList<ObservableList<String>>> getObservableLists();

/** Returns a list of lists of column titles, each list of column titles belong to a Schedule table*/
List<List<String>> getTitlesLists();

/**
* Returns the user prefs' GUI settings.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/logic/LogicManager.java
Expand Up @@ -78,6 +78,11 @@ public List<ObservableList<ObservableList<String>>> getObservableLists() {
return model.getObservableLists();
}

@Override
public List<List<String>> getTitlesLists() {
return model.getTitlesLists();
}

@Override
public GuiSettings getGuiSettings() {
return model.getGuiSettings();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Expand Up @@ -53,6 +53,9 @@ public interface Model {
*/
List<ObservableList<ObservableList<String>>> getObservableLists();

/** Returns a list of lists of column titles, each list of column titles belong to a Schedule table*/
List<List<String>> getTitlesLists();

/**
* Returns an Interviewee given the intervieweeName.
* The Interviewee must exist in the database.
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Expand Up @@ -123,6 +123,16 @@ public List<ObservableList<ObservableList<String>>> getObservableLists() {
return observableLists;
}

/** Returns a list of lists of column titles, each list of column titles belong to a Schedule table*/
@Override
public List<List<String>> getTitlesLists() {
List<List<String>> titlesLists = new LinkedList<>();
for (Schedule schedule : schedulesList) {
titlesLists.add(schedule.getTitles());
}
return titlesLists;
}

@Override
public Interviewee getInterviewee(String intervieweeName) throws NoSuchElementException {
Person person = getPerson(intervieweeName);
Expand Down
118 changes: 76 additions & 42 deletions src/main/java/seedu/address/model/Schedule.java
Expand Up @@ -17,11 +17,20 @@
*/
public class Schedule {
private String date;
private ObservableList<ObservableList<String>> table; // Include the first row which is the column titles
private ObservableList<String> titles;
private ObservableList<ObservableList<String>> data; // EXCLUDE the first row which is the column titles

public Schedule(String date, LinkedList<LinkedList<String>> list) {
this.date = date;
this.table = toTwoDimensionalObservableList(list);

ObservableList<ObservableList<String>> table = toTwoDimensionalObservableList(list);
if (table.isEmpty()) {
this.titles = FXCollections.observableList(new LinkedList<>());
} else {
this.titles = table.remove(0);
}

this.data = table;
}

private Schedule() {
Expand All @@ -31,17 +40,22 @@ public String getDate() {
return date;
}

public ObservableList<String> getTitles() {
return titles;
}

public ObservableList<ObservableList<String>> getObservableList() {
return table;
return data;
}

public List<Slot> getInterviewSlots(String intervieweeName) {
List<Slot> slots = new LinkedList<>();
int tableSize = table.size();
int tableSize = data.size();

// Exclude search in the first row as the first row is column titles
for (int i = 1; i < tableSize; i++) {
ObservableList<String> row = table.get(i);
// Need to search the first row as well because now the first row of data(table) is not the titles,
// it is data.
for (int i = 0; i < tableSize; i++) {
ObservableList<String> row = data.get(i);
int rowSize = row.size();

// Exclude search in the first cell as the first cell is the time slot
Expand All @@ -67,10 +81,9 @@ public List<Slot> getInterviewSlots(String intervieweeName) {
*/
public boolean hasInterviewer(Interviewer interviewer) {
String columnTitle = generateColumnTitle(interviewer);
ObservableList<String> firstRow = table.get(0);

boolean found = false;
for (String title : firstRow) {
for (String title : titles) {
if (title.equals(columnTitle)) {
found = true;
break;
Expand Down Expand Up @@ -100,9 +113,9 @@ public boolean addInterviewer(Interviewer interviewer) {
.collect(Collectors.toList());

boolean added = false;
int currRowIndex = 1;
int currRowIndex = 0;
for (String availability : availabilities) {
if (currRowIndex > table.size()) {
if (currRowIndex > data.size()) {
break;
}

Expand All @@ -115,10 +128,10 @@ public boolean addInterviewer(Interviewer interviewer) {
}

// Iterate through the table rows
int tableSize = table.size();
int i;
int tableSize = data.size();
for (i = currRowIndex; i < tableSize; i++) {
ObservableList<String> currRow = table.get(i);
ObservableList<String> currRow = data.get(i);
String currRowTime = currRow.get(0);

if (!currRowTime.equals(time)) {
Expand All @@ -132,12 +145,13 @@ public boolean addInterviewer(Interviewer interviewer) {
currRowIndex = i;
}

// Add 0 to other rows to ensure that the table rows size are correct
if (added) {
int initialRowSize = table.get(0).size();
table.get(0).add(columnTitle);
for (int i = 1; i < table.size(); i++) {
ObservableList<String> currRow = table.get(i);
int initialRowSize = titles.size();
titles.add(columnTitle);

// Add 0 to other rows to ensure that the table rows size are correct
for (int i = 0; i < data.size(); i++) {
ObservableList<String> currRow = data.get(i);
if (currRow.size() == initialRowSize) {
currRow.add("0");
}
Expand Down Expand Up @@ -169,47 +183,48 @@ public boolean equals(Object s) {
}
Schedule sCasted = (Schedule) s;
return date.equals(sCasted.date)
&& table.equals(sCasted.table);
&& titles.equals(sCasted.titles)
&& data.equals(sCasted.data);
}

/**
* Returns a copy of the @code{Schedule} object given.
*
* @param schedule the @code{Schedule} object to be copied.
* @return the copy of the @code{Schedule} object.
*/
public static Schedule cloneSchedule(Schedule schedule) {
Schedule clone = new Schedule();
clone.date = String.valueOf(schedule.date);
clone.table = cloneTable(schedule.table);
clone.titles = cloneRow(schedule.titles);
clone.data = cloneTable(schedule.data);
return clone;
}

/**
* Returns an independent copy of the table given in observable list form.
*
* @param table the table to copy.
* @return the copy of the table.
* Returns an independent deep copy of the table given in observable list form.
*/
private static ObservableList<ObservableList<String>> cloneTable(ObservableList<ObservableList<String>> table) {
ObservableList<ObservableList<String>> tableClone = FXCollections.observableList(new LinkedList<>());

for (ObservableList<String> row : table) {
ObservableList<String> rowClone = FXCollections.observableList(new LinkedList<>());
for (String string : row) {
rowClone.add(String.valueOf(string));
}
ObservableList<String> rowClone = cloneRow(row);
tableClone.add(rowClone);
}

return tableClone;
}

/**
* Returns an independent deep copy of the row given in observable list form.
*/
private static ObservableList<String> cloneRow(ObservableList<String> row) {
ObservableList<String> rowClone = FXCollections.observableList(new LinkedList<>());
for (String string : row) {
rowClone.add(String.valueOf(string));
}
return rowClone;
}

/**
* Convert a two-dimensional LinkedList into a two-dimensional Observable list.
*
* @param list a two-dimensional LinkedList
* @return the corresponding two-dimensional Observable list
*/
public static ObservableList<ObservableList<String>> toTwoDimensionalObservableList(
LinkedList<LinkedList<String>> list) {
Expand All @@ -226,15 +241,34 @@ public static ObservableList<ObservableList<String>> toTwoDimensionalObservableL

@Override
public String toString() {
StringBuffer buffer = new StringBuffer(450);
for (ObservableList<String> row : table) {
for (String value : row) {
buffer.append(value);
buffer.append(",");
}
buffer.append("\n");
StringBuilder builder = new StringBuilder(450);

// Append the title rows
String titleRep = rowToString(titles);
builder.append(titleRep);

// Append the other rows
for (ObservableList<String> row : data) {
String rowRep = rowToString(row);
builder.append(rowRep);
}

return builder.toString();
}

/**
* Convert a row to its string representation (each value separated by a comma, then the row ends with
* a newline character.
*/
private String rowToString(List<String> row) {
StringBuilder builder = new StringBuilder(110);

for (String value : row) {
builder.append(value);
builder.append(",");
}

return buffer.toString();
builder.append("\n");
return builder.toString();
}
}
12 changes: 6 additions & 6 deletions src/main/java/seedu/address/model/util/SampleDataUtil.java
Expand Up @@ -24,12 +24,12 @@ public class SampleDataUtil {
private static String[][] sampleFilledTable =
new String[][]{
{"10/9/2019", "Welfare - Hazel", "Technical - Johnathan", "Publicity - Lucia"},
{"18:00 - 18:30", "John", "Steven", "0"},
{"18:30 - 19:00", "Alex", "Clark", "John"},
{"19:00 - 19:30", "Alicia", "0", "charlie"},
{"19:30 - 20:00", "Charlie", "0", "Selina"},
{"20:00 - 20:30", "Selina", "0", "0"},
{"20:30 - 21:00", "Natal", "0", "0"}};
{"18:00-18:30", "John", "Steven", "0"},
{"18:30-19:00", "Alex", "Clark", "John"},
{"19:00-19:30", "Alicia", "0", "charlie"},
{"19:30-20:00", "Charlie", "0", "Selina"},
{"20:00-20:30", "Selina", "0", "0"},
{"20:30-21:00", "Natal", "0", "0"}};

public static Person[] getSamplePersons() {
return new Person[] {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/ui/MainWindow.java
Expand Up @@ -109,7 +109,7 @@ private void setAccelerator(MenuItem menuItem, KeyCombination keyCombination) {
* Fills up all the placeholders of this window.
*/
void fillInnerParts() {
scheduleView = new ScheduleView(logic.getObservableLists());
scheduleView = new ScheduleView(logic.getTitlesLists(), logic.getObservableLists());
schedulePanelPlaceholder.getChildren().add(scheduleView.getRoot());

resultDisplay = new ResultDisplay();
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/seedu/address/ui/ScheduleView.java
Expand Up @@ -16,13 +16,15 @@ public class ScheduleView extends UiPart<Region> {

private static final String FXML = "ScheduleView.fxml";

private List<ObservableList<ObservableList<String>>> scheduleList;
private List<List<String>> titles;
private List<ObservableList<ObservableList<String>>> scheduleList; // Excluding titles

@FXML
private TableView tableView;

ScheduleView(List<ObservableList<ObservableList<String>>> scheduleList) {
ScheduleView(List<List<String>> titles, List<ObservableList<ObservableList<String>>> scheduleList) {
super(FXML);
this.titles = titles;
this.scheduleList = scheduleList;
initialise();
}
Expand All @@ -31,11 +33,12 @@ public class ScheduleView extends UiPart<Region> {
* Allow the creation of table.
*/
private void initialise() {
for (int i = 0; i < this.scheduleList.get(0).get(0).size(); i++) {
// Currently the code here will only retrieve the first list of titles.
for (int i = 0; i < titles.get(0).size(); i++) {
final int finalIdx = i;
TableColumn<ObservableList<String>, String> column =
new TableColumn<ObservableList<String>, String>(
this.scheduleList.get(0).get(0).get(i)
titles.get(0).get(i)
);
column.setCellValueFactory(param ->
new ReadOnlyObjectWrapper<>(param.getValue().get(finalIdx))
Expand All @@ -44,7 +47,8 @@ private void initialise() {
this.tableView.setColumnResizePolicy(this.tableView.CONSTRAINED_RESIZE_POLICY);
}

for (int i = 1; i < this.scheduleList.get(0).size(); i++) {
// the data of the schedule now excludes the titles, so titles is not in the first row of data returned anymore
for (int i = 0; i < this.scheduleList.get(0).size(); i++) {
this.tableView.getItems().add(
this.scheduleList.get(0).get(i)
);
Expand Down
Expand Up @@ -120,6 +120,12 @@ public List<ObservableList<ObservableList<String>>> getObservableLists() {
throw new AssertionError("This method should not be called.");
}

@Override
public List<List<String>> getTitlesLists() {
// TODO: Implementation
return null;
}

@Override
public void emailInterviewee(Interviewee interviewee) {
throw new AssertionError("This method should not be called.");
Expand Down

0 comments on commit dae47ae

Please sign in to comment.