Merge branch 'main'

This commit is contained in:
2026-03-17 11:20:10 +01:00
44 changed files with 1971 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
package oving7.abstractaccount;
/**
* A bank consists of many different types of accounts: credit accounts, debit
* accounts, savings
* accounts, etc. Since these have a lot in common, e.g. all have a balance, it
* is practical to
* collect as much of the common logic as possible in a superclass, which all
* can inherit from.
* However, this superclass is not a type of account in itself, and therefore we
* make it
* {@code abstract}, so that it cannot be instantiated. The concrete account
* classes that inherit
* from it, must of course be instantiable. The methods defined in the
* {@code AbstractAccount} class
* is similar to that of the Account interface in the SavingsAccount task.
*/
public abstract class AbstractAccount {
// AbstractAccount has a state {@code balance} for the account balance. The
// balance should
// either be set to 0.0 by default or in the constructor
// TODO: Add fields and potentially a constructor here
/**
* Decreases the account balance by the specified amount. Note that the rules
* for withdrawals
* are different for the classes that implement {@code AbstractAccount}, and
* must therefore be
* implemented in each class.
*
* @param amount the amount to withdraw
* @throws IllegalArgumentException if the amount cannot be withdrawn
*/
// TODO: Define abstract method {@code void internalWithdraw} here
/**
* Increases the account balance by the specified amount.
*
* @param amount the amount to deposit
* @throws IllegalArgumentException if the amount is not positive
*/
public void deposit(double amount) {
// TODO: Implement this method
}
/**
* This method calls the {@link #internalWithdraw()} method, which is
* implemented in each
* subclass.
*
* @param amount the amount to withdraw
* @throws IllegalArgumentException if the amount is not positive
*/
public void withdraw(double amount) {
// TODO: Implement this method
}
/**
* @return the current balance of the account
*/
public double getBalance() {
// TODO: Implement this method
return 0.0;
}
}

View File

@@ -0,0 +1,54 @@
package oving7.abstractaccount;
/**
* A {@code CreditAccount} has in addition to {@code balance} a state for
* {@code creditLine}, i.e.
* available credit on the account. This credit line allows the account to be
* overdrawn (that the
* balance is negative) within the credit line. If {@link #internalWithdraw()}
* tries to withdraw
* more money than is available, taking the credit line into account, an
* {@code IllegalArgumentException} should be thrown.
*
* @see AbstractAccount
*/
public class CreditAccount extends AbstractAccount {
// TODO: Add fields here
/**
* Initializes a new {@code CreditAccount} with the specified credit line.
*
* @param creditLine the credit line
* @throws IllegalArgumentException if the credit line is negative
*/
public CreditAccount(double creditLine) {
// TODO: Implement this constructor
}
// TODO: Override abstract method here
/**
* @return the credit line
*
* @see CreditAccountTest#testCreditLine()
*/
public double getCreditLine() {
// TODO: Implement this method
return 0.0;
}
/**
* Sets the credit line.
*
* @param creditLine the credit line
* @throws IllegalArgumentException if the credit line is negative
* @throws IllegalStateException if the new credit line does not cover the
* existing balance
*
* @see CreditAccountTest#testCreditLine()
*/
public void setCreditLine(double creditLine) {
// TODO: Implement this method
}
}

View File

@@ -0,0 +1,19 @@
package oving7.abstractaccount;
/**
* A debit account is the simplest form of account, where the only requirement
* is that the balance
* at any time must be greater than or equal to {@code 0.0}.
* {@code DebitAccount} extends (inherits
* from) {@link AbstractAccount} and ensure that the balance never falls below
* {@code 0.0}. If an
* attempt is made to withdraw more money than is available, an
* {@code IllegalArgumentException}
* should be thrown.
*
* @see AbstractAccount
*/
public class DebitAccount extends AbstractAccount {
// TODO: Override abstract method here
}

View File

@@ -0,0 +1,34 @@
package oving7.abstractaccount;
/**
* A {@code SavingsAccount} can only have a positive balance. In addition, the
* account has
* withdrawal restrictions. A {@code SavingsAccount} has {@code x} number of
* {@code withdrawals}. If
* you want to withdraw money after all withdrawals have been used up, the
* balance should be charged
* a {@code fee}. If the balance is too low to cover the fee, an
* {@code IllegalArgumentException}
* should be thrown.
*
* @see AbstractAccount
*/
public class SavingsAccount extends AbstractAccount {
// TODO: Add fields here
/**
* Initializes a new {@code SavingsAccount} with the specified number of
* withdrawals and fee.
*
* @param withdrawals the number of withdrawals
* @param fee the fee
* @throws IllegalArgumentException if the number of withdrawals or the fee is
* negative
*/
public SavingsAccount(int withdrawals, double fee) {
// TODO: Implement this constructor
}
// TODO: Override abstract method here
}

View File

View File

@@ -0,0 +1,63 @@
package oving7.train;
/**
* One of two different types of train cars, both specialized versions for different purposes. A
* {@code CargoCar} represents a cargo car that transports various things and stuff.
*
* @see TrainCar
* @see PassengerCar
*/
public class CargoCar extends TrainCar {
// TODO: Add fields here
/**
* Constructor for the cargo car.
*
* @param deadWeight the weight of an empty cargo car
* @param cargoWeight the weight of the cargo in the cargo car
* @throws IllegalArgumentException if either deadWeight or cargoWeight is negative
*
* @see CargoCarTest#testWeight()
*/
public CargoCar(int deadWeight, int cargoWeight) {
// TODO: Implement this constructor
super(deadWeight);
}
/**
* @return the weight of the cargo in the cargo car
*
* @see CargoCarTest#testWeight()
*/
public int getCargoWeight() {
// TODO: Implement this method
return 0;
}
/**
* @param cargoWeight the weight of the cargo in the cargo car
* @throws IllegalArgumentException if cargoWeight is negative
*
* @see CargoCarTest#testWeight()
*/
public void setCargoWeight(int cargoWeight) {
// TODO: Implement this method
}
@Override
public int getTotalWeight() {
// TODO: Implement this method
return 0;
}
@Override
public String toString() {
// TODO: Implement this method
return null;
}
public static void main(String[] args) {
}
}

View File

@@ -0,0 +1,66 @@
package oving7.train;
/**
* One of two different types of train cars, both specialized versions for different purposes. A
* {@code PassengerCar} represents a passenger car that transports passengers.
*
* @see TrainCar
* @see CargoCar
*/
public class PassengerCar extends TrainCar {
// TODO: Add fields here
/**
* Constructor for the passenger car.
*
* @param deadWeight the weight of an empty passenger car
* @param passengerCount the number of passengers in the passenger car
* @throws IllegalArgumentException if either deadWeight or passengerCount is negative
*
* @see PassengerCarTest#testWeight()
*/
public PassengerCar(int deadWeight, int passengerCount) {
// TODO: Implement this constructor
super(deadWeight);
}
/**
* @return the number of passengers in the passenger car
*
* @see PassengerCarTest#testWeight()
*/
public int getPassengerCount() {
// TODO: Implement this method
return 0;
}
/**
* @param passengerCount the number of passengers in the passenger car
* @throws IllegalArgumentException if passengerCount is negative
*
* @see PassengerCarTest#testWeight()
*/
public void setPassengerCount(int passengerCount) {
// TODO: Implement this method
}
@Override
public int getTotalWeight() {
// To calculate the total weight of the passenger car, you can assume that an average
// passenger weighs 80 kg
// TODO: Implement this method
return 0;
}
@Override
public String toString() {
// TODO: Implement this method
return null;
}
public static void main(String[] args) {
}
}

View File

@@ -0,0 +1,81 @@
package oving7.train;
/**
* The class {@code Train} represents a train that consists of one or more train cars.
*
* @see TrainCar
* @see CargoCar
* @see PassengerCar
*/
public class Train {
// TODO: Add fields here
/**
* @param trainCar the train car to check for
* @return {@code true} if the train contains the train car, {@code false} otherwise
*
* @see TrainTest#testAddCarToTrain()
*/
public boolean contains(TrainCar trainCar) {
// TODO: Implement this method
return false;
}
/**
* Adds a train car to the train.
*
* @param trainCar the train car to add
* @throws IllegalArgumentException if the train car is {@code null}
*
* @see TrainTest#testAddCarToTrain()
*/
public void addTrainCar(TrainCar trainCar) {
// TODO: Implement this method
}
/**
* @return the sum of the total weight of all the train cars in the train. There is no need to
* take the weight of the locomotive into account
*
* @see TrainTest#testTotalTrainWeight()
*/
public int getTotalWeight() {
// TODO: Implement this method
return 0;
}
/**
* @return similar to {@link PassengerCar#getPassengerCount()}, but for the entire train
*
* @see TrainTest#testPassengerCount()
*/
public int getPassengerCount() {
// TODO: Implement this method
return 0;
}
/**
* @return similar to {@link CargoCar#getCargoWeight()}, but for the entire train
*
* @see TrainTest#testCargoWeight()
*/
public int getCargoWeight() {
// TODO: Implement this method
return 0;
}
/**
* @return a string representation of the train. The string should consist of the
* {@link #toString()}s of all train cars in the train
*/
@Override
public String toString() {
return null;
}
// TODO: Write a main method to test the class
public static void main(String[] args) {
}
}

View File

@@ -0,0 +1,71 @@
package oving7.train;
/**
* The class {@code TrainCar} represents a simple and general train car.
*/
public class TrainCar {
// TODO: Add fields here
/**
* Constructor for a train car.
*
* @param deadWeight the weight of an empty train car
* @throws IllegalArgumentException if deadWeight is negative
*
* @see TrainCarTest#testDeadWeight()
*/
public TrainCar(int deadWeight) {
// TODO: Implement this constructor
}
/**
* @param deadWeight the weight of an empty train car. In other words, the weight of only the
* carriage, without passengers and cargo
* @throws IllegalArgumentException if deadWeight is negative
*
* @see TrainCarTest#testDeadWeight()
*/
public void setDeadWeight(int deadWeight) {
// TODO: Implement this method
}
/**
* @return the weight of an empty train car. In other words, the weight of only the carriage,
* without passengers and cargo
*
* @see TrainCarTest#testDeadWeight()
*/
public int getDeadWeight() {
// TODO: Implement this method
return 0;
}
/**
* @return the total weight of the train car. Note that this method should also be callable on
* subclasses and still return the total weight of the train car (keyword:
* redefinition).
*
* @see TrainCarTest#testDeadWeight()
*/
public int getTotalWeight() {
// TODO: Implement this method
return 0;
}
/**
* @return a string representation of the train car. The string should contain the type of the
* train car and the total weight of the train car. For {@link PassengerCar}, the number
* of passengers should also be included. For {@link CargoCar}, the weight of the cargo
* should also be included.
*/
@Override
public String toString() {
// TODO: Implement this method
return null;
}
public static void main(String[] args) {
}
}

View File

@@ -0,0 +1,79 @@
package oving7.abstractaccount;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class CreditAccountTest {
private static final double epsilon = 0.0005;
private CreditAccount sub;
@BeforeEach
public void setUp() {
sub = new CreditAccount(10_000.0);
}
@Test
@DisplayName("Check that deposits work as expected")
public void testDeposit() {
assertEquals(0.0, sub.getBalance(), epsilon, "The account balance was incorrect");
sub.deposit(10_000.0);
assertEquals(10_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
assertThrows(IllegalArgumentException.class, () -> {
sub.deposit(-10_000.0);
}, "Negative deposit should have triggered an IllegalArgumentException!");
assertEquals(10_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
}
@Test
@DisplayName("Check that withdrawals work as expected")
public void testWithdraw() {
sub.deposit(20_000.0);
sub.withdraw(5000.0);
assertEquals(15_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
assertThrows(IllegalArgumentException.class, () -> {
sub.withdraw(-10_000.0);
}, "Negative withdrawal should have triggered an IllegalArgumentException!");
assertEquals(15_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
sub.withdraw(20_000.0);
assertEquals(-5000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
assertThrows(IllegalArgumentException.class, () -> {
sub.withdraw(20_000.0);
}, "Withdrawal exceeding the credit limit should have triggered an IllegalArgumentException");
}
@Test
@DisplayName("Check that the credit limit works as expected")
public void testCreditLine() {
assertEquals(10_000.0, sub.getCreditLine(), epsilon, "The credit limit was incorrect");
sub.setCreditLine(5000.0);
assertEquals(5000.0, sub.getCreditLine(), epsilon, "The credit limit was incorrect");
assertThrows(IllegalArgumentException.class, () -> {
sub.setCreditLine(-5000.0);
}, "Cannot have a negative credit limit");
assertEquals(5000.0, sub.getCreditLine(), epsilon, "The credit limit was incorrect");
sub.withdraw(4000.0);
assertThrows(IllegalStateException.class, () -> {
sub.setCreditLine(3000.0);
}, "Cannot set a credit limit that would result in an invalid balance");
assertEquals(-4000.0, sub.getBalance(), epsilon, "The balance was incorrect");
assertEquals(5000.0, sub.getCreditLine(), epsilon, "The credit limit was incorrect");
}
}

View File

@@ -0,0 +1,52 @@
package oving7.abstractaccount;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class DebitAccountTest {
private static final double epsilon = 0.0005;
private DebitAccount sub;
@BeforeEach
public void setUp() {
sub = new DebitAccount();
}
@Test
@DisplayName("Check that deposits work as expected")
public void testDeposit() {
assertEquals(0.0, sub.getBalance(), epsilon, "The account balance was incorrect");
sub.deposit(10_000.0);
assertEquals(10_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
assertThrows(IllegalArgumentException.class, () -> {
sub.deposit(-10_000.0);
}, "Negative deposit should have triggered an IllegalArgumentException!");
assertEquals(10_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
}
@Test
@DisplayName("Check that withdrawals work as expected")
public void testWithdraw() {
sub.deposit(20_000.0);
sub.withdraw(5000.0);
assertEquals(15_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
assertThrows(IllegalArgumentException.class, () -> {
sub.withdraw(-10_000.0);
}, "Negative withdrawal should have triggered an IllegalArgumentException!");
assertEquals(15_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
assertThrows(IllegalArgumentException.class, () -> {
sub.withdraw(20_000.0);
}, "Withdrawal exceeding the balance should have triggered an IllegalArgumentException");
}
}

View File

@@ -0,0 +1,65 @@
package oving7.abstractaccount;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class SavingsAccountTest {
private static final double epsilon = 0.0005;
private SavingsAccount sub;
@BeforeEach
public void setUp() {
sub = new SavingsAccount(1, 50.0);
}
@Test
@DisplayName("Check that deposits work as expected")
public void testDeposit() {
assertEquals(0.0, sub.getBalance(), epsilon, "The account balance was incorrect");
sub.deposit(10_000.0);
assertEquals(10_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
assertThrows(IllegalArgumentException.class, () -> {
sub.deposit(-10_000.0);
}, "Negative deposit should have triggered an IllegalArgumentException!");
assertEquals(10_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
}
@Test
@DisplayName("Check that withdrawals work as expected")
public void testWithdraw() {
sub.deposit(20_000.0);
sub.withdraw(5000.0);
assertEquals(15_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
assertThrows(IllegalArgumentException.class, () -> {
sub.withdraw(-10_000.0);
}, "Negative withdrawal should have triggered an IllegalArgumentException!");
assertEquals(15_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
assertThrows(IllegalArgumentException.class, () -> {
sub.withdraw(20_000.0);
}, "Withdrawal exceeding the balance should have triggered an IllegalArgumentException");
assertEquals(15_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
sub.withdraw(10_000.0);
assertEquals(4950.0, sub.getBalance(), epsilon,
"The account balance was incorrect after the fee was deducted");
assertThrows(IllegalArgumentException.class, () -> {
sub.withdraw(4930.0);
}, "Withdrawal exceeding the balance + fee should have triggered an IllegalArgumentException");
assertEquals(4950.0, sub.getBalance(), epsilon,
"The account balance was incorrect after the fee was deducted");
}
}

View File

@@ -0,0 +1,87 @@
package oving7.card;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class CardDeckTest {
private Card s1;
private Card s2;
private Card h1;
private Card h2;
private Card d1;
private Card d2;
private Card c1;
private Card c2;
private CardDeck deck;
private List<Card> expected;
private static void testCards(CardContainer it, List<Card> expected) {
assertEquals(expected.size(), it.getCardCount());
for (int i = 0; i < expected.size(); i++) {
Card expectedCard = expected.get(i);
Card actualCard = it.getCard(i);
assertEquals(expectedCard.getSuit(), actualCard.getSuit(),
String.format("Card number %d should have been %s, but was %s", i + 1,
expectedCard, actualCard));
assertEquals(expectedCard.getFace(), actualCard.getFace(),
String.format("Card number %d should have been %s, but was %s", i + 1,
expectedCard, actualCard));
i++;
}
}
private static void testCards(Iterator<Card> actual, Iterator<Card> expected) {
while (expected.hasNext()) {
assertTrue(actual.hasNext());
Card expectedCard = expected.next();
Card actualCard = actual.next();
assertEquals(expectedCard.getSuit(), actualCard.getSuit(), String
.format("The card should have been %s, but was %s", expectedCard, actualCard));
assertEquals(expectedCard.getFace(), actualCard.getFace(), String
.format("The card should have been %s, but was %s", expectedCard, actualCard));
}
}
@BeforeEach
public void setUp() {
deck = new CardDeck(2);
s1 = new Card('S', 1);
s2 = new Card('S', 2);
h1 = new Card('H', 1);
h2 = new Card('H', 2);
d1 = new Card('D', 1);
d2 = new Card('D', 2);
c1 = new Card('C', 1);
c2 = new Card('C', 2);
expected = new LinkedList<>(List.of(s1, s2, h1, h2, d1, d2, c1, c2));
}
@Test
@DisplayName("Test maxCardCount")
public void testMaxCardCount() {
assertTrue(deck instanceof CardContainerImpl);
assertEquals(52, deck.getMaxCardCount());
}
@Test
@DisplayName("Test that CardDeckImpl implements CardContainer")
public void testCardContainer() {
CardDeckTest.testCards(deck, expected);
}
@Test
@DisplayName("Test that CardDeckImpl implements Iterable")
public void testDeckIterator() {
CardDeckTest.testCards(deck.iterator(), expected.iterator());
}
}

View File

@@ -0,0 +1,82 @@
package oving7.card;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Iterator;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class CardHandTest {
private Card c2;
private Card s1;
private CardHand hand;
private List<Card> expected;
private static void testCards(CardContainer it, List<Card> expected) {
assertEquals(expected.size(), it.getCardCount());
for (int i = 0; i < expected.size(); i++) {
Card expectedCard = expected.get(i);
Card actualCard = it.getCard(i);
assertEquals(expectedCard.getSuit(), actualCard.getSuit(),
String.format("Card number %d should have been %s, but was %s", i + 1,
expectedCard, actualCard));
assertEquals(expectedCard.getFace(), actualCard.getFace(),
String.format("Card number %d should have been %s, but was %s", i + 1,
expectedCard, actualCard));
i++;
}
}
private static void testCards(Iterator<Card> actual, Iterator<Card> expected) {
while (expected.hasNext()) {
assertTrue(actual.hasNext());
Card expectedCard = expected.next();
Card actualCard = actual.next();
assertEquals(expectedCard.getSuit(), actualCard.getSuit(), String
.format("The card should have been %s, but was %s", expectedCard, actualCard));
assertEquals(expectedCard.getFace(), actualCard.getFace(), String
.format("The card should have been %s, but was %s", expectedCard, actualCard));
}
}
@BeforeEach
public void setUp() {
hand = new CardHand(2);
s1 = new Card('S', 1);
c2 = new Card('C', 2);
expected = List.of(s1, c2);
}
@Test
@DisplayName("Test cardCount")
public void testCardCount() {
assertTrue(hand instanceof CardContainerImpl);
assertEquals(0, hand.getCardCount(), "CardCount should have been 0 at the start");
hand.addCard(new Card('S', 1));
hand.addCard(new Card('S', 2));
assertEquals(2, hand.getCardCount(), "CardCount should have been 2 at the start");
}
@Test
@DisplayName("Test that cardDeckImpl implements cardContainer")
public void testCardContainer() {
hand.addCard(new Card('S', 1));
hand.addCard(new Card('C', 2));
CardHandTest.testCards(hand, expected);
}
@Test
@DisplayName("Test that cardDeckImpl implements iterable")
public void testDeckIterator() {
hand.addCard(new Card('S', 1));
hand.addCard(new Card('C', 2));
CardHandTest.testCards(hand.iterator(), expected.iterator());
}
}

View File

@@ -0,0 +1,162 @@
package oving7.observablelist;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class ObservableHighscoreListTest {
private int pos1;
private int pos2;
private ObservableHighscoreList highscoreList;
private static void checkHighscoreList(String contextMessage, ObservableHighscoreList list,
List<Integer> elements) {
assertEquals(elements.size(), list.size(),
contextMessage + " -> Testing the length of the highscore list");
int i = 0;
for (int element : elements) {
assertEquals(element, list.getElement(i),
contextMessage + " -> Testing that the element at position " + i + " matches");
i++;
}
}
private void addResultWithListener(int pos, int element) {
pos1 = pos;
highscoreList.addResult(element);
// Check that the position that was changed is the same as the one sent to the listener
assertEquals(pos1, pos2, "Added " + element + " at position " + pos
+ " -> Testing the position received by the listener");
}
@BeforeEach
public void setUp() {
highscoreList = new ObservableHighscoreList(3);
pos1 = -1;
pos2 = -1;
}
@Test
@DisplayName("Test constructor")
public void testConstructor() {
assertEquals(0, highscoreList.size(), "Testing initialization of the highscore list");
}
@Test
@DisplayName("Add results (simple)")
public void testAddElementSimple() {
highscoreList.addResult(5);
ObservableHighscoreListTest.checkHighscoreList("Added 5 to an empty list", highscoreList,
List.of(5));
highscoreList.addResult(6);
ObservableHighscoreListTest.checkHighscoreList("Added 6 to the list [5]", highscoreList,
List.of(5, 6));
highscoreList.addResult(2);
ObservableHighscoreListTest.checkHighscoreList("Added 2 to the list [5, 6]", highscoreList,
List.of(2, 5, 6));
}
@Test
@DisplayName("Add results - list becomes too long")
public void testAddElementMoreThanMax() {
highscoreList.addResult(5);
highscoreList.addResult(6);
highscoreList.addResult(2);
ObservableHighscoreListTest.checkHighscoreList("Added 5, 6, and 2 to the list",
highscoreList, List.of(2, 5, 6));
highscoreList.addResult(3);
ObservableHighscoreListTest.checkHighscoreList("Added 3 to the list [2, 5, 6]",
highscoreList, List.of(2, 3, 5));
highscoreList.addResult(7);
ObservableHighscoreListTest.checkHighscoreList("Added 7 to the list [2, 3, 5]",
highscoreList, List.of(2, 3, 5));
}
@Test
@DisplayName("Add two identical elements")
public void testAddElementDuplicate() {
highscoreList.addResult(5);
highscoreList.addResult(6);
highscoreList.addResult(2);
ObservableHighscoreListTest.checkHighscoreList("Added 5, 6, and 2 to the list",
highscoreList, List.of(2, 5, 6));
highscoreList.addResult(2);
ObservableHighscoreListTest.checkHighscoreList("Added 2 to the list [2, 5, 6]",
highscoreList, List.of(2, 2, 5));
}
@Test
@DisplayName("Test listeners (simple)")
public void testListListenersSimple() {
// Mock a listener
ObservableListListener listener = (list, pos) -> pos2 = pos;
highscoreList.addObservableListListener(listener);
this.addResultWithListener(0, 5);
ObservableHighscoreListTest.checkHighscoreList("Added 5 to the list []", highscoreList,
List.of(5));
this.addResultWithListener(1, 6);
ObservableHighscoreListTest.checkHighscoreList("Added 6 to the list [5]", highscoreList,
List.of(5, 6));
this.addResultWithListener(0, 2);
ObservableHighscoreListTest.checkHighscoreList("Added 2 to the list [5, 6]", highscoreList,
List.of(2, 5, 6));
}
@Test
@DisplayName("With listener - list becomes too long")
public void testListListenerMoreThanMax() {
// Mock a listener
ObservableListListener listener = (list, pos) -> pos2 = pos;
highscoreList.addObservableListListener(listener);
highscoreList.addResult(5);
highscoreList.addResult(6);
highscoreList.addResult(2);
ObservableHighscoreListTest.checkHighscoreList("Added 5, 6, and 2 to the list",
highscoreList, List.of(2, 5, 6));
this.addResultWithListener(1, 3);
ObservableHighscoreListTest.checkHighscoreList("Added 3 to the list [2, 5, 6]",
highscoreList, List.of(2, 3, 5));
// Reset pos2 since the next element falls outside the list and is therefore not updated by
// itself and sent to the listener
pos2 = -1;
this.addResultWithListener(-1, 7);
ObservableHighscoreListTest.checkHighscoreList("Added 7 to the list [2, 3, 5]",
highscoreList, List.of(2, 3, 5));
}
@Test
@DisplayName("With listener - two identical elements")
public void testListListenerDuplicate() {
// Mock a listener
ObservableListListener listener = (list, pos) -> pos2 = pos;
highscoreList.addObservableListListener(listener);
highscoreList.addResult(5);
highscoreList.addResult(6);
highscoreList.addResult(2);
ObservableHighscoreListTest.checkHighscoreList("Added 5, 6, and 2 to the list",
highscoreList, List.of(2, 5, 6));
this.addResultWithListener(1, 2);
ObservableHighscoreListTest.checkHighscoreList("Added 2 to the list [2, 5, 6]",
highscoreList, List.of(2, 2, 5));
}
}

View File

@@ -0,0 +1,106 @@
package oving7.observablelist;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class ObservableListTest {
private int pos1;
private int pos2;
private ObservableList observableList;
private static void checkObservableList(ObservableList list, List<Integer> elements,
String contextMessage) {
assertEquals(elements.size(), list.size(),
contextMessage + " -> Testing the length of observableList");
int i = 0;
for (int element : elements) {
assertEquals(element, list.getElement(i),
contextMessage + " -> Testing that the element at position " + i + " matches");
i++;
}
}
private void addElementWithListener(int pos, int element) {
pos1 = pos;
observableList.addElement(pos, element);
// Check that the position that was changed is the same as the one sent to the listener
assertEquals(pos1, pos2, "Added " + element + " at position " + pos
+ " -> Testing the position received by the listener");
}
@BeforeEach
public void setUp() {
observableList = new ObservableList() {
@Override
public boolean acceptsElement(Object element) {
return element instanceof Integer;
}
};
pos1 = -1;
pos2 = -1;
}
@Test
@DisplayName("Test constructor")
public void testConstructor() {
assertEquals(0, observableList.size());
}
@Test
@DisplayName("Test acceptance of elements")
public void testAcceptsElement() {
assertTrue(observableList.acceptsElement(5), "Testing that the list accepts integers");
assertFalse(observableList.acceptsElement("5"),
"Testing that the list does not accept strings");
assertThrows(IllegalArgumentException.class, () -> {
observableList.addElement("5");
}, "Testing that the list cannot accept elements of type string");
}
@Test
@DisplayName("Test adding elements")
public void testAddElement() {
observableList.addElement(5);
ObservableListTest.checkObservableList(observableList, List.of(5),
"Added 5 to an empty list");
observableList.addElement(6);
ObservableListTest.checkObservableList(observableList, List.of(5, 6),
"Added 6 to the list [5]");
observableList.addElement(0, 2);
ObservableListTest.checkObservableList(observableList, List.of(2, 5, 6),
"Added 2 at position 0 in the list [5, 6]");
}
@Test
@DisplayName("Test listener")
public void testListListener() {
ObservableListListener listener = (list, pos) -> pos2 = pos;
observableList.addObservableListListener(listener);
this.addElementWithListener(0, 5);
ObservableListTest.checkObservableList(observableList, List.of(5),
"Added 5 to the list []");
this.addElementWithListener(1, 6);
ObservableListTest.checkObservableList(observableList, List.of(5, 6),
"Added 6 to the list [5]");
this.addElementWithListener(0, 2);
ObservableListTest.checkObservableList(observableList, List.of(2, 5, 6),
"Added 2 to the list [5, 6]");
}
}

View File

@@ -0,0 +1,84 @@
package oving7.savingsaccount;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class BSUTest {
private static final double epsilon = 0.001;
private BSU bsu;
@BeforeEach
public void setUp() {
bsu = new BSU(0.05, 25_000.0);
}
@Test
@DisplayName("Check that deposits work as expected")
public void testDeposit() {
bsu.deposit(10_000.0);
assertEquals(10_000.0, bsu.getBalance(), epsilon, "The account balance was incorrect");
assertThrows(IllegalArgumentException.class, () -> {
bsu.deposit(-100.0);
}, "Negative deposit should have triggered an IllegalArgumentException!");
assertThrows(IllegalStateException.class, () -> {
bsu.deposit(20_000.0);
}, "Should not be able to deposit more money than the deposit limit");
bsu.endYearUpdate();
bsu.deposit(20_000.0);
assertEquals(10_000.0 * (1 + 0.05) + 20_000.0, bsu.getBalance(), epsilon,
"The account balance was incorrect");
}
@Test
@DisplayName("Check that withdrawals work as expected")
public void testWithdraw() {
bsu.deposit(20_000.0);
bsu.withdraw(5000.0);
assertEquals(15_000.0, bsu.getBalance(), epsilon, "The account balance was incorrect");
assertThrows(IllegalArgumentException.class, () -> {
bsu.withdraw(-10_000.0);
}, "Negative withdrawal should have triggered an IllegalArgumentException!");
assertEquals(15_000.0, bsu.getBalance(), epsilon, "The account balance was incorrect");
assertThrows(IllegalStateException.class, () -> {
bsu.withdraw(20_000);
}, "Should not be able to withdraw more money than deposited this year");
assertEquals(15_000.0, bsu.getBalance(), epsilon, "The account balance was incorrect");
bsu.endYearUpdate();
assertThrows(IllegalStateException.class, () -> {
bsu.withdraw(10_000);
}, "Should not be able to withdraw more money than deposited this year");
assertEquals(15_000 * (1 + 0.05), bsu.getBalance(), epsilon,
"The account balance was incorrect");
}
@Test
@DisplayName("Check that the tax deduction is correct")
public void testTaxDeduction() {
bsu.deposit(20_000.0);
assertEquals(20_000.0 * 0.20, bsu.getTaxDeduction(), epsilon,
"The tax deduction was incorrect");
bsu.endYearUpdate();
bsu.deposit(10_000.0);
assertEquals(10_000.0 * 0.20, bsu.getTaxDeduction(), epsilon,
"The tax deduction was incorrect");
bsu.endYearUpdate();
assertEquals(0.0, bsu.getTaxDeduction(), epsilon, "The tax deduction was incorrect");
}
}

View File

@@ -0,0 +1,72 @@
package oving7.savingsaccount;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class ForeldreSparTest {
private static final double epsilon = 0.001;
private ForeldreSpar foreldreSpar;
@BeforeEach
public void setUp() {
foreldreSpar = new ForeldreSpar(0.04, 3);
}
@Test
@DisplayName("Check that withdraw works as expected")
public void testWithdraw() {
foreldreSpar.deposit(10_000.0);
foreldreSpar.withdraw(1000.0);
assertEquals(9000.0, foreldreSpar.getBalance(), epsilon,
"The account balance is incorrect");
assertThrows(IllegalArgumentException.class, () -> {
foreldreSpar.withdraw(-10_000.0);
}, "Negative withdrawal should have triggered an IllegalArgumentException!");
assertEquals(9000, foreldreSpar.getBalance(), epsilon, "The account balance is incorrect");
assertThrows(IllegalStateException.class, () -> {
foreldreSpar.withdraw(10_000);
}, "Should not be able to withdraw more money than is in the account");
assertEquals(9000.0, foreldreSpar.getBalance(), epsilon,
"The account balance is incorrect");
foreldreSpar.withdraw(1000.0);
foreldreSpar.withdraw(1000.0);
assertThrows(IllegalStateException.class, () -> {
foreldreSpar.withdraw(1000.0);
}, "Should not be able to make more withdrawals than the set limit");
foreldreSpar.endYearUpdate();
foreldreSpar.withdraw(1000.0);
assertEquals(7000.0 * (1 + 0.04) - 1000.0, foreldreSpar.getBalance(), epsilon,
"The account balance is incorrect");
}
@Test
@DisplayName("Check that remaining withdrawals are always correct")
public void testRemainingWithdrawals() {
foreldreSpar.deposit(10_000.0);
foreldreSpar.withdraw(1000.0);
assertEquals(2, foreldreSpar.getRemainingWithdrawals());
foreldreSpar.withdraw(1000.0);
foreldreSpar.withdraw(1000.0);
assertEquals(0, foreldreSpar.getRemainingWithdrawals());
assertThrows(IllegalStateException.class, () -> {
foreldreSpar.withdraw(1000.0);
}, "Should not be able to make more withdrawals than the set limit");
foreldreSpar.endYearUpdate();
assertEquals(3, foreldreSpar.getRemainingWithdrawals());
}
}

View File

@@ -0,0 +1,76 @@
package oving7.savingsaccount;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class SavingsAccountTest {
private static final double epsilon = 0.001;
private SavingsAccount savingsAccount;
@BeforeEach
public void setUp() {
savingsAccount = new SavingsAccount(0.1);
}
@Test
@DisplayName("Check that the balance is correct after deposit")
public void testBalance() {
savingsAccount.deposit(100.0);
assertEquals(100.0, savingsAccount.getBalance(), epsilon,
"The account balance was incorrect");
}
@Test
@DisplayName("Test deposit and withdraw")
public void testDepositAndWithdraw() {
savingsAccount.deposit(100.0);
savingsAccount.withdraw(40.0);
assertEquals(60.0, savingsAccount.getBalance(), epsilon,
"The account balance was incorrect");
}
@Test
@DisplayName("Test deposit and withdraw with illegal input")
public void testDepositAndWithdrawIllegalInput() {
savingsAccount.deposit(10.0);
assertThrows(IllegalArgumentException.class, () -> {
savingsAccount.deposit(-100.0);
}, "Negative deposit should have triggered an IllegalArgumentException!");
assertEquals(10.0, savingsAccount.getBalance(), epsilon,
"The account balance was incorrect");
savingsAccount.deposit(10.0);
assertThrows(IllegalArgumentException.class, () -> {
savingsAccount.withdraw(-100.0);
}, "Negative withdrawal should have triggered an IllegalArgumentException!");
assertEquals(20.0, savingsAccount.getBalance(), epsilon,
"The account balance was incorrect");
savingsAccount.deposit(10.0);
assertThrows(IllegalStateException.class, () -> {
savingsAccount.withdraw(40.0);
}, "Withdrawal of more than the balance should have triggered an IllegalStateException");
assertEquals(30.0, savingsAccount.getBalance(), epsilon,
"The account balance was incorrect");
}
@Test
@DisplayName("Test that the interest is calculated correctly")
public void endYearUpdate() {
savingsAccount.deposit(100.0);
savingsAccount.endYearUpdate();
assertEquals(100.0 * (1 + 0.10), savingsAccount.getBalance(), epsilon,
"The account balance was incorrect after interest was added");
}
}

View File

@@ -0,0 +1,28 @@
package oving7.train;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class CargoCarTest {
private CargoCar cargoCar;
@BeforeEach
public void setUp() {
cargoCar = new CargoCar(3000, 2000);
}
@Test
@DisplayName("Check total weight")
public void testWeight() {
assertEquals(5000, cargoCar.getTotalWeight(), "Test total weight after initialization");
cargoCar.setCargoWeight(4000);
assertEquals(7000, cargoCar.getTotalWeight(),
"Test total weight after changing cargo weight");
assertEquals(4000, cargoCar.getCargoWeight(),
"Test cargo weight after changing the weight");
}
}

View File

@@ -0,0 +1,29 @@
package oving7.train;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class PassengerCarTest {
private PassengerCar passengerCar;
@BeforeEach
public void setUp() {
passengerCar = new PassengerCar(3000, 200);
}
@Test
@DisplayName("Check total weight")
public void testWeight() {
assertEquals(3000 + (200 * 80), passengerCar.getTotalWeight(),
"Test total weight after initialization");
passengerCar.setPassengerCount(100);
assertEquals(3000 + (100 * 80), passengerCar.getTotalWeight(),
"Test total weight after changing the number of passengers");
assertEquals(100, passengerCar.getPassengerCount(),
"Test passenger count after changing the number");
}
}

View File

@@ -0,0 +1,26 @@
package oving7.train;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class TrainCarTest {
private TrainCar trainCar;
@BeforeEach
public void setUp() {
trainCar = new TrainCar(3000);
}
@Test
@DisplayName("Dead weight equals total weight")
public void testDeadWeight() {
assertEquals(3000, trainCar.getTotalWeight(), "Test initialization of dead weight");
trainCar.setDeadWeight(5000);
assertEquals(5000, trainCar.getTotalWeight(),
"Test that total weight equals set dead weight");
}
}

View File

@@ -0,0 +1,85 @@
package oving7.train;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class TrainTest {
private CargoCar cc1;
private CargoCar cc2;
private PassengerCar pc1;
private PassengerCar pc2;
private Train train;
@BeforeEach
public void setUp() {
train = new Train();
pc1 = new PassengerCar(2000, 200);
pc2 = new PassengerCar(1500, 100);
cc1 = new CargoCar(3000, 5000);
cc2 = new CargoCar(2500, 7000);
}
@Test
@DisplayName("Add cars to train")
public void testAddCarToTrain() {
train.addTrainCar(pc1);
train.addTrainCar(pc2);
train.addTrainCar(cc1);
assertTrue(train.contains(pc1),
"Test if the train contains passenger car 1 after it has been added");
assertTrue(train.contains(pc2),
"Test if the train contains passenger car 2 after it has been added");
assertTrue(train.contains(cc1),
"Test if the train contains cargo car 1 after it has been added");
assertFalse(train.contains(cc2),
"Test if the train contains cargo car 2 without it being added");
assertThrows(IllegalArgumentException.class, () -> {
train.addTrainCar(null);
}, "Test if an IllegalArgumentException is thrown when adding a null car");
}
@Test
@DisplayName("Check total weight of the train")
public void testTotalTrainWeight() {
train.addTrainCar(pc1);
train.addTrainCar(cc1);
assertEquals(8000 + (2000 + (200 * 80)), train.getTotalWeight(),
"Test the train's total weight after adding a passenger car and a cargo car");
train.addTrainCar(pc2);
assertEquals(8000 + (2000 + (200 * 80)) + (1500 + (100 * 80)), train.getTotalWeight(),
"Test the train's total weight after adding another passenger car");
}
@Test
@DisplayName("Check passenger count on the train")
public void testPassengerCount() {
train.addTrainCar(pc1);
train.addTrainCar(pc2);
assertEquals(300, train.getPassengerCount(),
"Test passenger count after adding passenger cars");
train.addTrainCar(cc1);
assertEquals(300, train.getPassengerCount(),
"Test passenger count after adding a cargo car");
}
@Test
@DisplayName("Check cargo weight on the train")
public void testCargoWeight() {
train.addTrainCar(cc1);
train.addTrainCar(cc2);
assertEquals(12_000, train.getCargoWeight(), "Test cargo weight after adding cargo cars");
train.addTrainCar(pc1);
assertEquals(12_000, train.getCargoWeight(),
"Test cargo weight after adding a passenger car");
}
}