Skip to content

Commit 51dca28

Browse files
AlanAlan
authored andcommitted
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
1 parent 9c43827 commit 51dca28

File tree

5 files changed

+254
-249
lines changed

5 files changed

+254
-249
lines changed

dao/src/main/java/com/iluwatar/dao/Customer.java

Lines changed: 50 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,70 +6,63 @@
66
*
77
*/
88
public class Customer {
9-
10-
private int id;
11-
private String firstName;
12-
private String lastName;
139

14-
public Customer(final int id, final String firstName, final String lastName) {
15-
this.id = id;
16-
this.firstName = firstName;
17-
this.lastName = lastName;
18-
}
10+
private int id;
11+
private String firstName;
12+
private String lastName;
1913

20-
public int getId() {
21-
return id;
22-
}
14+
public Customer(final int id, final String firstName, final String lastName) {
15+
this.id = id;
16+
this.firstName = firstName;
17+
this.lastName = lastName;
18+
}
2319

24-
public void setId(final int id) {
25-
this.id = id;
26-
}
20+
public int getId() {
21+
return id;
22+
}
2723

28-
public String getFirstName() {
29-
return firstName;
30-
}
24+
public void setId(final int id) {
25+
this.id = id;
26+
}
3127

32-
public void setFirstName(final String firstName) {
33-
this.firstName = firstName;
34-
}
28+
public String getFirstName() {
29+
return firstName;
30+
}
3531

36-
public String getLastName() {
37-
return lastName;
38-
}
32+
public void setFirstName(final String firstName) {
33+
this.firstName = firstName;
34+
}
3935

40-
public void setLastName(final String lastName) {
41-
this.lastName = lastName;
42-
}
36+
public String getLastName() {
37+
return lastName;
38+
}
4339

44-
@Override
45-
public String toString() {
46-
return "Customer{" +
47-
"id=" + getId() +
48-
", firstName='" + getFirstName() + '\'' +
49-
", lastName='" + getLastName() + '\'' +
50-
'}';
51-
}
40+
public void setLastName(final String lastName) {
41+
this.lastName = lastName;
42+
}
5243

53-
/**
54-
* Checks if two objects are the same.
55-
*
56-
* @return true if the two objects are Customer objects and have the same id value, false otherwise.
57-
*/
58-
@Override
59-
public boolean equals(final Object o) {
60-
boolean isEqual = false;
61-
final Customer customer = (Customer) o;
62-
if (getId() == customer.getId()) {
63-
isEqual = true;
64-
}
65-
return isEqual;
66-
}
67-
68-
@Override
69-
public int hashCode() {
70-
int result = getId();
71-
id += getFirstName().hashCode();
72-
id += getLastName().hashCode();
73-
return result;
44+
@Override
45+
public String toString() {
46+
return "Customer{" + "id=" + getId() + ", firstName='" + getFirstName() + '\'' + ", lastName='"
47+
+ getLastName() + '\'' + '}';
48+
}
49+
50+
@Override
51+
public boolean equals(final Object o) {
52+
boolean isEqual = false;
53+
if (this == o) {
54+
isEqual = true;
55+
} else if (o != null && (getClass() == o.getClass())) {
56+
final Customer customer = (Customer) o;
57+
if (getId() == customer.getId())
58+
isEqual = true;
7459
}
75-
}
60+
return isEqual;
61+
}
62+
63+
@Override
64+
public int hashCode() {
65+
int result = getId();
66+
return result;
67+
}
68+
}

dao/src/main/java/com/iluwatar/dao/CustomerDao.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
*
99
*/
1010
public interface CustomerDao {
11-
12-
List<Customer> getAllCustomers();
13-
Customer getCustomerById(final int id);
14-
void addCustomer(final Customer customer);
15-
void updateCustomer(final Customer customer);
16-
void deleteCustomer(final Customer customer);
17-
}
11+
12+
List<Customer> getAllCustomers();
13+
14+
Customer getCustomerById(int id);
15+
16+
void addCustomer(Customer customer);
17+
18+
void updateCustomer(Customer customer);
19+
20+
void deleteCustomer(Customer customer);
21+
}

