Pass array of getters as a function parameter-Java

121 Views Asked by At

i'm writing a function to create xls sheets from JFXTableView.

XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("user details");
XSSFRow header = sheet.createRow(0);
String arrayOfHeaders [] = {"Sr. No.","Name of the Member", "Customized workout card status","Contact No.","Current programme taken","Current package taken",
                            "Purpose of taking customized workout card", "body type identified"," Current Body weight","Current height","payment amount",
                            "Mode of payment"};

for(int i=0; i<arrayOfHeaders.length; i++){
    header.createCell(i).setCellValue(arrayOfHeaders[i]);
}
int index=1;
for(Member item: tableView.getItems()){
    XSSFRow row = sheet.createRow(index);
    int cellIndex=0;
    for (Method m : item.getClass().getMethods()) {
        // The getter should start with "get" 
        // I ignore getClass() method because it never returns null
        if (m.getName().startsWith("get") && !m.getName().equals("getClass")) {

            row.createCell(cellIndex).setCellValue((String) m.invoke(item));

        }
        cellIndex++;

}
    index++;
}

With the above code, i'm able to retrieve values from the table through the getters, but the problems is item.getClass().getMethods() returns getters in a random order and that is not acceptable as i want the values according to the headers as defined. I have many such tables, each with their own Class and getters, and writing different functions for each one of them seems too lengthy. So, what i'm planning to do is write a function where i could pass on the getters of each different table object in an array, so it could loop through all the getters of the particular tableView object. Something like this:

createSheets(arrayOfHeaders, tableView.getItems(), arrayOfGetters);

The example of one such Member Class being used by my current tableView is:

public static class Member{



    private final SimpleStringProperty name;
    private final SimpleStringProperty status;
    private final SimpleStringProperty contact;
    private final SimpleStringProperty programme;
    private final SimpleStringProperty packages;
    private final SimpleStringProperty purpose;
    private final SimpleStringProperty bodyType;
    private final SimpleStringProperty weight;
    private final SimpleStringProperty height;
    private final SimpleIntegerProperty paymentAmount;
    private final SimpleStringProperty paymentMode;


    public Member (String name, String status, String contact, String programme, String packages, String purpose, 
            String bodyType, String weight, String height, int paymentAmount, String paymentMode){

    this.name = new SimpleStringProperty(name);
    this.status = new SimpleStringProperty(status);
    this.contact = new SimpleStringProperty(contact);
    this.programme = new SimpleStringProperty(programme);
    this.packages = new SimpleStringProperty(packages);
    this.purpose = new SimpleStringProperty(purpose);
    this.bodyType = new SimpleStringProperty(bodyType);
    this.weight = new SimpleStringProperty(weight);
    this.height = new SimpleStringProperty(height);
    this.paymentAmount = new SimpleIntegerProperty(paymentAmount);
    this.paymentMode = new SimpleStringProperty(paymentMode);

    }

    public String getName() {
    return name.get();
    }

    public String getStatus() {
    return status.get();
    }

    public String getContact() {
    return contact.get();
    }

    public String getProgramme() {
    return programme.get();
    }

    public String getPackages() {
    return packages.get();
    }

    public String getPurpose() {
    return purpose.get();
    }

    public String getBodyType() {
    return bodyType.get();
    }

    public String getWeight() {
    return weight.get();
    }

    public String getHeight() {
    return height.get();
    }

    public int getPaymentAmount() {
    return paymentAmount.get();
    }

    public String getPaymentMode() {
    return paymentMode.get();
    }




}
0

There are 0 best solutions below