diff --git a/JDBC Assignment.zip b/JDBC Assignment.zip new file mode 100644 index 0000000..8852893 Binary files /dev/null and b/JDBC Assignment.zip differ diff --git a/SQL.txt b/SQL.txt new file mode 100644 index 0000000..7810342 --- /dev/null +++ b/SQL.txt @@ -0,0 +1,44 @@ +CREATE table TBL_STOCK +(Product_ID VARCHAR2(6) PRIMARY KEY, +Product_Name VARCHAR2(20) UNIQUE, +Quantity_On_Hand NUMBER, +Product_Unit_Price NUMBER, +Reorder_Level NUMBER, +constraint TBL_STOCK_C1 check (Quantity_On_Hand >=0), +constraint TBL_STOCK_C2 check (Product_Unit_Price >=0), +constraint TBL_STOCK_C3 check (Reorder_Level >=0), +) + +CREATE table TBL_SALES ( +Sales_ID VARCHAR2(6) PRIMARY KEY, +Sales_Date DATE, +Product_ID VARCHAR2(6), +Quantity_Sold NUMBER, +Sales_Price_Per_Unit NUMBER, +constraint TBL_SALES_C1 check (Quantity_Sold >=0), +constraint TBL_SALES_C2 check (Sales_Price_Per_Unit >=0), +constraint TBL_SALES_C3 FOREIGN KEY (Product_ID) REFERENCES TBL_STOCK(Product_ID) +) + +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) + +create sequence SEQ_SALES_ID start with 1000 increment by 1 nocache nocycle noorder + +create sequence SEQ_PRODUCT_ID start with 1004 increment by 1 nocache nocycle noorder + +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 + +drop table TBL_STOCK +drop table TBL_SALES +drop sequence SEQ_SALES_ID +drop sequence SEQ_PRODUCT_ID +drop view V_SALES_REPORT \ No newline at end of file diff --git a/Sales.bat b/Sales.bat new file mode 100644 index 0000000..82b91cc --- /dev/null +++ b/Sales.bat @@ -0,0 +1 @@ +java -jar Sales.jar \ No newline at end of file diff --git a/Sales.jar b/Sales.jar new file mode 100644 index 0000000..52b0844 Binary files /dev/null and b/Sales.jar differ diff --git a/wipro/bin/bean/Sales.class b/wipro/bin/bean/Sales.class new file mode 100644 index 0000000..7d0e891 Binary files /dev/null and b/wipro/bin/bean/Sales.class differ diff --git a/wipro/bin/bean/SalesReport.class b/wipro/bin/bean/SalesReport.class new file mode 100644 index 0000000..4597493 Binary files /dev/null and b/wipro/bin/bean/SalesReport.class differ diff --git a/wipro/bin/bean/Stock.class b/wipro/bin/bean/Stock.class new file mode 100644 index 0000000..edc2c74 Binary files /dev/null and b/wipro/bin/bean/Stock.class differ diff --git a/wipro/bin/dao/SalesDao.class b/wipro/bin/dao/SalesDao.class new file mode 100644 index 0000000..7375b70 Binary files /dev/null and b/wipro/bin/dao/SalesDao.class differ diff --git a/wipro/bin/dao/StockDao.class b/wipro/bin/dao/StockDao.class new file mode 100644 index 0000000..f60e781 Binary files /dev/null and b/wipro/bin/dao/StockDao.class differ diff --git a/wipro/bin/main/SalesApplication.class b/wipro/bin/main/SalesApplication.class new file mode 100644 index 0000000..3774e5a Binary files /dev/null and b/wipro/bin/main/SalesApplication.class differ diff --git a/wipro/bin/module-info.class b/wipro/bin/module-info.class new file mode 100644 index 0000000..11222bb Binary files /dev/null and b/wipro/bin/module-info.class differ diff --git a/wipro/bin/services/Administrator.class b/wipro/bin/services/Administrator.class new file mode 100644 index 0000000..0b8b5b3 Binary files /dev/null and b/wipro/bin/services/Administrator.class differ diff --git a/wipro/bin/util/DBUtil.class b/wipro/bin/util/DBUtil.class new file mode 100644 index 0000000..b62bea3 Binary files /dev/null and b/wipro/bin/util/DBUtil.class differ diff --git a/wipro/sales/bean/Sales.java b/wipro/sales/bean/Sales.java new file mode 100644 index 0000000..bd8b4f9 --- /dev/null +++ b/wipro/sales/bean/Sales.java @@ -0,0 +1,42 @@ +package bean; + +import bean.*; +import java.util.Date; +public class Sales { + String salesID; + Date salesDate; + String productID; + int quantitySold; + 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/wipro/sales/bean/SalesReport.java b/wipro/sales/bean/SalesReport.java new file mode 100644 index 0000000..3228f3a --- /dev/null +++ b/wipro/sales/bean/SalesReport.java @@ -0,0 +1,64 @@ +package bean; + +import java.util.Date; + +public class SalesReport { + String salesID; + 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 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/wipro/sales/bean/Stock.java b/wipro/sales/bean/Stock.java new file mode 100644 index 0000000..3e2bff8 --- /dev/null +++ b/wipro/sales/bean/Stock.java @@ -0,0 +1,41 @@ +package 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/wipro/sales/dao/SalesDao.java b/wipro/sales/dao/SalesDao.java new file mode 100644 index 0000000..aec223d --- /dev/null +++ b/wipro/sales/dao/SalesDao.java @@ -0,0 +1,96 @@ +package dao; + +import bean.*; +import java.util.*; +import java.sql.*; +import util.*; + +public class SalesDao { + + //method 1 + public int insertSales(Sales sales) throws Exception { + Connection con = null; + PreparedStatement ps = null; + String sql = "INSERT INTO TBL_SALES VALUES(?, ?, ?, ?, ?)"; + + java.sql.Date sqlDate = new java.sql.Date(sales.getSalesDate().getTime()); + + try { + con = DBUtil.getDBConnection(); + ps = con.prepareStatement(sql); + ps.setString(1, sales.getSalesID()); + ps.setDate(2, sqlDate); + ps.setString(3, sales.getProductID()); + ps.setInt(4, sales.getQuantitySold()); + ps.setDouble(5, sales.getSalesPricePerUnit()); + + if (ps.executeUpdate() == 1) return 1; + else return 0; + } catch (SQLException e) { + e.printStackTrace(); + return 0; + } + } + + //method 2 + public static String generateSalesID(java.util.Date salesDate) throws Exception { + Connection con = null; + PreparedStatement ps = 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 { + con = DBUtil.getDBConnection(); + ps = con.prepareStatement(sql); + ResultSet rs = ps.executeQuery(); + + rs.next(); + SEQ_SALES_ID = rs.getInt(1); + + out += SEQ_SALES_ID; + return out; + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + + } + + //method 3 + public ArrayList getSalesReport() throws Exception{ + Connection con = null; + PreparedStatement ps = null; + String sql = "SELECT * FROM V_SALES_REPORT"; + + ArrayList list = new ArrayList(); + + try { + con = DBUtil.getDBConnection(); + ps = con.prepareStatement(sql); + ResultSet rs = ps.executeQuery(); + + int i=0; + 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); + + System.out.println(list.get(i).getSalesID()+" "+list.get(i).getSalesDate()+" "+list.get(i).getProductID()+" "+list.get(i).getProductName()+" "+list.get(i).getQuantitySold()+" "+list.get(i).getProductUnitPrice()+" "+list.get(i).getSalesPricePerUnit()+" "+list.get(i).getProfitAmount()); + ++i; + } + i=0; + } catch (SQLException e) {e.printStackTrace();return null;} + + return list; + + } +} diff --git a/wipro/sales/dao/StockDao.java b/wipro/sales/dao/StockDao.java new file mode 100644 index 0000000..ad5e9c3 --- /dev/null +++ b/wipro/sales/dao/StockDao.java @@ -0,0 +1,113 @@ +package dao; + +import bean.*; +import java.sql.*; +import util.*; + +public class StockDao { + + //method 1 + public int insertStock(Stock stock) throws Exception{ + Connection con = null; + PreparedStatement ps = null; + String sql = "INSERT INTO TBL_STOCK VALUES(?, ?, ?, ?, ?)"; + + try { + con = DBUtil.getDBConnection(); + ps = con.prepareStatement(sql); + ps.setString(1, stock.getProductID()); + ps.setString(2, stock.getProductName()); + ps.setInt(3, stock.getQuantityOnHand()); + ps.setDouble(4, stock.getProductUnitPrice()); + ps.setInt(5, stock.getReorderLevel()); + + if (ps.executeUpdate() == 1) return 1; + else return 0; + } catch (SQLException e) {e.printStackTrace();return 0;} + } + + //method 2 + public String generateProductID(String productName) throws Exception{ + Connection con = null; + PreparedStatement ps = null; + String sql = "SELECT SEQ_PRODUCT_ID.NEXTVAL FROM DUAL"; + + int SEQ_PRODUCT_ID = 0; + String out = ""; + + try { + con = DBUtil.getDBConnection(); + ps = con.prepareStatement(sql); + ResultSet rs = ps.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;} + } + + //method 3 + public int updateStock(String productID,int soldQty) throws Exception{ + Connection con = null; + PreparedStatement ps = null; + String sql = "UPDATE TBL_STOCK SET Quantity_On_Hand = Quantity_On_Hand - ?" + + "WHERE Product_ID = ?"; + + try { + con = DBUtil.getDBConnection(); + ps = con.prepareStatement(sql); + ps.setInt(1, soldQty); + ps.setString(2, productID); + + if (ps.executeUpdate() == 1) return 1; + else return 0; + } catch (SQLException e) {e.printStackTrace();return 0;} + } + + //method 4 + public Stock getStock(String productID) throws Exception { + Connection con = null; + PreparedStatement ps = null; + String sql = "SELECT * FROM TBL_STOCK WHERE Product_ID = ?"; + + try { + con = DBUtil.getDBConnection(); + ps = con.prepareStatement(sql); + ps.setString(1, productID); + + ResultSet rs = ps.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;} + + } + + //method 5 + public int deleteStock(String productID) throws Exception{ + Connection con = null; + PreparedStatement ps = null; + String sql = "DELETE TBL_STOCK WHERE Product_ID = ?"; + + try { + con = DBUtil.getDBConnection(); + ps = con.prepareStatement(sql); + ps.setString(1, productID); + + if (ps.executeUpdate() == 1) return 1; + else return 0; + } catch (SQLException e) {e.printStackTrace();return 0;} + } +} diff --git a/wipro/sales/main/SalesApplication.java b/wipro/sales/main/SalesApplication.java new file mode 100644 index 0000000..d53c15b --- /dev/null +++ b/wipro/sales/main/SalesApplication.java @@ -0,0 +1,81 @@ +package main; + +import bean.*; +import services.*; +import util.DBUtil; + +import java.util.*; +import java.text.*; + +public class SalesApplication { + + public static void main(String[] args) throws Exception { + // TODO Auto-generated method stub + DBUtil.getDBConnection(); + try { + Scanner sc = new Scanner(System.in); + + Administrator admin = new Administrator(); + + int choice; + + do { + System.out.print("\n"); + 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("\n"); + System.out.print("Enter your Choice: "); + choice = sc.nextInt(); + System.out.print("\n"); + + switch (choice) { + case 1: + Stock stock = new Stock(); + System.out.print("Enter product name: "); + stock.setProductName(sc.next()); + System.out.print("Enter quantity on hand: "); + stock.setQuantityOnHand(sc.nextInt()); + System.out.print("Enter product unit price: "); + stock.setProductUnitPrice(sc.nextDouble()); + System.out.print("Enter product reorder level: "); + stock.setReorderLevel(sc.nextInt()); + admin.insertStock(stock); + break; + case 2: + System.out.print("Enter product id to be deleted: "); + String removeId = sc.next(); + removeId = admin.deleteStock(removeId); + break; + case 3: + Sales sales = new Sales(); + System.out.print("Enter date (mm-dd-yyyy): "); + String sDate = sc.next(); + Date date = new SimpleDateFormat("mm-dd-yyyy").parse(sDate); + sales.setSalesDate(date); + System.out.print("Enter product id: "); + sales.setProductID(sc.next()); + 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("Wrong Choice"); + break; + } + } while (choice >= 1 && choice <= 4); + sc.close(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} diff --git a/wipro/sales/module-info.java b/wipro/sales/module-info.java new file mode 100644 index 0000000..5f36522 --- /dev/null +++ b/wipro/sales/module-info.java @@ -0,0 +1,3 @@ +module wipro { + requires java.sql; +} \ No newline at end of file diff --git a/wipro/sales/services/Administrator.java b/wipro/sales/services/Administrator.java new file mode 100644 index 0000000..667ef0c --- /dev/null +++ b/wipro/sales/services/Administrator.java @@ -0,0 +1,62 @@ +package services; + +import bean.*; +import dao.*; +import java.util.*; + +public class Administrator { + private static StockDao stockDao = new StockDao(); + private static SalesDao salesDao = new SalesDao(); + + //method 1 + public synchronized String insertStock(Stock stockobj) throws Exception { + 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"; + } + } + + //method 2 + public String deleteStock(String ProductID) throws Exception { + if (stockDao.deleteStock(ProductID) == 1) + return "deleted"; + else + return "record cannot be deleted"; + } + + //method 3 + public String insertSales(Sales salesobj) throws Exception { + if (salesobj == null) + return "Object not valid for insertion"; + else if (stockDao.getStock(salesobj.getProductID()) == null) + return "Unknown Product for sales"; + else if (stockDao.getStock(salesobj.getProductID()).getQuantityOnHand() < salesobj.getQuantitySold()) + return "Not enough stock on hand for sales"; + else if (salesobj.getSalesDate().before(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 record inserted successfully"; + else + return "Error"; + } else { + return "Error"; + } + + } + + //method 4 + public ArrayList getSalesReport() throws Exception { + return salesDao.getSalesReport(); + } +} diff --git a/wipro/sales/util/DBUtil.java b/wipro/sales/util/DBUtil.java new file mode 100644 index 0000000..70007c3 --- /dev/null +++ b/wipro/sales/util/DBUtil.java @@ -0,0 +1,69 @@ +package util; + +import java.sql.*; + +public class DBUtil { + + private static Connection con = null; + + public static Connection getDBConnection() throws Exception { + String url="jdbc:oracle:thin:@localhost:1521:xe"; + String user="system"; + String password="412589"; + try { + Class.forName("oracle.jdbc.OracleDriver"); + con = DriverManager.getConnection(url, user, password); + //System.out.println("connection established\n"); + + //Statement st=con.createStatement(); + //st.executeUpdate("drop table TBL_SALES"); + //st.executeUpdate("drop table TBL_STOCK"); + /*st.executeUpdate("CREATE table TBL_STOCK " + + "( Product_ID VARCHAR2(6) PRIMARY KEY," + + "Product_Name VARCHAR2(20) UNIQUE," + + "Quantity_On_Hand NUMBER," + + "Product_Unit_Price NUMBER," + + "Reorder_Level NUMBER," + + "constraint TBL_STOCK_C1 check (Quantity_On_Hand >=0)," + + "constraint TBL_STOCK_C2 check (Product_Unit_Price >=0)," + + "constraint TBL_STOCK_C3 check (Reorder_Level >=0) )");*/ + + /*st.executeUpdate("CREATE table TBL_SALES (\r\n" + + "Sales_ID VARCHAR2(6) PRIMARY KEY,\r\n" + + "Sales_Date DATE,\r\n" + + "Product_ID VARCHAR2(6),\r\n" + + "Quantity_Sold NUMBER,\r\n" + + "Sales_Price_Per_Unit NUMBER,\r\n" + + "constraint TBL_SALES_C1 check (Quantity_Sold >=0),\r\n" + + "constraint TBL_SALES_C2 check (Sales_Price_Per_Unit >=0),\r\n" + + "constraint TBL_SALES_C3 FOREIGN KEY (Product_ID) REFERENCES TBL_STOCK(Product_ID) \r\n" + + ")");*/ + //st.executeUpdate("insert into TBL_STOCK values('RE1001','REDMI Note 3',20,12000,5)"); + //st.executeUpdate("insert into TBL_STOCK values('IP1002','Iphone 5S',10,21000,2)"); + //st.executeUpdate("insert into TBL_STOCK values('PA1003','Panasonic P55',50,5500,5)"); + + //st.executeUpdate("create sequence SEQ_SALES_ID start with 1000 increment by 1 nocache nocycle noorder"); + //st.executeUpdate("create sequence SEQ_PRODUCT_ID start with 1004 increment by 1 nocache nocycle noorder"); + //st.executeUpdate("drop sequence SEQ_PRODUCT_ID"); + //st.executeUpdate("drop sequence SEQ_SALES_ID"); + + /*st.executeUpdate("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");*/ + //st.executeUpdate("commit"); + //for testing purpuse + /*ResultSet rs=st.executeQuery("select Sales_ID from V_SALES_REPORT"); + while(rs!=null&&rs.next()) + System.out.println(rs.getString(1)); + + rs=st.executeQuery("select Product_ID from TBL_STOCK"); + while(rs!=null&&rs.next()) + System.out.println(rs.getString(1));*/ + } + catch(SQLException e) {System.out.println("Connection Failed");e.printStackTrace();} + return con; + } +}