Finished Question 1
This commit is contained in:
@@ -1,23 +1,39 @@
|
|||||||
// I put BankAccount and SavingsAccount into a package for the sake of member access
|
// I put BankAccount and SavingsAccount into a package for the sake of member access
|
||||||
package BankAccount;
|
package BankAccount;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BankAccount class represents a bank account with balance, and tracks many things like numDeposits, withdrawls, etc
|
||||||
|
*/
|
||||||
public abstract class BankAccount {
|
public abstract class BankAccount {
|
||||||
float balance;
|
float balance; // Account balance
|
||||||
int numDeposits;
|
int numDeposits; // Count of deposits
|
||||||
int numWithdrawals;
|
int numWithdrawals; // Count of withdrawls
|
||||||
float annualInterest;
|
float annualInterest; // Annual interest rate
|
||||||
float serviceCharge;
|
float serviceCharge; // Monthly service charge
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor with balance and anual interest
|
||||||
|
* @param balance Starting balance
|
||||||
|
* @param annualInterest Annual interest to be charged
|
||||||
|
*/
|
||||||
public BankAccount(float balance, float annualInterest) {
|
public BankAccount(float balance, float annualInterest) {
|
||||||
this.balance = balance;
|
this.balance = balance;
|
||||||
this.annualInterest = annualInterest;
|
this.annualInterest = annualInterest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deposits to the account
|
||||||
|
* @param amount Amount to be deposited
|
||||||
|
*/
|
||||||
public void deposit(float amount) {
|
public void deposit(float amount) {
|
||||||
balance += amount;
|
balance += amount;
|
||||||
numDeposits++;
|
numDeposits++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Withdrawls from account, if balance is sufficient
|
||||||
|
* @param amount Amount to withdraw from account
|
||||||
|
*/
|
||||||
public void withdraw(float amount) {
|
public void withdraw(float amount) {
|
||||||
if(balance >= amount) {
|
if(balance >= amount) {
|
||||||
balance -= amount;
|
balance -= amount;
|
||||||
@@ -26,20 +42,29 @@ public abstract class BankAccount {
|
|||||||
else {
|
else {
|
||||||
System.out.println("Insufficient balance");
|
System.out.println("Insufficient balance");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates interest based on annual interest and monthly rate
|
||||||
|
*/
|
||||||
void calculateInterest() {
|
void calculateInterest() {
|
||||||
float monthlyRate = annualInterest / 12;
|
float monthlyRate = annualInterest / 12;
|
||||||
float monthlyInterest = balance * monthlyRate;
|
float monthlyInterest = balance * monthlyRate;
|
||||||
balance += monthlyInterest;
|
balance += monthlyInterest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Processes monthly charges
|
||||||
|
*/
|
||||||
void monthlyProcess() {
|
void monthlyProcess() {
|
||||||
balance -= serviceCharge;
|
balance -= serviceCharge;
|
||||||
calculateInterest();
|
calculateInterest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* String representation of the object, includes the balance
|
||||||
|
* @return String representation of object
|
||||||
|
*/
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Balance: " + balance;
|
return "Balance: " + balance;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,29 @@
|
|||||||
package BankAccount;
|
package BankAccount;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class that represents a savings account, a type of bank account which features a status depending on whether
|
||||||
|
* the balance is above or below 25. Monthly processing is changed depending on overwithdrawls
|
||||||
|
*/
|
||||||
public class SavingsAccount extends BankAccount {
|
public class SavingsAccount extends BankAccount {
|
||||||
|
// Active status of bank account
|
||||||
private boolean status;
|
private boolean status;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor with balance and annual interest
|
||||||
|
* @param balance initial balance of account
|
||||||
|
* @param annualInterest interest to be accumulated annually
|
||||||
|
*/
|
||||||
public SavingsAccount(float balance, float annualInterest) {
|
public SavingsAccount(float balance, float annualInterest) {
|
||||||
super(balance, annualInterest);
|
super(balance, annualInterest);
|
||||||
updateStatus();
|
updateStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Withdraws amount from the balance. If account is inactive, there is no withdraw.
|
||||||
|
* Updates status after withdrawl
|
||||||
|
* @param amount Amount of money to withdraw
|
||||||
|
*/
|
||||||
public void withdraw(float amount) {
|
public void withdraw(float amount) {
|
||||||
if(status) {
|
if(status) {
|
||||||
super.withdraw(amount);
|
super.withdraw(amount);
|
||||||
@@ -17,12 +33,21 @@ public class SavingsAccount extends BankAccount {
|
|||||||
System.out.println("Cannot withdraw, account is inactive");
|
System.out.println("Cannot withdraw, account is inactive");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deposits specific amount into the account
|
||||||
|
* Updates status afterward
|
||||||
|
* @param amount The amount to deposit
|
||||||
|
*/
|
||||||
public void deposit(float amount) {
|
public void deposit(float amount) {
|
||||||
super.deposit(amount);
|
super.deposit(amount);
|
||||||
updateStatus();
|
updateStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Applies monthly processing for the account
|
||||||
|
* Updates status after processing
|
||||||
|
*/
|
||||||
void monthlyProcess() {
|
void monthlyProcess() {
|
||||||
if(numWithdrawals > 4) {
|
if(numWithdrawals > 4) {
|
||||||
int withdrawlsOverLimit = numWithdrawals - 4;
|
int withdrawlsOverLimit = numWithdrawals - 4;
|
||||||
@@ -32,9 +57,18 @@ public class SavingsAccount extends BankAccount {
|
|||||||
updateStatus();
|
updateStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the active/inactive status of the account, based of balance being above or below 25
|
||||||
|
*/
|
||||||
|
private void updateStatus() { status = balance >= 25; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns string representation of the account. Includes balance and status
|
||||||
|
* @return String representation of the account
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Balance: " + balance + "\n Account is active: " + status;
|
return "Balance: " + balance + "\n Account is active: " + status;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateStatus() { status = balance >= 25; }
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user