For some types of accounts it is important to keep track of the mininmum balance
ID: 3668186 • Letter: F
Question
For some types of accounts it is important to keep track of the mininmum balance over a period. Also, some accounts have adjustements made on a periodic basis: monthly fees, interest earned
Requirements
To implement tracking the minimum balance in BankAccount, make the following changes to the class.
Instance field minBalance
A ‘getter’ for minBalance: getMinBalance
Method resetMinBalance
No parameters
Sets minBalance to the value of balance
Method updateMinBalance
No parameters
If minBalance is greater than balance, sets minBalance to the value of balance
Any constructor will call resetMinBalance at the end
deposit and withdraw both call updateMinBalance at the end
The starter project contains tests for the new features.
Stage 2
Objectives
Some accounts have adjustments made on a periodic basis: monthly fees, interest earned. A new type of account is introduced
Requirements
Add a method to carry out tasks that must be taken care of at the end of each month
Method month_end_process
public, can be used externally
Calls resetMinBalance
Using this method at the end of each month means that the min balance is always the minimum balance during the past month
Create a new class SavingsAccount that extends BankAccount
There is an instance field, annualInterestRate
Getter and setter for the annual interest rate
Provide constructors
The first constructor takes two parameters: the account id and the annual interest rate
The second constructor takes three parameters: the account id, the initial balance and the annual interest rate
Override the monthly processing method
Add the interest earned that month to the account
The amount of interest earned in a month is the mininum balance during the month times the monthly interest rate
The monthly interest rate is the annual interest rate divided by 12
After adding interest to the account, call the monthly processing method in BankAccount.
A new test suite: the archive lab0831-stage2-test.zip contains just the new test files. These will replace the test files in the original project.
Stage 3
Objectives
This part of the lab uses ArrayList to let customers of a bank have direct access to their own accounts
Requirements
This part requires additions to the Person class.
Add a new instance field accountsOwned that is an ArrayList
Each constructor should initialize accountsOwned to an empty list
Add a ‘getter’ for accountsOwned
Add a method named totalBalanceOfAccountsOwned that takes no parameters and returns the total balance of all accounts owned by that person
(Bonus 5 pts) Add a method named ownerOfAllAccountsOwned that returns true if all the accounts in accountsOwned actually specify this Person as the owner. Otherwise, the method returns false.
A new test suite: the archive lab0831-stage3-test.zip contains just the new test files. These will replace the test files in the original project. Note: If you are not doing the bonus method, then delete the file TestOwnershipTest. If you are doing the bonus, keep the file and that test file will have to be executed separately (it is not part of AllTests)
package bankAcccount.test;
import bankAcccount.BankAccount;
import bankAcccount.SavingsAccount;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Created by Ben on 8/29/2015.
*/
public class TestSavingsAccount {
private final static double EPS = 1e-10;
private String id0 = "acct001";
private String id1 = "acct002";
private double bal1 = 200;
private double irate0 = .12;
private double irate1 = .12;
private SavingsAccount sa0, sa1;
@Before
public void init() {
sa0 = new SavingsAccount(id0, irate0);
sa1 = new SavingsAccount(id1, bal1, irate1);
}
@Test
public void test1() {
assertEquals("Annual interest rate not stored properly in SavingsAccount",irate1, sa1.getAnnualInterestRate(), EPS);
}
/**
* Start with 200 balance, deposit and withdraw to leave balance higher, min balance should be 200
*/
@Test
public void test2() {
sa1.deposit(200);
sa1.withdraw(50);
double expectedInterest = sa1.getMinBalance() * irate1 / 12;
double expectedBalance = sa1.getBalance() + expectedInterest;
sa1.month_end_process();
assertEquals("Savings interest not computed properly", expectedBalance, sa1.getBalance(), EPS);
}
/**
* Start with 200 balance, deposit and withdraw to leave balance lower at the end.
*/
@Test
public void test3() {
sa1.deposit(200);
sa1.withdraw(250);
double expectedInterest = sa1.getMinBalance() * irate1 / 12;
double expectedBalance = sa1.getBalance() + expectedInterest;
sa1.month_end_process();
assertEquals("Savings interest not computed properly", expectedBalance, sa1.getBalance(), EPS);
}
}
package bankAcccount.test;
import bankAcccount.SavingsAccount;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Created by Ben on 8/11/2015.
*/
public class TestMinBalanceForSavings {
private final static double EPS = 1e-10;
private String id0 = "acct001";
private String id1 = "acct002";
private double bal1 = 200;
private SavingsAccount sa0, sa1;
@Before
public void init() {
sa0 = new SavingsAccount(id0, .12);
sa1 = new SavingsAccount(id1, bal1, .12);
}
/**
* Start with 0 balance, perform some actions, check that the minbalance is 0.
*/
@Test
public void test1() {
sa0.deposit(200);
sa0.withdraw(50);
assertEquals("Min balance is not correct", 0, sa0.getMinBalance(), EPS);
}
/**
* Start with 200 balance, deposit and withdraw to leave balance higher, min balance should be 200
*/
@Test
public void test2() {
sa1.deposit(200);
sa1.withdraw(50);
assertEquals("Min balance is not correct", bal1, sa1.getMinBalance(), EPS);
}
/**
* Start with 200 balance, deposit and withdraw to leave balance lower at the end.
*/
@Test
public void test3() {
sa1.deposit(200);
sa1.withdraw(250);
assertEquals("Min balance is not correct", bal1-50, sa1.getMinBalance(), EPS);
}
/**
* Start with 200 balance, deposit, withdraw, deposit to leave lowest balance in the middle.
*/
@Test
public void test4() {
sa1.deposit(200);
sa1.withdraw(250);
sa1.deposit(100);
assertEquals("Min balance is not correct", bal1-50, sa1.getMinBalance(), EPS);
}
/**
* Start with 200 balance, deposit, withdraw, deposit, withdraw, deposit so min balance is after
* the first withdraw
*/
@Test
public void test5() {
sa1.deposit(200);
sa1.withdraw(250);
sa1.deposit(100);
sa1.withdraw(50);
sa1.deposit(100);
assertEquals("Min balance is not correct", bal1 - 50, sa1.getMinBalance(), EPS);
}
/**
* Tests resetting the min balance.
* The min balance before the reset is lower than the min balance after the reset.
*/
@Test
public void test6() {
sa1.withdraw(50); // 150
sa1.deposit(200); // 350
assertEquals("", 150, sa1.getMinBalance(), EPS);
sa1.resetMinBalance();
sa1.withdraw(100); // 250
sa1.deposit(50); // 300
sa1.withdraw(100); // 200
sa1.deposit(100); // 300
assertEquals("", 200, sa1.getMinBalance(), EPS );
}
}
package bankAcccount.test;
import bankAcccount.BankAccount;
import bankAcccount.Person;
import bankAcccount.SavingsAccount;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Created by Ben on 8/29/2015.
*/
public class TestAccountsOwned {
private final static double EPS = 1e-10;
private Person p1, p2, p3;
private BankAccount ba1_1, ba1_2, ba1_3, ba2_1, ba2_2;
@Before
public void init() {
p1 = new Person("id001");
p2 = new Person("id002");
p3 = new Person("id003");
ba1_1 = new BankAccount("act001", 100);
ba1_2 = new BankAccount("act001", 200);
ba1_3 = new BankAccount("act001", 300);
ba2_1 = new BankAccount("act001", 400);
ba2_2 = new BankAccount("act001", 500);
ba1_1.setOwner(p1);
ba1_2.setOwner(p1);
ba1_3.setOwner(p1);
p1.getAccountsOwned().add(ba1_1);
p1.getAccountsOwned().add(ba1_2);
p1.getAccountsOwned().add(ba1_3);
p2.getAccountsOwned().add(ba2_1);
p2.getAccountsOwned().add(ba2_2);
p2.getAccountsOwned().add(ba1_3);
}
@Test
public void test_total_balance1() {
// normal total balance
assertEquals(600, p1.totalBalanceOfAccountsOwned(), EPS);
}
@Test
public void test_total_balance2() {
// balance still works even though p2 doesn't really own one of the accounts included
assertEquals(1200, p2.totalBalanceOfAccountsOwned(), EPS);
}
@Test
public void test_total_balance3() {
// no accounts means 0 total balance
assertEquals(0, p3.totalBalanceOfAccountsOwned(), EPS);
}
}
package bankAcccount.test;
import bankAcccount.BankAccount;
import bankAcccount.Person;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
/**
* Created by Ben on 8/29/2015.
*/
public class TestOwnershipTest {
private final static double EPS = 1e-10;
private Person p1, p2, p3;
private BankAccount ba1_1, ba1_2, ba1_3, ba2_1, ba2_2;
@Before
public void init() {
p1 = new Person("id001");
p2 = new Person("id002");
p3 = new Person("id003");
ba1_1 = new BankAccount("act001", 100);
ba1_2 = new BankAccount("act001", 200);
ba1_3 = new BankAccount("act001", 300);
ba2_1 = new BankAccount("act001", 400);
ba2_2 = new BankAccount("act001", 500);
ba1_1.setOwner(p1);
ba1_2.setOwner(p1);
ba1_3.setOwner(p1);
p1.getAccountsOwned().add(ba1_1);
p1.getAccountsOwned().add(ba1_2);
p1.getAccountsOwned().add(ba1_3);
p2.getAccountsOwned().add(ba2_1);
p2.getAccountsOwned().add(ba2_2);
p2.getAccountsOwned().add(ba1_3);
}
@Test
public void test_ownership1() {
// Ownership is consistent
assertTrue(p1.ownerOfAllAccountsOwned());
}
@Test
public void test_ownership2() {
// Ownership is not consistent
assertFalse(p2.ownerOfAllAccountsOwned());
}
@Test
public void test_ownership3() {
// Ownership is consistent even when there are no accounts owned
assertTrue(p1.ownerOfAllAccountsOwned());
}
}
programme should meet all test given
Explanation / Answer
BankAccount.java
public class BankAccount {
//public static double minBalance = 100.00;
private double balance; // account balance
private String accountId; // unique account identifier
private Person owner; // owner of the account
private double minBalance;
/**
* Initializes the account id to the parameter value and initializes the
* balance to 0. The owner of the account is set to null.
*
* @param initialAccountId Should be a valid account id. (can't check it at
* this time)
*/
public BankAccount(String initialAccountId) {
accountId = initialAccountId;
balance = 0.0; // not necessary since balance will default to 0.0 if nothing else is done
// to initialize it.
updateMinBalance();
}
/**
* Initializes the account id and balance from the parameter values. The
* owneer of the account is set to null;
*
* @param initialAccountId Should be a valid account id. (can't check it at
* this time)
* @param initialBalance Should be positive
*/
public BankAccount(String initialAccountId, double initialBalance) {
accountId = initialAccountId;
if (initialBalance <= 0) {
// error
System.err.println("Initial balance for an account should be positive: " + initialBalance);
System.exit(1);
} else {
balance = initialBalance;
}
updateMinBalance();
}
public BankAccount(String initialAccountid, double initialBalance, Person initialOwner) {
accountId = initialAccountid;
owner = initialOwner;
if (initialBalance <= 0) {
// error
System.err.println("Initial balance for an account should be positive: " + initialBalance);
System.exit(1);
} else {
balance = initialBalance;
}
updateMinBalance();
}
//getter method
public double getMinBalance() {
return minBalance;
}
//resetting balance
public void resetMinBalance(){
minBalance = balance;
}
//update min balance
public void updateMinBalance(){
if(minBalance>balance){
minBalance = balance;
}
}
public String getAccountId() {
return accountId;
}
public double getBalance() {
return balance;
}
/**
* Deposit the given amount in this bank account. This will add amount to
* the balance.
*
* @param amount Should be a positive number, that is, greater than 0
*/
public void deposit(double amount) {
if (amount > 0) {
// go ahead and process the deposit
balance += amount;
} else {
// report an error
System.err.println("Amount of deposit should be positive: " + amount);
System.exit(1);
}
updateMinBalance();
}
/**
* Withdraw the given amount from this bank account. The amount will be
* deducted from the account balance.
*
* @param amount Should be a positive value. Should be no bigger than the
* current balance.
*/
public void withdraw(double amount) {
if (amount <= 0) {
// error: amount should be positive
System.err.println("Amount of withdrawal should be positive: " + amount);
System.exit(1);
} else if (amount > balance) {
// error: amount should be no bigger than the balance
System.err.println("Amount of withdrawal should be no bigger than the balance: " + amount + " " + balance);
System.exit(1);
} else {
// amount is ok, process the withdrawal
balance -= amount;
}
updateMinBalance();
}
public void month_end_process(){
resetMinBalance();
}
public void setOwner(Person newOwner) {
owner = newOwner;
}
public Person getOwner() {
return owner;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------
Person.java
import java.util.ArrayList;
/**
*
* @author prmsh
*/
public class Person {
private String name;
private String personId;
private String e_mail;
private ArrayList<BankAccount> accountsOwned;
/**
* Initialize the properties of this object to the given parameter values.
* If a value is not known, use null.
*/
public Person(String name, String personId, String e_mail,ArrayList<BankAccount> accountsOwned) {
this.name = name;
this.personId = personId;
this.e_mail = e_mail;
this.accountsOwned = accountsOwned;
}
public Person(String personId,ArrayList<BankAccount> accountsOwned) {
this.personId = personId;
this.accountsOwned = accountsOwned;
}
//getter method
public ArrayList getAccountsOwned() {
return accountsOwned;
}
//method to get balance
public double totalBalanceOfAccountsOwned(){
double totalBalance=0;
for(int i=0;i<accountsOwned.size();i++){
totalBalance = totalBalance + accountsOwned.get(i).getBalance();
}
return totalBalance;
}
public boolean ownerOfAllAccountsOwned(){
boolean result = true;
for(int i=0;i<accountsOwned.size();i++){
if(!accountsOwned.get(i).getOwner().equals(this.name)){
return false;
}
}
return result;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getE_mail() {
return e_mail;
}
public void setE_mail(String e_mail) {
this.e_mail = e_mail;
}
public String getPersonId() {
return personId;
}
}
---------------------------------------------------------------------------------------------------------------------------------------
SavingsAccount.java
public class SavingsAccount extends BankAccount{
//declaring instance variable
public double annualInterestRate;
//constructors
public SavingsAccount(String initialAccountId, double annualInterestRate) {
super(initialAccountId);
this.annualInterestRate = annualInterestRate;
}
public SavingsAccount(String initialAccountId, double initialBalance, double annualInterestRate) {
super(initialAccountId,initialBalance);
this.annualInterestRate = annualInterestRate;
}
//getter setter methods
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
//adding interst method
public void interestEarned(){
double interest = annualInterestRate/12;
deposit(interest);
}
}