package inventorysystem.data;
import inventorysystem.gui.Supplier;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Date;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Epic
*/
public class Product {
private int productId;
private int productQuantity;
private float productPrice;
private String shippingWeight
;
private String productManufactureDate
;
private String productExpiryDate
;
private float costPrice;
private DBConnectionClass connectionClass;
public Product() {
this.connectionClass = new DBConnectionClass();
}
public Product
(int productID,
String productName,
int productQuantity,
float productPrice,
String shippingWeight,
String productManufactureDate,
String productBatch,
float costPrice
) {
this.productId = productID;
this.productName = productName;
this.productQuantity = productQuantity;
this.productPrice = productPrice;
this.shippingWeight = shippingWeight;
this.description = description;
this.productManufactureDate = productManufactureDate;
this.productExpiryDate = productExpiryDate;
this.productBatch = productBatch;
this.costPrice = costPrice;
this.connectionClass = new DBConnectionClass();
}
/**
* @return the productId
*/
public int getProductId() {
return productId;
}
/**
* @param productId the productId to set
*/
public void setProductId(int productId) {
this.productId = productId;
}
/**
* @return the productName
*/
public String getProductName
() {
return productName;
}
/**
* @param productName the productName to set
*/
public void setProductName
(String productName
) {
this.productName = productName;
}
/**
* @return the productQuantity
*/
public int getProductQuantity() {
return productQuantity;
}
/**
* @param productQuantity the productQuantity to set
*/
public void setProductQuantity(int productQuantity) {
this.productQuantity = productQuantity;
}
/**
* @return the productPrice
*/
public float getProductPrice() {
return productPrice;
}
/**
* @param productPrice the productPrice to set
*/
public void setProductPrice(float productPrice) {
this.productPrice = productPrice;
}
/**
* @return the shippingWeight
*/
public String getShippingWeight
() {
return shippingWeight;
}
/**
* @param shippingWeight the shippingWeight to set
*/
public void setShippingWeight
(String shippingWeight
) {
this.shippingWeight = shippingWeight;
}
/**
* @return the description
*/
public String getDescription
() {
return description;
}
/**
* @param description the description to set
*/
public void setDescription
(String description
) {
this.description = description;
}
/**
* @return the productManufactureDate
*/
public String getProductManufactureDate
() {
return productManufactureDate;
}
/**
* @param productManufactureDate the productManufactureDate to set
*/
public void setProductManufactureDate
(String productManufactureDate
) {
/*
s - a String object representing a date in in the format "yyyy-[m]m-[d]d".
The leading zero for mm and dd may also be omitted.
*/
this.productManufactureDate.valueOf(productManufactureDate);
}
/**
* @return the productExpiryDate
*/
public String getProductExpiryDate
() {
return productExpiryDate;
}
/**
* @param productExpiryDate the productExpiryDate to set
*/
public void setProductExpiryDate
(String productExpiryDate
) {
/*
s - a String object representing a date in in the format
"yyyy-[m]m-[d]d". The leading zero for mm and dd may also be omitted.
*/
this.productExpiryDate = productExpiryDate;
}
/**
* @return the productBatch
*/
public String getProductBatch
() {
return productBatch;
}
/**
* @param productBatch the productBatch to set
*/
public void setProductBatch
(String productBatch
) {
this.productBatch = productBatch;
}
/**
* Add New Product to inventory
*
* @param supplier_id supplier of this product
* @param employee_id employee that added product to inventory
* @return Boolean true is product is saved to db and false if not saved
* @throws SQLException
*/
boolean isSaved = false;
int lastInsertID = 0;
Statement addProductStatement
= this.
connectionClass.
getConnection().
createStatement();
Suppler s = new Suppler();
ArrayList<Suppler> supplierId = s.getSupplier(supplier_id, false);
if (!addProductStatement.execute("INSERT INTO products ("
+ "product_name,product_description,product_batch_no,"
+ "product_manufacture_date,product_expiry_date) "
+ "VALUES('" + getProductName() + "','" + getDescription() + "',"
+ "'" + getProductBatch() + "',"
+ "'" + getProductManufactureDate
() + "','" + getProductExpiryDate
() + "')",
Statement.
RETURN_GENERATED_KEYS)) {
ResultSet rs
= addProductStatement.
getGeneratedKeys();
if (rs.first()) {
lastInsertID = rs.getInt(1);
if (this.insertStockingData(addProductStatement, supplierId, employee_id).first()) {
int lastStockId = rs.getInt(1);
this.addStockDetailData(addProductStatement, lastStockId, lastInsertID);
}
}
isSaved = true;
}
return isSaved;
}
private void addStockDetailData
(Statement addProductStatement,
int lastStockId,
int lastInsertID
)
{
try {
addProductStatement.execute("INSERT INTO stock_details "
+ "(stocking_id,product_id,stock_quantity,cost_price,sell_price) "
+ "VALUES('" + lastStockId + "','" + lastInsertID + "','" + getProductQuantity() + "','" + getCostPrice() + "','" + getProductPrice() + "')");
Logger.getLogger(Product.class.getName()).log(Level.SEVERE, null, ex);
}
}
private ResultSet insertStockingData
(Statement addProductStatement, ArrayList
<Suppler
> supplierId,
int employee_id
) {
java.
util.
Date date
= new java.
util.
Date();
try {
addProductStatement.execute("INSERT INTO stocking (supplier_id,employee_id,date) "
+ "VALUES('" + supplierId.
get(0).
getSupplerId() + "','" + employee_id
+ "','" + dateFormat.
format(date
) + "')",
Statement.
RETURN_GENERATED_KEYS);
rs = addProductStatement.getGeneratedKeys();
e.printStackTrace();
}
return rs;
}
/**
* Delete product from the inventory
*
* @param productId
* @return Boolean true if product is deleted from inventory
* @throws SQLException
*/
boolean isSaved = false;
Statement addProductStatement
= this.
connectionClass.
getConnection().
createStatement();
if (addProductStatement.execute("DELETE FROM products WHERE product_id = '" + productId + "'")) {
isSaved = true;
}
return isSaved;
}
/**
* Update product information
*
* @param productId
* @return boolean true if product information is successfully updated
* @throws SQLException
*/
boolean isSaved = false;
Statement addProductStatement
= this.
connectionClass.
getConnection().
createStatement();
if (addProductStatement.execute("UPDATE products SET "
+ "product_name = '" + getProductName() + "',product_description = '" + getDescription() + "'"
+ ",product_batch_no = '" + getProductBatch() + "',"
+ "product_manufacture_date = '" + getProductManufactureDate().toString() + "',"
+ "product_expiry_date = '" + getProductExpiryDate().toString() + "'"
+ " WHERE product_id = '" + productId + "' "
)) {
isSaved = true;
}
return isSaved;
}
Product product = null;
Statement s
= this.
connectionClass.
getConnection().
createStatement();
ResultSet rs
= s.
executeQuery("SELECT * FROM products INNER JOIN stock_details ON products.product_id = stock_details.product_id WHERE product_name like '" + productName
+ "%'");
while (rs.next()) {
product = new Product(
rs.getInt("product_id"),
rs.getString("product_name"),
rs.getInt("stock_quantity"),
rs.getFloat("sell_price"),
"shipping_weight not set",
rs.getString("product_description"),
rs.getString("product_manufacture_date"),
rs.getString("product_expiry_date"),
rs.getString("product_batch_no"),
rs.getFloat("cost_price")
);
}
return product;
}
/**
* @return the costPrice
*/
public float getCostPrice() {
return costPrice;
}
/**
* @param costPrice the costPrice to set
*/
public void setCostPrice(float costPrice) {
this.costPrice = costPrice;
}
}
Uzoka