Inclusion of log4j dependency rather than relying on
System.out.println() statements. Added unit tests for doa module.
This commit is contained in:
@@ -3,6 +3,8 @@ package com.iluwatar.dao;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
* With the DAO pattern, we can use various method calls to retrieve/add/delete/update data without directly
|
||||
@@ -11,43 +13,38 @@ import java.util.List;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
private static Logger LOGGER = Logger.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers());
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
System.out.println("customerDao.getCusterById(2): " + customerDao.getCusterById(2));
|
||||
|
||||
Customer customer = new Customer(4, "Dan", "Danson");
|
||||
public static void main(final String[] args) {
|
||||
final CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers());
|
||||
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
LOGGER.info("customerDao.getCusterById(2): " + customerDao.getCustomerById(2));
|
||||
final Customer customer = new Customer(4, "Dan", "Danson");
|
||||
customerDao.addCustomer(customer);
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
|
||||
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
customer.setFirstName("Daniel");
|
||||
customer.setLastName("Danielson");
|
||||
customerDao.updateCustomer(customer);
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
|
||||
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
customerDao.deleteCustomer(customer);
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
LOGGER.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate customers
|
||||
* @return list of customers
|
||||
* Generate customers.
|
||||
*
|
||||
* @return list of customers.
|
||||
*/
|
||||
public static List<Customer> generateSampleCustomers() {
|
||||
Customer customer1 = new Customer(1, "Adam", "Adamson");
|
||||
Customer customer2 = new Customer(2, "Bob", "Bobson");
|
||||
Customer customer3 = new Customer(3, "Carl", "Carlson");
|
||||
|
||||
List<Customer> customers = new ArrayList<Customer>();
|
||||
final Customer customer1 = new Customer(1, "Adam", "Adamson");
|
||||
final Customer customer2 = new Customer(2, "Bob", "Bobson");
|
||||
final Customer customer3 = new Customer(3, "Carl", "Carlson");
|
||||
final List<Customer> customers = new ArrayList<Customer>();
|
||||
customers.add(customer1);
|
||||
customers.add(customer2);
|
||||
customers.add(customer3);
|
||||
|
||||
@@ -11,7 +11,7 @@ public class Customer {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Customer(int id, String firstName, String lastName) {
|
||||
public Customer(final int id, final String firstName, final String lastName) {
|
||||
this.id = id;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
@@ -21,7 +21,7 @@ public class Customer {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ public class Customer {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
public void setFirstName(final String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
@@ -37,34 +37,39 @@ public class Customer {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
public void setLastName(final String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer{" +
|
||||
"id=" + id +
|
||||
", firstName='" + firstName + '\'' +
|
||||
", lastName='" + lastName + '\'' +
|
||||
"id=" + getId() +
|
||||
", firstName='" + getFirstName() + '\'' +
|
||||
", lastName='" + getLastName() + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if two objects are the same.
|
||||
*
|
||||
* @return true if the two objects are Customer objects and have the same id value, false otherwise.
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Customer customer = (Customer) o;
|
||||
|
||||
if (id != customer.id) return false;
|
||||
|
||||
return true;
|
||||
public boolean equals(final Object o) {
|
||||
boolean isEqual = false;
|
||||
final Customer customer = (Customer) o;
|
||||
if (getId() == customer.getId()) {
|
||||
isEqual = true;
|
||||
}
|
||||
return isEqual;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = id;
|
||||
int result = getId();
|
||||
id += getFirstName().hashCode();
|
||||
id += getLastName().hashCode();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,9 @@ import java.util.List;
|
||||
*/
|
||||
public interface CustomerDao {
|
||||
|
||||
public List<Customer> getAllCustomers();
|
||||
public Customer getCusterById(int id);
|
||||
public void addCustomer(Customer customer);
|
||||
public void updateCustomer(Customer customer);
|
||||
public void deleteCustomer(Customer customer);
|
||||
List<Customer> getAllCustomers();
|
||||
Customer getCustomerById(final int id);
|
||||
void addCustomer(final Customer customer);
|
||||
void updateCustomer(final Customer customer);
|
||||
void deleteCustomer(final Customer customer);
|
||||
}
|
||||
@@ -17,7 +17,7 @@ public class CustomerDaoImpl implements CustomerDao {
|
||||
// Note: Normally this would be in the form of an actual database and not part of the Dao Impl.
|
||||
private List<Customer> customers;
|
||||
|
||||
public CustomerDaoImpl(List<Customer> customers) {
|
||||
public CustomerDaoImpl(final List<Customer> customers) {
|
||||
this.customers = customers;
|
||||
}
|
||||
|
||||
@@ -27,31 +27,33 @@ public class CustomerDaoImpl implements CustomerDao {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Customer getCusterById(int id) {
|
||||
for (int i = 0; i < customers.size(); i++) {
|
||||
if (customers.get(i).getId() == id) {
|
||||
return customers.get(i);
|
||||
public Customer getCustomerById(final int id) {
|
||||
Customer customer = null;
|
||||
for (final Customer cus : getAllCustomers()) {
|
||||
if (cus.getId() == id) {
|
||||
customer = cus;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// No customer found
|
||||
return null;
|
||||
return customer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCustomer(Customer customer) {
|
||||
public void addCustomer(final Customer customer) {
|
||||
customers.add(customer);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void updateCustomer(Customer customer) {
|
||||
if (customers.contains(customer)) {
|
||||
customers.set(customers.indexOf(customer), customer);
|
||||
public void updateCustomer(final Customer customer) {
|
||||
if (getAllCustomers().contains(customer)) {
|
||||
final int index = getAllCustomers().indexOf(customer);
|
||||
getAllCustomers().set(index, customer);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteCustomer(Customer customer) {
|
||||
customers.remove(customer);
|
||||
public void deleteCustomer(final Customer customer) {
|
||||
getAllCustomers().remove(customer);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user