From 4b3f2bb59ff8559dcee399a46de39c8314c7a32a Mon Sep 17 00:00:00 2001 From: teridax Date: Thu, 22 Jun 2023 11:02:47 +0200 Subject: [PATCH] implemented `hashcode`, `equals` for data classes --- src/me/teridax/jcash/banking/Account.java | 30 ++++++++++++------- src/me/teridax/jcash/banking/Bank.java | 28 +++++++---------- .../banking/BankingManagementSystem.java | 8 +++-- src/me/teridax/jcash/banking/Girokonto.java | 6 ++-- src/me/teridax/jcash/banking/Owner.java | 2 +- .../teridax/jcash/banking/SavingsAccount.java | 6 ++-- 6 files changed, 41 insertions(+), 39 deletions(-) diff --git a/src/me/teridax/jcash/banking/Account.java b/src/me/teridax/jcash/banking/Account.java index 36b534a..d3055f3 100644 --- a/src/me/teridax/jcash/banking/Account.java +++ b/src/me/teridax/jcash/banking/Account.java @@ -1,25 +1,20 @@ package me.teridax.jcash.banking; +import java.util.Objects; + @SuppressWarnings("unused") public abstract class Account { - private int iban; - private int pin; - private double balance; + private final int iban; + private final int pin; + private final double balance; - private final Owner owner; - - public Account(int iban, int pin, Owner owner) { - this.owner = owner; + public Account(int iban, int pin) { this.iban = iban; this.pin = pin; this.balance = 0.0d; } - public Owner getOwner() { - return owner; - } - public int getIban() { return iban; } @@ -31,4 +26,17 @@ public abstract class Account { public double getBalance() { return balance; } + + @Override + public int hashCode() { + return Objects.hash(iban); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof Account) + return iban == ((Account)obj).iban; + + return false; + } } diff --git a/src/me/teridax/jcash/banking/Bank.java b/src/me/teridax/jcash/banking/Bank.java index 026b5d5..d4f8564 100644 --- a/src/me/teridax/jcash/banking/Bank.java +++ b/src/me/teridax/jcash/banking/Bank.java @@ -1,26 +1,20 @@ package me.teridax.jcash.banking; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; +import java.util.*; @SuppressWarnings("unused") public final class Bank { - private String name; - private int blz; - private List accounts; + private final String name; + private final String blz; + private final Map> accounts; - private Bank(int blz, String name) { + private Bank(String blz, String name) { this.blz = blz; this.name = name; - this.accounts = new ArrayList<>(); + this.accounts = new HashMap<>(); } - public Account[] getAccounts() { - return accounts.toArray(new Account[0]); - } - - public int getBlz() { + public String getBlz() { return blz; } @@ -31,20 +25,18 @@ public final class Bank { @Override public boolean equals(Object obj) { if (obj instanceof Bank) { - return blz == ((Bank) obj).blz; + return Objects.equals(blz, ((Bank) obj).blz); } return false; } @Override public int hashCode() { - return Objects.hash(blz, name); + return Objects.hash(blz); } public Account[] getAccountsOfOwner(Owner owner) { - return accounts.stream() - .filter(account -> account.getOwner().equals(owner)) - .toArray(Account[]::new); + return accounts.get(owner).toArray(Account[]::new); } private static String validateBlz(String maybeBlz) { diff --git a/src/me/teridax/jcash/banking/BankingManagementSystem.java b/src/me/teridax/jcash/banking/BankingManagementSystem.java index 1cd09ae..49c6dfb 100644 --- a/src/me/teridax/jcash/banking/BankingManagementSystem.java +++ b/src/me/teridax/jcash/banking/BankingManagementSystem.java @@ -4,14 +4,16 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; public final class BankingManagementSystem { - private List banks; + private final Set banks; - private BankingManagementSystem(List banks) { - this.banks = banks; + private BankingManagementSystem() { + this.banks = new HashSet<>(); } public static BankingManagementSystem loadFromCsv(File file) { diff --git a/src/me/teridax/jcash/banking/Girokonto.java b/src/me/teridax/jcash/banking/Girokonto.java index 68ba9f5..5fbea62 100644 --- a/src/me/teridax/jcash/banking/Girokonto.java +++ b/src/me/teridax/jcash/banking/Girokonto.java @@ -2,10 +2,10 @@ package me.teridax.jcash.banking; public class Girokonto extends Account { - private double overdraft; + private final double overdraft; - public Girokonto(int iban, int pin, double overdraft, Owner owner) { - super(iban, pin, owner); + public Girokonto(int iban, int pin, double overdraft) { + super(iban, pin); this.overdraft = overdraft; } diff --git a/src/me/teridax/jcash/banking/Owner.java b/src/me/teridax/jcash/banking/Owner.java index 1f337e8..dcc06d4 100644 --- a/src/me/teridax/jcash/banking/Owner.java +++ b/src/me/teridax/jcash/banking/Owner.java @@ -48,7 +48,7 @@ public final class Owner { @Override public int hashCode() { - return Objects.hash(uid, familyName, name, zip, city, street); + return Objects.hash(uid); } @Override diff --git a/src/me/teridax/jcash/banking/SavingsAccount.java b/src/me/teridax/jcash/banking/SavingsAccount.java index 7e36c58..0e0570c 100644 --- a/src/me/teridax/jcash/banking/SavingsAccount.java +++ b/src/me/teridax/jcash/banking/SavingsAccount.java @@ -3,10 +3,10 @@ package me.teridax.jcash.banking; @SuppressWarnings("unused") public class SavingsAccount extends Account { - private double interest; + private final double interest; - public SavingsAccount(int iban, int pin, double interest, Owner owner) { - super(iban, pin, owner); + public SavingsAccount(int iban, int pin, double interest) { + super(iban, pin); this.interest = interest; }