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
|
||||
package BankAccount;
|
||||
|
||||
/**
|
||||
* BankAccount class represents a bank account with balance, and tracks many things like numDeposits, withdrawls, etc
|
||||
*/
|
||||
public abstract class BankAccount {
|
||||
float balance;
|
||||
int numDeposits;
|
||||
int numWithdrawals;
|
||||
float annualInterest;
|
||||
float serviceCharge;
|
||||
float balance; // Account balance
|
||||
int numDeposits; // Count of deposits
|
||||
int numWithdrawals; // Count of withdrawls
|
||||
float annualInterest; // Annual interest rate
|
||||
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) {
|
||||
this.balance = balance;
|
||||
this.annualInterest = annualInterest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deposits to the account
|
||||
* @param amount Amount to be deposited
|
||||
*/
|
||||
public void deposit(float amount) {
|
||||
balance += amount;
|
||||
numDeposits++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Withdrawls from account, if balance is sufficient
|
||||
* @param amount Amount to withdraw from account
|
||||
*/
|
||||
public void withdraw(float amount) {
|
||||
if(balance >= amount) {
|
||||
balance -= amount;
|
||||
@@ -26,20 +42,29 @@ public abstract class BankAccount {
|
||||
else {
|
||||
System.out.println("Insufficient balance");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates interest based on annual interest and monthly rate
|
||||
*/
|
||||
void calculateInterest() {
|
||||
float monthlyRate = annualInterest / 12;
|
||||
float monthlyInterest = balance * monthlyRate;
|
||||
balance += monthlyInterest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes monthly charges
|
||||
*/
|
||||
void monthlyProcess() {
|
||||
balance -= serviceCharge;
|
||||
calculateInterest();
|
||||
}
|
||||
|
||||
/**
|
||||
* String representation of the object, includes the balance
|
||||
* @return String representation of object
|
||||
*/
|
||||
public String toString() {
|
||||
return "Balance: " + balance;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,29 @@
|
||||
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 {
|
||||
// Active status of bank account
|
||||
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) {
|
||||
super(balance, annualInterest);
|
||||
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) {
|
||||
if(status) {
|
||||
super.withdraw(amount);
|
||||
@@ -17,12 +33,21 @@ public class SavingsAccount extends BankAccount {
|
||||
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) {
|
||||
super.deposit(amount);
|
||||
updateStatus();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Applies monthly processing for the account
|
||||
* Updates status after processing
|
||||
*/
|
||||
void monthlyProcess() {
|
||||
if(numWithdrawals > 4) {
|
||||
int withdrawlsOverLimit = numWithdrawals - 4;
|
||||
@@ -32,9 +57,18 @@ public class SavingsAccount extends BankAccount {
|
||||
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() {
|
||||
return "Balance: " + balance + "\n Account is active: " + status;
|
||||
}
|
||||
|
||||
private void updateStatus() { status = balance >= 25; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user