diff --git a/myapp/src/main/java/com/com1028/cw/mt00969/BaseQuery.java b/myapp/src/main/java/com/com1028/cw/mt00969/BaseQuery.java index 3ad914cf89a2bfca85574992506f84b9620e7282..2aa78232e209d657010c10efc32bd3a2f63e76b4 100644 --- a/myapp/src/main/java/com/com1028/cw/mt00969/BaseQuery.java +++ b/myapp/src/main/java/com/com1028/cw/mt00969/BaseQuery.java @@ -188,8 +188,7 @@ public class BaseQuery { } public static String showAllOrders() { - CustomerDAOImpl customer = new CustomerDAOImpl(); - List<Customer> customers = customer.getCustomers(); + int counter = 0; String pattern = "###.00"; String out = ""; diff --git a/myapp/src/main/java/com/com1028/cw/mt00969/Class Diagram.ucls b/myapp/src/main/java/com/com1028/cw/mt00969/Class Diagram.ucls index dc240b693f962b149299e9f1c5e39f2bac99cdfc..fe7355a8421ee6eec551e56914dabaa3149edf1e 100644 --- a/myapp/src/main/java/com/com1028/cw/mt00969/Class Diagram.ucls +++ b/myapp/src/main/java/com/com1028/cw/mt00969/Class Diagram.ucls @@ -10,34 +10,7 @@ <operations public="true" package="true" protected="true" private="true" static="true"/> </display> </class> - <class id="2" language="java" name="com.com1028.cw.mt00969.CustomerDAOImpl" project="myapp" - file="/myapp/src/main/java/com/com1028/cw/mt00969/CustomerDAOImpl.java" binary="false" corner="BOTTOM_RIGHT"> - <position height="-1" width="-1" x="188" y="533"/> - <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" - sort-features="false" accessors="true" visibility="true"> - <attributes public="true" package="true" protected="true" private="true" static="true"/> - <operations public="true" package="true" protected="true" private="true" static="true"/> - </display> - </class> - <class id="3" language="java" name="com.com1028.cw.mt00969.DAOFactory" project="myapp" - file="/myapp/src/main/java/com/com1028/cw/mt00969/DAOFactory.java" binary="false" corner="BOTTOM_RIGHT"> - <position height="-1" width="-1" x="845" y="166"/> - <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" - sort-features="false" accessors="true" visibility="true"> - <attributes public="true" package="true" protected="true" private="true" static="true"/> - <operations public="true" package="true" protected="true" private="true" static="true"/> - </display> - </class> - <interface id="4" language="java" name="com.com1028.cw.mt00969.CustomerDAO" project="myapp" - file="/myapp/src/main/java/com/com1028/cw/mt00969/CustomerDAO.java" binary="false" corner="BOTTOM_RIGHT"> - <position height="-1" width="-1" x="194" y="214"/> - <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" - sort-features="true" accessors="true" visibility="true"> - <attributes public="true" package="true" protected="true" private="true" static="true"/> - <operations public="true" package="true" protected="true" private="true" static="true"/> - </display> - </interface> - <class id="5" language="java" name="com.com1028.cw.mt00969.Order" project="myapp" + <class id="2" language="java" name="com.com1028.cw.mt00969.Order" project="myapp" file="/myapp/src/main/java/com/com1028/cw/mt00969/Order.java" binary="false" corner="BOTTOM_LEFT"> <position height="-1" width="-1" x="1329" y="969"/> <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" @@ -46,34 +19,7 @@ <operations public="true" package="true" protected="true" private="true" static="true"/> </display> </class> - <interface id="6" language="java" name="com.com1028.cw.mt00969.OrderDAO" project="myapp" - file="/myapp/src/main/java/com/com1028/cw/mt00969/OrderDAO.java" binary="false" corner="BOTTOM_RIGHT"> - <position height="-1" width="-1" x="1443" y="305"/> - <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" - sort-features="false" accessors="true" visibility="true"> - <attributes public="true" package="true" protected="true" private="true" static="true"/> - <operations public="true" package="true" protected="true" private="true" static="true"/> - </display> - </interface> - <class id="7" language="java" name="com.com1028.cw.mt00969.OrderDAOImpl" project="myapp" - file="/myapp/src/main/java/com/com1028/cw/mt00969/OrderDAOImpl.java" binary="false" corner="BOTTOM_RIGHT"> - <position height="-1" width="-1" x="1435" y="608"/> - <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" - sort-features="false" accessors="true" visibility="true"> - <attributes public="true" package="true" protected="true" private="true" static="true"/> - <operations public="true" package="true" protected="true" private="true" static="true"/> - </display> - </class> - <class id="8" language="java" name="com.com1028.cw.mt00969.PaymentDAOImpl" project="myapp" - file="/myapp/src/main/java/com/com1028/cw/mt00969/PaymentDAOImpl.java" binary="false" corner="BOTTOM_RIGHT"> - <position height="-1" width="-1" x="862" y="635"/> - <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" - sort-features="false" accessors="true" visibility="true"> - <attributes public="true" package="true" protected="true" private="true" static="true"/> - <operations public="true" package="true" protected="true" private="true" static="true"/> - </display> - </class> - <class id="9" language="java" name="com.com1028.cw.mt00969.Payment" project="myapp" + <class id="3" language="java" name="com.com1028.cw.mt00969.Payment" project="myapp" file="/myapp/src/main/java/com/com1028/cw/mt00969/Payment.java" binary="false" corner="BOTTOM_RIGHT"> <position height="-1" width="-1" x="896" y="860"/> <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" @@ -82,100 +28,31 @@ <operations public="true" package="true" protected="true" private="true" static="true"/> </display> </class> - <interface id="10" language="java" name="com.com1028.cw.mt00969.PaymentDAO" project="myapp" - file="/myapp/src/main/java/com/com1028/cw/mt00969/PaymentDAO.java" binary="false" corner="BOTTOM_RIGHT"> - <position height="-1" width="-1" x="845" y="404"/> - <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" - sort-features="false" accessors="true" visibility="true"> - <attributes public="true" package="true" protected="true" private="true" static="true"/> - <operations public="true" package="true" protected="true" private="true" static="true"/> - </display> - </interface> - <realization id="11"> - <end type="SOURCE" refId="8"/> - <end type="TARGET" refId="10"/> - </realization> - <association id="12"> - <end type="SOURCE" refId="3" navigable="false" variant="ASSOCIATION"> - <attribute id="13" name="orderDAO"/> - <multiplicity id="14" minimum="0" maximum="1"/> - </end> - <end type="TARGET" refId="6" navigable="true" variant="ASSOCIATION"/> - <display labels="true" multiplicity="true"/> - </association> - <dependency id="15"> - <bendpoint x="1168" y="543"/> - <end type="SOURCE" refId="6"/> - <end type="TARGET" refId="5"/> - </dependency> - <association id="16"> - <end type="SOURCE" refId="3" navigable="false" variant="ASSOCIATION"> - <attribute id="17" name="customerDAO"> - <position height="0" width="0" x="-332" y="-272"/> - </attribute> - <multiplicity id="18" minimum="0" maximum="1"> - <position height="0" width="0" x="-332" y="-272"/> - </multiplicity> - </end> - <end type="TARGET" refId="4" navigable="true" variant="ASSOCIATION"/> - <display labels="true" multiplicity="true"/> - </association> - <dependency id="19"> - <end type="SOURCE" refId="8"/> - <end type="TARGET" refId="9"/> - </dependency> - <association id="20"> - <end type="SOURCE" refId="3" navigable="false" variant="ASSOCIATION"> - <attribute id="21" name="paymentDAO"/> - <multiplicity id="22" minimum="0" maximum="1"/> - </end> - <end type="TARGET" refId="10" navigable="true" variant="ASSOCIATION"/> - <display labels="true" multiplicity="true"/> - </association> - <realization id="23"> - <end type="SOURCE" refId="2"/> - <end type="TARGET" refId="4"/> - </realization> - <realization id="24"> - <end type="SOURCE" refId="7"/> - <end type="TARGET" refId="6"/> - </realization> - <dependency id="25"> - <bendpoint x="356" y="522"/> - <end type="SOURCE" refId="4"/> - <end type="TARGET" refId="1"/> - </dependency> - <dependency id="26"> - <bendpoint x="682" y="548"/> - <bendpoint x="696" y="714"/> - <end type="SOURCE" refId="10"/> - <end type="TARGET" refId="9"/> - </dependency> - <association id="27"> + <association id="4"> <bendpoint x="865" y="1106"/> <end type="SOURCE" refId="1" navigable="false" variant="ASSOCIATION"> - <attribute id="28" name="order"/> - <multiplicity id="29" minimum="0" maximum="1"/> + <attribute id="5" name="order"> + <position height="0" width="0" x="0" y="0"/> + </attribute> + <multiplicity id="6" minimum="0" maximum="1"> + <position height="0" width="0" x="0" y="0"/> + </multiplicity> </end> - <end type="TARGET" refId="5" navigable="true" variant="ASSOCIATION"/> + <end type="TARGET" refId="2" navigable="true" variant="ASSOCIATION"/> <display labels="true" multiplicity="true"/> </association> - <association id="30"> + <association id="7"> <end type="SOURCE" refId="1" navigable="false" variant="ASSOCIATION"> - <attribute id="31" name="payment"/> - <multiplicity id="32" minimum="0" maximum="1"/> + <attribute id="8" name="payment"> + <position height="0" width="0" x="0" y="0"/> + </attribute> + <multiplicity id="9" minimum="0" maximum="1"> + <position height="0" width="0" x="0" y="0"/> + </multiplicity> </end> - <end type="TARGET" refId="9" navigable="true" variant="ASSOCIATION"/> + <end type="TARGET" refId="3" navigable="true" variant="ASSOCIATION"/> <display labels="true" multiplicity="true"/> </association> - <dependency id="33"> - <end type="SOURCE" refId="7"/> - <end type="TARGET" refId="5"/> - </dependency> - <dependency id="34"> - <end type="SOURCE" refId="2"/> - <end type="TARGET" refId="1"/> - </dependency> <classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" sort-features="false" accessors="true" visibility="true"> <attributes public="true" package="true" protected="true" private="true" static="true"/> diff --git a/myapp/src/main/java/com/com1028/cw/mt00969/CustomerDAO.java b/myapp/src/main/java/com/com1028/cw/mt00969/CustomerDAO.java deleted file mode 100644 index 3cf115e1b5f5fceed07f32bdbe85d4b8729cd60e..0000000000000000000000000000000000000000 --- a/myapp/src/main/java/com/com1028/cw/mt00969/CustomerDAO.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.com1028.cw.mt00969; - -import java.util.List; - -public interface CustomerDAO { - - public List<Customer> getCustomers(); - - public String showAllCustomers(); - - public void openConnection(); - - public void closeConnection(); - -} diff --git a/myapp/src/main/java/com/com1028/cw/mt00969/CustomerDAOImpl.java b/myapp/src/main/java/com/com1028/cw/mt00969/CustomerDAOImpl.java deleted file mode 100644 index 66b9de43e3543f9b07ba2cf31af2f54b874729cc..0000000000000000000000000000000000000000 --- a/myapp/src/main/java/com/com1028/cw/mt00969/CustomerDAOImpl.java +++ /dev/null @@ -1,128 +0,0 @@ -package com.com1028.cw.mt00969; - -// select customerName,city,salesRepEmployeeNumber from customers where salesRepEmployeeNumber is null ORDER BY customerNumber asc; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.List; - -public class CustomerDAOImpl implements CustomerDAO { - - private Connection connect; - private Statement statement; - - public CustomerDAOImpl() { - super(); - this.connect = null; - this.statement = null; - this.openConnection(); - - } - - @Override - public void openConnection() { - - try { - // recreate the connection if needed - if (this.connect == null || this.connect.isClosed()) { - // change the DB Path - // - this.connect = DriverManager.getConnection( - "jdbc:mysql://localhost:3306/classicmodels?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", - "root", "1234"); - - } - - // recreate the statement if needed - if (this.statement == null || this.statement.isClosed()) { - this.statement = this.connect.createStatement(); - } - - } catch (SQLException e) { - System.out.println("ERRRO - Failed to create a connection to the database"); - throw new RuntimeException(e); - } - - } - - @Override - public void closeConnection() { - try { - - if (this.statement != null) { - this.statement.close(); - } - if (this.connect != null) { - this.connect.close(); - } - System.out.println("Closed the connection to the database\n"); - } catch (Exception e) { - System.out.print("ERROR-Failed to close the connection to the database"); - throw new RuntimeException(e); - } - - } - - @Override - public List<Customer> getCustomers() { - ArrayList<Customer> customers = new ArrayList<Customer>(); - try { - // This is our prepared query, that selects everything from customer - // table - String query = "select * from customers"; - - // Executes the query and stores the results. - ResultSet results = this.statement.executeQuery(query); - - while (results.next()) { - - /* - * Assign results from query to their own variable. We can reference columns by - * their name of index value e.g. 0 - */ - int customerNumber = results.getInt("customerNumber"); - String customerName = results.getString("customerName"); - String customerFirstName = results.getString("contactFirstName"); - String customerLastName = results.getString("contactLastName"); - String city = results.getString("city"); - int salesRepEmployeeNumber = results.getInt("salesRepEmployeeNumber"); - customers.add(new Customer(customerNumber, customerName, customerFirstName, customerLastName, city, - salesRepEmployeeNumber)); - - } - - } catch (SQLException e) { - System.out.println("SQLException happened while retrieving records- abort programmme"); - throw new RuntimeException(e); - } - return customers; - } - - @Override - public String showAllCustomers() { - - List<Customer> customers = getCustomers(); - int counter = 0; - - String out = ""; - - for (Customer c : customers) { - if (c.getSalesRepEmployeeNumber() == 0) { - - // Prints results to console - out += "\ncustomerName: " + c.getCustomerName() + "\n" + "contactFullName: " + c.getContactFirstName() - + c.getContactLastName() + "\n" + "City: " + c.getCity() + "\n"; - counter++; - } - - } - - return out + "\n" + counter + " records\n"; - - } - -} diff --git a/myapp/src/main/java/com/com1028/cw/mt00969/DAOFactory.java b/myapp/src/main/java/com/com1028/cw/mt00969/DAOFactory.java deleted file mode 100644 index cb31bd793bb5c1bbfdc634ae66727a2ca2e3559d..0000000000000000000000000000000000000000 --- a/myapp/src/main/java/com/com1028/cw/mt00969/DAOFactory.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.com1028.cw.mt00969; - -public class DAOFactory { - - private static final CustomerDAO customerDAO = new CustomerDAOImpl(); - - public static CustomerDAO getCustomerDAO() { - return DAOFactory.customerDAO; - } - - private static final PaymentDAO paymentDAO = new PaymentDAOImpl(); - - public static PaymentDAO getPaymentDAO() { - return DAOFactory.paymentDAO; - } - - private static final OrderDAO orderDAO = new OrderDAOImpl(); - - public static OrderDAO getOrderDAO() { - return DAOFactory.orderDAO; - } - -} diff --git a/myapp/src/main/java/com/com1028/cw/mt00969/Main.java b/myapp/src/main/java/com/com1028/cw/mt00969/Main.java deleted file mode 100644 index b6dff4b685a85effdd13e663b4a83762c67256a3..0000000000000000000000000000000000000000 --- a/myapp/src/main/java/com/com1028/cw/mt00969/Main.java +++ /dev/null @@ -1,133 +0,0 @@ -/** - * - */ -package com.com1028.cw.mt00969; - -/** - * @author alita - * - * pwd for db is 1234 - */ -public class Main { - -// public Main(String uname, String pwd) { -// super(uname, pwd); -// } - - /** - * @param args - * - */ - public static void main(String[] args) { - - CustomerDAO customerDAO = null; - - try { - customerDAO = DAOFactory.getCustomerDAO(); - - System.out.println( - "\n----- Report the name and city of customers who don't have sales representatives ----- "); - - System.out.println(customerDAO.showAllCustomers()); - - System.out.println("End of printing..."); - - } - - catch (Exception e) { - // catch any exceptions and log them - e.printStackTrace(); - } finally { - // IMPORTANT : now close the connection to the database - DO NOT FORGET TO DO - // THIS! - if (customerDAO != null) { - customerDAO.closeConnection(); - } - } - - PaymentDAO paymentDAO = null; - - try { - - paymentDAO = DAOFactory.getPaymentDAO(); - - System.out.println( - "----- Report those payments greater than $100,000. -----\n----- Sort the report so the customer who made the highest payment appears first. ----- \n"); - - System.out.println(paymentDAO.showAllPayments()); - - System.out.println("End of printing..."); - - } - - catch (Exception e) { - // catch any exceptions and log them - e.printStackTrace(); - } finally { - // IMPORTANT : now close the connection to the database - DO NOT FORGET TO DO - // THIS! - if (paymentDAO != null) { - paymentDAO.closeConnection(); - } - } - - OrderDAO orderDAO = null; - - try { - - orderDAO = DAOFactory.getOrderDAO(); - - System.out.println( - "----- Compute the average time between order date and ship date for each customer. -----\n----- Order it by the largest difference ----- \n"); - - System.out.println(orderDAO.showAllOrders()); - - System.out.println("End of printing..."); - - } - - catch (Exception e) { - // catch any exceptions and log them - e.printStackTrace(); - } finally { - // IMPORTANT : now close the connection to the database - DO NOT FORGET TO DO - // THIS! - if (orderDAO != null) { - orderDAO.closeConnection(); - } - } - - } -// Main obj = new Main("root", "1234"); -// try { -// ResultSet rs = obj.useTable("orders"); -// while (rs.next()) { -// String name = rs.getString("orderDate"); -// System.out.println(" "); -// String name1 = rs.getString("orderNumber"); -// System.out.println(name + " " + name1); -// } -// } catch (SQLException e) { -// e.printStackTrace(); -// } - -// try { -// ResultSet rs1 = obj.useTable1("customers"); -// System.out.println(rs1); -//// while (rs1.next()) { -//// String customerName = rs1.getString("customerName"); -//// String city = rs1.getString("city"); -//// String salesRepEmployeeNumber = rs1.getString("salesRepEmployeeNumber"); -//// System.out.println(customerName + "\t" + city + "\t" + salesRepEmployeeNumber); -//// // String name = rs.getString("orderDate"); -//// // System.out.println(" "); -//// // String name1 = rs.getString("orderNumber"); -//// // System.out.println(name + " " + name1); -//// } -// } catch (SQLException e) { -// e.printStackTrace(); -// } -// -// } - -} \ No newline at end of file diff --git a/myapp/src/main/java/com/com1028/cw/mt00969/OrderDAO.java b/myapp/src/main/java/com/com1028/cw/mt00969/OrderDAO.java deleted file mode 100644 index 90a45b9d4fefc1d3c8e7d0b7556710a9c1b7e2a6..0000000000000000000000000000000000000000 --- a/myapp/src/main/java/com/com1028/cw/mt00969/OrderDAO.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.com1028.cw.mt00969; - -import java.util.List; - -public interface OrderDAO { - - public List<Order> getOrders(); - - public String showAllOrders(); - - public void openConnection(); - - public void closeConnection(); - -} diff --git a/myapp/src/main/java/com/com1028/cw/mt00969/OrderDAOImpl.java b/myapp/src/main/java/com/com1028/cw/mt00969/OrderDAOImpl.java deleted file mode 100644 index c9cf5b95aeb39e7c214805eb2b78fd3c38ca2432..0000000000000000000000000000000000000000 --- a/myapp/src/main/java/com/com1028/cw/mt00969/OrderDAOImpl.java +++ /dev/null @@ -1,173 +0,0 @@ -package com.com1028.cw.mt00969; - -import static java.lang.Math.toIntExact; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.text.DecimalFormat; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.Date; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -/** - * @author alita - * - */ -public class OrderDAOImpl implements OrderDAO { - - private Connection connect; - private Statement statement; - - public OrderDAOImpl() { - super(); - this.connect = null; - this.statement = null; - this.openConnection(); - - } - - @Override - public void openConnection() { - try { - // recreate the connection if needed - if (this.connect == null || this.connect.isClosed()) { - // change the DB Path - // - this.connect = DriverManager.getConnection( - "jdbc:mysql://localhost:3306/classicmodels?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", - "root", "1234"); - - } - - // recreate the statement if needed - if (this.statement == null || this.statement.isClosed()) { - this.statement = this.connect.createStatement(); - } - - } catch (SQLException e) { - System.out.println("ERROR - Failed to create a connection to the database"); - throw new RuntimeException(e); - } - - } - - @Override - public void closeConnection() { - try { - - if (this.statement != null) { - this.statement.close(); - } - if (this.connect != null) { - this.connect.close(); - } - System.out.println("Closed the connection to the database\n"); - } catch (Exception e) { - System.out.print("ERROR-Failed to close the connection to the database"); - throw new RuntimeException(e); - } - - } - - @Override - public List<Order> getOrders() { - ArrayList<Order> orders = new ArrayList<Order>(); - try { - String query = "select * from orders"; - ResultSet results = this.statement.executeQuery(query); - - while (results.next()) { - - int orderNumber = results.getInt("orderNumber"); - int customerNumber = results.getInt("customerNumber"); - Date orderDate = results.getDate("orderDate"); - Date shippedDate = results.getDate("shippedDate"); - - if (shippedDate != null) { - int dateDiff = toIntExact( - (results.getDate("shippedDate").getTime() - results.getDate("orderDate").getTime()) - / 86400000); - orders.add(new Order(orderNumber, customerNumber, orderDate, shippedDate, dateDiff)); - } - - } - - } catch ( - - SQLException e) { - System.out.println("SQLException happened while retrieving records- abort programmme"); - throw new RuntimeException(e); - } - return orders; - } - - public static Map<Integer, Double> sortByValue(Map<Integer, Double> map) { - // Create a list from elements of HashMap - List<Map.Entry<Integer, Double>> list = new LinkedList<Map.Entry<Integer, Double>>(map.entrySet()); - - // Sort the list - Collections.sort(list, new Comparator<Map.Entry<Integer, Double>>() { - @Override - public int compare(Map.Entry<Integer, Double> o1, Map.Entry<Integer, Double> o2) { - return (o1.getKey()).compareTo(o2.getKey()); - } - }); - - Collections.sort(list, new Comparator<Map.Entry<Integer, Double>>() { - @Override - public int compare(Map.Entry<Integer, Double> o1, Map.Entry<Integer, Double> o2) { - return (o2.getValue()).compareTo(o1.getValue()); - } - }); - - // put data from sorted list to hashmap - HashMap<Integer, Double> temp = new LinkedHashMap<Integer, Double>(); - for (Map.Entry<Integer, Double> aa : list) { - temp.put(aa.getKey(), aa.getValue()); - } - return temp; - } - - @Override - public String showAllOrders() { - List<Order> orders = getOrders(); - CustomerDAOImpl customer = new CustomerDAOImpl(); - List<Customer> customers = customer.getCustomers(); - int counter = 0; - String pattern = "###.00"; - String out = ""; - DecimalFormat df = new DecimalFormat(pattern); - - Map<Integer, Double> map = orders.stream().collect( - Collectors.groupingBy(p -> p.getCustomerNumber(), Collectors.averagingDouble(p -> p.getDateDiff()))); - - Map<Integer, Double> map1 = sortByValue(map); - - for (Map.Entry<Integer, Double> e : map1.entrySet()) { - for (Customer c : customers) { - if (e.getKey() == c.getCustomerNumber()) { - - out += "CustomerName: " + c.getCustomerName() + "\n" + "CustomerNumber: " + e.getKey() + "\n" - + "AverageDeliveryTime: " + df.format(e.getValue()) + " days\n" + "\n"; - - counter++; - } - } - - } - - return out + counter + " records\n"; - - } - -} diff --git a/myapp/src/main/java/com/com1028/cw/mt00969/PaymentDAO.java b/myapp/src/main/java/com/com1028/cw/mt00969/PaymentDAO.java deleted file mode 100644 index 4216249ce1c1e4c66c504e1a862d3aff33f303c5..0000000000000000000000000000000000000000 --- a/myapp/src/main/java/com/com1028/cw/mt00969/PaymentDAO.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.com1028.cw.mt00969; - -import java.util.List; - -public interface PaymentDAO { - - public List<Payment> getPayments(); - - public String showAllPayments(); - - public void openConnection(); - - public void closeConnection(); - -} diff --git a/myapp/src/main/java/com/com1028/cw/mt00969/PaymentDAOImpl.java b/myapp/src/main/java/com/com1028/cw/mt00969/PaymentDAOImpl.java deleted file mode 100644 index b6e3aaeab0625b1f82cc9974e98f6385d59bfcfe..0000000000000000000000000000000000000000 --- a/myapp/src/main/java/com/com1028/cw/mt00969/PaymentDAOImpl.java +++ /dev/null @@ -1,182 +0,0 @@ -package com.com1028.cw.mt00969; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.text.NumberFormat; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import java.util.Locale; - -public class PaymentDAOImpl implements PaymentDAO { - - private Connection connect; - private Statement statement; - - public PaymentDAOImpl() { - super(); - this.connect = null; - this.statement = null; - this.openConnection(); - - } - - @Override - public void openConnection() { - - try { - // recreate the connection if needed - if (this.connect == null || this.connect.isClosed()) { - // change the DB Path - // - this.connect = DriverManager.getConnection( - "jdbc:mysql://localhost:3306/classicmodels?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", - "root", "1234"); - - } - - // recreate the statement if needed - if (this.statement == null || this.statement.isClosed()) { - this.statement = this.connect.createStatement(); - } - - } catch (SQLException e) { - System.out.println("ERROR - Failed to create a connection to the database"); - - throw new RuntimeException(e); - } - - } - - @Override - public void closeConnection() { - try { - - if (this.statement != null) { - this.statement.close(); - } - if (this.connect != null) { - this.connect.close(); - } - System.out.println("Closed the connection to the database\n"); - } catch (Exception e) { - System.out.print("ERROR-Failed to close the connection to the database"); - throw new RuntimeException(e); - } - - } - - @Override - public List<Payment> getPayments() { - ArrayList<Payment> payments = new ArrayList<Payment>(); - try { - // This is our prepared query, that selects everything from book - // table - // select customers.customerNumber, customerName, payments.amount as 'Amount($)' - // from customers inner join payments on - // payments.customerNumber=customers.customerNumber where payments.amount > - // 100000 order by amount desc - String query = "select * from payments"; - // Executes the query and stores the results. - ResultSet results = this.statement.executeQuery(query); - - while (results.next()) { - - /* - * Assign results from query to their own variable. We can reference columns by - * their name of index value e.g. 0 - */ - int customerNumber = results.getInt("customerNumber"); - double amount = results.getDouble("amount"); - payments.add(new Payment(customerNumber, amount)); - Collections.sort(payments, PaymentDAOImpl.OrderByAmount); - - } - - } catch (SQLException e) { - System.out.println("SQLException happened while retrieving records- abort programmme"); - throw new RuntimeException(e); - } - return payments; - } - - public static Comparator<Payment> OrderByAmount = new Comparator<Payment>() { - - @Override - public int compare(Payment p1, Payment p2) { - double amount1 = p1.getAmount(); - double amount2 = p2.getAmount(); - return (int) (amount2 - amount1); - } - - }; - -// public List<Customer> getCustomers1() { -// ArrayList<Customer> customers1 = new ArrayList<Customer>(); -// try { -// // This is our prepared query, that selects everything from book -// // table -// String query = "select * from customers"; -// -// // Executes the query and stores the results. -// ResultSet results = this.statement.executeQuery(query); -// -// while (results.next()) { -// -// /* -// * Assign results from query to their own variable. We can reference columns by -// * their name of index value e.g. 0 -// */ -// int customerNumber = results.getInt("customerNumber"); -// String customerName = results.getString("customerName"); -// String city = results.getString("city"); -// String salesRepEmployeeNumber = results.getString("salesRepEmployeeNumber"); -// customers1.add(new Customer(customerNumber, customerName, city, salesRepEmployeeNumber)); -// -// } -// -// } catch (SQLException e) { -// System.out.println("SQLException happened while retrieving records- abort programmme"); -// throw new RuntimeException(e); -// } -// return customers1; -// } - - @Override - public String showAllPayments() { - - List<Payment> payments = getPayments(); - CustomerDAOImpl c1 = new CustomerDAOImpl(); - List<Customer> customers = c1.getCustomers(); - int counter = 0; - String out = ""; - // Iterator<Payment> iter = payments.iterator(); - - // While there are still results... - // Payment tmpPayment; - // while (iter.hasNext()) { - // tmpPayment = iter.next(); - // Prints results to console - for (Payment p : payments) { - for (Customer c : customers) { - - if (p.getAmount() > 100000) { - if (c.getCustomerNumber() == p.getCustomerNumber()) { - - out += "customerNumber: " + p.getCustomerNumber() + "\n" + "customerName: " - + c.getCustomerName() + "\n" + "contactFullName: " + c.getContactFirstName() + " " - + c.getContactLastName() + "\n" + "amount: $" - + NumberFormat.getNumberInstance(Locale.US).format(p.getAmount()) + "\n\n"; - counter++; - } - } - } - } - return out + counter + " records\n"; - } - -}