diff --git a/bin/com/wipro/sales/bean/Sales.class b/bin/com/wipro/sales/bean/Sales.class new file mode 100644 index 0000000..cb4deed Binary files /dev/null and b/bin/com/wipro/sales/bean/Sales.class differ diff --git a/bin/com/wipro/sales/bean/SalesReport.class b/bin/com/wipro/sales/bean/SalesReport.class new file mode 100644 index 0000000..cd4603a Binary files /dev/null and b/bin/com/wipro/sales/bean/SalesReport.class differ diff --git a/bin/com/wipro/sales/bean/Stock.class b/bin/com/wipro/sales/bean/Stock.class new file mode 100644 index 0000000..5fbe9d9 Binary files /dev/null and b/bin/com/wipro/sales/bean/Stock.class differ diff --git a/bin/com/wipro/sales/dao/SalesDao.class b/bin/com/wipro/sales/dao/SalesDao.class new file mode 100644 index 0000000..e572763 Binary files /dev/null and b/bin/com/wipro/sales/dao/SalesDao.class differ diff --git a/bin/com/wipro/sales/dao/StockDao.class b/bin/com/wipro/sales/dao/StockDao.class new file mode 100644 index 0000000..3111730 Binary files /dev/null and b/bin/com/wipro/sales/dao/StockDao.class differ diff --git a/bin/com/wipro/sales/main/SalesApplication.class b/bin/com/wipro/sales/main/SalesApplication.class new file mode 100644 index 0000000..88c8505 Binary files /dev/null and b/bin/com/wipro/sales/main/SalesApplication.class differ diff --git a/bin/com/wipro/sales/service/Administrator.class b/bin/com/wipro/sales/service/Administrator.class new file mode 100644 index 0000000..a0d4cba Binary files /dev/null and b/bin/com/wipro/sales/service/Administrator.class differ diff --git a/bin/com/wipro/sales/util/DBUtil.class b/bin/com/wipro/sales/util/DBUtil.class new file mode 100644 index 0000000..07f7c94 Binary files /dev/null and b/bin/com/wipro/sales/util/DBUtil.class differ diff --git a/src/com/wipro/sales/bean/Sales.java b/src/com/wipro/sales/bean/Sales.java new file mode 100644 index 0000000..9a8f050 --- /dev/null +++ b/src/com/wipro/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/src/com/wipro/sales/bean/SalesReport.java b/src/com/wipro/sales/bean/SalesReport.java new file mode 100644 index 0000000..440703d --- /dev/null +++ b/src/com/wipro/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/src/com/wipro/sales/bean/Stock.java b/src/com/wipro/sales/bean/Stock.java new file mode 100644 index 0000000..464d63d --- /dev/null +++ b/src/com/wipro/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/src/com/wipro/sales/dao/SalesDao.java b/src/com/wipro/sales/dao/SalesDao.java new file mode 100644 index 0000000..1d24d3c --- /dev/null +++ b/src/com/wipro/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/src/com/wipro/sales/dao/StockDao.java b/src/com/wipro/sales/dao/StockDao.java new file mode 100644 index 0000000..3d68c4b --- /dev/null +++ b/src/com/wipro/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/src/com/wipro/sales/main/SalesApplication.java b/src/com/wipro/sales/main/SalesApplication.java new file mode 100644 index 0000000..13f1e6f --- /dev/null +++ b/src/com/wipro/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/src/com/wipro/sales/service/Administrator.java b/src/com/wipro/sales/service/Administrator.java new file mode 100644 index 0000000..6067754 --- /dev/null +++ b/src/com/wipro/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/src/com/wipro/sales/util/DBUtil.java b/src/com/wipro/sales/util/DBUtil.java new file mode 100644 index 0000000..89210fa --- /dev/null +++ b/src/com/wipro/sales/util/DBUtil.java @@ -0,0 +1,23 @@ +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 { + //Register driver + Class.forName("oracle.jdbc.driver.OracleDriver"); + //create connection + con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521", "username", "password"); + return con; + } catch (Exception e) { + System.out.println("Connection could not be estanlished"); + e.printStackTrace(); + return null; + } + } +}