mmayoub

Bank, Bank class

Aug 10th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.90 KB | None | 0 0
  1. package BankPkg;
  2.  
  3. import java.time.LocalDate;
  4. import java.util.Arrays;
  5.  
  6. import BankAccounts.Account;
  7. import BankAccounts.Business;
  8. import BankAccounts.LoanableAccount;
  9. import BankAccounts.Regular;
  10. import BankAccounts.Soldier;
  11. import BankAccounts.Student;
  12. import BankAccounts.Teenage;
  13. import BankClients.Client;
  14. import BankExceptions.BankException;
  15. import BankExceptions.BankIdException;
  16. import BankInterfaces.Bankable;
  17.  
  18. public class Bank implements Bankable {
  19.     protected Client[] myClients;
  20.     protected Account[] myAccounts;
  21.  
  22.     // default constructor
  23.     public Bank() {
  24.         myClients = new Client[0];
  25.         myAccounts = new Account[0];
  26.     }
  27.  
  28.     private int getClientIndex(int id) {
  29.         for (int i = 0; i < myClients.length; i += 1) {
  30.             if (myClients[i].getId() == id) {
  31.                 return i;
  32.             }
  33.         }
  34.         return -1;
  35.     }
  36.  
  37.     private int getClientIndex(Client aClient) {
  38.         for (int i = 0; i < myClients.length; i += 1) {
  39.             if (myClients[i].equals(aClient)) {
  40.                 return i;
  41.             }
  42.         }
  43.         return -1;
  44.     }
  45.  
  46.     private int getAccountIndex(int accountId) {
  47.         for (int i = 0; i < myAccounts.length; i += 1) {
  48.             if (myAccounts[i].getAccountId() == accountId) {
  49.                 return i;
  50.             }
  51.         }
  52.         return -1;
  53.     }
  54.  
  55.     @Override
  56.     public boolean addClient(Client aClient) {
  57.         try {
  58.             // if exist with same details
  59.             if (this.getClientIndex(aClient) != -1) {
  60.                 return true;
  61.             }
  62.  
  63.             // if exist id with different details
  64.             if (this.getClientIndex(aClient.getId()) != -1) {
  65.                 throw new BankIdException(getClient(aClient.getId()), aClient);
  66.             }
  67.  
  68.             // add new client to list
  69.             myClients = (Client[]) BankUtils.addToArray(myClients, new Client(
  70.                     aClient));
  71.             return true;
  72.         } catch (BankIdException e) {
  73.             // TODO Auto-generated catch block
  74.             e.printStackTrace();
  75.         }
  76.         return false;
  77.     }
  78.  
  79.     @Override
  80.     public boolean addClient(String clientName, int clientId,
  81.             LocalDate birthDate) {
  82.  
  83.         try {
  84.             Client newClient = new Client(clientName, clientId, birthDate);
  85.             return addClient(newClient);
  86.         } catch (Exception e) {
  87.             // TODO Auto-generated catch block
  88.             e.printStackTrace();
  89.         }
  90.  
  91.         return false;
  92.     }
  93.  
  94.     @Override
  95.     public boolean UpdateClientName(int clientId, String clientName) {
  96.         for (Client item : myClients) {
  97.             if (item.getId() == clientId) {
  98.                 try {
  99.                     item.setName(clientName);
  100.                     return true;
  101.                 } catch (Exception e) {
  102.                     // TODO Auto-generated catch block
  103.                     e.printStackTrace();
  104.                 }
  105.             }
  106.         }
  107.         return false;
  108.     }
  109.  
  110.     @Override
  111.     public boolean RemoveClient(int clientId) {
  112.         try {
  113.             if (getClientAccounts(clientId).length > 0)
  114.                 throw new BankException("Error removing active client, id="
  115.                         + clientId);
  116.  
  117.             int clientIndex = getClientIndex(clientId);
  118.  
  119.             if (clientIndex != -1) {
  120.                 for (int i = clientIndex; i < myClients.length - 1; i += 1) {
  121.                     myClients[i] = myClients[i + 1];
  122.                 }
  123.                 myClients = Arrays.copyOf(myClients, myClients.length - 1);
  124.             }
  125.             return true;
  126.         } catch (BankException e) {
  127.             // TODO Auto-generated catch block
  128.             e.printStackTrace();
  129.         }
  130.  
  131.         return false;
  132.     }
  133.  
  134.     @Override
  135.     public Client getClient(int clientId) {
  136.         for (Client item : myClients) {
  137.             if (item.getId() == clientId) {
  138.                 return new Client(item);
  139.             }
  140.         }
  141.         return null;
  142.     }
  143.  
  144.     @Override
  145.     public Client[] getClients(String clientName) {
  146.         Client[] resArray = new Client[0];
  147.  
  148.         for (Client item : myClients) {
  149.             if (item.getName().contains(clientName)) {
  150.                 resArray = (Client[]) BankUtils.addToArray(resArray,
  151.                         new Client(item));
  152.             }
  153.         }
  154.         return resArray;
  155.     }
  156.  
  157.     @Override
  158.     public Client[] getClients(LocalDate birthDate) {
  159.         Client[] resArray = new Client[0];
  160.  
  161.         for (Client item : myClients) {
  162.             if (item.getBirthDate().equals(birthDate)) {
  163.                 resArray = (Client[]) BankUtils.addToArray(resArray,
  164.                         new Client(item));
  165.             }
  166.         }
  167.         return resArray;
  168.     }
  169.  
  170.     @Override
  171.     // to be deleted
  172.     public Client[] getClientsAtAge(LocalDate atDate, int minAge, int maxAge) {
  173.         Client[] resArray = new Client[0];
  174.  
  175.         for (Client item : myClients) {
  176.             if (BankUtils.isBetween(item.getAge(atDate), minAge, maxAge)) {
  177.                 resArray = (Client[]) BankUtils.addToArray(resArray,
  178.                         new Client(item));
  179.             }
  180.         }
  181.         return resArray;
  182.     }
  183.  
  184.     @Override
  185.     public int addAccount(int ownerId, AccountType type) {
  186.         try {
  187.             int ownerIndex = this.getClientIndex(ownerId);
  188.             // error adding client
  189.             if (ownerIndex == -1) {
  190.                 throw new BankException(
  191.                         "Faild to create account! Invalid client id ("
  192.                                 + ownerId + ")");
  193.             }
  194.             Account newAccount;
  195.             if (type == AccountType.regular) {
  196.                 newAccount = new Regular(myClients[ownerIndex]);
  197.             } else if (type == AccountType.teenage) {
  198.                 newAccount = new Teenage(myClients[ownerIndex]);
  199.             } else if (type == AccountType.business) {
  200.                 newAccount = new Business(myClients[ownerIndex]);
  201.             } else if (type == AccountType.student) {
  202.                 newAccount = new Student(myClients[ownerIndex]);
  203.             } else if (type == AccountType.soldier) {
  204.                 newAccount = new Soldier(myClients[ownerIndex]);
  205.             } else {
  206.                 newAccount = null;
  207.             }
  208.  
  209.             myAccounts = (Account[]) BankUtils.addToArray(myAccounts,
  210.                     newAccount);
  211.             return newAccount.getAccountId();
  212.         } catch (Exception e) {
  213.             // TODO Auto-generated catch block
  214.             e.printStackTrace();
  215.         }
  216.         return -1;
  217.     }
  218.  
  219.     @Override
  220.     public boolean removeAccount(int accountId) {
  221.         try {
  222.             int accountIndex = this.getAccountIndex(accountId);
  223.  
  224.             if (accountIndex == -1) {
  225.                 return true;
  226.             }
  227.  
  228.             if (myAccounts[accountIndex].canCloseAccount()) {
  229.                 int ownerId = myAccounts[accountIndex].getOwner().getId();
  230.                 for (int i = accountIndex; i < myAccounts.length - 1; i += 1) {
  231.                     myAccounts[i] = myAccounts[i + 1];
  232.                 }
  233.                 myAccounts = Arrays.copyOf(myAccounts, myAccounts.length - 1);
  234.                 if (this.getClientAccounts(ownerId).length == 0) {
  235.                     RemoveClient(ownerId);
  236.                 }
  237.                 return true;
  238.             } else {
  239.                 throw new BankException(
  240.                         "Account cann't be removed!. accountId = " + accountId);
  241.             }
  242.         } catch (Exception e) {
  243.             e.printStackTrace();
  244.         }
  245.         return this.getAccountIndex(accountId) == -1;
  246.     }
  247.  
  248.     @Override
  249.     public Account getAccount(int accountId) {
  250.         int accountIndex = getAccountIndex(accountId);
  251.         if (accountIndex == -1) {
  252.             return null;
  253.         }
  254.  
  255.         return Account.copyOf(myAccounts[accountIndex]);
  256.     }
  257.  
  258.     @Override
  259.     public boolean setAccountFee(int accountId, double customFee) {
  260.         int accountIndex = getAccountIndex(accountId);
  261.         if (accountIndex == -1) {
  262.             return false;
  263.         }
  264.         return myAccounts[accountIndex].setAccountFee(customFee);
  265.  
  266.     }
  267.  
  268.     @Override
  269.     public boolean setAccountFee(int accountId) {
  270.         int accountIndex = getAccountIndex(accountId);
  271.         if (accountIndex == -1) {
  272.             return false;
  273.         }
  274.         return myAccounts[accountIndex].setAccountFee(-1);
  275.     }
  276.  
  277.     @Override
  278.     public boolean setAccountCredit(int accountId) {
  279.         int accountIndex = getAccountIndex(accountId);
  280.         if (accountIndex == -1) {
  281.             return false;
  282.         }
  283.         return myAccounts[accountIndex].setAccountCredit();
  284.     }
  285.  
  286.     @Override
  287.     public boolean setAccountCredit(int accountId, int customCredit) {
  288.         int accountIndex = getAccountIndex(accountId);
  289.         if (accountIndex == -1) {
  290.             return false;
  291.         }
  292.         return myAccounts[accountIndex].setAccountCredit(customCredit);
  293.     }
  294.  
  295.     public boolean withdraw(int accountId, double amount) {
  296.         int accountIndex = getAccountIndex(accountId);
  297.         if (accountIndex == -1) {
  298.             return false;
  299.         }
  300.         return myAccounts[accountIndex].withDraw(amount);
  301.     }
  302.  
  303.     public boolean deposit(int accountId, double amount) {
  304.         int accountIndex = getAccountIndex(accountId);
  305.         if (accountIndex == -1) {
  306.             return false;
  307.         }
  308.         myAccounts[accountIndex].deposit(amount);
  309.         return true;
  310.     }
  311.  
  312.     public Account[] getAccounts() {
  313.         Account[] resArray = new Account[0];
  314.  
  315.         for (Account item : myAccounts) {
  316.             resArray = (Account[]) BankUtils.addToArray(resArray,
  317.                     Account.copyOf(item));
  318.         }
  319.         return resArray;
  320.     }
  321.  
  322.     public Account[] getClientAccounts(int clientId) {
  323.         Account[] resArray = new Account[0];
  324.  
  325.         for (Account item : myAccounts) {
  326.             if (item.getOwner().getId() == clientId) {
  327.                 resArray = (Account[]) BankUtils.addToArray(resArray,
  328.                         Account.copyOf(item));
  329.             }
  330.         }
  331.         return resArray;
  332.     }
  333.  
  334.     @Override
  335.     public Account[] getAccountsByType(AccountType type) {
  336.         Account[] resArray = new Account[0];
  337.  
  338.         for (Account item : myAccounts) {
  339.             if (item.getAccountType().equals(type)) {
  340.                 resArray = (Account[]) BankUtils.addToArray(resArray,
  341.                         Account.copyOf(item));
  342.             }
  343.         }
  344.         return resArray;
  345.     }
  346.  
  347.     @Override
  348.     public Account[] getInvalidAgeAccounts(LocalDate atDate) {
  349.         Account[] resArray = new Account[0];
  350.  
  351.         for (Account item : myAccounts) {
  352.             int ageAtDate = (int) BankUtils.getAge(item.getOwner()
  353.                     .getBirthDate(), atDate);
  354.             if (!BankUtils.isBetween(ageAtDate, item.getAccountType()
  355.                     .getMinAge(), item.getAccountType().getMaxAge())) {
  356.                 resArray = (Account[]) BankUtils.addToArray(resArray,
  357.                         Account.copyOf(item));
  358.             }
  359.         }
  360.         return resArray;
  361.     }
  362.  
  363.     @Override
  364.     public Account[] getAccountsByBalance(int minCredit, int maxCredit) {
  365.         // TODO Auto-generated method stub
  366.         return null;
  367.     }
  368.  
  369.     @Override
  370.     public Account[] getAccountsByBalance(double minUseage, int maxUseage) {
  371.         Account[] resArray = new Account[0];
  372.  
  373.         for (Account item : myAccounts) {
  374.             double balance = item.getAccountBalance();
  375.             double credit = item.getAccountCredit();
  376.             double usage = balance >= 0 ? 0 : balance / credit;
  377.  
  378.             if (BankUtils.isBetween(usage, minUseage, maxUseage)) {
  379.                 resArray = (Account[]) BankUtils.addToArray(resArray,
  380.                         Account.copyOf(item));
  381.             }
  382.         }
  383.         return resArray;
  384.     }
  385.  
  386.     public boolean giveLoan(int accountId, double amount, int payments) {
  387.         int accountIndex = getAccountIndex(accountId);
  388.         if (accountIndex == -1) {
  389.             return false;
  390.         }
  391.  
  392.         if (myAccounts[accountIndex] instanceof LoanableAccount) {
  393.             return ((LoanableAccount) myAccounts[accountIndex]).giveLoan(
  394.                     amount, payments);
  395.         }
  396.  
  397.         return false;
  398.     }
  399.  
  400.     public boolean loanPaymentReturn(int accountId) {
  401.         LoanableAccount acc = (LoanableAccount) myAccounts[getAccountIndex(accountId)];
  402.         return acc.loanPaymentReturn();
  403.     }
  404.  
  405.     @Override
  406.     public LoanableAccount[] getLoans() {
  407.         LoanableAccount[] resArray = new LoanableAccount[0];
  408.  
  409.         for (Account item : myAccounts) {
  410.             if (item instanceof LoanableAccount) {
  411.                 if (((LoanableAccount) item).getLoanAmount() != 0) {
  412.                     resArray = (LoanableAccount[]) BankUtils.addToArray(
  413.                             resArray, Account.copyOf(item));
  414.                 }
  415.             }
  416.         }
  417.         return resArray;
  418.     }
  419.  
  420.     @Override
  421.     public boolean setLoanFee(double loanFee) {
  422.         return BankUtils.setLoanFee(loanFee);
  423.     }
  424.  
  425.     @Override
  426.     public String toString() {
  427.         String res = "Accounts List (" + myAccounts.length + "):\n";
  428.  
  429.         for (Account item : myAccounts) {
  430.             res += item + "\n";
  431.         }
  432.         res += "\nClients List (" + myClients.length + "):\n";
  433.         for (Client item : myClients) {
  434.             res += item + "\n";
  435.         }
  436.         return res;
  437.     }
  438.  
  439. }
Add Comment
Please, Sign In to add comment