From d11f8070cac02fca3a040289add3bb69bae2791d Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 1 Feb 2026 14:56:00 -0700 Subject: [PATCH] Fixed Views for Product and Suppliers --- src/main/java/module-info.java | 1 + .../petshopdesktop/DTOs/ProductDTO.java | 131 ++++++++++++++++++ .../controllers/ProductController.java | 44 +++--- .../controllers/SupplierController.java | 12 +- .../SupplierDialogController.java | 5 + .../petshopdesktop/database/ProductDB.java | 41 ++++++ .../petshopdesktop/database/SupplierDB.java | 9 ++ .../petshopdesktop/models/Product.java | 15 +- .../petshopdesktop/models/Supplier.java | 13 ++ 9 files changed, 242 insertions(+), 29 deletions(-) create mode 100644 src/main/java/org/example/petshopdesktop/DTOs/ProductDTO.java create mode 100644 src/main/java/org/example/petshopdesktop/controllers/dialogcontrollers/SupplierDialogController.java diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 4dcea055..0c7ee955 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -3,6 +3,7 @@ module org.example.petshopdesktop { requires javafx.fxml; requires java.sql; + opens org.example.petshopdesktop.DTOs to javafx.base; opens org.example.petshopdesktop.models to javafx.base; opens org.example.petshopdesktop to javafx.fxml; exports org.example.petshopdesktop; diff --git a/src/main/java/org/example/petshopdesktop/DTOs/ProductDTO.java b/src/main/java/org/example/petshopdesktop/DTOs/ProductDTO.java new file mode 100644 index 00000000..865af1de --- /dev/null +++ b/src/main/java/org/example/petshopdesktop/DTOs/ProductDTO.java @@ -0,0 +1,131 @@ +package org.example.petshopdesktop.DTOs; + +import javafx.beans.property.SimpleDoubleProperty; +import javafx.beans.property.SimpleIntegerProperty; +import javafx.beans.property.SimpleStringProperty; +import org.example.petshopdesktop.models.Product; + +/** + * The class for productDTO, all product data is store here but also gets categoryName + */ +public class ProductDTO { + private SimpleIntegerProperty prodId; + private SimpleStringProperty prodName; + private SimpleStringProperty prodSku; + private SimpleDoubleProperty prodPrice; + private SimpleIntegerProperty categoryId; //used for edit and delete + private SimpleStringProperty categoryName; + private SimpleStringProperty prodDesc; + + //constructor + public ProductDTO(int prodId, String prodName, String prodSku, double prodPrice, int categoryId, String categoryName, String prodDesc) { + this.prodId = new SimpleIntegerProperty(prodId); + this.prodName = new SimpleStringProperty(prodName); + this.prodSku = new SimpleStringProperty(prodSku); + this.prodPrice = new SimpleDoubleProperty(prodPrice); + this.categoryId = new SimpleIntegerProperty(categoryId); + this.categoryName = new SimpleStringProperty(categoryName); + this.prodDesc = new SimpleStringProperty(prodDesc); + } + + //getter and setters + public int getProdId() { + return prodId.get(); + } + + public SimpleIntegerProperty prodIdProperty() { + return prodId; + } + + public void setProdId(int prodId) { + this.prodId.set(prodId); + } + + public String getProdName() { + return prodName.get(); + } + + public SimpleStringProperty prodNameProperty() { + return prodName; + } + + public void setProdName(String prodName) { + this.prodName.set(prodName); + } + + public String getProdSku() { + return prodSku.get(); + } + + public SimpleStringProperty prodSkuProperty() { + return prodSku; + } + + public void setProdSku(String prodSku) { + this.prodSku.set(prodSku); + } + + public double getProdPrice() { + return prodPrice.get(); + } + + public SimpleDoubleProperty prodPriceProperty() { + return prodPrice; + } + + public void setProdPrice(double prodPrice) { + this.prodPrice.set(prodPrice); + } + + public String getCategoryName() { + return categoryName.get(); + } + + public SimpleStringProperty categoryNameProperty() { + return categoryName; + } + + public void setCategoryName(String categoryName) { + this.categoryName.set(categoryName); + } + + public String getProdDesc() { + return prodDesc.get(); + } + + public SimpleStringProperty prodDescProperty() { + return prodDesc; + } + + public void setProdDesc(String prodDesc) { + this.prodDesc.set(prodDesc); + } + + public int getCategoryId() { + return categoryId.get(); + } + + public SimpleIntegerProperty categoryIdProperty() { + return categoryId; + } + + public void setCategoryId(int categoryId) { + this.categoryId.set(categoryId); + } + + /** + * Converts DTO into product for editing and deleting + * @return + */ + public Product toProduct(){ + Product product = new Product( + getProdId(), + getProdName(), + getProdSku(), + getProdPrice(), + getCategoryId(), + getProdDesc() + ); + return product; + } +} diff --git a/src/main/java/org/example/petshopdesktop/controllers/ProductController.java b/src/main/java/org/example/petshopdesktop/controllers/ProductController.java index 3b71614f..c79110fd 100644 --- a/src/main/java/org/example/petshopdesktop/controllers/ProductController.java +++ b/src/main/java/org/example/petshopdesktop/controllers/ProductController.java @@ -9,11 +9,15 @@ import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.TextField; import javafx.scene.control.cell.PropertyValueFactory; +import org.example.petshopdesktop.DTOs.ProductDTO; import org.example.petshopdesktop.database.ProductDB; import org.example.petshopdesktop.models.Product; import java.sql.SQLException; +/** + * The controller for any operations in the products view + */ public class ProductController { @FXML @@ -26,54 +30,52 @@ public class ProductController { private Button btnEdit; @FXML - private TableColumn colProductCategory; + private TableColumn colProductCategory; @FXML - private TableColumn colProductDesc; + private TableColumn colProductDesc; @FXML - private TableColumn colProductId; + private TableColumn colProductId; @FXML - private TableColumn colProductName; + private TableColumn colProductName; @FXML - private TableColumn colProductPrice; + private TableColumn colProductPrice; @FXML - private TableColumn colProductSKU; + private TableColumn colProductSKU; @FXML - private TableView tvProducts; + private TableView tvProducts; @FXML private TextField txtSearch; //data declaration - private ObservableList data = FXCollections.observableArrayList(); //empty + private ObservableList data = FXCollections.observableArrayList(); //empty + private String mode = null; /** - * Set up the table view for fees and display it when starting up + * Set up the table view for products and display it when starting up */ @FXML void initialize() { //set up table columns - colProductId.setCellValueFactory(new PropertyValueFactory("prodId")); - colProductName.setCellValueFactory(new PropertyValueFactory("prodName")); - colProductSKU.setCellValueFactory(new PropertyValueFactory("prodSku")); - colProductPrice.setCellValueFactory(new PropertyValueFactory("prodPrice")); - colProductCategory.setCellValueFactory(new PropertyValueFactory("categoryId")); - colProductDesc.setCellValueFactory(new PropertyValueFactory("prodDesc")); + colProductId.setCellValueFactory(new PropertyValueFactory("prodId")); + colProductName.setCellValueFactory(new PropertyValueFactory("prodName")); + colProductSKU.setCellValueFactory(new PropertyValueFactory("prodSku")); + colProductPrice.setCellValueFactory(new PropertyValueFactory("prodPrice")); + colProductCategory.setCellValueFactory(new PropertyValueFactory("categoryName")); + colProductDesc.setCellValueFactory(new PropertyValueFactory("prodDesc")); displayProducts(); - //TODO MUST DISPLAY CATEGORY NAME INSTEAD OF ID - - } /** - * Display the products to table view + * Display the productDTO to table view */ private void displayProducts(){ //Erase old content @@ -81,9 +83,9 @@ public class ProductController { //get Products from database try{ - data = ProductDB.getProducts(); + data = ProductDB.getProductDTO(); } catch (SQLException e) { - throw new RuntimeException(e); + System.out.println(e.getMessage()); } //put data in the table diff --git a/src/main/java/org/example/petshopdesktop/controllers/SupplierController.java b/src/main/java/org/example/petshopdesktop/controllers/SupplierController.java index 07730787..6ac02698 100644 --- a/src/main/java/org/example/petshopdesktop/controllers/SupplierController.java +++ b/src/main/java/org/example/petshopdesktop/controllers/SupplierController.java @@ -14,6 +14,9 @@ import org.example.petshopdesktop.models.Supplier; import java.sql.SQLException; +/** + * The controller for any operations in the supplier view + */ public class SupplierController { @FXML @@ -48,19 +51,24 @@ public class SupplierController { private ObservableList data = FXCollections.observableArrayList(); + /** + * Set up the table view for suppliers and display it when starting up + */ @FXML void initialize(){ colSupplierId.setCellValueFactory(new PropertyValueFactory("supId")); colSupplierName.setCellValueFactory(new PropertyValueFactory("supCompany")); - colContactPerson.setCellValueFactory(new PropertyValueFactory("supContactFirstName")); + colContactPerson.setCellValueFactory(new PropertyValueFactory("supFullName")); colSupplierEmail.setCellValueFactory(new PropertyValueFactory("supEmail")); colSupplierPhone.setCellValueFactory(new PropertyValueFactory("supPhone")); displaySupplier(); - //TODO MUST DISPLAY FULL NAME INSTEAD OF FIRST NAME ALSO ADD COMMENTS LATER } + /** + * Display the suppliers to table view + */ private void displaySupplier(){ data.clear(); diff --git a/src/main/java/org/example/petshopdesktop/controllers/dialogcontrollers/SupplierDialogController.java b/src/main/java/org/example/petshopdesktop/controllers/dialogcontrollers/SupplierDialogController.java new file mode 100644 index 00000000..de8c1f6d --- /dev/null +++ b/src/main/java/org/example/petshopdesktop/controllers/dialogcontrollers/SupplierDialogController.java @@ -0,0 +1,5 @@ +package org.example.petshopdesktop.controllers.dialogcontrollers; + +public class SupplierDialogController { + +} diff --git a/src/main/java/org/example/petshopdesktop/database/ProductDB.java b/src/main/java/org/example/petshopdesktop/database/ProductDB.java index d113d679..5eaf1e89 100644 --- a/src/main/java/org/example/petshopdesktop/database/ProductDB.java +++ b/src/main/java/org/example/petshopdesktop/database/ProductDB.java @@ -2,6 +2,7 @@ package org.example.petshopdesktop.database; import javafx.collections.FXCollections; import javafx.collections.ObservableList; +import org.example.petshopdesktop.DTOs.ProductDTO; import org.example.petshopdesktop.models.Product; import java.io.FileInputStream; @@ -14,6 +15,11 @@ import java.util.Properties; */ public class ProductDB { + /** + * gets all the products into an observable list + * @return a list of all the products + * @throws SQLException if failed to find products in the database + */ public static ObservableList getProducts() throws SQLException{ //Connect to the database ObservableList products = FXCollections.observableArrayList(); @@ -39,4 +45,39 @@ public class ProductDB { conn.close(); return products; } + + /** + * gets all the ProductDTOs into an observable list for display (displays categoryName instead of categoryId) + * @return the list of all the ProductDTOs + * @throws SQLException if failed to find products in the database + */ + public static ObservableList getProductDTO() throws SQLException{ + //Connect to the database + ObservableList products = FXCollections.observableArrayList(); + Connection conn = ConnectionDB.getConnection(); + + //Execute Query + Statement stmt = conn.createStatement(); + String sql = "SELECT p.prodId, p.prodName, p.prodSku, p.prodPrice, p.categoryId, c.categoryName, p.prodDesc " + + "FROM product p " + + "LEFT JOIN category c ON p.categoryId = c.categoryId"; + ResultSet rs = stmt.executeQuery(sql); + + //While there is still data add products to the list + while(rs.next()){ + ProductDTO product = new ProductDTO( + rs.getInt(1), + rs.getString(2), + rs.getString(3), + rs.getDouble(4), + rs.getInt(5), + rs.getString(6), + rs.getString(7)); + products.add(product); + } + + //close connection and return products + conn.close(); + return products; + } } diff --git a/src/main/java/org/example/petshopdesktop/database/SupplierDB.java b/src/main/java/org/example/petshopdesktop/database/SupplierDB.java index 55426d3d..ca41bc30 100644 --- a/src/main/java/org/example/petshopdesktop/database/SupplierDB.java +++ b/src/main/java/org/example/petshopdesktop/database/SupplierDB.java @@ -10,7 +10,16 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +/** + * A class containing all the methods relating to CRUD on Suppliers table + */ public class SupplierDB { + + /** + * gets all the suppliers into an observable list + * @return a list of all the suppliers + * @throws SQLException if failed to find suppliers in the database + */ public static ObservableList getSuppliers() throws SQLException { //Connect to the database ObservableList suppliers = FXCollections.observableArrayList(); diff --git a/src/main/java/org/example/petshopdesktop/models/Product.java b/src/main/java/org/example/petshopdesktop/models/Product.java index 62217315..1a7333b3 100644 --- a/src/main/java/org/example/petshopdesktop/models/Product.java +++ b/src/main/java/org/example/petshopdesktop/models/Product.java @@ -4,13 +4,16 @@ import javafx.beans.property.SimpleDoubleProperty; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleStringProperty; +/** + * The class for the entity of products (contains all data relating to products) + */ public class Product { - public SimpleIntegerProperty prodId; - public SimpleStringProperty prodName; - public SimpleStringProperty prodSku; - public SimpleDoubleProperty prodPrice; - public SimpleIntegerProperty categoryId; - public SimpleStringProperty prodDesc; + private SimpleIntegerProperty prodId; + private SimpleStringProperty prodName; + private SimpleStringProperty prodSku; + private SimpleDoubleProperty prodPrice; + private SimpleIntegerProperty categoryId; + private SimpleStringProperty prodDesc; //constructor public Product(int prodId, String prodName, String prodSku, double prodPrice, int categoryId, String prodDesc) { diff --git a/src/main/java/org/example/petshopdesktop/models/Supplier.java b/src/main/java/org/example/petshopdesktop/models/Supplier.java index 5822a977..36f05094 100644 --- a/src/main/java/org/example/petshopdesktop/models/Supplier.java +++ b/src/main/java/org/example/petshopdesktop/models/Supplier.java @@ -3,6 +3,9 @@ package org.example.petshopdesktop.models; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleStringProperty; +/** + * The class for the entity of supplier (contains all data relating to suppliers) + */ public class Supplier { SimpleIntegerProperty supId; SimpleStringProperty supCompany; @@ -93,4 +96,14 @@ public class Supplier { public void setSupPhone(String supPhone) { this.supPhone.set(supPhone); } + + //custom methods + + /** + * Get the full name of supplier contact to display in a single cell + * @return full name of supplier contact + */ + public String getSupFullName() { + return getSupContactFirstName() + " " + getSupContactLastName(); + } }