Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package BankPkg;
- import java.time.LocalDate;
- import java.util.Arrays;
- import BankAccounts.Account;
- import BankAccounts.Business;
- import BankAccounts.LoanableAccount;
- import BankAccounts.Regular;
- import BankAccounts.Soldier;
- import BankAccounts.Student;
- import BankAccounts.Teenage;
- import BankClients.Client;
- import BankExceptions.BankException;
- import BankExceptions.BankIdException;
- import BankInterfaces.Bankable;
- public class Bank implements Bankable {
- protected Client[] myClients;
- protected Account[] myAccounts;
- // default constructor
- public Bank() {
- myClients = new Client[0];
- myAccounts = new Account[0];
- }
- private int getClientIndex(int id) {
- for (int i = 0; i < myClients.length; i += 1) {
- if (myClients[i].getId() == id) {
- return i;
- }
- }
- return -1;
- }
- private int getClientIndex(Client aClient) {
- for (int i = 0; i < myClients.length; i += 1) {
- if (myClients[i].equals(aClient)) {
- return i;
- }
- }
- return -1;
- }
- private int getAccountIndex(int accountId) {
- for (int i = 0; i < myAccounts.length; i += 1) {
- if (myAccounts[i].getAccountId() == accountId) {
- return i;
- }
- }
- return -1;
- }
- @Override
- public boolean addClient(Client aClient) {
- try {
- // if exist with same details
- if (this.getClientIndex(aClient) != -1) {
- return true;
- }
- // if exist id with different details
- if (this.getClientIndex(aClient.getId()) != -1) {
- throw new BankIdException(getClient(aClient.getId()), aClient);
- }
- // add new client to list
- myClients = (Client[]) BankUtils.addToArray(myClients, new Client(
- aClient));
- return true;
- } catch (BankIdException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return false;
- }
- @Override
- public boolean addClient(String clientName, int clientId,
- LocalDate birthDate) {
- try {
- Client newClient = new Client(clientName, clientId, birthDate);
- return addClient(newClient);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return false;
- }
- @Override
- public boolean UpdateClientName(int clientId, String clientName) {
- for (Client item : myClients) {
- if (item.getId() == clientId) {
- try {
- item.setName(clientName);
- return true;
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- return false;
- }
- @Override
- public boolean RemoveClient(int clientId) {
- try {
- if (getClientAccounts(clientId).length > 0)
- throw new BankException("Error removing active client, id="
- + clientId);
- int clientIndex = getClientIndex(clientId);
- if (clientIndex != -1) {
- for (int i = clientIndex; i < myClients.length - 1; i += 1) {
- myClients[i] = myClients[i + 1];
- }
- myClients = Arrays.copyOf(myClients, myClients.length - 1);
- }
- return true;
- } catch (BankException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return false;
- }
- @Override
- public Client getClient(int clientId) {
- for (Client item : myClients) {
- if (item.getId() == clientId) {
- return new Client(item);
- }
- }
- return null;
- }
- @Override
- public Client[] getClients(String clientName) {
- Client[] resArray = new Client[0];
- for (Client item : myClients) {
- if (item.getName().contains(clientName)) {
- resArray = (Client[]) BankUtils.addToArray(resArray,
- new Client(item));
- }
- }
- return resArray;
- }
- @Override
- public Client[] getClients(LocalDate birthDate) {
- Client[] resArray = new Client[0];
- for (Client item : myClients) {
- if (item.getBirthDate().equals(birthDate)) {
- resArray = (Client[]) BankUtils.addToArray(resArray,
- new Client(item));
- }
- }
- return resArray;
- }
- @Override
- // to be deleted
- public Client[] getClientsAtAge(LocalDate atDate, int minAge, int maxAge) {
- Client[] resArray = new Client[0];
- for (Client item : myClients) {
- if (BankUtils.isBetween(item.getAge(atDate), minAge, maxAge)) {
- resArray = (Client[]) BankUtils.addToArray(resArray,
- new Client(item));
- }
- }
- return resArray;
- }
- @Override
- public int addAccount(int ownerId, AccountType type) {
- try {
- int ownerIndex = this.getClientIndex(ownerId);
- // error adding client
- if (ownerIndex == -1) {
- throw new BankException(
- "Faild to create account! Invalid client id ("
- + ownerId + ")");
- }
- Account newAccount;
- if (type == AccountType.regular) {
- newAccount = new Regular(myClients[ownerIndex]);
- } else if (type == AccountType.teenage) {
- newAccount = new Teenage(myClients[ownerIndex]);
- } else if (type == AccountType.business) {
- newAccount = new Business(myClients[ownerIndex]);
- } else if (type == AccountType.student) {
- newAccount = new Student(myClients[ownerIndex]);
- } else if (type == AccountType.soldier) {
- newAccount = new Soldier(myClients[ownerIndex]);
- } else {
- newAccount = null;
- }
- myAccounts = (Account[]) BankUtils.addToArray(myAccounts,
- newAccount);
- return newAccount.getAccountId();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return -1;
- }
- @Override
- public boolean removeAccount(int accountId) {
- try {
- int accountIndex = this.getAccountIndex(accountId);
- if (accountIndex == -1) {
- return true;
- }
- if (myAccounts[accountIndex].canCloseAccount()) {
- int ownerId = myAccounts[accountIndex].getOwner().getId();
- for (int i = accountIndex; i < myAccounts.length - 1; i += 1) {
- myAccounts[i] = myAccounts[i + 1];
- }
- myAccounts = Arrays.copyOf(myAccounts, myAccounts.length - 1);
- if (this.getClientAccounts(ownerId).length == 0) {
- RemoveClient(ownerId);
- }
- return true;
- } else {
- throw new BankException(
- "Account cann't be removed!. accountId = " + accountId);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return this.getAccountIndex(accountId) == -1;
- }
- @Override
- public Account getAccount(int accountId) {
- int accountIndex = getAccountIndex(accountId);
- if (accountIndex == -1) {
- return null;
- }
- return Account.copyOf(myAccounts[accountIndex]);
- }
- @Override
- public boolean setAccountFee(int accountId, double customFee) {
- int accountIndex = getAccountIndex(accountId);
- if (accountIndex == -1) {
- return false;
- }
- return myAccounts[accountIndex].setAccountFee(customFee);
- }
- @Override
- public boolean setAccountFee(int accountId) {
- int accountIndex = getAccountIndex(accountId);
- if (accountIndex == -1) {
- return false;
- }
- return myAccounts[accountIndex].setAccountFee(-1);
- }
- @Override
- public boolean setAccountCredit(int accountId) {
- int accountIndex = getAccountIndex(accountId);
- if (accountIndex == -1) {
- return false;
- }
- return myAccounts[accountIndex].setAccountCredit();
- }
- @Override
- public boolean setAccountCredit(int accountId, int customCredit) {
- int accountIndex = getAccountIndex(accountId);
- if (accountIndex == -1) {
- return false;
- }
- return myAccounts[accountIndex].setAccountCredit(customCredit);
- }
- public boolean withdraw(int accountId, double amount) {
- int accountIndex = getAccountIndex(accountId);
- if (accountIndex == -1) {
- return false;
- }
- return myAccounts[accountIndex].withDraw(amount);
- }
- public boolean deposit(int accountId, double amount) {
- int accountIndex = getAccountIndex(accountId);
- if (accountIndex == -1) {
- return false;
- }
- myAccounts[accountIndex].deposit(amount);
- return true;
- }
- public Account[] getAccounts() {
- Account[] resArray = new Account[0];
- for (Account item : myAccounts) {
- resArray = (Account[]) BankUtils.addToArray(resArray,
- Account.copyOf(item));
- }
- return resArray;
- }
- public Account[] getClientAccounts(int clientId) {
- Account[] resArray = new Account[0];
- for (Account item : myAccounts) {
- if (item.getOwner().getId() == clientId) {
- resArray = (Account[]) BankUtils.addToArray(resArray,
- Account.copyOf(item));
- }
- }
- return resArray;
- }
- @Override
- public Account[] getAccountsByType(AccountType type) {
- Account[] resArray = new Account[0];
- for (Account item : myAccounts) {
- if (item.getAccountType().equals(type)) {
- resArray = (Account[]) BankUtils.addToArray(resArray,
- Account.copyOf(item));
- }
- }
- return resArray;
- }
- @Override
- public Account[] getInvalidAgeAccounts(LocalDate atDate) {
- Account[] resArray = new Account[0];
- for (Account item : myAccounts) {
- int ageAtDate = (int) BankUtils.getAge(item.getOwner()
- .getBirthDate(), atDate);
- if (!BankUtils.isBetween(ageAtDate, item.getAccountType()
- .getMinAge(), item.getAccountType().getMaxAge())) {
- resArray = (Account[]) BankUtils.addToArray(resArray,
- Account.copyOf(item));
- }
- }
- return resArray;
- }
- @Override
- public Account[] getAccountsByBalance(int minCredit, int maxCredit) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public Account[] getAccountsByBalance(double minUseage, int maxUseage) {
- Account[] resArray = new Account[0];
- for (Account item : myAccounts) {
- double balance = item.getAccountBalance();
- double credit = item.getAccountCredit();
- double usage = balance >= 0 ? 0 : balance / credit;
- if (BankUtils.isBetween(usage, minUseage, maxUseage)) {
- resArray = (Account[]) BankUtils.addToArray(resArray,
- Account.copyOf(item));
- }
- }
- return resArray;
- }
- public boolean giveLoan(int accountId, double amount, int payments) {
- int accountIndex = getAccountIndex(accountId);
- if (accountIndex == -1) {
- return false;
- }
- if (myAccounts[accountIndex] instanceof LoanableAccount) {
- return ((LoanableAccount) myAccounts[accountIndex]).giveLoan(
- amount, payments);
- }
- return false;
- }
- public boolean loanPaymentReturn(int accountId) {
- LoanableAccount acc = (LoanableAccount) myAccounts[getAccountIndex(accountId)];
- return acc.loanPaymentReturn();
- }
- @Override
- public LoanableAccount[] getLoans() {
- LoanableAccount[] resArray = new LoanableAccount[0];
- for (Account item : myAccounts) {
- if (item instanceof LoanableAccount) {
- if (((LoanableAccount) item).getLoanAmount() != 0) {
- resArray = (LoanableAccount[]) BankUtils.addToArray(
- resArray, Account.copyOf(item));
- }
- }
- }
- return resArray;
- }
- @Override
- public boolean setLoanFee(double loanFee) {
- return BankUtils.setLoanFee(loanFee);
- }
- @Override
- public String toString() {
- String res = "Accounts List (" + myAccounts.length + "):\n";
- for (Account item : myAccounts) {
- res += item + "\n";
- }
- res += "\nClients List (" + myClients.length + "):\n";
- for (Client item : myClients) {
- res += item + "\n";
- }
- return res;
- }
- }
Add Comment
Please, Sign In to add comment