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

71 lines
1.8 KiB
Java

package me.teridax.jcash.banking;
import java.util.*;
import java.util.regex.Pattern;
@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 pattern = Pattern.compile("[\\w\\d-_]+");
var matcher = pattern.matcher(maybeBlz);
if (matcher.find())
return matcher.group();
throw new IllegalArgumentException("not a valid BLZ: " + maybeBlz);
}
public Optional<Profile> getAccount(int iban) {
for (var owner : this.accounts.entrySet()) {
for (var account : owner.getValue()) {
var tmp = account.getIban();
if (tmp == iban) {
return Optional.of(new Profile(owner.getKey(), this, account, getAccountsOfOwner(owner.getKey())));
}
}
}
return Optional.empty();
}
}