diff --git a/JDBC-Assignment/com/wipro/sales/bean/Sales.java b/JDBC-Assignment/com/wipro/sales/bean/Sales.java new file mode 100644 index 0000000..0dae5a1 --- /dev/null +++ b/JDBC-Assignment/com/wipro/sales/bean/Sales.java @@ -0,0 +1,52 @@ +package com.wipro.sales.bean; + +import java.util.Date; + +public class Sales { + private String salesID; + private Date salesDate; + private String productID; + private int quantitySold; + private double salesPricePerUnit; + + + public String getSalesID() { + return salesID; + } + + public void setSalesID(String salesID) { + this.salesID = salesID; + } + + public Date getSalesDate() { + return salesDate; + } + + public void setSalesDate(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/JDBC-Assignment/com/wipro/sales/bean/SalesReport.java b/JDBC-Assignment/com/wipro/sales/bean/SalesReport.java new file mode 100644 index 0000000..6cf5508 --- /dev/null +++ b/JDBC-Assignment/com/wipro/sales/bean/SalesReport.java @@ -0,0 +1,79 @@ +package com.wipro.sales.bean; + +import java.util.Date; + +public class SalesReport { + + private String salesID; + private Date salesDate; + private String productID; + private String productName; + private int quantitySold; + private double productUnitPrice; + private double salesPricePerUnit; + private double profitAmount; + + public String getSalesID() { + return salesID; + } + + public void setSalesID(String salesID) { + this.salesID = salesID; + } + + public Date getSalesDate() { + return salesDate; + } + + public void setSalesDate(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/JDBC-Assignment/com/wipro/sales/bean/Stock.java b/JDBC-Assignment/com/wipro/sales/bean/Stock.java new file mode 100644 index 0000000..e92fbbc --- /dev/null +++ b/JDBC-Assignment/com/wipro/sales/bean/Stock.java @@ -0,0 +1,53 @@ +package com.wipro.sales.bean; + +//As there is no stock class defined in bean as per the pdf, +// I assume stock is same as Product and thus going with Stock class +public class Stock { + + private String productID; + private String productName; + private int quantityOnHand; + private double productUnitPrice; + private 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/JDBC-Assignment/com/wipro/sales/dao/SalesDao.java b/JDBC-Assignment/com/wipro/sales/dao/SalesDao.java new file mode 100644 index 0000000..8cc8903 --- /dev/null +++ b/JDBC-Assignment/com/wipro/sales/dao/SalesDao.java @@ -0,0 +1,119 @@ +package com.wipro.sales.dao; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; + + +import com.wipro.sales.bean.Sales; +import com.wipro.sales.bean.SalesReport; +import com.wipro.sales.util.DBUtil; + +public class SalesDao { + + public int insertSales(Sales sales) { + Connection connection; + PreparedStatement preparedStatement; + String sql = "INSERT INTO TBL_SALES VALUES(?, ?, ?, ?, ?)"; + Date date = Calendar.getInstance().getTime(); + DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy"); + String strDate = dateFormat.format(date); + int salesInserted = 0; + try { + connection = DBUtil.getDBConnection(); + validateConnection(connection); + preparedStatement = connection.prepareStatement(sql); + validatePreparedStatement(preparedStatement); + preparedStatement.setString(1, sales.getSalesID()); + preparedStatement.setString(2, strDate); + preparedStatement.setString(3, sales.getProductID()); + preparedStatement.setInt(4, sales.getQuantitySold()); + preparedStatement.setDouble(5, sales.getSalesPricePerUnit()); + + if (preparedStatement.executeUpdate() == 1) { + salesInserted = 1; + } + } catch (SQLException e) { + e.printStackTrace(); + } + return salesInserted; + } + + public String generateSalesID(Date salesDate) { + Connection connection; + PreparedStatement preparedStatement; + String sql = "SELECT SEQ_SALES_ID.NEXTVAL FROM DUAL"; + + int SEQ_SALES_ID; + final String stringDate = salesDate.toString(); + String lastTwoDigit = stringDate.substring(stringDate.length() - 2); + String output; + + try { + connection = DBUtil.getDBConnection(); + validateConnection(connection); + preparedStatement = connection.prepareStatement(sql); + validatePreparedStatement(preparedStatement); + ResultSet resultSet = preparedStatement.executeQuery(); + resultSet.next(); + SEQ_SALES_ID = resultSet.getInt(1); + output = lastTwoDigit + SEQ_SALES_ID; + return output; + + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + } + + public ArrayList getSalesReport() { + Connection connection; + PreparedStatement preparedStatement; + String sql = "SELECT * FROM V_SALES_REPORT"; + + ArrayList list = new ArrayList(); + + try { + connection = DBUtil.getDBConnection(); + validateConnection(connection); + preparedStatement = connection.prepareStatement(sql); + validatePreparedStatement(preparedStatement); + ResultSet resultSet = preparedStatement.executeQuery(); + + while (resultSet.next()) { + SalesReport salesReport = new SalesReport(); + salesReport.setSalesID(resultSet.getString(1)); + salesReport.setSalesDate(resultSet.getDate(2)); + salesReport.setProductID(resultSet.getString(3)); + salesReport.setProductName(resultSet.getString(4)); + salesReport.setQuantitySold(resultSet.getInt(5)); + salesReport.setProductUnitPrice(resultSet.getDouble(6)); + salesReport.setSalesPricePerUnit(resultSet.getDouble(7)); + salesReport.setProfitAmount(resultSet.getDouble(8)); + list.add(salesReport); + } + } catch (SQLException e) { + e.printStackTrace(); + } + + return list; + } + + public void validateConnection(Connection connection) { + if (connection == null) { + throw new RuntimeException("Connection object is null"); + } + } + + public void validatePreparedStatement(PreparedStatement preparedStatement) { + if (preparedStatement == null) { + throw new RuntimeException("Prepared statement object is null"); + } + } +} diff --git a/JDBC-Assignment/com/wipro/sales/dao/StockDao.java b/JDBC-Assignment/com/wipro/sales/dao/StockDao.java new file mode 100644 index 0000000..a03dba0 --- /dev/null +++ b/JDBC-Assignment/com/wipro/sales/dao/StockDao.java @@ -0,0 +1,156 @@ +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 connection; + PreparedStatement preparedStatement; + String sql = "INSERT INTO TBL_STOCK VALUES(?, ?, ?, ?, ?)"; + int stocksInserted = 0; + + try { + connection = DBUtil.getDBConnection(); + validateConnection(connection); + preparedStatement = connection.prepareStatement(sql); + validatePreparedStatement(preparedStatement); + preparedStatement.setString(1, stock.getProductID()); + preparedStatement.setString(2, stock.getProductName()); + preparedStatement.setInt(3, stock.getQuantityOnHand()); + preparedStatement.setDouble(4, stock.getProductUnitPrice()); + preparedStatement.setInt(5, stock.getReorderLevel()); + + if (preparedStatement.executeUpdate() == 1) { + stocksInserted = 1; + } + } catch (SQLException e) { + e.printStackTrace(); + } + return stocksInserted; + } + + + public String generateProductID(String productName) { + Connection connection; + PreparedStatement preparedStatement; + String sql = "SELECT SEQ_PRODUCT_ID.NEXTVAL FROM DUAL"; + + int SEQ_PRODUCT_ID; + String output = ""; + + try { + connection = DBUtil.getDBConnection(); + validateConnection(connection); + preparedStatement = connection.prepareStatement(sql); + validatePreparedStatement(preparedStatement); + ResultSet resultSet = preparedStatement.executeQuery(); + + resultSet.next(); + SEQ_PRODUCT_ID = resultSet.getInt(1); + + output += productName.substring(0, 2); + output += SEQ_PRODUCT_ID; + + return output; + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + } + + + public int updateStock(String productID, int soldQty) { + Connection connection; + PreparedStatement preparedStatement; + String sql = "UPDATE TBL_STOCK SET Quantity_On_Hand = Quantity_On_Hand - ? WHERE Product_ID = ?"; + int stocksUpdated = 0; + + try { + connection = DBUtil.getDBConnection(); + validateConnection(connection); + preparedStatement = connection.prepareStatement(sql); + validatePreparedStatement(preparedStatement); + preparedStatement.setInt(1, soldQty); + preparedStatement.setString(2, productID); + + if (preparedStatement.executeUpdate() == 1) { + stocksUpdated = 1; + } + } catch (SQLException e) { + e.printStackTrace(); + } + return stocksUpdated; + } + + public Stock getStock(String productID) { + Connection connection; + PreparedStatement preparedStatement; + String sql = "SELECT * FROM TBL_STOCK WHERE Product_ID = ?"; + + try { + connection = DBUtil.getDBConnection(); + validateConnection(connection); + preparedStatement = connection.prepareStatement(sql); + validatePreparedStatement(preparedStatement); + preparedStatement.setString(1, productID); + + ResultSet resultSet = preparedStatement.executeQuery(); + + resultSet.next(); + Stock stock = new Stock(); + stock.setProductID(resultSet.getString(1)); + stock.setProductName(resultSet.getString(2)); + stock.setQuantityOnHand(resultSet.getInt(3)); + stock.setProductUnitPrice(resultSet.getDouble(4)); + stock.setReorderLevel(resultSet.getInt(5)); + + return stock; + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + } + + + public int deleteStock(String productID) { + Connection connection; + PreparedStatement preparedStatement; + String sql = "DELETE TBL_STOCK WHERE Product_ID = ?"; + int stockDeleted = 0; + + try { + connection = DBUtil.getDBConnection(); + validateConnection(connection); + preparedStatement = connection.prepareStatement(sql); + validatePreparedStatement(preparedStatement); + preparedStatement.setString(1, productID); + + if (preparedStatement.executeUpdate() == 1) { + stockDeleted = 1; + } + } catch (SQLException e) { + e.printStackTrace(); + } + return stockDeleted; + } + + public void validateConnection(Connection connection) { + if (connection == null) { + throw new RuntimeException("Connection object is null"); + } + } + + public void validatePreparedStatement(PreparedStatement preparedStatement) { + if (preparedStatement == null) { + throw new RuntimeException("Prepared statement object is null"); + } + } + +} diff --git a/JDBC-Assignment/com/wipro/sales/main/SalesApplication.java b/JDBC-Assignment/com/wipro/sales/main/SalesApplication.java new file mode 100644 index 0000000..2c56745 --- /dev/null +++ b/JDBC-Assignment/com/wipro/sales/main/SalesApplication.java @@ -0,0 +1,102 @@ +package com.wipro.sales.main; + +import com.wipro.sales.bean.Stock; +import com.wipro.sales.bean.Sales; +import com.wipro.sales.service.Administrator; + +import java.util.Date; +import java.util.Scanner; + +import java.text.ParseException; +import java.text.SimpleDateFormat; + +public class SalesApplication { + + public static void main(String[] args) throws ParseException { + Administrator administrator = new Administrator(); + Scanner scanner = new Scanner(System.in); + displayMenu(); + int choice = selectChoice(scanner); + + switch (choice) { + case 1: + insertStock(administrator, scanner); + break; + case 2: + deleteProduct(administrator, scanner); + break; + case 3: + insertSales(administrator, scanner); + break; + case 4: + administrator.getSalesReport(); + break; + default: + System.out.println("Not a valid option!!"); + break; + } + scanner.close(); + } + + public static void displayMenu() { + System.out.println("Enter your Choice. Press "); + System.out.println("1 for Insert Stock"); + System.out.println("2 for Delete Stock"); + System.out.println("3 for Insert Sales"); + System.out.println("4 for View Sales Report"); + } + + public static int selectChoice(Scanner scanner) { + return scanner.nextInt(); + } + + public static void insertStock(Administrator administrator, Scanner scanner) { + Stock stock = new Stock(); + + System.out.println("Enter product ID: "); + String productID = scanner.next(); + stock.setProductID(productID); + + System.out.println("Enter product name: "); + String productName = scanner.next(); + stock.setProductName(productName); + + System.out.println("Enter quantity on hand: "); + stock.setQuantityOnHand(scanner.nextInt()); + scanner.nextLine(); + + System.out.println("Enter product unit price: "); + stock.setProductUnitPrice(scanner.nextDouble()); + + System.out.println("Enter product reorder level: "); + stock.setReorderLevel(scanner.nextInt()); + scanner.nextLine(); + administrator.insertStock(stock); + } + + public static void deleteProduct(Administrator administrator, Scanner scanner) { + System.out.print("Enter product id to be deleted: "); + String removeId = scanner.nextLine(); + removeId = administrator.deleteStock(removeId); + if (removeId != null) System.out.println(removeId + " removed successfully"); + } + + public static void insertSales(Administrator administrator, Scanner scanner) throws ParseException { + Sales sales = new Sales(); + System.out.print("Enter sales id: "); + sales.setSalesID(scanner.nextLine()); + System.out.print("Enter date (dd-mm-yyyy): "); + String sDate = scanner.nextLine(); + Date date = new SimpleDateFormat("dd-mm-yyyy").parse(sDate); + sales.setSalesDate(date); + System.out.print("Enter product id: "); + sales.setProductID(scanner.nextLine()); + System.out.print("Enter quantity sold: "); + sales.setQuantitySold(scanner.nextInt()); + scanner.nextLine(); + System.out.print("Enter sales price per unit: "); + sales.setSalesPricePerUnit(scanner.nextDouble()); + administrator.insertSales(sales); + } + +} diff --git a/JDBC-Assignment/com/wipro/sales/service/Administrator.java b/JDBC-Assignment/com/wipro/sales/service/Administrator.java new file mode 100644 index 0000000..0b8260e --- /dev/null +++ b/JDBC-Assignment/com/wipro/sales/service/Administrator.java @@ -0,0 +1,68 @@ +package com.wipro.sales.service; + +import java.sql.*; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; + +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().after(new Date())) + 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 completed"; + else + return "Error"; + } else { + return "Error"; + } + } + + public ArrayList getSalesReport() { + return salesDao.getSalesReport(); + } +} diff --git a/JDBC-Assignment/com/wipro/sales/util/DBUtil.java b/JDBC-Assignment/com/wipro/sales/util/DBUtil.java new file mode 100644 index 0000000..578ed23 --- /dev/null +++ b/JDBC-Assignment/com/wipro/sales/util/DBUtil.java @@ -0,0 +1,23 @@ +package com.wipro.sales.util; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class DBUtil { + + static String databaseUrl = "jdbc:oracle:thin:@localhost:1521"; + static Connection connection = null; + static String username = "admin"; + static String password = "password"; + + public static Connection getDBConnection() { + try { + connection = DriverManager.getConnection(databaseUrl, username, password); + } catch (SQLException e) { + System.out.println("Connection could not be established. Stack trace details : "); + e.printStackTrace(); + } + return connection; + } +} diff --git a/JDBC-Assignment/database.txt b/JDBC-Assignment/database.txt new file mode 100644 index 0000000..8136378 --- /dev/null +++ b/JDBC-Assignment/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