Skip to content
Snippets Groups Projects
Commit 124061da authored by Tahir, Ali (UG - Comp Sci & Elec Eng)'s avatar Tahir, Ali (UG - Comp Sci & Elec Eng)
Browse files

Deletion of files

parent 074be281
No related branches found
No related tags found
No related merge requests found
package com.com1028.cw.mt00969;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ TestReq1.class, TestReq2.class, TestReq3.class })
public class AllTests {
}
package com.com1028.cw.mt00969;
import static org.junit.Assert.assertEquals;
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;
import org.junit.Test;
/**
* @author alita
*
*/
public class TestReq1 {
private Connection connect;
private Statement statement;
CustomerDAO customerDAO = null;
public List<Customer> getCustomers() {
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", "");
}
// 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);
}
ArrayList<Customer> customers = new ArrayList<Customer>();
try {
// This is our prepared query, that selects everything from book
// table
String query = "select * from customers where salesRepEmployeeNumber is null ORDER BY customerNumber asc";
// 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);
} finally {
// IMPORTANT : now close the connection to the database - DO NOT FORGET TO DO
// THIS!
if (customerDAO != null) {
customerDAO.closeConnection();
}
}
return customers;
}
public String showAllCustomers() {
List<Customer> customers = getCustomers();
int counter = 0;
String out = "";
for (Customer c : customers) {
// 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";
}
@Test
public void testreq1() {
CustomerDAOImpl customer = new CustomerDAOImpl();
assertEquals(showAllCustomers(), customer.showAllCustomers());
}
}
package com.com1028.cw.mt00969;
import static org.junit.Assert.assertEquals;
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.List;
import java.util.Locale;
import org.junit.Test;
/**
* @author alita
*
*/
public class TestReq2 {
private Connection connect;
private Statement statement;
PaymentDAO paymentDAO = null;
public List<Payment> getPayments() {
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", "");
}
// 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);
}
ArrayList<Payment> payments = new ArrayList<Payment>();
try {
// This is our prepared query, that selects everything from book
// table
String query = "select customers.customerNumber, customerName, payments.amount from customers inner join payments on payments.customerNumber=customers.customerNumber where payments.amount > 100000 order by amount desc";
// 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");
int amount = results.getInt("amount");
payments.add(new Payment(customerNumber, amount));
}
} catch (SQLException e) {
System.out.println("SQLException happened while retrieving records- abort programmme");
throw new RuntimeException(e);
} finally {
// IMPORTANT : now close the connection to the database - DO NOT FORGET TO DO
// THIS!
if (paymentDAO != null) {
paymentDAO.closeConnection();
}
}
return payments;
}
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";
}
@Test
public void testreq2() {
PaymentDAO payment = new PaymentDAOImpl();
assertEquals(showAllPayments(), payment.showAllPayments());
}
}
package com.com1028.cw.mt00969;
import static org.junit.Assert.assertEquals;
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.Date;
import java.util.List;
import org.junit.Test;
/**
* @author alita
*
*/
public class TestReq3 {
private Connection connect;
private Statement statement;
OrderDAO orderDAO = null;
public List<Order> getOrders() {
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", "");
}
// 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);
}
ArrayList<Order> orders = new ArrayList<Order>();
try {
String query = "select orderNumber, orderDate, shippedDate, customers.customerNumber, customers.customerName, avg(TIMESTAMPDIFF(DAY,orderDate,shippedDate)) as'AverageDeliveryTimeInDays' from orders inner join customers on customers.customerNumber = orders.customerNumber where shippedDate is not null group by orders.customerNumber order by AverageDeliveryTimeInDays desc, customerNumber asc";
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");
double dateDiff = results.getDouble("AverageDeliveryTimeInDays");
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);
} finally {
// IMPORTANT : now close the connection to the database - DO NOT FORGET TO DO
// THIS!
if (orderDAO != null) {
orderDAO.closeConnection();
}
}
return orders;
}
public String showAllOrders() {
List<Order> orders = getOrders();
CustomerDAOImpl customer = new CustomerDAOImpl();
List<Customer> customers = customer.getCustomers();
int counter = 0;
String out = "";
String pattern = "###.00";
DecimalFormat df = new DecimalFormat(pattern);
for (Order o : orders) {
for (Customer c : customers) {
if (o.getCustomerNumber() == c.getCustomerNumber()) {
out += "CustomerName: " + c.getCustomerName() + "\n" + "CustomerNumber: " + o.getCustomerNumber()
+ "\n" + "AverageDeliveryTime: " + df.format(o.getDateDiff()) + " days\n" + "\n";
counter++;
}
}
}
return out + counter + " records\n";
}
@Test
public void testreq2() {
OrderDAO order = new OrderDAOImpl();
assertEquals(showAllOrders(), order.showAllOrders());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment