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;
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;
}
}

View File

@ -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<Account> accounts;
private final String name;
private final String blz;
private final Map<Owner, Set<Account>> 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) {

View File

@ -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<Bank> banks;
private final Set<Bank> banks;
private BankingManagementSystem(List<Bank> banks) {
this.banks = banks;
private BankingManagementSystem() {
this.banks = new HashSet<>();
}
public static BankingManagementSystem loadFromCsv(File file) {

View File

@ -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;
}

View File

@ -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

View File

@ -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;
}