samedi 17 octobre 2020

applying java design patterns [closed]

I have these two classes and I need to apply for them any of these patterns (template, strategy, factory, singleton) and I can't understand how I can apply any of these for these classes. First class is basically constructor setters and getters for this controller.

public class LaptopTableController implements Initializable {

@FXML
private TableView<ModelLaptopTable> table;

@FXML
private TableColumn<ModelLaptopTable, Integer> col_laptopid;

@FXML
private TableColumn<ModelLaptopTable, String> col_laptopname;

@FXML
private TableColumn<ModelLaptopTable, String> col_laptopprice;

ObservableList<ModelLaptopTable> oblist;
ObservableList<ModelLaptopTable> dataList;

@FXML
private TextField txt_laptopname;

@FXML
private TextField txt_laptopprice;

@FXML
private TextField txt_laptopid;

@FXML
private TextField searchField;

int index = -1;

PreparedStatement pst = null;

@Override
public void initialize(URL url, ResourceBundle rb) {
    try {
        UpdateTable();
        search_laptops();
    } catch (SQLException e) {
        e.printStackTrace();
    }

}

public static ObservableList<ModelLaptopTable> getDataLaptops() throws SQLException {
    Connection con;
    ObservableList<ModelLaptopTable> oblist = FXCollections.observableArrayList();

    try {
        con = DBConnector.getConnection();

        ResultSet rs = con.createStatement().executeQuery("select * from laptops");

        while (rs.next()) {
            oblist.add(new ModelLaptopTable(Integer.parseInt(rs.getString(1)), rs.getString(2), rs.getString(3)));
        }

    } catch (SQLException e) {
        e.printStackTrace();
    }

    return oblist;
}

public void Add_Laptops() {
    Connection con;
    String sql = "insert into laptops (laptop_id, laptop_name, laptop_price)values(?, ?, ?)";
    try {
        con = DBConnector.getConnection();
        pst = con.prepareStatement(sql);
        pst.setString(1, txt_laptopid.getText());
        pst.setString(2, txt_laptopname.getText());
        pst.setString(3, txt_laptopprice.getText());
        pst.execute();
        UpdateTable();
        search_laptops();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

public void getSelected(MouseEvent event) {
    index = table.getSelectionModel().getSelectedIndex();
    if (index <= -1) {

        return;
    }
    txt_laptopid.setText(col_laptopid.getCellData(index).toString());
    txt_laptopname.setText(col_laptopname.getCellData(index).toString());
    txt_laptopprice.setText(col_laptopprice.getCellData(index).toString());
}

public void Delete() {
    Connection con;
    String sql = "delete from laptops where laptop_id = ?";
    try {
        con = DBConnector.getConnection();
        pst = con.prepareStatement(sql);
        pst.setString(1, txt_laptopid.getText());
        pst.execute();
        UpdateTable();
        search_laptops();
    } catch (Exception e) {

    }

}

public void UpdateTable() throws SQLException {
    col_laptopid.setCellValueFactory(new PropertyValueFactory<>("laptopid"));
    col_laptopname.setCellValueFactory(new PropertyValueFactory<>("laptopname"));
    col_laptopprice.setCellValueFactory(new PropertyValueFactory<>("laptopprice"));

    oblist = getDataLaptops();
    table.setItems(oblist);
}

public void search_laptops() throws SQLException {
    col_laptopid.setCellValueFactory(new PropertyValueFactory<ModelLaptopTable, Integer>("laptopid"));
    col_laptopname.setCellValueFactory(new PropertyValueFactory<ModelLaptopTable, String>("laptopname"));
    col_laptopprice.setCellValueFactory(new PropertyValueFactory<ModelLaptopTable, String>("laptopprice"));

    dataList = getDataLaptops();
    table.setItems(dataList);
    FilteredList<ModelLaptopTable> filteredData = new FilteredList<>(dataList, b -> true);
    searchField.textProperty().addListener((observable, oldValue, newValue) -> {
        filteredData.setPredicate(person -> {
            if (newValue == null || newValue.isEmpty()) {
                return true;
            }
            String lowerCaseFilter = newValue.toLowerCase();

            if (person.getLaptopname().toLowerCase().indexOf(lowerCaseFilter) != -1) {
                return true;
            } else if (person.getLaptopprice().toLowerCase().indexOf(lowerCaseFilter) != -1) {
                return true;
            }

            else
                return false;
        });
    });
    SortedList<ModelLaptopTable> sortedData = new SortedList<>(filteredData);
    sortedData.comparatorProperty().bind(table.comparatorProperty());
    table.setItems(sortedData);

Aucun commentaire:

Enregistrer un commentaire