diff --git a/sales/bean/Sales.java b/sales/bean/Sales.java new file mode 100644 index 0000000..1ab977d --- /dev/null +++ b/sales/bean/Sales.java @@ -0,0 +1,42 @@ +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/sales/bean/SalesReport.java b/sales/bean/SalesReport.java new file mode 100644 index 0000000..c2513b5 --- /dev/null +++ b/sales/bean/SalesReport.java @@ -0,0 +1,65 @@ +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/sales/bean/Stock.java b/sales/bean/Stock.java new file mode 100644 index 0000000..6fc99bb --- /dev/null +++ b/sales/bean/Stock.java @@ -0,0 +1,42 @@ +package com.wipro.sales.bean; + +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/sales/dao/SalesDao.java b/sales/dao/SalesDao.java new file mode 100644 index 0000000..c0cfe8f --- /dev/null +++ b/sales/dao/SalesDao.java @@ -0,0 +1,107 @@ +package com.wipro.sales.dao; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +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 { + /** + * This method is used to insert the given sales obj into TBL_SALES table + * */ + public int insertSales(Sales sales) { + Connection conn = null; + PreparedStatement pstmt = null; + String sql = "INSERT INTO TBL_SALES VALUES(?, ?, ?, ?, ?)"; + + //java.util.Date utilDate = new java.util.Date(); + java.sql.Date sqlDate = new java.sql.Date(sales.getSalesDate().getTime()); + + try { + conn = DBUtil.getDBConnection(); + pstmt = conn.prepareStatement(sql); + pstmt.setString(1, sales.getSalesID()); + pstmt.setDate(2, sqlDate); + pstmt.setString(3, sales.getProductID()); + pstmt.setInt(4, sales.getQuantitySold()); + pstmt.setDouble(5, sales.getSalesPricePerUnit()); + + if (pstmt.executeUpdate() == 1) return 1; + else return 0; + } catch (SQLException e) { + e.printStackTrace(); + return 0; + } + } + + /** + * This method is used to generate Sales ID using the last2digit of the + * year part of the given date concatenated with the SEQ_SALES_ID sequence + * generated number. + */ + 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; + } + } + + /** + * This method runs the V_SALES_REPORT view and stores every record in + * SalesREport Bean adding them to an arraylist. Which is return back to the user + */ + 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..361fbdb --- /dev/null +++ b/sales/dao/StockDao.java @@ -0,0 +1,154 @@ +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 { + + /** + * This method is used to insert the given stock obj into TBL_STOCK table + * @param sales + */ + 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; + } + } + + /** + * This method is used to generate StockID using the First 2 letters of the given + * product name concatenated with the SEQ_PRODUCT_ID sequence generated number. + * @param productName + * @return + */ + 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; + } + } + + /** + * This method is used to update the Stock table by subtracting the current + * Quantity_On_Hand by the given soldQty of the given productID. + * @param productID + * @param soldQty + * @return + */ + 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; + } + } + + /** + * This method is used to fetch a specific record details from the Stock table + * for the given productID, store the information to a Stock bean object the + * return the same. + * @param productID + * @return + */ + 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; + } + } + + /** + * This method is used to delete the stock record of the given ProductID + * @param productID + * @return + */ + 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..d1ae6c7 --- /dev/null +++ b/sales/main/SalesApplication.java @@ -0,0 +1,99 @@ +package com.wipro.sales.main; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Scanner; + +import com.wipro.sales.bean.Sales; +import com.wipro.sales.bean.Stock; +import com.wipro.sales.service.Administrator; + +public class SalesApplication { + + /** + * This method has to display a main menu with following Options: + * + * 1. Insert Stock + * 2. Delete Stock + * 3. Insert Sales + * 4. View Sales Report + * Enter your Choice: + * + * On selecting the choice It should accept the required data from the user + * create appropriate object and call the valid method from the Administrator class. + * Eg: if the selected option is 1. Then create Stock bean object and get all + * Stock bean data from user and set it to the object and call insertStock + * method from the Administrator class. + * @param args + * @throws ParseException + */ + 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 = 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..10978c7 --- /dev/null +++ b/sales/service/Administrator.java @@ -0,0 +1,109 @@ +package com.wipro.sales.service; + +import java.util.ArrayList; +import java.util.Date; + +import com.wipro.sales.bean.Sales; +import com.wipro.sales.bean.SalesReport; +import com.wipro.sales.bean.Stock; +import com.wipro.sales.dao.SalesDao; +import com.wipro.sales.dao.StockDao; + +public class Administrator { + private static StockDao stockDao = new StockDao(); + private static SalesDao salesDao = new SalesDao(); + + /** + * This method is used to insert the given stock obj into the TBL_STOCK table + * using StockDao class insertStock method if the below conditions are successful. + * 1. Stock obj should not be null + * 2. ProductName should be of minimum 2 letters in length + * 3. If above 2 are valid generate Product Id using StockDao class generateProductId + * method and store the same in the ProductID member of the given Stock Object + * + * If any of the above conditions fail return “Data not Valid for insertion” + * Else Return the generated ProductId + * @param stock + * @return + */ + public synchronized String insertStock(Stock stock) { + if (stock != null && stock.getProductName().length() >= 2) { + String productID = stockDao.generateProductID(stock.getProductName()); + stock.setProductID(productID); + if (stockDao.insertStock(stock) == 1) + return productID; + else + return "Data not Valid for insertion"; + } else { + return "Data not Valid for insertion"; + } + } + + /** + * Delete the record of the given Product id using StockDao class deleteStock method, + * if delete is successful return “deleted” + * else return “record cannot be deleted” + * @param ProductID + * @return + */ + public String deleteStock(String ProductID) { + if (stockDao.deleteStock(ProductID) == 1) + return "deleted"; + else + return "record cannot be deleted"; + } + + /** + * This method is used to insert the given sales obj into the TBL_SALEStable using + * SalesDao class insertSales method if the below conditions are successful. + * 1. Sales obj should not be null else return “Object not valid for insertion” + * 2. ProductID should be present in the TBL_STOCK table else return “Unknown + * Product for sales” + * 3. Products current QuatityOnHand value should be more than the QuantitySold value + * else return “Not enough stock on hand for sales” + * 4. SalesDate should be current date or earlier date and not future date, + * else return “Invalid date” + * 5. If above 4 are valid generate SalesId using SalesDao class generateSalesId method + * and store the same in the SalesID member of the given Sales Object Call the + * insertSalesmethod of SalesDao and insert the record. + * If insertion is successful call the updateStock method of the StockDao and + * update the sold quantity to the stock. + * On successful completion of both the transaction return “Sales Completed”else “Error”. + * @param sales + * @return + */ + public String insertSales(Sales sales) { + if (sales == null) + return "Object not valid for insertion"; + + if (stockDao.getStock(sales.getProductID()) == null) + return "Unknown Product for sales"; + + if (stockDao.getStock(sales.getProductID()).getQuantityOnHand() < sales.getQuantitySold()) + return "Not enough stock on hand for sales"; + + if (sales.getSalesDate().before(new Date())) + return "Invalid date"; + + String salesID = salesDao.generateSalesID(sales.getSalesDate()); + sales.setSalesID(salesID); + + if (salesDao.insertSales(sales) == 1) { + if (stockDao.updateStock(sales.getProductID(), sales.getQuantitySold()) == 1) + return "sales record inserted successfully"; + else + return "Error"; + } else { + return "Error"; + } + } + + /** + * This method calls the getSalesReport of the SalesDao and returns the ArrayList + * @return + */ + public ArrayList getSalesReport() { + return salesDao.getSalesReport(); + } + +} \ No newline at end of file diff --git a/sales/util/DBUtil.java b/sales/util/DBUtil.java new file mode 100644 index 0000000..3249ab8 --- /dev/null +++ b/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 { + private static Connection conn = null; + + /** + * Establish a connection to the database and return the java.sql.Connection reference + * */ + public static Connection getDBConnection() { + try { + conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521", "scott", "tiger"); + return conn; + } catch (SQLException e) { + System.out.println("Connection could not be estanlished"); + e.printStackTrace(); + return null; + } + } +}