Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import static org.junit.Assert.*; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import org.junit.Test; public class SavingsAccountIA6Test extends FormatTester { public static final String CLASSNAME =

image text in transcribed

image text in transcribed

import static org.junit.Assert.*;

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import org.junit.Test;

public class SavingsAccountIA6Test extends FormatTester

{

public static final String CLASSNAME = "SavingsAccount";

public SavingsAccountIA6Test()

{

super(CLASSNAME, false);

}

private void testInterface()

{

String[] instanceVars = {"double annualInterestRate"};

assertTrue("Instance variables should be private with correct name and signature.", instanceVariablesArePrivate(instanceVars));

assertFalse("Should not override getBalance.", hasMethod("double getBalance()"));

assertFalse("Should not override or call monthEndUpdate.", hasMethod("monthEndUpdate"));

}

// test constructors

// @Test

// public void test_creation_defaultConstructor(){

// testInterface();

// SavingsAccount s = new SavingsAccount();

// assertEquals("Expected initial balance to be 0.0", 0.0, s.getBalance(), 0.00001);

// assertEquals("Expected annual interest rate to be 0.05 (5%)", 0.05, s.getAnnualInterestRate(), 0.00001);

// }

//

//

// @Test

// public void test_constructorWithInterest()

// {

// testInterface();

// SavingsAccount s = new SavingsAccount(0.01);

//

// assertEquals("Unexpected balance", 0.0, s.getBalance(), 0.00001);

// assertEquals("Unexpected interest rate", 0.01, s.getAnnualInterestRate(), 0.00001);

// }

//

// @Test

// public void test_constructorWithInterestAndBalance()

// {

// testInterface();

// SavingsAccount s = new SavingsAccount(50.0, 0.15);

//

// assertEquals("Unexpected balance", 50.0, s.getBalance(), 0.00001);

// assertEquals("Unexpected interest rate", 0.15, s.getAnnualInterestRate(), 0.00001);

// }

//

// @Test

// public void test_constructorWithCustomerAndBalance()

// {

// testInterface();

// Customer c = new Customer("John Doe", 321);

// SavingsAccount s = new SavingsAccount(c, 50.0);

//

// assertEquals("Unexpected balance",50.0,s.getBalance(), 0.00001);

// assertEquals("Unexpected interest rate (default)", 0.05, s.getAnnualInterestRate(), 0.00001);

// assertEquals("Unexpected customer", "John Doe", s.getAccountHolder().getName());

// }

//

// @Test

// public void test_constructorWithCustomerBalanceAndInterestRate()

// {

// testInterface();

// Customer c = new Customer("John Doe", 321);

// SavingsAccount s = new SavingsAccount(c, 551.0, 0.7);

//

// assertEquals("Unexpected balance",551.0,s.getBalance(), 0.00001);

// assertEquals("Unexpected interest rate", 0.7, s.getAnnualInterestRate(), 0.00001);

// assertEquals("Unexpected customer", "John Doe", s.getAccountHolder().getName());

// }

//

// // test setters and getters

// @Test

// public void test_setAnnualInterestRate_zero()

// {

// testInterface();

// SavingsAccount s = new SavingsAccount();

// s.setAnnualInterestRate(0.0);

// assertEquals("Changed annual interest rate is 0.0", 0.0, s.getAnnualInterestRate(), 0.000001);

// }

//

// @Test

// public void test_setAnnualInterestRate_one()

// {

// testInterface();

// SavingsAccount s = new SavingsAccount();

// s.setAnnualInterestRate(1.0);

// assertEquals("Changed annual interest rate is 1.0", 1.0, s.getAnnualInterestRate(), 0.000001);

// }

//

// @Test

// public void test_setAnnualInterestRate_negativeRate()

// {

// testInterface();

// SavingsAccount s = new SavingsAccount();

// s.setAnnualInterestRate(-0.25);

// assertEquals("Annual interest rate should not be negative and should have been left unchanged.", 0.05, s.getAnnualInterestRate(), 0.000001);

// }

//

// @Test

// public void test_setAnnualInterestRate_biggerThanOne()

// {

// testInterface();

// SavingsAccount s = new SavingsAccount();

// s.setAnnualInterestRate(1.1);

// assertEquals("Annual interest rate should be at most 1 (100%) and should have been left unchanged.", 0.05, s.getAnnualInterestRate(), 0.000001);

// }

//

// testing remaining methods

@Test

public void test_getMonthlyFeesAndInterest_defaultRate()

{

testInterface();

SavingsAccount s = new SavingsAccount();

s.deposit(100);

double interest = s.getMonthlyFeesAndInterest();

assertEquals("Balance ($100) should be unchanged", 100.00, s.getBalance(), 0.00001);

assertEquals("Testing computed interest", 0.416666667, interest, 0.00001);

}

@Test

public void test_getMonthlyFeesAndInterest_zeroBalance()

{

testInterface();

SavingsAccount s = new SavingsAccount();

double interest = s.getMonthlyFeesAndInterest();

assertEquals("Balance ($0) should be unchanged", 0.00, s.getBalance(), 0.00001);

assertEquals("Testing computed interest", 0.0, interest, 0.00001);

}

@Test

public void test_getMonthlyFeesAndInterest_zeroPercent()

{

testInterface();

SavingsAccount s = new SavingsAccount();

s.setAnnualInterestRate(0.0);

s.deposit(100.0);

double interest = s.getMonthlyFeesAndInterest();

assertEquals("Balance ($100) should be unchanged", 100.00, s.getBalance(), 0.00001);

assertEquals("Testing computed interest", 0.00, interest, 0.00001);

}

@Test

public void test_getMontlyFeesAndInterest_hundredPercent()

{

testInterface();

SavingsAccount s = new SavingsAccount();

s.setAnnualInterestRate(1.0);

s.deposit(100.0);

double interest = s.getMonthlyFeesAndInterest();

assertEquals("Balance ($100) should be unchanged", 100.00, s.getBalance(), 0.00001);

assertEquals("Testing computed interest", 8.33333, interest, 0.00001);

}

// @Test

// public void test_withdraw_fundsAvailable()

// {

// testInterface();

// SavingsAccount s = new SavingsAccount();

// s.deposit(500);

// s.setMinimumBalance(100.0);

//

// s.withdraw(400);

//

// assertEquals("Savings account with 500.0 balance and 100 min balance, should be able to withdraw 400.", 100.0, s.getBalance(), 0.000001);

// }

//

@Test

public void test_withdraw_WouldGoBelowMinBalance()

{

testInterface();

SavingsAccount s = new SavingsAccount();

s.deposit(500);

s.setMinimumBalance(100.0);

s.withdraw(401);

assertEquals("Savings account with 500.0 balance and 100 min balance, can't withdraw 400.", 500.0, s.getBalance(), 0.000001);

}

}

1 public class SavingsAccount extends BankAccount private double annualInterestRate; private double minimumBa lance 4 public SavingsAccount) super); this.annuaLinterestRate 0.05; this.minimumBa lance.; 10 public double getAnnualInterestRate) 12 13 return this.annualInterestRate; public void setAnnualInterestRate(double d) f 15 16 17 18 19 20 21 if (d 1.0) ( this.annualInterestRate 0.05 else this.annualInterestRate d; 23 public void depositMonthlyInterest) t double bal-getBalance); bal bal+bal * annualInterestRate/ 12; 25 26 27 28 29 30 public void withdraw( double d) t if(d d) 32 balance = balance - d; 34 35 36 37 38 39 40 41 public void setMinimumBalance(double d) f this.minimumBalanced; Update BankAccount to Make BankAccount an abstract class Add abstract method getMonthlyFeesAndlnterest with return type double and access protected Add method monthEndUpdate which doesn't return anything but calls getMonthlyFeesAndlnterest and updates the balance by the amount returned. (If the amount returned is negative, the balance is reduced, when the amount returned is positive the balance is increased.) It is possible that the account has a negative balance after this method is called. . Update SavingsAccount to . Remove the method depositMonthlylnterest Override the getMonthlyFeesAndinterest method such that it returns the monthly interest (Same computation as depositMonthlylnterest did, but the account balance is not changed.) 1 public class SavingsAccount extends BankAccount private double annualInterestRate; private double minimumBa lance 4 public SavingsAccount) super); this.annuaLinterestRate 0.05; this.minimumBa lance.; 10 public double getAnnualInterestRate) 12 13 return this.annualInterestRate; public void setAnnualInterestRate(double d) f 15 16 17 18 19 20 21 if (d 1.0) ( this.annualInterestRate 0.05 else this.annualInterestRate d; 23 public void depositMonthlyInterest) t double bal-getBalance); bal bal+bal * annualInterestRate/ 12; 25 26 27 28 29 30 public void withdraw( double d) t if(d d) 32 balance = balance - d; 34 35 36 37 38 39 40 41 public void setMinimumBalance(double d) f this.minimumBalanced; Update BankAccount to Make BankAccount an abstract class Add abstract method getMonthlyFeesAndlnterest with return type double and access protected Add method monthEndUpdate which doesn't return anything but calls getMonthlyFeesAndlnterest and updates the balance by the amount returned. (If the amount returned is negative, the balance is reduced, when the amount returned is positive the balance is increased.) It is possible that the account has a negative balance after this method is called. . Update SavingsAccount to . Remove the method depositMonthlylnterest Override the getMonthlyFeesAndinterest method such that it returns the monthly interest (Same computation as depositMonthlylnterest did, but the account balance is not changed.)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions