Updated unit .equals() and .hashCode() methods
Formatted code using this formatter: https://github.com/google/styleguide/blob/gh-pages/eclipse-java-google-style.xml Removed argument final declaration on interface Updated addCustomer logic for cases where the added customer already exists
This commit is contained in:
@@ -6,70 +6,63 @@ package com.iluwatar.dao;
|
||||
*
|
||||
*/
|
||||
public class Customer {
|
||||
|
||||
private int id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Customer(final int id, final String firstName, final String lastName) {
|
||||
this.id = id;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
private int id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
public Customer(final int id, final String firstName, final String lastName) {
|
||||
this.id = id;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setFirstName(final String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
public void setFirstName(final String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public void setLastName(final String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer{" +
|
||||
"id=" + getId() +
|
||||
", firstName='" + getFirstName() + '\'' +
|
||||
", lastName='" + getLastName() + '\'' +
|
||||
'}';
|
||||
}
|
||||
public void setLastName(final String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(final Object o) {
|
||||
boolean isEqual = false;
|
||||
final Customer customer = (Customer) o;
|
||||
if (getId() == customer.getId()) {
|
||||
isEqual = true;
|
||||
}
|
||||
return isEqual;
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer{" + "id=" + getId() + ", firstName='" + getFirstName() + '\'' + ", lastName='"
|
||||
+ getLastName() + '\'' + '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object o) {
|
||||
boolean isEqual = false;
|
||||
if (this == o) {
|
||||
isEqual = true;
|
||||
} else if (o != null && (getClass() == o.getClass())) {
|
||||
final Customer customer = (Customer) o;
|
||||
if (getId() == customer.getId())
|
||||
isEqual = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = getId();
|
||||
id += getFirstName().hashCode();
|
||||
id += getLastName().hashCode();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return isEqual;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = getId();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,10 +8,14 @@ import java.util.List;
|
||||
*
|
||||
*/
|
||||
public interface CustomerDao {
|
||||
|
||||
List<Customer> getAllCustomers();
|
||||
Customer getCustomerById(final int id);
|
||||
void addCustomer(final Customer customer);
|
||||
void updateCustomer(final Customer customer);
|
||||
void deleteCustomer(final Customer customer);
|
||||
}
|
||||
|
||||
List<Customer> getAllCustomers();
|
||||
|
||||
Customer getCustomerById(int id);
|
||||
|
||||
void addCustomer(Customer customer);
|
||||
|
||||
void updateCustomer(Customer customer);
|
||||
|
||||
void deleteCustomer(Customer customer);
|
||||
}
|
||||
|
||||
@@ -4,56 +4,59 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* The data access object (DAO) is an object that provides an abstract interface to some type of database or other persistence mechanism.
|
||||
* By mapping application calls to the persistence layer, DAO provide some specific data operations without exposing details of the database.
|
||||
* This isolation supports the Single responsibility principle. It separates what data accesses the application needs, in terms of
|
||||
* domain-specific objects and data types (the public interface of the DAO), from how these needs can be satisfied with a specific DBMS,
|
||||
* database schema, etc.
|
||||
* The data access object (DAO) is an object that provides an abstract interface to some type of
|
||||
* database or other persistence mechanism. By mapping application calls to the persistence layer,
|
||||
* DAO provide some specific data operations without exposing details of the database. This
|
||||
* isolation supports the Single responsibility principle. It separates what data accesses the
|
||||
* application needs, in terms of domain-specific objects and data types (the public interface of
|
||||
* the DAO), from how these needs can be satisfied with a specific DBMS, database schema, etc.
|
||||
*
|
||||
*/
|
||||
public class CustomerDaoImpl implements CustomerDao {
|
||||
|
||||
// Represents the DB structure for our example so we don't have to managed it ourselves
|
||||
// Note: Normally this would be in the form of an actual database and not part of the Dao Impl.
|
||||
private List<Customer> customers;
|
||||
// Represents the DB structure for our example so we don't have to managed it ourselves
|
||||
// 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(final List<Customer> customers) {
|
||||
this.customers = customers;
|
||||
public CustomerDaoImpl(final List<Customer> customers) {
|
||||
this.customers = customers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Customer> getAllCustomers() {
|
||||
return customers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Customer getCustomerById(final int id) {
|
||||
Customer customer = null;
|
||||
for (final Customer cus : getAllCustomers()) {
|
||||
if (cus.getId() == id) {
|
||||
customer = cus;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return customer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Customer> getAllCustomers() {
|
||||
return customers;
|
||||
@Override
|
||||
public void addCustomer(final Customer customer) {
|
||||
if (getCustomerById(customer.getId()) == null) {
|
||||
customers.add(customer);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Customer getCustomerById(final int id) {
|
||||
Customer customer = null;
|
||||
for (final Customer cus : getAllCustomers()) {
|
||||
if (cus.getId() == id) {
|
||||
customer = cus;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return customer;
|
||||
|
||||
@Override
|
||||
public void updateCustomer(final Customer customer) {
|
||||
if (getAllCustomers().contains(customer)) {
|
||||
final int index = getAllCustomers().indexOf(customer);
|
||||
getAllCustomers().set(index, customer);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCustomer(final Customer customer) {
|
||||
customers.add(customer);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void updateCustomer(final Customer customer) {
|
||||
if (getAllCustomers().contains(customer)) {
|
||||
final int index = getAllCustomers().indexOf(customer);
|
||||
getAllCustomers().set(index, customer);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteCustomer(final Customer customer) {
|
||||
getAllCustomers().remove(customer);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void deleteCustomer(final Customer customer) {
|
||||
getAllCustomers().remove(customer);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user