×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Israel Edet
Added: Dec 4, 2016 6:35 PM
Modified: Jan 4, 2017 10:01 PM
Views: 20
Tags: no tags
Product Class
  1. package inventorysystem.data;
  2.  
  3. import inventorysystem.gui.Supplier;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.sql.Date;
  7. import java.sql.ResultSet;
  8. import java.text.DateFormat;
  9. import java.text.SimpleDateFormat;
  10. import java.util.ArrayList;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13.  
  14. /**
  15.  *
  16.  * @author Epic
  17.  */
  18. public class Product {
  19.  
  20.     private int productId;
  21.     private String productName;
  22.     private int productQuantity;
  23.     private float productPrice;
  24.     private String shippingWeight;
  25.     private String description;
  26.     private String productManufactureDate;
  27.     private String productExpiryDate;
  28.     private String productBatch;
  29.     private float costPrice;
  30.     private DBConnectionClass connectionClass;
  31.  
  32.     public Product() {
  33.         this.connectionClass = new DBConnectionClass();
  34.     }
  35.  
  36.     public Product(int productID, String productName, int productQuantity,
  37.             float productPrice, String shippingWeight,
  38.             String description,
  39.             String productManufactureDate,
  40.             String productExpiryDate,
  41.             String productBatch, float costPrice) {
  42.         this.productId = productID;
  43.         this.productName = productName;
  44.         this.productQuantity = productQuantity;
  45.         this.productPrice = productPrice;
  46.         this.shippingWeight = shippingWeight;
  47.         this.description = description;
  48.         this.productManufactureDate = productManufactureDate;
  49.         this.productExpiryDate = productExpiryDate;
  50.         this.productBatch = productBatch;
  51.         this.costPrice = costPrice;
  52.         this.connectionClass = new DBConnectionClass();
  53.     }
  54.  
  55.     /**
  56.      * @return the productId
  57.      */
  58.     public int getProductId() {
  59.         return productId;
  60.     }
  61.  
  62.     /**
  63.      * @param productId the productId to set
  64.      */
  65.     public void setProductId(int productId) {
  66.         this.productId = productId;
  67.     }
  68.  
  69.     /**
  70.      * @return the productName
  71.      */
  72.     public String getProductName() {
  73.         return productName;
  74.     }
  75.  
  76.     /**
  77.      * @param productName the productName to set
  78.      */
  79.     public void setProductName(String productName) {
  80.         this.productName = productName;
  81.     }
  82.  
  83.     /**
  84.      * @return the productQuantity
  85.      */
  86.     public int getProductQuantity() {
  87.         return productQuantity;
  88.     }
  89.  
  90.     /**
  91.      * @param productQuantity the productQuantity to set
  92.      */
  93.     public void setProductQuantity(int productQuantity) {
  94.         this.productQuantity = productQuantity;
  95.     }
  96.  
  97.     /**
  98.      * @return the productPrice
  99.      */
  100.     public float getProductPrice() {
  101.         return productPrice;
  102.     }
  103.  
  104.     /**
  105.      * @param productPrice the productPrice to set
  106.      */
  107.     public void setProductPrice(float productPrice) {
  108.         this.productPrice = productPrice;
  109.     }
  110.  
  111.     /**
  112.      * @return the shippingWeight
  113.      */
  114.     public String getShippingWeight() {
  115.         return shippingWeight;
  116.     }
  117.  
  118.     /**
  119.      * @param shippingWeight the shippingWeight to set
  120.      */
  121.     public void setShippingWeight(String shippingWeight) {
  122.         this.shippingWeight = shippingWeight;
  123.     }
  124.  
  125.     /**
  126.      * @return the description
  127.      */
  128.     public String getDescription() {
  129.         return description;
  130.     }
  131.  
  132.     /**
  133.      * @param description the description to set
  134.      */
  135.     public void setDescription(String description) {
  136.         this.description = description;
  137.     }
  138.  
  139.     /**
  140.      * @return the productManufactureDate
  141.      */
  142.     public String getProductManufactureDate() {
  143.         return productManufactureDate;
  144.     }
  145.  
  146.     /**
  147.      * @param productManufactureDate the productManufactureDate to set
  148.      */
  149.     public void setProductManufactureDate(String productManufactureDate) {
  150.         /*
  151.         s - a String object representing a date in in the format "yyyy-[m]m-[d]d".
  152.         The leading zero for mm and dd may also be omitted.
  153.          */
  154.         this.productManufactureDate.valueOf(productManufactureDate);
  155.     }
  156.  
  157.     /**
  158.      * @return the productExpiryDate
  159.      */
  160.     public String getProductExpiryDate() {
  161.         return productExpiryDate;
  162.     }
  163.  
  164.     /**
  165.      * @param productExpiryDate the productExpiryDate to set
  166.      */
  167.     public void setProductExpiryDate(String productExpiryDate) {
  168.         /*
  169.             s - a String object representing a date in in the format
  170.             "yyyy-[m]m-[d]d". The leading zero for mm and dd may also be omitted.
  171.          */
  172.         this.productExpiryDate = productExpiryDate;
  173.     }
  174.  
  175.     /**
  176.      * @return the productBatch
  177.      */
  178.     public String getProductBatch() {
  179.         return productBatch;
  180.     }
  181.  
  182.     /**
  183.      * @param productBatch the productBatch to set
  184.      */
  185.     public void setProductBatch(String productBatch) {
  186.         this.productBatch = productBatch;
  187.     }
  188.  
  189.     /**
  190.      * Add New Product to inventory
  191.      *
  192.      * @param supplier_id supplier of this product
  193.      * @param employee_id employee that added product to inventory
  194.      * @return Boolean true is product is saved to db and false if not saved
  195.      * @throws SQLException
  196.      */
  197.     public Boolean addProduct(String supplier_id, int employee_id) throws SQLException {
  198.         boolean isSaved = false;
  199.         int lastInsertID = 0;
  200.  
  201.         Statement addProductStatement = this.connectionClass.getConnection().createStatement();
  202.         Suppler s = new Suppler();
  203.         ArrayList<Suppler> supplierId = s.getSupplier(supplier_id, false);
  204.  
  205.         if (!addProductStatement.execute("INSERT INTO products ("
  206.                 + "product_name,product_description,product_batch_no,"
  207.                 + "product_manufacture_date,product_expiry_date) "
  208.                 + "VALUES('" + getProductName() + "','" + getDescription() + "',"
  209.                 + "'" + getProductBatch() + "',"
  210.                 + "'" + getProductManufactureDate() + "','" + getProductExpiryDate() + "')", Statement.RETURN_GENERATED_KEYS)) {
  211.             ResultSet rs = addProductStatement.getGeneratedKeys();
  212.  
  213.             if (rs.first()) {
  214.                 lastInsertID = rs.getInt(1);
  215.                 if (this.insertStockingData(addProductStatement, supplierId, employee_id).first()) {
  216.                     int lastStockId = rs.getInt(1);
  217.                     this.addStockDetailData(addProductStatement, lastStockId, lastInsertID);
  218.                 }
  219.             }
  220.             isSaved = true;
  221.         }
  222.  
  223.         return isSaved;
  224.     }
  225.     private void addStockDetailData(Statement addProductStatement,int lastStockId,int lastInsertID)
  226.     {
  227.         try {
  228.             addProductStatement.execute("INSERT INTO stock_details "
  229.                     + "(stocking_id,product_id,stock_quantity,cost_price,sell_price) "
  230.                     + "VALUES('" + lastStockId + "','" + lastInsertID + "','" + getProductQuantity() + "','" + getCostPrice() + "','" + getProductPrice() + "')");
  231.         } catch (SQLException ex) {
  232.             Logger.getLogger(Product.class.getName()).log(Level.SEVERE, null, ex);
  233.         }
  234.     }
  235.     private ResultSet insertStockingData(Statement addProductStatement, ArrayList<Suppler> supplierId, int employee_id) {
  236.         DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
  237.         java.util.Date date = new java.util.Date();
  238.         ResultSet rs = null;
  239.         try {
  240.             addProductStatement.execute("INSERT INTO stocking (supplier_id,employee_id,date) "
  241.                     + "VALUES('" + supplierId.get(0).getSupplerId() + "','" + employee_id + "','" + dateFormat.format(date) + "')", Statement.RETURN_GENERATED_KEYS);
  242.             rs = addProductStatement.getGeneratedKeys();
  243.  
  244.         } catch (Exception e) {
  245.             e.printStackTrace();
  246.         }
  247.  
  248.         return rs;
  249.     }
  250.  
  251.     /**
  252.      * Delete product from the inventory
  253.      *
  254.      * @param productId
  255.      * @return Boolean true if product is deleted from inventory
  256.      * @throws SQLException
  257.      */
  258.     public Boolean removeProduct(int productId) throws SQLException {
  259.         boolean isSaved = false;
  260.         Statement addProductStatement = this.connectionClass.getConnection().createStatement();
  261.  
  262.         if (addProductStatement.execute("DELETE FROM products WHERE product_id = '" + productId + "'")) {
  263.             isSaved = true;
  264.         }
  265.  
  266.         return isSaved;
  267.     }
  268.  
  269.     /**
  270.      * Update product information
  271.      *
  272.      * @param productId
  273.      * @return boolean true if product information is successfully updated
  274.      * @throws SQLException
  275.      */
  276.     public Boolean updateProduct(int productId) throws SQLException {
  277.         boolean isSaved = false;
  278.         Statement addProductStatement = this.connectionClass.getConnection().createStatement();
  279.  
  280.         if (addProductStatement.execute("UPDATE products SET "
  281.                 + "product_name = '" + getProductName() + "',product_description = '" + getDescription() + "'"
  282.                 + ",product_batch_no = '" + getProductBatch() + "',"
  283.                 + "product_manufacture_date = '" + getProductManufactureDate().toString() + "',"
  284.                 + "product_expiry_date = '" + getProductExpiryDate().toString() + "'"
  285.                 + " WHERE product_id = '" + productId + "' "
  286.         )) {
  287.             isSaved = true;
  288.         }
  289.  
  290.         return isSaved;
  291.     }
  292.  
  293.     public Product getProduct(String productName) throws SQLException {
  294.         Product product = null;
  295.         Statement s = this.connectionClass.getConnection().createStatement();
  296.  
  297.         ResultSet rs = s.executeQuery("SELECT * FROM products  INNER JOIN stock_details ON products.product_id = stock_details.product_id WHERE product_name like '" + productName + "%'");
  298.         while (rs.next()) {
  299.             product = new Product(
  300.                     rs.getInt("product_id"),
  301.                     rs.getString("product_name"),
  302.                     rs.getInt("stock_quantity"),
  303.                     rs.getFloat("sell_price"),
  304.                     "shipping_weight not set",
  305.                     rs.getString("product_description"),
  306.                     rs.getString("product_manufacture_date"),
  307.                     rs.getString("product_expiry_date"),
  308.                     rs.getString("product_batch_no"),
  309.                     rs.getFloat("cost_price")
  310.             );
  311.         }
  312.         return product;
  313.     }
  314.  
  315.     /**
  316.      * @return the costPrice
  317.      */
  318.     public float getCostPrice() {
  319.         return costPrice;
  320.     }
  321.  
  322.     /**
  323.      * @param costPrice the costPrice to set
  324.      */
  325.     public void setCostPrice(float costPrice) {
  326.         this.costPrice = costPrice;
  327.     }
  328. }
  329.  

1 comment

Michael Uzoka 9 months ago
The structure looks good to me so far. Is everybody contributing to the code? I need each person that contributes to write comments on what they have done.
Uzoka

Write a comment