From e130d101acac7e71e64c744cf68f8656e900cf52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=97=E3=81=AE=E3=81=B3?= <44221117+AayushBhatt98@users.noreply.github.com> Date: Fri, 20 Mar 2020 20:12:50 +0530 Subject: [PATCH] Add files via upload --- database.txt | 41 ++++++++++ sales/bean/Sales.java | 40 ++++++++++ sales/bean/SalesReport.java | 61 ++++++++++++++ sales/bean/Stock.java | 40 ++++++++++ sales/dao/SalesDao.java | 96 ++++++++++++++++++++++ sales/dao/StockDao.java | 132 +++++++++++++++++++++++++++++++ sales/main/SalesApplication.java | 79 ++++++++++++++++++ sales/service/Administrator.java | 65 +++++++++++++++ sales/util/DBUtil.java | 20 +++++ 9 files changed, 574 insertions(+) create mode 100644 database.txt create mode 100644 sales/bean/Sales.java create mode 100644 sales/bean/SalesReport.java create mode 100644 sales/bean/Stock.java create mode 100644 sales/dao/SalesDao.java create mode 100644 sales/dao/StockDao.java create mode 100644 sales/main/SalesApplication.java create mode 100644 sales/service/Administrator.java create mode 100644 sales/util/DBUtil.java diff --git a/database.txt b/database.txt new file mode 100644 index 0000000..8136378 --- /dev/null +++ b/database.txt @@ -0,0 +1,41 @@ +CREATE TABLE TBL_STOCK ( + Product_ID Varchar2(6), + Product_Name Varchar2(20), + Quantity_On_Hand Number(11), + Product_Unit_Price Number(11,2), + Reorder_Level Number(11), + CONSTRAINT PK00 PRIMARY KEY(Product_ID), + CONSTRAINT UQ01 UNIQUE(Product_Name), + CONSTRAINT CH02 CHECK(Quantity_On_Hand >= 0), + CONSTRAINT CH03 CHECK(Product_Unit_Price >= 0), + CONSTRAINT CH04 CHECK(Reorder_Level >= 0) +); + +CREATE TABLE TBL_SALES ( + Sales_ID Varchar2(6), + Sales_Date Date, + Product_ID Varchar2(6), + Quantity_Sold Number(11), + Sales_Price_Per_Unit Number(11,2), + CONSTRAINT PK10 PRIMARY KEY(Sales_ID), + CONSTRAINT FK11 FOREIGN KEY(Product_ID) REFERENCES TBL_STOCK(Product_ID), + CONSTRAINT CH12 CHECK(Quantity_Sold >= 0), + CONSTRAINT CH13 CHECK(Sales_Price_Per_Unit >= 0) +); + +INSERT INTO TBL_STOCK VALUES('RE1001', 'REDMI Note 3', 20, 12000, 5); +INSERT INTO TBL_STOCK VALUES('ip1002', 'Iphone 5S', 10, 21000, 2); +INSERT INTO TBL_STOCK VALUES('PA1003', 'Panasonic P55', 50, 5500, 5); + +DROP SEQUENCE SEQ_SALES_ID; +DROP SEQUENCE SEQ_PRODUCT_ID; +CREATE SEQUENCE SEQ_SALES_ID START WITH 1000 INCREMENT BY 1; +CREATE SEQUENCE SEQ_PRODUCT_ID START WITH 1004 INCREMENT BY 1; + +DROP VIEW V_SALES_REPORT; +CREATE VIEW V_SALES_REPORT AS + SELECT Sales_ID, Sales_Date, Product_ID, Product_Name, + Quantity_Sold, Product_Unit_Price, Sales_Price_Per_Unit, + (Sales_Price_Per_Unit - Product_Unit_Price) Profit_Amount + FROM TBL_STOCK NATURAL JOIN TBL_SALES + ORDER BY Profit_Amount DESC, Sales_ID ASC; \ No newline at end of file diff --git a/sales/bean/Sales.java b/sales/bean/Sales.java new file mode 100644 index 0000000..9a8f050 --- /dev/null +++ b/sales/bean/Sales.java @@ -0,0 +1,40 @@ +package com.wipro.sales.bean; + +public class Sales { + String salesID; + java.util.Date salesDate; + String productID; + int quantitySold; + double salesPricePerUnit; + public String getSalesID() { + return salesID; + } + public void setSalesID(String salesID) { + this.salesID = salesID; + } + public java.util.Date getSalesDate() { + return salesDate; + } + public void setSalesDate(java.util.Date salesDate) { + this.salesDate = salesDate; + } + public String getProductID() { + return productID; + } + public void setProductID(String productID) { + this.productID = productID; + } + public int getQuantitySold() { + return quantitySold; + } + public void setQuantitySold(int quantitySold) { + this.quantitySold = quantitySold; + } + public double getSalesPricePerUnit() { + return salesPricePerUnit; + } + public void setSalesPricePerUnit(double salesPricePerUnit) { + this.salesPricePerUnit = salesPricePerUnit; + } + +} diff --git a/sales/bean/SalesReport.java b/sales/bean/SalesReport.java new file mode 100644 index 0000000..440703d --- /dev/null +++ b/sales/bean/SalesReport.java @@ -0,0 +1,61 @@ +package com.wipro.sales.bean; + +public class SalesReport { + String salesID; + java.util.Date salesDate; + String productID; + String productName; + int quantitySold; + double productUnitPrice; + double salesPricePerUnit; + double profitAmount; + public String getSalesID() { + return salesID; + } + public void setSalesID(String salesID) { + this.salesID = salesID; + } + public java.util.Date getSalesDate() { + return salesDate; + } + public void setSalesDate(java.util.Date salesDate) { + this.salesDate = salesDate; + } + public String getProductID() { + return productID; + } + public void setProductID(String productID) { + this.productID = productID; + } + public String getProductName() { + return productName; + } + public void setProductName(String productName) { + this.productName = productName; + } + public int getQuantitySold() { + return quantitySold; + } + public void setQuantitySold(int quantitySold) { + this.quantitySold = quantitySold; + } + public double getProductUnitPrice() { + return productUnitPrice; + } + public void setProductUnitPrice(double productUnitPrice) { + this.productUnitPrice = productUnitPrice; + } + public double getSalesPricePerUnit() { + return salesPricePerUnit; + } + public void setSalesPricePerUnit(double salesPricePerUnit) { + this.salesPricePerUnit = salesPricePerUnit; + } + public double getProfitAmount() { + return profitAmount; + } + public void setProfitAmount(double profitAmount) { + this.profitAmount = profitAmount; + } + +} diff --git a/sales/bean/Stock.java b/sales/bean/Stock.java new file mode 100644 index 0000000..464d63d --- /dev/null +++ b/sales/bean/Stock.java @@ -0,0 +1,40 @@ +package com.wipro.sales.bean; + +public class Stock { + String productID; + String productName; + int quantityOnHand; + double productUnitPrice; + int reorderLevel; + public String getProductID() { + return productID; + } + public void setProductID(String productID) { + this.productID = productID; + } + public String getProductName() { + return productName; + } + public void setProductName(String productName) { + this.productName = productName; + } + public int getQuantityOnHand() { + return quantityOnHand; + } + public void setQuantityOnHand(int quantityOnHand) { + this.quantityOnHand = quantityOnHand; + } + public double getProductUnitPrice() { + return productUnitPrice; + } + public void setProductUnitPrice(double productUnitPrice) { + this.productUnitPrice = productUnitPrice; + } + public int getReorderLevel() { + return reorderLevel; + } + public void setReorderLevel(int reorderLevel) { + this.reorderLevel = reorderLevel; + } + +} diff --git a/sales/dao/SalesDao.java b/sales/dao/SalesDao.java new file mode 100644 index 0000000..1d24d3c --- /dev/null +++ b/sales/dao/SalesDao.java @@ -0,0 +1,96 @@ +package com.wipro.sales.dao; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.*; + +import com.wipro.sales.bean.*; +import com.wipro.sales.bean.SalesReport; +import com.wipro.sales.util.DBUtil; + +public class SalesDao { + + + public int insertSales(Sales salesobj) { + Connection conn = null; + PreparedStatement pstmt = null; + String sql = "INSERT INTO TBL_SALES VALUES(?, ?, ?, ?, ?)"; + java.sql.Date sqlDate = new java.sql.Date(salesobj.getSalesDate().getTime()); + + try { + conn = DBUtil.getDBConnection(); + pstmt = conn.prepareStatement(sql); + pstmt.setString(1, salesobj.getSalesID()); + pstmt.setDate(2, sqlDate); + pstmt.setString(3, salesobj.getProductID()); + pstmt.setInt(4, salesobj.getQuantitySold()); + pstmt.setDouble(5, salesobj.getSalesPricePerUnit()); + + if (pstmt.executeUpdate() == 1) return 1; + else return 0; + } catch (SQLException e) { + e.printStackTrace(); + return 0; + } + } + + + public String generateSalesID(Date salesDate) { + Connection conn = null; + PreparedStatement pstmt = null; + String sql = "SELECT SEQ_SALES_ID.NEXTVAL FROM DUAL"; + + int SEQ_SALES_ID = 0; + String out = salesDate.toString().substring(salesDate.toString().length()-2, salesDate.toString().length()); + + try { + conn = DBUtil.getDBConnection(); + pstmt = conn.prepareStatement(sql); + ResultSet rs = pstmt.executeQuery(); + + rs.next(); + SEQ_SALES_ID = rs.getInt(1); + + out += SEQ_SALES_ID; + return out; + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + } + + + + public ArrayList getSalesReport(){ + Connection conn = null; + PreparedStatement pstmt = null; + String sql = "SELECT * FROM V_SALES_REPORT"; + + ArrayList list = new ArrayList(); + + try { + conn = DBUtil.getDBConnection(); + pstmt = conn.prepareStatement(sql); + ResultSet rs = pstmt.executeQuery(); + + while (rs.next()) { + SalesReport salesReport = new SalesReport(); + salesReport.setSalesID(rs.getString(1)); + salesReport.setSalesDate(rs.getDate(2)); + salesReport.setProductID(rs.getString(3)); + salesReport.setProductName(rs.getString(4)); + salesReport.setQuantitySold(rs.getInt(5)); + salesReport.setProductUnitPrice(rs.getDouble(6)); + salesReport.setSalesPricePerUnit(rs.getDouble(7)); + salesReport.setProfitAmount(rs.getDouble(8)); + list.add(salesReport); + } + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + + return list; + } +} diff --git a/sales/dao/StockDao.java b/sales/dao/StockDao.java new file mode 100644 index 0000000..3d68c4b --- /dev/null +++ b/sales/dao/StockDao.java @@ -0,0 +1,132 @@ +package com.wipro.sales.dao; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import com.wipro.sales.bean.Stock; +import com.wipro.sales.util.DBUtil; + +public class StockDao { + + public int insertStock(Stock stock) { + Connection conn = null; + PreparedStatement pstmt = null; + String sql = "INSERT INTO TBL_STOCK VALUES(?, ?, ?, ?, ?)"; + + try { + conn = DBUtil.getDBConnection(); + pstmt = conn.prepareStatement(sql); + pstmt.setString(1, stock.getProductID()); + pstmt.setString(2, stock.getProductName()); + pstmt.setInt(3, stock.getQuantityOnHand()); + pstmt.setDouble(4, stock.getProductUnitPrice()); + pstmt.setInt(5, stock.getReorderLevel()); + + if (pstmt.executeUpdate() == 1) return 1; + else return 0; + } catch (SQLException e) { + e.printStackTrace(); + return 0; + } + } + + + public String generateProductID(String productName) { + Connection conn = null; + PreparedStatement pstmt = null; + String sql = "SELECT SEQ_PRODUCT_ID.NEXTVAL FROM DUAL"; + + int SEQ_PRODUCT_ID = 0; + String out = ""; + + try { + conn = DBUtil.getDBConnection(); + pstmt = conn.prepareStatement(sql); + ResultSet rs = pstmt.executeQuery(); + + rs.next(); + SEQ_PRODUCT_ID = rs.getInt(1); + + out += productName.substring(0, 2); + out += SEQ_PRODUCT_ID; + + return out; + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + } + + + public int updateStock(String productID,int soldQty) { + Connection conn = null; + PreparedStatement pstmt = null; + String sql = "UPDATE TBL_STOCK SET Quantity_On_Hand = Quantity_On_Hand - ?" + + "WHERE Product_ID = ?"; + + try { + conn = DBUtil.getDBConnection(); + pstmt = conn.prepareStatement(sql); + pstmt.setInt(1, soldQty); + pstmt.setString(2, productID); + + if (pstmt.executeUpdate() == 1) return 1; + else return 0; + } catch (SQLException e) { + e.printStackTrace(); + return 0; + } + } + + + public Stock getStock(String productID) { + Connection conn = null; + PreparedStatement pstmt = null; + String sql = "SELECT * FROM TBL_STOCK WHERE Product_ID = ?"; + + try { + conn = DBUtil.getDBConnection(); + pstmt = conn.prepareStatement(sql); + pstmt.setString(1, productID); + + ResultSet rs = pstmt.executeQuery(); + + rs.next(); + Stock stock = new Stock(); + stock.setProductID(rs.getString(1)); + stock.setProductName(rs.getString(2)); + stock.setQuantityOnHand(rs.getInt(3)); + stock.setProductUnitPrice(rs.getDouble(4)); + stock.setReorderLevel(rs.getInt(5)); + + return stock; + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + } + + + public int deleteStock(String productID) { + Connection conn = null; + PreparedStatement pstmt = null; + String sql = "DELETE TBL_STOCK WHERE Product_ID = ?"; + + try { + conn = DBUtil.getDBConnection(); + pstmt = conn.prepareStatement(sql); + pstmt.setString(1, productID); + + if (pstmt.executeUpdate() == 1) + return 1; + else + return 0; + } catch (SQLException e) { + e.printStackTrace(); + return 0; + } + } + +} diff --git a/sales/main/SalesApplication.java b/sales/main/SalesApplication.java new file mode 100644 index 0000000..13f1e6f --- /dev/null +++ b/sales/main/SalesApplication.java @@ -0,0 +1,79 @@ +package com.wipro.sales.main; + +import java.util.Scanner; +import com.wipro.sales.bean.*; +import com.wipro.sales.service.*; +import java.sql.*; +import java.text.ParseException; +import java.text.SimpleDateFormat; +public class SalesApplication { + + public static void main(String[] args) throws ParseException { + Scanner sc = new Scanner(System.in); + + Administrator admin = new Administrator(); + + int choice = 0; + + do { + System.out.println("1. Insert Stock"); + System.out.println("2. Delete Stock"); + System.out.println("3. Insert Sales"); + System.out.println("4. View Sales Report"); + System.out.print("Enter your Choice: "); + choice = sc.nextInt(); + + switch (choice) { + case 1: + Stock stock = new Stock(); + System.out.print("Enter product ID: "); + stock.setProductID(sc.nextLine()); + System.out.print("Enter product name: "); + stock.setProductName(sc.nextLine()); + System.out.print("Enter quantity on hand: "); + stock.setQuantityOnHand(sc.nextInt()); + sc.nextLine(); + System.out.print("Enter product unit price: "); + stock.setProductUnitPrice(sc.nextDouble()); + System.out.print("Enter product reorder level: "); + stock.setReorderLevel(sc.nextInt()); + sc.nextLine(); + admin.insertStock(stock); + break; + case 2: + System.out.print("Enter product id to be deleted: "); + String removeId = sc.nextLine(); + removeId = admin.deleteStock(removeId); + if (removeId != null) System.out.println(removeId + " removed successfully"); + break; + case 3: + Sales sales = new Sales(); + System.out.print("Enter sales id: "); + sales.setSalesID(sc.nextLine()); + System.out.print("Enter date (dd-mm-yyyy): "); + String sDate = sc.nextLine(); + Date date = (Date) new SimpleDateFormat("dd-mm-yyyy").parse(sDate); + sales.setSalesDate(date); + System.out.print("Enter product id: "); + sales.setProductID(sc.nextLine()); + System.out.print("Enter quantity sold: "); + sales.setQuantitySold(sc.nextInt()); + sc.nextLine(); + System.out.print("Enter sales price per unit: "); + sales.setSalesPricePerUnit(sc.nextDouble()); + admin.insertSales(sales); + break; + case 4: + admin.getSalesReport(); + break; + default: + System.out.println("Exiting..."); + choice = 0; + break; + } + } while (choice >= 1 && choice <= 4); + + sc.close(); + } + +} diff --git a/sales/service/Administrator.java b/sales/service/Administrator.java new file mode 100644 index 0000000..6067754 --- /dev/null +++ b/sales/service/Administrator.java @@ -0,0 +1,65 @@ +package com.wipro.sales.service; + +import java.sql.*; +import java.util.ArrayList; +import com.wipro.sales.dao.*; +import com.wipro.sales.bean.*; + +public class Administrator { + + private static StockDao stockDao = new StockDao(); + private static SalesDao salesDao = new SalesDao(); + + public synchronized String insertStock(Stock stockobj) { + if (stockobj != null && stockobj.getProductName().length() >= 2) { + String productID = stockDao.generateProductID(stockobj.getProductName()); + stockobj.setProductID(productID); + if (stockDao.insertStock(stockobj) == 1) + return productID; + else + return "Data not Valid for insertion"; + } else { + return "Data not Valid for insertion"; + } + } + + + public String deleteStock(String productID) { + if (stockDao.deleteStock(productID) == 1) + return "deleted"; + else + return "record cannot be deleted"; + } + + + public String insertSales(Sales salesobj) { + if (salesobj == null) + return "Object not valid for insertion"; + + if (stockDao.getStock(salesobj.getProductID()) == null) + return "Unknown Product for sales"; + + if (stockDao.getStock(salesobj.getProductID()).getQuantityOnHand() < salesobj.getQuantitySold()) + return "Not enough stock on hand for sales"; + + if (salesobj.getSalesDate().before(new Date(0))) + return "Invalid date"; + + String salesID = salesDao.generateSalesID(salesobj.getSalesDate()); + salesobj.setSalesID(salesID); + + if (salesDao.insertSales(salesobj) == 1) { + if (stockDao.updateStock(salesobj.getProductID(), salesobj.getQuantitySold()) == 1) + return "sales record inserted successfully"; + else + return "Error"; + } else { + return "Error"; + } + } + + + public ArrayList getSalesReport(){ + return salesDao.getSalesReport(); + } +} diff --git a/sales/util/DBUtil.java b/sales/util/DBUtil.java new file mode 100644 index 0000000..464b5b3 --- /dev/null +++ b/sales/util/DBUtil.java @@ -0,0 +1,20 @@ +package com.wipro.sales.util; + +import java.sql.*; + +public class DBUtil { + static String db_url = "jdbc:oracle:thin:@localhost:1521:xe"; + static Connection con = null; + static String user = "username"; + static String pass = "password"; + public static Connection getDBConnection() { + try { + con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521", "scott", "tiger"); + return con; + } catch (SQLException e) { + System.out.println("Connection could not be estanlished"); + e.printStackTrace(); + return null; + } + } +}