oppgave: savingsaccount
This commit is contained in:
9
src/main/java/oving7/savingsaccount/Account.java
Normal file
9
src/main/java/oving7/savingsaccount/Account.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package oving7.savingsaccount;
|
||||
|
||||
public interface Account {
|
||||
void deposit(double amount);
|
||||
|
||||
void withdraw(double amount);
|
||||
|
||||
double getBalance();
|
||||
}
|
||||
41
src/main/java/oving7/savingsaccount/BSU.java
Normal file
41
src/main/java/oving7/savingsaccount/BSU.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package oving7.savingsaccount;
|
||||
|
||||
public class BSU extends SavingsAccount {
|
||||
private double depositedThisYear = 0.0;
|
||||
private double depositMax;
|
||||
|
||||
BSU(double interestRate, double depositMax) {
|
||||
super(interestRate);
|
||||
this.depositMax = depositMax;
|
||||
}
|
||||
|
||||
public double getTaxDeduction() {
|
||||
return depositedThisYear * 0.2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deposit(double amount) {
|
||||
if ((depositedThisYear + amount) > depositMax) {
|
||||
throw new IllegalStateException(
|
||||
"cannot deposit more than the maximum deposit, which has a value of " + depositMax);
|
||||
}
|
||||
super.deposit(amount);
|
||||
depositedThisYear += amount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void withdraw(double amount) {
|
||||
if (amount > depositedThisYear) {
|
||||
throw new IllegalStateException(
|
||||
"cannot withdraw more than has been deposited this year, which has a value of "
|
||||
+ depositedThisYear);
|
||||
}
|
||||
super.withdraw(amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endYearUpdate() {
|
||||
super.endYearUpdate();
|
||||
depositedThisYear = 0.0;
|
||||
}
|
||||
}
|
||||
31
src/main/java/oving7/savingsaccount/ForeldreSpar.java
Normal file
31
src/main/java/oving7/savingsaccount/ForeldreSpar.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package oving7.savingsaccount;
|
||||
|
||||
public class ForeldreSpar extends SavingsAccount {
|
||||
private int withdrawalsMax;
|
||||
private int remainingWithdrawals;
|
||||
|
||||
ForeldreSpar(double interestRate, int withdrawalsMax) {
|
||||
super(interestRate);
|
||||
this.withdrawalsMax = withdrawalsMax;
|
||||
remainingWithdrawals = withdrawalsMax;
|
||||
}
|
||||
|
||||
int getRemainingWithdrawals() {
|
||||
return remainingWithdrawals;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void withdraw(double amount) {
|
||||
if (remainingWithdrawals <= 0) {
|
||||
throw new IllegalStateException("Cannot withdraw more than " + withdrawalsMax + " times a year");
|
||||
}
|
||||
super.withdraw(amount);
|
||||
remainingWithdrawals -= 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endYearUpdate() {
|
||||
super.endYearUpdate();
|
||||
remainingWithdrawals = withdrawalsMax;
|
||||
}
|
||||
}
|
||||
35
src/main/java/oving7/savingsaccount/SavingsAccount.java
Normal file
35
src/main/java/oving7/savingsaccount/SavingsAccount.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package oving7.savingsaccount;
|
||||
|
||||
public class SavingsAccount implements Account {
|
||||
private double balance = 0.0;
|
||||
private double interestRate;
|
||||
|
||||
SavingsAccount(double interestRate) {
|
||||
this.interestRate = interestRate;
|
||||
}
|
||||
|
||||
public void deposit(double amount) {
|
||||
if (amount <= 0) {
|
||||
throw new IllegalArgumentException("deposit must be greater than zero");
|
||||
}
|
||||
balance += amount;
|
||||
}
|
||||
|
||||
public void withdraw(double amount) {
|
||||
if (amount <= 0) {
|
||||
throw new IllegalArgumentException("withdraw must be greater than zero");
|
||||
}
|
||||
if (amount > balance) {
|
||||
throw new IllegalStateException("cannot withdraw more than current balance");
|
||||
}
|
||||
balance -= amount;
|
||||
}
|
||||
|
||||
public double getBalance() {
|
||||
return balance;
|
||||
}
|
||||
|
||||
public void endYearUpdate() {
|
||||
balance += balance * interestRate;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user