dao/src/main/java/com/iluwatar/dao/CustomerDaoImpl.java

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,59 @@
44

55
/**
66
*
7-
* The data access object (DAO) is an object that provides an abstract interface to some type of database or other persistence mechanism.
8-
* By mapping application calls to the persistence layer, DAO provide some specific data operations without exposing details of the database.
9-
* This isolation supports the Single responsibility principle. It separates what data accesses the application needs, in terms of
10-
* domain-specific objects and data types (the public interface of the DAO), from how these needs can be satisfied with a specific DBMS,
11-
* database schema, etc.
7+
* The data access object (DAO) is an object that provides an abstract interface to some type of
8+
* database or other persistence mechanism. By mapping application calls to the persistence layer,
9+
* DAO provide some specific data operations without exposing details of the database. This
10+
* isolation supports the Single responsibility principle. It separates what data accesses the
11+
* application needs, in terms of domain-specific objects and data types (the public interface of
12+
* the DAO), from how these needs can be satisfied with a specific DBMS, database schema, etc.
1213
*
1314
*/
1415
public class CustomerDaoImpl implements CustomerDao {
1516

16-
// Represents the DB structure for our example so we don't have to managed it ourselves
17-
// Note: Normally this would be in the form of an actual database and not part of the Dao Impl.
18-
private List<Customer> customers;
19-
20-
public CustomerDaoImpl(final List<Customer> customers) {
21-
this.customers = customers;
22-
}
23-
24-
@Override
25-
public List<Customer> getAllCustomers() {
26-
return customers;
17+
// Represents the DB structure for our example so we don't have to managed it ourselves
18+
// Note: Normally this would be in the form of an actual database and not part of the Dao Impl.
19+
private List<Customer> customers;
20+
21+
public CustomerDaoImpl(final List<Customer> customers) {
22+
this.customers = customers;
23+
}
24+
25+
@Override
26+
public List<Customer> getAllCustomers() {
27+
return customers;
28+
}
29+
30+
@Override
31+
public Customer getCustomerById(final int id) {
32+
Customer customer = null;
33+
for (final Customer cus : getAllCustomers()) {
34+
if (cus.getId() == id) {
35+
customer = cus;
36+
break;
37+
}
2738
}
39+
return customer;
40+
}
2841

29-
@Override
30-
public Customer getCustomerById(final int id) {
31-
Customer customer = null;
32-
for (final Customer cus : getAllCustomers()) {
33-
if (cus.getId() == id) {
34-
customer = cus;
35-
break;
36-
}
37-
}
38-
return customer;
39-
}
40-
41-
@Override
42-
public void addCustomer(final Customer customer) {
43-
customers.add(customer);
42+
@Override
43+
public void addCustomer(final Customer customer) {
44+
if (getCustomerById(customer.getId()) == null) {
45+
customers.add(customer);
4446
}
47+
}
4548

4649

47-
@Override
48-
public void updateCustomer(final Customer customer) {
49-
if (getAllCustomers().contains(customer)) {
50-
final int index = getAllCustomers().indexOf(customer);
51-
getAllCustomers().set(index, customer);
52-
}
50+
@Override
51+
public void updateCustomer(final Customer customer) {
52+
if (getAllCustomers().contains(customer)) {
53+
final int index = getAllCustomers().indexOf(customer);
54+
getAllCustomers().set(index, customer);
5355
}
56+
}
5457

55-
@Override
56-
public void deleteCustomer(final Customer customer) {
57-
getAllCustomers().remove(customer);
58-
}
59-
}
58+
@Override
59+
public void deleteCustomer(final Customer customer) {
60+
getAllCustomers().remove(customer);
61+
}
62+
}

0 commit comments

Comments
 (0)