mmayoub

Bank, LoanableAccount abstarct class

Aug 10th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. package BankAccounts;
  2.  
  3. import java.time.LocalDate;
  4.  
  5. import BankClients.Client;
  6. import BankExceptions.BankLoanException;
  7. import BankInterfaces.Loanable;
  8. import BankPkg.AccountType;
  9. import BankPkg.BankUtils;
  10.  
  11. public abstract class LoanableAccount extends Account implements Loanable {
  12.  
  13.     protected double loanAmount;
  14.     protected int payments;
  15.  
  16.     public LoanableAccount(String personName, int personId,
  17.             LocalDate birthDate, AccountType type) throws Exception {
  18.         this(new Client(personName, personId, birthDate), type);
  19.     }
  20.  
  21.     public LoanableAccount(LoanableAccount other) {
  22.         super(other);
  23.         this.loanAmount = other.loanAmount;
  24.         this.payments = other.payments;
  25.     }
  26.  
  27.     public LoanableAccount(Client owner, AccountType type) throws Exception {
  28.         super(owner, type);
  29.         this.loanAmount = 0;
  30.         this.payments = 0;
  31.     }
  32.  
  33.     public double getLoanAmount() {
  34.         return this.loanAmount;
  35.     }
  36.  
  37.     public int getPayments() {
  38.         return this.payments;
  39.     }
  40.  
  41.     @Override
  42.     public boolean giveLoan(double sum, int payments) {
  43.  
  44.         if (this.loanAmount == 0 || this.payments == 0) {
  45.             double amountWithFee = sum * (1 + BankUtils.getLoanfee());
  46.  
  47.             this.loanAmount = amountWithFee;
  48.             this.payments = payments;
  49.  
  50.             return true;
  51.         }
  52.         try {
  53.             throw new BankLoanException(this, sum);
  54.         } catch (BankLoanException e) {
  55.             // TODO Auto-generated catch block
  56.             e.printStackTrace();
  57.         }
  58.         return false;
  59.     }
  60.  
  61.     public boolean loanPaymentReturn() {
  62.         double onePayment = this.loanAmount / this.payments;
  63.         double onePaymentWithFee = onePayment * (1 + BankUtils.getLoanfee());
  64.  
  65.         if (this.accountBalance - onePaymentWithFee >= this.accountCredit) {
  66.             this.accountBalance -= onePaymentWithFee;
  67.             this.loanAmount -= onePayment;
  68.             this.payments -= 1;
  69.             return true;
  70.         }
  71.         return false;
  72.     }
  73.  
  74.     @Override
  75.     public String loanReturnByMonth() {
  76.         double amountWithFee = this.loanAmount * (1 + BankUtils.getLoanfee());
  77.         double onePayment = amountWithFee / this.payments;
  78.         String result = String.format(
  79.                 "Account id=%d, Loan amount=%f, payments number=%d\n",
  80.                 this.accountId, this.loanAmount, this.payments);
  81.         for (int i = 0; i < this.payments; i += 1) {
  82.             result += String.format("Payment #%02d : %10.2f\n", i, onePayment);
  83.         }
  84.         return result;
  85.     }
  86.  
  87.     @Override
  88.     protected String feildsAsString() {
  89.         return super.feildsAsString()
  90.                 + String.format(", loanAmount=%f, payments=%d", loanAmount,
  91.                         payments);
  92.     }
  93. }
Add Comment
Please, Sign In to add comment