oppgave: abstractaccount
This commit is contained in:
@@ -23,6 +23,8 @@ public abstract class AbstractAccount {
|
||||
|
||||
// TODO: Add fields and potentially a constructor here
|
||||
|
||||
protected double balance = 0.0;
|
||||
|
||||
/**
|
||||
* Decreases the account balance by the specified amount. Note that the rules
|
||||
* for withdrawals
|
||||
@@ -35,6 +37,8 @@ public abstract class AbstractAccount {
|
||||
*/
|
||||
// TODO: Define abstract method {@code void internalWithdraw} here
|
||||
|
||||
protected abstract void internalWithdraw(double amount);
|
||||
|
||||
/**
|
||||
* Increases the account balance by the specified amount.
|
||||
*
|
||||
@@ -42,7 +46,10 @@ public abstract class AbstractAccount {
|
||||
* @throws IllegalArgumentException if the amount is not positive
|
||||
*/
|
||||
public void deposit(double amount) {
|
||||
// TODO: Implement this method
|
||||
if (amount < 0.0) {
|
||||
throw new IllegalArgumentException("cannot deposit amount less than zero");
|
||||
}
|
||||
balance += amount;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,13 +62,16 @@ public abstract class AbstractAccount {
|
||||
*/
|
||||
public void withdraw(double amount) {
|
||||
// TODO: Implement this method
|
||||
if (amount < 0.0) {
|
||||
throw new IllegalArgumentException("withdrawal amount cannot be less than zero");
|
||||
}
|
||||
internalWithdraw(amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the current balance of the account
|
||||
*/
|
||||
public double getBalance() {
|
||||
// TODO: Implement this method
|
||||
return 0.0;
|
||||
return balance;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ package oving7.abstractaccount;
|
||||
*/
|
||||
public class CreditAccount extends AbstractAccount {
|
||||
|
||||
// TODO: Add fields here
|
||||
private double creditLine;
|
||||
|
||||
/**
|
||||
* Initializes a new {@code CreditAccount} with the specified credit line.
|
||||
@@ -23,10 +23,15 @@ public class CreditAccount extends AbstractAccount {
|
||||
* @throws IllegalArgumentException if the credit line is negative
|
||||
*/
|
||||
public CreditAccount(double creditLine) {
|
||||
// TODO: Implement this constructor
|
||||
this.creditLine = creditLine;
|
||||
}
|
||||
|
||||
// TODO: Override abstract method here
|
||||
protected void internalWithdraw(double amount) {
|
||||
if (amount > (balance + creditLine)) {
|
||||
throw new IllegalArgumentException("cannot withdraw more than balance plus creditLine");
|
||||
}
|
||||
balance -= amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the credit line
|
||||
@@ -34,8 +39,7 @@ public class CreditAccount extends AbstractAccount {
|
||||
* @see CreditAccountTest#testCreditLine()
|
||||
*/
|
||||
public double getCreditLine() {
|
||||
// TODO: Implement this method
|
||||
return 0.0;
|
||||
return creditLine;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,6 +53,13 @@ public class CreditAccount extends AbstractAccount {
|
||||
* @see CreditAccountTest#testCreditLine()
|
||||
*/
|
||||
public void setCreditLine(double creditLine) {
|
||||
// TODO: Implement this method
|
||||
if (creditLine < 0.0) {
|
||||
throw new IllegalArgumentException("credit line cannot be less than zero");
|
||||
}
|
||||
if (creditLine + balance < 0.0) {
|
||||
throw new IllegalStateException("credit line plus balance must be greater than or equal to zero");
|
||||
}
|
||||
|
||||
this.creditLine = creditLine;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,5 +15,11 @@ package oving7.abstractaccount;
|
||||
*/
|
||||
public class DebitAccount extends AbstractAccount {
|
||||
|
||||
// TODO: Override abstract method here
|
||||
@Override
|
||||
protected void internalWithdraw(double amount) {
|
||||
if (balance < amount) {
|
||||
throw new IllegalArgumentException("cannot withdraw amount greater than balance for debit account");
|
||||
}
|
||||
balance -= amount;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,8 @@ package oving7.abstractaccount;
|
||||
*/
|
||||
public class SavingsAccount extends AbstractAccount {
|
||||
|
||||
// TODO: Add fields here
|
||||
private int withdrawals;
|
||||
private double fee;
|
||||
|
||||
/**
|
||||
* Initializes a new {@code SavingsAccount} with the specified number of
|
||||
@@ -27,8 +28,22 @@ public class SavingsAccount extends AbstractAccount {
|
||||
* negative
|
||||
*/
|
||||
public SavingsAccount(int withdrawals, double fee) {
|
||||
// TODO: Implement this constructor
|
||||
if (withdrawals < 0 || fee < 0.0) {
|
||||
throw new IllegalArgumentException("cannot have less than zero withdrawals or fee of less than zero");
|
||||
}
|
||||
this.withdrawals = withdrawals;
|
||||
this.fee = fee;
|
||||
}
|
||||
|
||||
// TODO: Override abstract method here
|
||||
protected void internalWithdraw(double amount) {
|
||||
withdrawals -= 1;
|
||||
double s = amount;
|
||||
if (withdrawals < 0) {
|
||||
s += fee;
|
||||
}
|
||||
if (balance < s) {
|
||||
throw new IllegalArgumentException("balance not great enough to cover fees");
|
||||
}
|
||||
balance -= s;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user