From e97c2e772ebd7df67d1007f2f0b779ab86c2e63e Mon Sep 17 00:00:00 2001 From: manav0299 <61261978+manav0299@users.noreply.github.com> Date: Fri, 20 Mar 2020 22:45:14 +0530 Subject: [PATCH] Add files via upload --- manav(1710991453)/Administrator.java | 65 ++++++++++++ manav(1710991453)/DBUtil.java | 20 ++++ manav(1710991453)/Sales.java | 40 +++++++ manav(1710991453)/SalesApplication.java | 79 ++++++++++++++ manav(1710991453)/SalesReport.java | 61 +++++++++++ manav(1710991453)/Stock.java | 40 +++++++ manav(1710991453)/StockDao.java | 132 ++++++++++++++++++++++++ manav(1710991453)/database.txt | 41 ++++++++ 8 files changed, 478 insertions(+) create mode 100644 manav(1710991453)/Administrator.java create mode 100644 manav(1710991453)/DBUtil.java create mode 100644 manav(1710991453)/Sales.java create mode 100644 manav(1710991453)/SalesApplication.java create mode 100644 manav(1710991453)/SalesReport.java create mode 100644 manav(1710991453)/Stock.java create mode 100644 manav(1710991453)/StockDao.java create mode 100644 manav(1710991453)/database.txt diff --git a/manav(1710991453)/Administrator.java b/manav(1710991453)/Administrator.java new file mode 100644 index 0000000..5c638ea --- /dev/null +++ b/manav(1710991453)/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 "; + } else { + return "Data not Valid "; + } + } + + + 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"; + + if (stockDao.getStock(salesobj.getProductID()).getQuantityOnHand() < salesobj.getQuantitySold()) + return "Not enough stock "; + + 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 " record successfully"; + else + return "Error"; + } else { + return "Error"; + } + } + + + public ArrayList getSalesReport(){ + return salesDao.getSalesReport(); + } +} diff --git a/manav(1710991453)/DBUtil.java b/manav(1710991453)/DBUtil.java new file mode 100644 index 0000000..464b5b3 --- /dev/null +++ b/manav(1710991453)/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; + } + } +} diff --git a/manav(1710991453)/Sales.java b/manav(1710991453)/Sales.java new file mode 100644 index 0000000..9a8f050 --- /dev/null +++ b/manav(1710991453)/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/manav(1710991453)/SalesApplication.java b/manav(1710991453)/SalesApplication.java new file mode 100644 index 0000000..13f1e6f --- /dev/null +++ b/manav(1710991453)/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/manav(1710991453)/SalesReport.java b/manav(1710991453)/SalesReport.java new file mode 100644 index 0000000..440703d --- /dev/null +++ b/manav(1710991453)/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/manav(1710991453)/Stock.java b/manav(1710991453)/Stock.java new file mode 100644 index 0000000..464d63d --- /dev/null +++ b/manav(1710991453)/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/manav(1710991453)/StockDao.java b/manav(1710991453)/StockDao.java new file mode 100644 index 0000000..3d68c4b --- /dev/null +++ b/manav(1710991453)/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/manav(1710991453)/database.txt b/manav(1710991453)/database.txt new file mode 100644 index 0000000..8136378 --- /dev/null +++ b/manav(1710991453)/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