Skip to content

Commit

Permalink
feat: add equals and hashcode methods for product related models
Browse files Browse the repository at this point in the history
  • Loading branch information
akadir committed Sep 12, 2020
1 parent d3a5afb commit c46ffe8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/io/github/akadir/casestudy/product/Category.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.akadir.casestudy.product;

import java.util.Objects;

public class Category {
private String title;
private Category parentCategory;
Expand Down Expand Up @@ -28,4 +30,18 @@ public Category getParentCategory() {
public void setParentCategory(Category parentCategory) {
this.parentCategory = parentCategory;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Category category = (Category) o;
return title.equals(category.title) &&
Objects.equals(parentCategory, category.parentCategory);
}

@Override
public int hashCode() {
return Objects.hash(title, parentCategory);
}
}
17 changes: 17 additions & 0 deletions src/main/java/io/github/akadir/casestudy/product/Product.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.akadir.casestudy.product;

import java.util.Objects;

public class Product {
private String title;
private double price;
Expand Down Expand Up @@ -34,4 +36,19 @@ public Category getCategory() {
public void setCategory(Category category) {
this.category = category;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Product product = (Product) o;
return Double.compare(product.price, price) == 0 &&
title.equals(product.title) &&
Objects.equals(category, product.category);
}

@Override
public int hashCode() {
return Objects.hash(title, price, category);
}
}

0 comments on commit c46ffe8

Please sign in to comment.