Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package BankAccounts;
- import java.time.LocalDate;
- import BankClients.Client;
- import BankExceptions.BankLoanException;
- import BankInterfaces.Loanable;
- import BankPkg.AccountType;
- import BankPkg.BankUtils;
- public abstract class LoanableAccount extends Account implements Loanable {
- protected double loanAmount;
- protected int payments;
- public LoanableAccount(String personName, int personId,
- LocalDate birthDate, AccountType type) throws Exception {
- this(new Client(personName, personId, birthDate), type);
- }
- public LoanableAccount(LoanableAccount other) {
- super(other);
- this.loanAmount = other.loanAmount;
- this.payments = other.payments;
- }
- public LoanableAccount(Client owner, AccountType type) throws Exception {
- super(owner, type);
- this.loanAmount = 0;
- this.payments = 0;
- }
- public double getLoanAmount() {
- return this.loanAmount;
- }
- public int getPayments() {
- return this.payments;
- }
- @Override
- public boolean giveLoan(double sum, int payments) {
- if (this.loanAmount == 0 || this.payments == 0) {
- double amountWithFee = sum * (1 + BankUtils.getLoanfee());
- this.loanAmount = amountWithFee;
- this.payments = payments;
- return true;
- }
- try {
- throw new BankLoanException(this, sum);
- } catch (BankLoanException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return false;
- }
- public boolean loanPaymentReturn() {
- double onePayment = this.loanAmount / this.payments;
- double onePaymentWithFee = onePayment * (1 + BankUtils.getLoanfee());
- if (this.accountBalance - onePaymentWithFee >= this.accountCredit) {
- this.accountBalance -= onePaymentWithFee;
- this.loanAmount -= onePayment;
- this.payments -= 1;
- return true;
- }
- return false;
- }
- @Override
- public String loanReturnByMonth() {
- double amountWithFee = this.loanAmount * (1 + BankUtils.getLoanfee());
- double onePayment = amountWithFee / this.payments;
- String result = String.format(
- "Account id=%d, Loan amount=%f, payments number=%d\n",
- this.accountId, this.loanAmount, this.payments);
- for (int i = 0; i < this.payments; i += 1) {
- result += String.format("Payment #%02d : %10.2f\n", i, onePayment);
- }
- return result;
- }
- @Override
- protected String feildsAsString() {
- return super.feildsAsString()
- + String.format(", loanAmount=%f, payments=%d", loanAmount,
- payments);
- }
- }
Add Comment
Please, Sign In to add comment