├── README.md ├── bin └── com │ └── wipro │ └── sales │ ├── bean │ ├── Sales.class │ ├── Stock.class │ └── SalesReport.class │ ├── util │ └── DBUtil.class │ ├── dao │ ├── SalesDao.class │ ├── StockDao.class │ ├── SalesDaoTest.class │ └── StockDaoTest.class │ ├── main │ └── SalesApplication.class │ └── service │ ├── Administrator.class │ └── AdministratorTest.class ├── src └── com │ └── wipro │ └── sales │ ├── util │ └── DBUtil.java │ ├── bean │ ├── Sales.java │ ├── Stock.java │ └── SalesReport.java │ ├── main │ └── SalesApplication.java │ ├── dao │ ├── SalesDao.java │ └── StockDao.java │ └── service │ └── Administrator.java ├── test └── com │ └── wipro │ └── sales │ ├── service │ └── AdministratorTest.java │ └── dao │ ├── SalesDaoTest.java │ └── StockDaoTest.java └── db.sql /README.md: -------------------------------------------------------------------------------- 1 | Wipro TalentNext PBL 2 | 3 | Topics Covered 4 | 5 | SQL & JDBC -------------------------------------------------------------------------------- /bin/com/wipro/sales/bean/Sales.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Module7---Inventory-Sales-System/HEAD/bin/com/wipro/sales/bean/Sales.class -------------------------------------------------------------------------------- /bin/com/wipro/sales/bean/Stock.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Module7---Inventory-Sales-System/HEAD/bin/com/wipro/sales/bean/Stock.class -------------------------------------------------------------------------------- /bin/com/wipro/sales/util/DBUtil.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Module7---Inventory-Sales-System/HEAD/bin/com/wipro/sales/util/DBUtil.class -------------------------------------------------------------------------------- /bin/com/wipro/sales/dao/SalesDao.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Module7---Inventory-Sales-System/HEAD/bin/com/wipro/sales/dao/SalesDao.class -------------------------------------------------------------------------------- /bin/com/wipro/sales/dao/StockDao.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Module7---Inventory-Sales-System/HEAD/bin/com/wipro/sales/dao/StockDao.class -------------------------------------------------------------------------------- /bin/com/wipro/sales/bean/SalesReport.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Module7---Inventory-Sales-System/HEAD/bin/com/wipro/sales/bean/SalesReport.class -------------------------------------------------------------------------------- /bin/com/wipro/sales/dao/SalesDaoTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Module7---Inventory-Sales-System/HEAD/bin/com/wipro/sales/dao/SalesDaoTest.class -------------------------------------------------------------------------------- /bin/com/wipro/sales/dao/StockDaoTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Module7---Inventory-Sales-System/HEAD/bin/com/wipro/sales/dao/StockDaoTest.class -------------------------------------------------------------------------------- /bin/com/wipro/sales/main/SalesApplication.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Module7---Inventory-Sales-System/HEAD/bin/com/wipro/sales/main/SalesApplication.class -------------------------------------------------------------------------------- /bin/com/wipro/sales/service/Administrator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Module7---Inventory-Sales-System/HEAD/bin/com/wipro/sales/service/Administrator.class -------------------------------------------------------------------------------- /bin/com/wipro/sales/service/AdministratorTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Module7---Inventory-Sales-System/HEAD/bin/com/wipro/sales/service/AdministratorTest.class -------------------------------------------------------------------------------- /src/com/wipro/sales/util/DBUtil.java: -------------------------------------------------------------------------------- 1 | package com.wipro.sales.util; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.SQLException; 6 | 7 | public class DBUtil { 8 | private static Connection conn = null; 9 | 10 | /** 11 | * Establish a connection to the database and return the java.sql.Connection reference 12 | * */ 13 | public static Connection getDBConnection() { 14 | try { 15 | conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521", "scott", "tiger"); 16 | return conn; 17 | } catch (SQLException e) { 18 | System.out.println("Connection could not be estanlished"); 19 | e.printStackTrace(); 20 | return null; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /test/com/wipro/sales/service/AdministratorTest.java: -------------------------------------------------------------------------------- 1 | package com.wipro.sales.service; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | import com.wipro.sales.bean.Stock; 8 | 9 | public class AdministratorTest { 10 | Administrator admin = new Administrator(); 11 | //@Test 12 | public void testInsertStock() { 13 | Stock stock = new Stock(); 14 | // stock.setProductID(productID); 15 | stock.setProductName("Test Product5"); 16 | stock.setQuantityOnHand(10); 17 | stock.setProductUnitPrice(40000); 18 | stock.setReorderLevel(3); 19 | 20 | assertNotEquals("Data not Valid for insertion", admin.insertStock(stock)); 21 | } 22 | 23 | @Test 24 | public void testDeleteStock() { 25 | assertEquals("deleted", admin.deleteStock("Te1016")); 26 | } 27 | 28 | @Test 29 | public void testInsertSales() { 30 | //fail("Not yet implemented"); 31 | } 32 | 33 | @Test 34 | public void testGetSalesReport() { 35 | //fail("Not yet implemented"); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/com/wipro/sales/bean/Sales.java: -------------------------------------------------------------------------------- 1 | package com.wipro.sales.bean; 2 | 3 | import java.util.Date; 4 | 5 | public class Sales { 6 | private String salesID; 7 | private Date salesDate; 8 | private String productID; 9 | private int quantitySold; 10 | private double salesPricePerUnit; 11 | 12 | public String getSalesID() { 13 | return salesID; 14 | } 15 | public void setSalesID(String salesID) { 16 | this.salesID = salesID; 17 | } 18 | public Date getSalesDate() { 19 | return salesDate; 20 | } 21 | public void setSalesDate(Date salesDate) { 22 | this.salesDate = salesDate; 23 | } 24 | public String getProductID() { 25 | return productID; 26 | } 27 | public void setProductID(String productID) { 28 | this.productID = productID; 29 | } 30 | public int getQuantitySold() { 31 | return quantitySold; 32 | } 33 | public void setQuantitySold(int quantitySold) { 34 | this.quantitySold = quantitySold; 35 | } 36 | public double getSalesPricePerUnit() { 37 | return salesPricePerUnit; 38 | } 39 | public void setSalesPricePerUnit(double salesPricePerUnit) { 40 | this.salesPricePerUnit = salesPricePerUnit; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/com/wipro/sales/bean/Stock.java: -------------------------------------------------------------------------------- 1 | package com.wipro.sales.bean; 2 | 3 | public class Stock { 4 | private String productID; 5 | private String productName; 6 | private int quantityOnHand; 7 | private double productUnitPrice; 8 | private int reorderLevel; 9 | 10 | public String getProductID() { 11 | return productID; 12 | } 13 | public void setProductID(String productID) { 14 | this.productID = productID; 15 | } 16 | public String getProductName() { 17 | return productName; 18 | } 19 | public void setProductName(String productName) { 20 | this.productName = productName; 21 | } 22 | public int getQuantityOnHand() { 23 | return quantityOnHand; 24 | } 25 | public void setQuantityOnHand(int quantityOnHand) { 26 | this.quantityOnHand = quantityOnHand; 27 | } 28 | public double getProductUnitPrice() { 29 | return productUnitPrice; 30 | } 31 | public void setProductUnitPrice(double productUnitPrice) { 32 | this.productUnitPrice = productUnitPrice; 33 | } 34 | public int getReorderLevel() { 35 | return reorderLevel; 36 | } 37 | public void setReorderLevel(int reorderLevel) { 38 | this.reorderLevel = reorderLevel; 39 | } 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /test/com/wipro/sales/dao/SalesDaoTest.java: -------------------------------------------------------------------------------- 1 | package com.wipro.sales.dao; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Date; 7 | 8 | import org.junit.Test; 9 | 10 | import com.wipro.sales.bean.Sales; 11 | import com.wipro.sales.bean.SalesReport; 12 | import com.wipro.sales.dao.SalesDao; 13 | 14 | public class SalesDaoTest { 15 | 16 | @Test 17 | public void testInsertSales() { 18 | SalesDao sdao = new SalesDao(); 19 | String salesId = sdao.generateSalesID(new Date()); 20 | 21 | Sales sales = new Sales(); 22 | sales.setSalesID(salesId); 23 | sales.setSalesDate(new Date()); 24 | sales.setProductID("RE1001"); 25 | sales.setQuantitySold(10); 26 | sales.setSalesPricePerUnit(15000); 27 | 28 | int expected = 1; 29 | int actual = sdao.insertSales(sales); 30 | 31 | assertEquals(expected, actual); 32 | } 33 | 34 | // @Test 35 | // public void testGenerateSalesID() { 36 | // SalesDao sdao = new SalesDao(); 37 | // 38 | // String seq = sdao.generateSalesID(new Date()); 39 | // System.out.println(seq); 40 | // 41 | // assertEquals("181006", seq); 42 | // } 43 | 44 | @Test 45 | public void testGetSalesReport() { 46 | SalesDao sdao = new SalesDao(); 47 | ArrayList list = sdao.getSalesReport(); 48 | 49 | assertNotNull(list); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /test/com/wipro/sales/dao/StockDaoTest.java: -------------------------------------------------------------------------------- 1 | package com.wipro.sales.dao; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | import com.wipro.sales.bean.Stock; 8 | import com.wipro.sales.dao.StockDao; 9 | 10 | public class StockDaoTest { 11 | 12 | //@Test 13 | public void testInsertStock() { 14 | StockDao sdao = new StockDao(); 15 | 16 | String productName = "Galaxy S5"; 17 | String productID = sdao.generateProductID(productName); 18 | 19 | Stock stock = new Stock(); 20 | stock.setProductID(productID); 21 | stock.setProductName(productName); 22 | stock.setQuantityOnHand(10); 23 | stock.setProductUnitPrice(40000); 24 | stock.setReorderLevel(3); 25 | 26 | assertEquals(1, sdao.insertStock(stock)); 27 | } 28 | 29 | //@Test 30 | public void testGenerateProductID() { 31 | StockDao sdao = new StockDao(); 32 | 33 | String productName = "Redmi Note 5"; 34 | String productID = sdao.generateProductID(productName); 35 | 36 | assertEquals("Re1006", productID); 37 | } 38 | 39 | @Test 40 | public void testUpdateStock() { 41 | StockDao sdao = new StockDao(); 42 | 43 | assertEquals(1, sdao.updateStock("1008Ga", 1)); 44 | } 45 | 46 | //@Test 47 | public void testGetStock() { 48 | StockDao sdao = new StockDao(); 49 | 50 | assertNotNull(sdao.getStock("Ga1009")); 51 | } 52 | 53 | //@Test 54 | public void testDeleteStock() { 55 | StockDao sdao = new StockDao(); 56 | 57 | assertEquals(1, sdao.deleteStock("Te1016")); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /db.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE TBL_SALES; 2 | DROP TABLE TBL_STOCK; 3 | CREATE TABLE TBL_STOCK ( 4 | Product_ID Varchar2(6), 5 | Product_Name Varchar2(20), 6 | Quantity_On_Hand Number(11), 7 | Product_Unit_Price Number(11,2), 8 | Reorder_Level Number(11), 9 | CONSTRAINT PK00 PRIMARY KEY(Product_ID), 10 | CONSTRAINT UQ01 UNIQUE(Product_Name), 11 | CONSTRAINT CH02 CHECK(Quantity_On_Hand >= 0), 12 | CONSTRAINT CH03 CHECK(Product_Unit_Price >= 0), 13 | CONSTRAINT CH04 CHECK(Reorder_Level >= 0) 14 | ); 15 | 16 | CREATE TABLE TBL_SALES ( 17 | Sales_ID Varchar2(6), 18 | Sales_Date Date, 19 | Product_ID Varchar2(6), 20 | Quantity_Sold Number(11), 21 | Sales_Price_Per_Unit Number(11,2), 22 | CONSTRAINT PK10 PRIMARY KEY(Sales_ID), 23 | CONSTRAINT FK11 FOREIGN KEY(Product_ID) REFERENCES TBL_STOCK(Product_ID), 24 | CONSTRAINT CH12 CHECK(Quantity_Sold >= 0), 25 | CONSTRAINT CH13 CHECK(Sales_Price_Per_Unit >= 0) 26 | ); 27 | 28 | INSERT INTO TBL_STOCK VALUES('RE1001', 'REDMI Note 3', 20, 12000, 5); 29 | INSERT INTO TBL_STOCK VALUES('ip1002', 'Iphone 5S', 10, 21000, 2); 30 | INSERT INTO TBL_STOCK VALUES('PA1003', 'Panasonic P55', 50, 5500, 5); 31 | 32 | DROP SEQUENCE SEQ_SALES_ID; 33 | DROP SEQUENCE SEQ_PRODUCT_ID; 34 | CREATE SEQUENCE SEQ_SALES_ID START WITH 1000 INCREMENT BY 1; 35 | CREATE SEQUENCE SEQ_PRODUCT_ID START WITH 1004 INCREMENT BY 1; 36 | 37 | DROP VIEW V_SALES_REPORT; 38 | CREATE VIEW V_SALES_REPORT AS 39 | SELECT Sales_ID, Sales_Date, Product_ID, Product_Name, 40 | Quantity_Sold, Product_Unit_Price, Sales_Price_Per_Unit, 41 | (Sales_Price_Per_Unit - Product_Unit_Price) Profit_Amount 42 | FROM TBL_STOCK NATURAL JOIN TBL_SALES 43 | ORDER BY Profit_Amount DESC, Sales_ID ASC; 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /src/com/wipro/sales/bean/SalesReport.java: -------------------------------------------------------------------------------- 1 | package com.wipro.sales.bean; 2 | 3 | import java.util.Date; 4 | 5 | public class SalesReport { 6 | private String salesID; 7 | private Date salesDate; 8 | private String productID; 9 | private String productName; 10 | private int quantitySold; 11 | private double productUnitPrice; 12 | private double salesPricePerUnit; 13 | private double profitAmount; 14 | 15 | public String getSalesID() { 16 | return salesID; 17 | } 18 | public void setSalesID(String salesID) { 19 | this.salesID = salesID; 20 | } 21 | public Date getSalesDate() { 22 | return salesDate; 23 | } 24 | public void setSalesDate(Date salesDate) { 25 | this.salesDate = salesDate; 26 | } 27 | public String getProductID() { 28 | return productID; 29 | } 30 | public void setProductID(String productID) { 31 | this.productID = productID; 32 | } 33 | public String getProductName() { 34 | return productName; 35 | } 36 | public void setProductName(String productName) { 37 | this.productName = productName; 38 | } 39 | public int getQuantitySold() { 40 | return quantitySold; 41 | } 42 | public void setQuantitySold(int quantitySold) { 43 | this.quantitySold = quantitySold; 44 | } 45 | public double getProductUnitPrice() { 46 | return productUnitPrice; 47 | } 48 | public void setProductUnitPrice(double productUnitPrice) { 49 | this.productUnitPrice = productUnitPrice; 50 | } 51 | public double getSalesPricePerUnit() { 52 | return salesPricePerUnit; 53 | } 54 | public void setSalesPricePerUnit(double salesPricePerUnit) { 55 | this.salesPricePerUnit = salesPricePerUnit; 56 | } 57 | public double getProfitAmount() { 58 | return profitAmount; 59 | } 60 | public void setProfitAmount(double profitAmount) { 61 | this.profitAmount = profitAmount; 62 | } 63 | 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/com/wipro/sales/main/SalesApplication.java: -------------------------------------------------------------------------------- 1 | package com.wipro.sales.main; 2 | 3 | import java.text.ParseException; 4 | import java.text.SimpleDateFormat; 5 | import java.util.Date; 6 | import java.util.Scanner; 7 | 8 | import com.wipro.sales.bean.Sales; 9 | import com.wipro.sales.bean.Stock; 10 | import com.wipro.sales.service.Administrator; 11 | 12 | public class SalesApplication { 13 | 14 | /** 15 | * This method has to display a main menu with following Options: 16 | * 17 | * 1. Insert Stock 18 | * 2. Delete Stock 19 | * 3. Insert Sales 20 | * 4. View Sales Report 21 | * Enter your Choice: 22 | * 23 | * On selecting the choice It should accept the required data from the user 24 | * create appropriate object and call the valid method from the Administrator class. 25 | * Eg: if the selected option is 1. Then create Stock bean object and get all 26 | * Stock bean data from user and set it to the object and call insertStock 27 | * method from the Administrator class. 28 | * @param args 29 | * @throws ParseException 30 | */ 31 | public static void main(String[] args) throws ParseException { 32 | Scanner sc = new Scanner(System.in); 33 | 34 | Administrator admin = new Administrator(); 35 | 36 | int choice = 0; 37 | 38 | do { 39 | System.out.println("1. Insert Stock"); 40 | System.out.println("2. Delete Stock"); 41 | System.out.println("3. Insert Sales"); 42 | System.out.println("4. View Sales Report"); 43 | System.out.print("Enter your Choice: "); 44 | choice = sc.nextInt(); 45 | 46 | switch (choice) { 47 | case 1: 48 | Stock stock = new Stock(); 49 | System.out.print("Enter product ID: "); 50 | stock.setProductID(sc.nextLine()); 51 | System.out.print("Enter product name: "); 52 | stock.setProductName(sc.nextLine()); 53 | System.out.print("Enter quantity on hand: "); 54 | stock.setQuantityOnHand(sc.nextInt()); 55 | sc.nextLine(); 56 | System.out.print("Enter product unit price: "); 57 | stock.setProductUnitPrice(sc.nextDouble()); 58 | System.out.print("Enter product reorder level: "); 59 | stock.setReorderLevel(sc.nextInt()); 60 | sc.nextLine(); 61 | admin.insertStock(stock); 62 | break; 63 | case 2: 64 | System.out.print("Enter product id to be deleted: "); 65 | String removeId = sc.nextLine(); 66 | removeId = admin.deleteStock(removeId); 67 | if (removeId != null) System.out.println(removeId + " removed successfully"); 68 | break; 69 | case 3: 70 | Sales sales = new Sales(); 71 | System.out.print("Enter sales id: "); 72 | sales.setSalesID(sc.nextLine()); 73 | System.out.print("Enter date (dd-mm-yyyy): "); 74 | String sDate = sc.nextLine(); 75 | Date date = new SimpleDateFormat("dd-mm-yyyy").parse(sDate); 76 | sales.setSalesDate(date); 77 | System.out.print("Enter product id: "); 78 | sales.setProductID(sc.nextLine()); 79 | System.out.print("Enter quantity sold: "); 80 | sales.setQuantitySold(sc.nextInt()); 81 | sc.nextLine(); 82 | System.out.print("Enter sales price per unit: "); 83 | sales.setSalesPricePerUnit(sc.nextDouble()); 84 | admin.insertSales(sales); 85 | break; 86 | case 4: 87 | admin.getSalesReport(); 88 | break; 89 | default: 90 | System.out.println("Exiting..."); 91 | choice = 0; 92 | break; 93 | } 94 | } while (choice >= 1 && choice <= 4); 95 | 96 | sc.close(); 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /src/com/wipro/sales/dao/SalesDao.java: -------------------------------------------------------------------------------- 1 | package com.wipro.sales.dao; 2 | 3 | import java.sql.Connection; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | import java.util.ArrayList; 8 | import java.util.Date; 9 | 10 | import com.wipro.sales.bean.Sales; 11 | import com.wipro.sales.bean.SalesReport; 12 | import com.wipro.sales.util.DBUtil; 13 | 14 | public class SalesDao { 15 | /** 16 | * This method is used to insert the given sales obj into TBL_SALES table 17 | * */ 18 | public int insertSales(Sales sales) { 19 | Connection conn = null; 20 | PreparedStatement pstmt = null; 21 | String sql = "INSERT INTO TBL_SALES VALUES(?, ?, ?, ?, ?)"; 22 | 23 | //java.util.Date utilDate = new java.util.Date(); 24 | java.sql.Date sqlDate = new java.sql.Date(sales.getSalesDate().getTime()); 25 | 26 | try { 27 | conn = DBUtil.getDBConnection(); 28 | pstmt = conn.prepareStatement(sql); 29 | pstmt.setString(1, sales.getSalesID()); 30 | pstmt.setDate(2, sqlDate); 31 | pstmt.setString(3, sales.getProductID()); 32 | pstmt.setInt(4, sales.getQuantitySold()); 33 | pstmt.setDouble(5, sales.getSalesPricePerUnit()); 34 | 35 | if (pstmt.executeUpdate() == 1) return 1; 36 | else return 0; 37 | } catch (SQLException e) { 38 | e.printStackTrace(); 39 | return 0; 40 | } 41 | } 42 | 43 | /** 44 | * This method is used to generate Sales ID using the last2digit of the 45 | * year part of the given date concatenated with the SEQ_SALES_ID sequence 46 | * generated number. 47 | */ 48 | public String generateSalesID(Date salesDate) { 49 | Connection conn = null; 50 | PreparedStatement pstmt = null; 51 | String sql = "SELECT SEQ_SALES_ID.NEXTVAL FROM DUAL"; 52 | 53 | int SEQ_SALES_ID = 0; 54 | String out = salesDate.toString().substring(salesDate.toString().length()-2, salesDate.toString().length()); 55 | 56 | try { 57 | conn = DBUtil.getDBConnection(); 58 | pstmt = conn.prepareStatement(sql); 59 | ResultSet rs = pstmt.executeQuery(); 60 | 61 | rs.next(); 62 | SEQ_SALES_ID = rs.getInt(1); 63 | 64 | out += SEQ_SALES_ID; 65 | return out; 66 | } catch (SQLException e) { 67 | e.printStackTrace(); 68 | return null; 69 | } 70 | } 71 | 72 | /** 73 | * This method runs the V_SALES_REPORT view and stores every record in 74 | * SalesREport Bean adding them to an arraylist. Which is return back to the user 75 | */ 76 | public ArrayList getSalesReport() { 77 | Connection conn = null; 78 | PreparedStatement pstmt = null; 79 | String sql = "SELECT * FROM V_SALES_REPORT"; 80 | 81 | ArrayList list = new ArrayList(); 82 | 83 | try { 84 | conn = DBUtil.getDBConnection(); 85 | pstmt = conn.prepareStatement(sql); 86 | ResultSet rs = pstmt.executeQuery(); 87 | 88 | while (rs.next()) { 89 | SalesReport salesReport = new SalesReport(); 90 | salesReport.setSalesID(rs.getString(1)); 91 | salesReport.setSalesDate(rs.getDate(2)); 92 | salesReport.setProductID(rs.getString(3)); 93 | salesReport.setProductName(rs.getString(4)); 94 | salesReport.setQuantitySold(rs.getInt(5)); 95 | salesReport.setProductUnitPrice(rs.getDouble(6)); 96 | salesReport.setSalesPricePerUnit(rs.getDouble(7)); 97 | salesReport.setProfitAmount(rs.getDouble(8)); 98 | list.add(salesReport); 99 | } 100 | } catch (SQLException e) { 101 | e.printStackTrace(); 102 | return null; 103 | } 104 | 105 | return list; 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/com/wipro/sales/service/Administrator.java: -------------------------------------------------------------------------------- 1 | package com.wipro.sales.service; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Date; 5 | 6 | import com.wipro.sales.bean.Sales; 7 | import com.wipro.sales.bean.SalesReport; 8 | import com.wipro.sales.bean.Stock; 9 | import com.wipro.sales.dao.SalesDao; 10 | import com.wipro.sales.dao.StockDao; 11 | 12 | public class Administrator { 13 | private static StockDao stockDao = new StockDao(); 14 | private static SalesDao salesDao = new SalesDao(); 15 | 16 | /** 17 | * This method is used to insert the given stock obj into the TBL_STOCK table 18 | * using StockDao class insertStock method if the below conditions are successful. 19 | * 1. Stock obj should not be null 20 | * 2. ProductName should be of minimum 2 letters in length 21 | * 3. If above 2 are valid generate Product Id using StockDao class generateProductId 22 | * method and store the same in the ProductID member of the given Stock Object 23 | * 24 | * If any of the above conditions fail return “Data not Valid for insertion” 25 | * Else Return the generated ProductId 26 | * @param stock 27 | * @return 28 | */ 29 | public synchronized String insertStock(Stock stock) { 30 | if (stock != null && stock.getProductName().length() >= 2) { 31 | String productID = stockDao.generateProductID(stock.getProductName()); 32 | stock.setProductID(productID); 33 | if (stockDao.insertStock(stock) == 1) 34 | return productID; 35 | else 36 | return "Data not Valid for insertion"; 37 | } else { 38 | return "Data not Valid for insertion"; 39 | } 40 | } 41 | 42 | /** 43 | * Delete the record of the given Product id using StockDao class deleteStock method, 44 | * if delete is successful return “deleted” 45 | * else return “record cannot be deleted” 46 | * @param ProductID 47 | * @return 48 | */ 49 | public String deleteStock(String ProductID) { 50 | if (stockDao.deleteStock(ProductID) == 1) 51 | return "deleted"; 52 | else 53 | return "record cannot be deleted"; 54 | } 55 | 56 | /** 57 | * This method is used to insert the given sales obj into the TBL_SALEStable using 58 | * SalesDao class insertSales method if the below conditions are successful. 59 | * 1. Sales obj should not be null else return “Object not valid for insertion” 60 | * 2. ProductID should be present in the TBL_STOCK table else return “Unknown 61 | * Product for sales” 62 | * 3. Products current QuatityOnHand value should be more than the QuantitySold value 63 | * else return “Not enough stock on hand for sales” 64 | * 4. SalesDate should be current date or earlier date and not future date, 65 | * else return “Invalid date” 66 | * 5. If above 4 are valid generate SalesId using SalesDao class generateSalesId method 67 | * and store the same in the SalesID member of the given Sales Object Call the 68 | * insertSalesmethod of SalesDao and insert the record. 69 | * If insertion is successful call the updateStock method of the StockDao and 70 | * update the sold quantity to the stock. 71 | * On successful completion of both the transaction return “Sales Completed”else “Error”. 72 | * @param sales 73 | * @return 74 | */ 75 | public String insertSales(Sales sales) { 76 | if (sales == null) 77 | return "Object not valid for insertion"; 78 | 79 | if (stockDao.getStock(sales.getProductID()) == null) 80 | return "Unknown Product for sales"; 81 | 82 | if (stockDao.getStock(sales.getProductID()).getQuantityOnHand() < sales.getQuantitySold()) 83 | return "Not enough stock on hand for sales"; 84 | 85 | if (sales.getSalesDate().before(new Date())) 86 | return "Invalid date"; 87 | 88 | String salesID = salesDao.generateSalesID(sales.getSalesDate()); 89 | sales.setSalesID(salesID); 90 | 91 | if (salesDao.insertSales(sales) == 1) { 92 | if (stockDao.updateStock(sales.getProductID(), sales.getQuantitySold()) == 1) 93 | return "sales record inserted successfully"; 94 | else 95 | return "Error"; 96 | } else { 97 | return "Error"; 98 | } 99 | } 100 | 101 | /** 102 | * This method calls the getSalesReport of the SalesDao and returns the ArrayList 103 | * @return 104 | */ 105 | public ArrayList getSalesReport() { 106 | return salesDao.getSalesReport(); 107 | } 108 | 109 | } -------------------------------------------------------------------------------- /src/com/wipro/sales/dao/StockDao.java: -------------------------------------------------------------------------------- 1 | package com.wipro.sales.dao; 2 | 3 | import java.sql.Connection; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | import com.wipro.sales.bean.Stock; 9 | import com.wipro.sales.util.DBUtil; 10 | 11 | public class StockDao { 12 | 13 | /** 14 | * This method is used to insert the given stock obj into TBL_STOCK table 15 | * @param sales 16 | */ 17 | public int insertStock(Stock stock) { 18 | Connection conn = null; 19 | PreparedStatement pstmt = null; 20 | String sql = "INSERT INTO TBL_STOCK VALUES(?, ?, ?, ?, ?)"; 21 | 22 | try { 23 | conn = DBUtil.getDBConnection(); 24 | pstmt = conn.prepareStatement(sql); 25 | pstmt.setString(1, stock.getProductID()); 26 | pstmt.setString(2, stock.getProductName()); 27 | pstmt.setInt(3, stock.getQuantityOnHand()); 28 | pstmt.setDouble(4, stock.getProductUnitPrice()); 29 | pstmt.setInt(5, stock.getReorderLevel()); 30 | 31 | if (pstmt.executeUpdate() == 1) return 1; 32 | else return 0; 33 | } catch (SQLException e) { 34 | e.printStackTrace(); 35 | return 0; 36 | } 37 | } 38 | 39 | /** 40 | * This method is used to generate StockID using the First 2 letters of the given 41 | * product name concatenated with the SEQ_PRODUCT_ID sequence generated number. 42 | * @param productName 43 | * @return 44 | */ 45 | public String generateProductID(String productName) { 46 | Connection conn = null; 47 | PreparedStatement pstmt = null; 48 | String sql = "SELECT SEQ_PRODUCT_ID.NEXTVAL FROM DUAL"; 49 | 50 | int SEQ_PRODUCT_ID = 0; 51 | String out = ""; 52 | 53 | try { 54 | conn = DBUtil.getDBConnection(); 55 | pstmt = conn.prepareStatement(sql); 56 | ResultSet rs = pstmt.executeQuery(); 57 | 58 | rs.next(); 59 | SEQ_PRODUCT_ID = rs.getInt(1); 60 | 61 | out += productName.substring(0, 2); 62 | out += SEQ_PRODUCT_ID; 63 | 64 | return out; 65 | } catch (SQLException e) { 66 | e.printStackTrace(); 67 | return null; 68 | } 69 | } 70 | 71 | /** 72 | * This method is used to update the Stock table by subtracting the current 73 | * Quantity_On_Hand by the given soldQty of the given productID. 74 | * @param productID 75 | * @param soldQty 76 | * @return 77 | */ 78 | public int updateStock(String productID, int soldQty) { 79 | Connection conn = null; 80 | PreparedStatement pstmt = null; 81 | String sql = "UPDATE TBL_STOCK SET Quantity_On_Hand = Quantity_On_Hand - ?" 82 | + "WHERE Product_ID = ?"; 83 | 84 | try { 85 | conn = DBUtil.getDBConnection(); 86 | pstmt = conn.prepareStatement(sql); 87 | pstmt.setInt(1, soldQty); 88 | pstmt.setString(2, productID); 89 | 90 | if (pstmt.executeUpdate() == 1) return 1; 91 | else return 0; 92 | } catch (SQLException e) { 93 | e.printStackTrace(); 94 | return 0; 95 | } 96 | } 97 | 98 | /** 99 | * This method is used to fetch a specific record details from the Stock table 100 | * for the given productID, store the information to a Stock bean object the 101 | * return the same. 102 | * @param productID 103 | * @return 104 | */ 105 | public Stock getStock(String productID) { 106 | Connection conn = null; 107 | PreparedStatement pstmt = null; 108 | String sql = "SELECT * FROM TBL_STOCK WHERE Product_ID = ?"; 109 | 110 | try { 111 | conn = DBUtil.getDBConnection(); 112 | pstmt = conn.prepareStatement(sql); 113 | pstmt.setString(1, productID); 114 | 115 | ResultSet rs = pstmt.executeQuery(); 116 | 117 | rs.next(); 118 | Stock stock = new Stock(); 119 | stock.setProductID(rs.getString(1)); 120 | stock.setProductName(rs.getString(2)); 121 | stock.setQuantityOnHand(rs.getInt(3)); 122 | stock.setProductUnitPrice(rs.getDouble(4)); 123 | stock.setReorderLevel(rs.getInt(5)); 124 | 125 | return stock; 126 | } catch (SQLException e) { 127 | e.printStackTrace(); 128 | return null; 129 | } 130 | } 131 | 132 | /** 133 | * This method is used to delete the stock record of the given ProductID 134 | * @param productID 135 | * @return 136 | */ 137 | public int deleteStock(String productID) { 138 | Connection conn = null; 139 | PreparedStatement pstmt = null; 140 | String sql = "DELETE TBL_STOCK WHERE Product_ID = ?"; 141 | 142 | try { 143 | conn = DBUtil.getDBConnection(); 144 | pstmt = conn.prepareStatement(sql); 145 | pstmt.setString(1, productID); 146 | 147 | if (pstmt.executeUpdate() == 1) return 1; 148 | else return 0; 149 | } catch (SQLException e) { 150 | e.printStackTrace(); 151 | return 0; 152 | } 153 | } 154 | } 155 | --------------------------------------------------------------------------------