📄 loanaccount.java
字号:
import java.text.DecimalFormat;
import java.util.ArrayList;
/**
* This is LoanAccount class whcih is one type of Account class.
* This class deals with the Loan account which allows people to make a loan
*
*/
public class LoanAccount extends Account {
private static final long serialVersionUID = 1L;
public static double minimumLoanAmount; //May be static
private double numberOfYear;
private double fixedInterestRate;
private double loanRepaymentAmount;
/**
* The constructor has a responsible to initial value for all essential data when the object is created
*/
public LoanAccount(){
this.setAccountNumber(0);
this.setAccountBalance(0.00);
this.setOpeningBalance(0.00);
this.setStatus('C');
this.setAccountOwnerArrayList(null);
this.setFixedInterestRate(0.00);
this.setLoanRepaymentAmount(0.00);
this.setNumberOfYear(0.00);
}
/**
* The constructor has a responsible to initial value for all essential data when the object is created
* @param accountNumber : Account number of Account ID --> seven digit number
* @param openingLoanAmount : the initail loan amount
* @param interestRate : the interest rate for calculating the loan repayment
* @param numberOfYear : the duration of loan which can only be a number of years
* @param customerArrayListObject : ArrayList of Customer class to keep the owner of this account
*/
public LoanAccount( int accountNumber,
double openingLoanAmount,
double interestRate,
double numberOfYear,// Whether be int or not
ArrayList<Customer> customerArrayListObject){
this.setAccountNumber(accountNumber);
this.setAccountBalance(-openingLoanAmount);
this.setOpeningBalance(-openingLoanAmount);
this.setStatus('O');
this.setAccountOwnerArrayList(customerArrayListObject);
this.setFixedInterestRate(interestRate);
this.setNumberOfYear(numberOfYear);
this.setLoanRepaymentAmount(calculateLoan(openingLoanAmount,interestRate,numberOfYear));
DecimalFormat decimalPattern = new DecimalFormat("0.00");
System.out.println("Monthly Repayment: " + decimalPattern.format(this.getLoanRepaymentAmount()));
}
public void deposit( int accountNumber,
double repaymentAmount,
ArrayList<Transaction> transactionArrayListObject){
if(this.getAccountNumber() == accountNumber){
if(repaymentAmount >= this.loanRepaymentAmount){
double tempAccountBalance = this.getAccountBalance();
tempAccountBalance = tempAccountBalance + repaymentAmount;
if(tempAccountBalance <= 0){
this.setAccountBalance(tempAccountBalance);
Transaction tObject = new Transaction();
tObject.setAccountNumber(accountNumber);
tObject.setTransactionType("DEPOSIT");
tObject.setAmount(repaymentAmount);
tObject.setBalance(tempAccountBalance);
if(this.getAccountBalance() == 0.00){
this.setStatus('C');
}
//Add transaction object to ArrayList of Transaction class
transactionArrayListObject.add(tObject);
this.getTransactionHistoryArrayList().add(tObject);
}else{
DecimalFormat decimalPattern = new DecimalFormat("0.00");
System.out.println("ERROR DEPOSIT : You can deposit only $"+decimalPattern.format(this.getAccountBalance()));
}
}else{
DecimalFormat decimalPattern = new DecimalFormat("0.00");
System.out.println("ERROR DEPOSIT : The deposit amount must >= loan repayment amount("
+decimalPattern.format(this.loanRepaymentAmount)+")");
}
}else{
System.out.println("Error!!! Wrong account number");
}
}
public void withdraw(int accountNumber, double amount, ArrayList<Transaction> transactionArrayListObject){
System.out.println("ERROR WITHDRAW LOAN account cannot withdraw!!!");
}
public void transaction(int accountNumber){
DecimalFormat decimalPattern = new DecimalFormat("0.00");
if(this.getAccountNumber() == accountNumber){
System.out.println("Account number: "+this.getAccountNumber());
System.out.print("Account owners: ");
for(Customer c: this.getAccountOwnerArrayList()){
System.out.print(c.getCustomerID());
System.out.print(" ");
}
System.out.println("");
System.out.println("Interest rate: "+decimalPattern.format(this.getFixedInterestRate())+" %");
System.out.println("Duration: "+this.getNumberOfYear());
System.out.println("Repayment: "+decimalPattern.format(this.getLoanRepaymentAmount()));
System.out.printf("Opening balance: ");
System.out.printf("$%-20.2f",this.getOpeningBalance());
System.out.println();
for(Transaction t:this.getTransactionHistoryArrayList())
{
System.out.printf(t.getTransactionType()+" \t");
System.out.printf("\t$%-20.2f\t$%-20.2f%n",t.getAmount(),t.getBalance());
}
}else{
System.out.println("Error!!! Wrong account number");
}
}
/**
* This method is calculating the loan repayment amount
* @param loanAmount : the amount of loan
* @param interestRate : the interest rate for calculating the loan repayment
* @param numberOfYear : the duration of loan which can only be a number of years
* @return repaymentAmount : the amount of repayment per month
*/
private double calculateLoan( double loanAmount,
double interestRate,
double numberOfYear
){
interestRate = (interestRate/100); // To transform the input interest into percentage
int numberOfMonths = 0;// Input only be years so I assume that month = 0
double repaymentAmount
=(loanAmount*interestRate*(Math.pow((1+interestRate),numberOfYear))*(12+(interestRate*numberOfMonths)))/
(12*(interestRate*numberOfMonths+((12+interestRate*numberOfMonths)*((Math.pow((1+interestRate),numberOfYear))-1))));
return repaymentAmount;
}
public double getFixedInterestRate() {
return fixedInterestRate;
}
public void setFixedInterestRate(double fixedInterestRate) {
this.fixedInterestRate = fixedInterestRate;
}
public double getLoanRepaymentAmount() {
return loanRepaymentAmount;
}
public void setLoanRepaymentAmount(double loanRepaymentAmount) {
this.loanRepaymentAmount = loanRepaymentAmount;
}
public double getMinimumLoanAmount() {
return minimumLoanAmount;
}
public double getNumberOfYear() {
return numberOfYear;
}
public void setNumberOfYear(double numberOfYear) {
this.numberOfYear = numberOfYear;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -