Commit d1cecca7 authored by Dr. Martin Goik's avatar Dr. Martin Goik

Accounts having negative balances

parent 6b40ada2
/.settings
/target
/.classpath
/.project
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.hdm-stuttgart.de.sd1</groupId>
<artifactId>interest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>V1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javadocDestdir>~/tmp</javadocDestdir>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<linksource>true</linksource>
<!-- Setting destDir interferes with taglet class -->
<additionalJOptions>
<additionalJOption>-d ${javadocDestdir}</additionalJOption>
</additionalJOptions>
<taglets>
<taglet>
<tagletClass>de.hdm_stuttgart.de.sd1.taglet.HtmlExtensionTaglet</tagletClass>
</taglet>
</taglets>
<tagletpath>../../../../../../../ws/eclipse/HtmlExtensionTaglet/target/classes</tagletpath>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include> **/*.properties</include>
</includes>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package de.hdm_stuttgart.mi.sd1.interest;
/**
* Dealing with account balances and interest calculations.
*
*/
public class Account {
private static double
interestRate = 1.5, // applied to positive balances
defaultInterestRate = 15.; // applied to negative balances
private double balance; // Balances apply per-account thus in contrast
// "static" must not appear here.
/**
* Create a new account having a balance of 0.
*/
public Account() { // Default Constructor. This one has no parameters and
setBalance(0); // thus creates an initially empty account.
}
/**
* Create a new account having of given initial balance.
*
* @param balance
* The account's initial balance.
*/
public Account(double balance) { // Non-default constructor creating an account
setBalance(balance); // with (possibly) non-zero balance.
}
/**
* @return The account's current balance.
*/
public double getBalance() {
return balance;
}
/**
* Setting a (possibly) new balance.
*
* @param balance
* The desired new balance value. If greater than 10000
* a warning message is being issued and the accounts current value will be retained.
*/
public void setBalance(double balance) {
if (balance <= 10000) {
this.balance = balance; // "this" required to resolve name shadowing conflict of formal
// parameter name "double stand" with respect to Account.stand .
} else {
System.out.println("Balance" + balance + " exceeds " + 10000);
}
}
/**
* Setting the interest rate common to all accounts. This one
* will be applied to positive balances only. For handling
* negative balances see {{@link #setDefaultInterestRate(double)}.
*
* @param interestRate The desired (global) interest rate.
*/
public static void setInterestRate(double interestRate) {
Account.interestRate = interestRate;
}
public static double getInterestRate() {
return interestRate;
}
/**
* @return
* the current default interest rate value.
*/
public static double getDefaultInterestRate() {
return defaultInterestRate;
}
/**
* This interest rate will be applied to negative balances. In contrast
* {{@link #setInterestRate(double)} will handle positive balance values.
*
* @param defaultInterestRate
* the desired default interest rate value.
*/
public static void setDefaultInterestRate(double defaultInterestRate) {
Account.defaultInterestRate = defaultInterestRate;
}
/**
* <p>Adding the annual interest to the current balance according to:</p>
* <math display="block" xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msub>
<mi>balance</mi>
<mi>1</mi>
</msub>
<mo>=</mo>
<mrow>
<mi>balance</mi>
<mrow>
<mo>(</mo>
<mrow>
<mi>1</mi>
<mo>+</mo>
<mfrac>
<mi>interestRate</mi>
<mi>100</mi>
</mfrac>
</mrow>
<mo>)</mo>
</mrow>
</mrow>
</mrow>
</math>
* <p>Positive balances will receive the value of {{@link #setInterestRate(double)} whereas the
* calculation of negative balances will be based on {{@link #setDefaultInterestRate(double)}.</p>
*/
public void applyInterest() {
if (0 < balance) {
balance = balance * (1 + interestRate / 100);
} else if (balance < 0){
balance = balance * (1 + defaultInterestRate / 100);
}
}
/**
* <p>Adding the interest of several years to the current balance according to:</p>
* <math display="block" xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msub>
<mi>balance</mi>
<mi>years</mi>
</msub>
<mo>=</mo>
<mrow>
<mi>balance</mi>
<msup>
<mrow>
<mo>(</mo>
<mrow>
<mi>1</mi>
<mo>+</mo>
<mfrac>
<mi>interestRate</mi>
<mi>100</mi>
</mfrac>
</mrow>
<mo>)</mo>
</mrow>
<mi>years</mi>
</msup>
</mrow>
</mrow>
</math>
*
*<p>Positive balances will receive the value of {{@link #setInterestRate(double)} whereas the
* calculation of negative balances will be based on {{@link #setDefaultInterestRate(double)}.</p>
*
* @param years
* the given time period.
*/
public void applyInterest(int years) {
if (0 < balance) {
balance = balance * Math.pow((1 + interestRate / 100), years) ;
} else if (balance < 0){
balance = balance * Math.pow((1 + defaultInterestRate / 100), years) ;
}
}
/**
* Calculate the expected annual interest without changing the account's balance.
*
* @return
* Annual interest to be expected
*/
public double getYearlyInterest() {
return balance * interestRate / 100;
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.sd1.interest;
/**
* Testing the simple account
*
*/
public class App {
public static void main(String[] args) {
Account account = new Account(20.); // Create a new instance of class Account
account.setBalance(100.); // set account's balance
System.out.println("Balance:" + account.getBalance()); // Display account's balance
Account.setInterestRate(1.0); // Setting the global interest rate
account.applyInterest(3); // raise balance by three years compounded interest
System.out.println("balance:" + account.getBalance()); // Display account's balance again
Account.setInterestRate(15.);
account.setBalance(-1000.); // You owe me something!
account.applyInterest(3);
System.out.println("balance:" + account.getBalance()); // Display account's balance again
}
}
package de.hdm_stuttgart.mi.sd1.interest;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AccountTest extends TestCase {
/**
* @return the suite of tests being tested
*/
public static Test suite() {
final TestSuite testsuite = new TestSuite("Testing accounts");
testsuite.addTestSuite(AccountTest.class);
return testsuite;
}
/**
* Testing account balance and interest calculatios
*/
public void testApp() {
Account account = new Account(20.); // Create a new instance of class Account
account.setBalance(100.); // set account's balance
Account.setInterestRate(1.0); // Setting the global interest rate
account.applyInterest(); // raise balance by one year's interest.
account.applyInterest(3); // raise balance by three years compounded interest
assertTrue(account.getBalance() == 104.06040100000001);
account.setBalance(-1000.); // You owe me something!
account.applyInterest(3);
assertTrue(account.getBalance() == -1520.8749999999998);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment