JCash/src/me/teridax/jcash/banking/Bank.java

71 lines
1.8 KiB
Java

package me.teridax.jcash.banking;
import java.util.*;
@SuppressWarnings("unused")
public final class Bank {
private final String name;
private final String blz;
private final Map<Owner, Set<Account>> accounts;
Bank(String blz, String name) {
this.blz = blz;
this.name = name;
this.accounts = new HashMap<>();
}
public String getBlz() {
return blz;
}
public String getName() {
return name;
}
public void addAccount(Owner owner, Account account) {
var set = this.accounts.getOrDefault(owner, new HashSet<>());
set.add(account);
this.accounts.put(owner, set);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Bank) {
return Objects.equals(blz, ((Bank) obj).blz);
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(blz);
}
public Account[] getAccountsOfOwner(Owner owner) {
return accounts.get(owner).toArray(Account[]::new);
}
public static String validateBlz(String maybeBlz) {
var builder = new StringBuilder();
maybeBlz.trim().chars().filter(ch -> Character.isDigit(ch) || Character.isLetter(ch)).forEach(builder::append);
var blz = builder.toString();
if (blz.isEmpty()) {
throw new IllegalArgumentException("Bank Blz must only contain letters and digits");
}
return blz;
}
public Optional<Account> getAccount(int iban) {
for (var owner : this.accounts.entrySet()) {
for (var account : owner.getValue()) {
if (account.getIban() == iban) {
return Optional.of(account);
}
}
}
return Optional.empty();
}
}