implemented `hashcode`, `equals` for data classes

This commit is contained in:
Sven Vogel 2023-06-22 11:02:47 +02:00
parent ec23a2b32f
commit 4b3f2bb59f
6 changed files with 41 additions and 39 deletions

View File

@ -1,25 +1,20 @@
package me.teridax.jcash.banking; package me.teridax.jcash.banking;
import java.util.Objects;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public abstract class Account { public abstract class Account {
private int iban; private final int iban;
private int pin; private final int pin;
private double balance; private final double balance;
private final Owner owner; public Account(int iban, int pin) {
public Account(int iban, int pin, Owner owner) {
this.owner = owner;
this.iban = iban; this.iban = iban;
this.pin = pin; this.pin = pin;
this.balance = 0.0d; this.balance = 0.0d;
} }
public Owner getOwner() {
return owner;
}
public int getIban() { public int getIban() {
return iban; return iban;
} }
@ -31,4 +26,17 @@ public abstract class Account {
public double getBalance() { public double getBalance() {
return balance; 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;
}
} }

View File

@ -1,26 +1,20 @@
package me.teridax.jcash.banking; package me.teridax.jcash.banking;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Objects;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public final class Bank { public final class Bank {
private String name; private final String name;
private int blz; private final String blz;
private List<Account> accounts; private final Map<Owner, Set<Account>> accounts;
private Bank(int blz, String name) { private Bank(String blz, String name) {
this.blz = blz; this.blz = blz;
this.name = name; this.name = name;
this.accounts = new ArrayList<>(); this.accounts = new HashMap<>();
} }
public Account[] getAccounts() { public String getBlz() {
return accounts.toArray(new Account[0]);
}
public int getBlz() {
return blz; return blz;
} }
@ -31,20 +25,18 @@ public final class Bank {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj instanceof Bank) { if (obj instanceof Bank) {
return blz == ((Bank) obj).blz; return Objects.equals(blz, ((Bank) obj).blz);
} }
return false; return false;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(blz, name); return Objects.hash(blz);
} }
public Account[] getAccountsOfOwner(Owner owner) { public Account[] getAccountsOfOwner(Owner owner) {
return accounts.stream() return accounts.get(owner).toArray(Account[]::new);
.filter(account -> account.getOwner().equals(owner))
.toArray(Account[]::new);
} }
private static String validateBlz(String maybeBlz) { private static String validateBlz(String maybeBlz) {

View File

@ -4,14 +4,16 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
public final class BankingManagementSystem { public final class BankingManagementSystem {
private List<Bank> banks; private final Set<Bank> banks;
private BankingManagementSystem(List<Bank> banks) { private BankingManagementSystem() {
this.banks = banks; this.banks = new HashSet<>();
} }
public static BankingManagementSystem loadFromCsv(File file) { public static BankingManagementSystem loadFromCsv(File file) {

View File

@ -2,10 +2,10 @@ package me.teridax.jcash.banking;
public class Girokonto extends Account { public class Girokonto extends Account {
private double overdraft; private final double overdraft;
public Girokonto(int iban, int pin, double overdraft, Owner owner) { public Girokonto(int iban, int pin, double overdraft) {
super(iban, pin, owner); super(iban, pin);
this.overdraft = overdraft; this.overdraft = overdraft;
} }

View File

@ -48,7 +48,7 @@ public final class Owner {
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(uid, familyName, name, zip, city, street); return Objects.hash(uid);
} }
@Override @Override

View File

@ -3,10 +3,10 @@ package me.teridax.jcash.banking;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class SavingsAccount extends Account { public class SavingsAccount extends Account {
private double interest; private final double interest;
public SavingsAccount(int iban, int pin, double interest, Owner owner) { public SavingsAccount(int iban, int pin, double interest) {
super(iban, pin, owner); super(iban, pin);
this.interest = interest; this.interest = interest;
} }