Fixed Views for Product and Suppliers
This commit is contained in:
131
src/main/java/org/example/petshopdesktop/DTOs/ProductDTO.java
Normal file
131
src/main/java/org/example/petshopdesktop/DTOs/ProductDTO.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<Product, Integer> colProductCategory;
|
||||
private TableColumn<ProductDTO, String> colProductCategory;
|
||||
|
||||
@FXML
|
||||
private TableColumn<Product, String> colProductDesc;
|
||||
private TableColumn<ProductDTO, String> colProductDesc;
|
||||
|
||||
@FXML
|
||||
private TableColumn<Product, Integer> colProductId;
|
||||
private TableColumn<ProductDTO, Integer> colProductId;
|
||||
|
||||
@FXML
|
||||
private TableColumn<Product, String> colProductName;
|
||||
private TableColumn<ProductDTO, String> colProductName;
|
||||
|
||||
@FXML
|
||||
private TableColumn<Product, Double> colProductPrice;
|
||||
private TableColumn<ProductDTO, Double> colProductPrice;
|
||||
|
||||
@FXML
|
||||
private TableColumn<Product, String> colProductSKU;
|
||||
private TableColumn<ProductDTO, String> colProductSKU;
|
||||
|
||||
@FXML
|
||||
private TableView<Product> tvProducts;
|
||||
private TableView<ProductDTO> tvProducts;
|
||||
|
||||
@FXML
|
||||
private TextField txtSearch;
|
||||
|
||||
//data declaration
|
||||
private ObservableList<Product> data = FXCollections.observableArrayList(); //empty
|
||||
private ObservableList<ProductDTO> 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<Product,Integer>("prodId"));
|
||||
colProductName.setCellValueFactory(new PropertyValueFactory<Product,String>("prodName"));
|
||||
colProductSKU.setCellValueFactory(new PropertyValueFactory<Product,String>("prodSku"));
|
||||
colProductPrice.setCellValueFactory(new PropertyValueFactory<Product,Double>("prodPrice"));
|
||||
colProductCategory.setCellValueFactory(new PropertyValueFactory<Product,Integer>("categoryId"));
|
||||
colProductDesc.setCellValueFactory(new PropertyValueFactory<Product,String>("prodDesc"));
|
||||
colProductId.setCellValueFactory(new PropertyValueFactory<ProductDTO,Integer>("prodId"));
|
||||
colProductName.setCellValueFactory(new PropertyValueFactory<ProductDTO,String>("prodName"));
|
||||
colProductSKU.setCellValueFactory(new PropertyValueFactory<ProductDTO,String>("prodSku"));
|
||||
colProductPrice.setCellValueFactory(new PropertyValueFactory<ProductDTO,Double>("prodPrice"));
|
||||
colProductCategory.setCellValueFactory(new PropertyValueFactory<ProductDTO,String>("categoryName"));
|
||||
colProductDesc.setCellValueFactory(new PropertyValueFactory<ProductDTO,String>("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
|
||||
|
||||
@@ -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<Supplier> data = FXCollections.observableArrayList();
|
||||
|
||||
/**
|
||||
* Set up the table view for suppliers and display it when starting up
|
||||
*/
|
||||
@FXML
|
||||
void initialize(){
|
||||
colSupplierId.setCellValueFactory(new PropertyValueFactory<Supplier, Integer>("supId"));
|
||||
colSupplierName.setCellValueFactory(new PropertyValueFactory<Supplier, String>("supCompany"));
|
||||
colContactPerson.setCellValueFactory(new PropertyValueFactory<Supplier, String>("supContactFirstName"));
|
||||
colContactPerson.setCellValueFactory(new PropertyValueFactory<Supplier, String>("supFullName"));
|
||||
colSupplierEmail.setCellValueFactory(new PropertyValueFactory<Supplier, String>("supEmail"));
|
||||
colSupplierPhone.setCellValueFactory(new PropertyValueFactory<Supplier, String>("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();
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.example.petshopdesktop.controllers.dialogcontrollers;
|
||||
|
||||
public class SupplierDialogController {
|
||||
|
||||
}
|
||||
@@ -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<Product> getProducts() throws SQLException{
|
||||
//Connect to the database
|
||||
ObservableList<Product> 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<ProductDTO> getProductDTO() throws SQLException{
|
||||
//Connect to the database
|
||||
ObservableList<ProductDTO> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Supplier> getSuppliers() throws SQLException {
|
||||
//Connect to the database
|
||||
ObservableList<Supplier> suppliers = FXCollections.observableArrayList();
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user