I have 3 classes the first one is Library Item this is the super class. The other two classes are Book and Movie. When I want to fill my table view I want to make sure the correct property is called when populating the table view. I know it is easier to just call the director and author the same for ease of use, but I want to get it working for learning purposes. I have left out packages and imports for relevance.
LibraryItem class
public abstract class LibraryItem {
private int itemCode;
private String title;
private boolean availability;
private int memberIdentifier;
private LocalDate dateLent;
protected LibraryItem(int itemCode, String title, boolean availability, int memberIdentifier, LocalDate dateLent) {
this.itemCode = itemCode;
this.title = title;
this.availability = availability;
this.memberIdentifier = memberIdentifier;
this.dateLent = dateLent;
}
public int getItemCode() {
return itemCode;
}
public String getTitle() {
return title;
}
public boolean isAvailability() {
return availability;
}
public void setAvailability(boolean availability) {
this.availability = availability;
}
public int getMemberIdentifier() {
return memberIdentifier;
}
public void setMemberIdentifier(int memberIdentifier) {
this.memberIdentifier = memberIdentifier;
}
public LocalDate getDateLent() {
return dateLent;
}
public void setDateLent(LocalDate dateLent) {
this.dateLent = dateLent;
}
}
Book class
public class Book extends LibraryItem {
private String author;
protected Book(int itemCode, String title, boolean isLent, int memberIdentifier, LocalDate dateLent, String author) {
super(itemCode, title, isLent, memberIdentifier, dateLent);
this.author = author;
}
}
Movie class
public class Movie extends LibraryItem {
private String director;
protected Movie(int itemCode, String title, boolean isLent, int memberIdentifier, LocalDate dateLent, String director) {
super(itemCode, title, isLent, memberIdentifier, dateLent);
this.director = director;
}
}
I was thinking maybe there is some kind of check I can do for each row implemented so the correct value will be given,
This was my attempt:
public class CollectionController implements Initializable {
@FXML
private TableView<LibraryItem> libraryItemsTable;
@FXML
private TableColumn<LibraryItem, String> itemCodeColumn;
@FXML
private TableColumn<LibraryItem, String> availableColumn;
@FXML
private TableColumn<LibraryItem, String> titleColumn;
@FXML
private TableColumn<LibraryItem, String> authorDirectorColumn;
private LibraryService libraryService = new LibraryService();
@Override
public void initialize(URL location, ResourceBundle resources) {
initializeTableView();
}
private void initializeTableView() {
List<LibraryItem> libraryItems = libraryService.getLibraryItems();
itemCodeColumn.setCellValueFactory(new PropertyValueFactory<>("itemCode"));
availableColumn.setCellValueFactory(new PropertyValueFactory<>("availability"));
titleColumn.setCellValueFactory(new PropertyValueFactory<>("title"));
// implement here check for each new row
if (checkIfBook(row))
authorDirectorColumn.setCellValueFactory(new PropertyValueFactory<>("author"));
else
authorDirectorColumn.setCellValueFactory(new PropertyValueFactory<>("director"));
//
libraryItemsTable.getItems().addAll(libraryItems);
}
CodePudding user response: