Skip to content

Tests finished #2

Merged
merged 21 commits into from
Feb 18, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d802061
chore: Google java style cleanup (checkstyle)
pawelsa Feb 13, 2026
a4e968c
chore: Set unspecified variable
pawelsa Feb 13, 2026
b30e599
chore: JavaDocs, Set unspecified return.
pawelsa Feb 13, 2026
766a141
chore(Purchase, Sale): Add JavaDoc and reorganize imports.
pawelsa Feb 13, 2026
f25f4fb
feat: add JUnit tests structure
pawelsa Feb 13, 2026
df77f35
fix: pom.xml dependency/plugins reorganization
pawelsa Feb 13, 2026
6ca4068
feat(Stock): Add initial tests and fix corresponding class.
pawelsa Feb 13, 2026
b5f06fb
feat(Share): Add initial tests.
pawelsa Feb 13, 2026
cbda1fb
feat(Portfolio): Add initial tests and fix corresponding class.
pawelsa Feb 14, 2026
5ab252d
feat(PurchaseCalculator): Add initial tests.
pawelsa Feb 15, 2026
e85d4ae
feat(SaleCalculator): Add initial tests and fix corresponding class.
pawelsa Feb 15, 2026
a497a75
feat(Purchase): Add initial tests.
pawelsa Feb 15, 2026
1c4ce00
feat(Sale): Add initial tests.
pawelsa Feb 15, 2026
a1facac
chore: Removed redundant test classes.
pawelsa Feb 15, 2026
553bc00
feat(TransactionArchive): Add initial tests and fix corresponding class.
pawelsa Feb 15, 2026
778cb4d
feat(Player): Add initial tests.
pawelsa Feb 15, 2026
1cef437
feat(Exchange): Add initial tests and fix corresponding class.
pawelsa Feb 16, 2026
3a6400d
chore(Test classes): cleanup code and some JavaDocs.
pawelsa Feb 16, 2026
1ed22c0
feat(Portfolio): Add exception.
pawelsa Feb 16, 2026
b2fb41e
refactor: README.md about tests.
pawelsa Feb 16, 2026
27f5b4a
chore: Cleanup.i
Feb 18, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
# Mappevurdering-IDATT2003

The exam of IDATT2003, "Programming 2" subject.

# Guide to tests

- The test generally start with a @BeforeEach method that sets up everything necessary for positive testing.
- Then there comes a positive test section and at last the negative tests.
- Complicated test methods have necessary JavaDocs and often have references pointing to other classes that share content or hold relevant information.
- Dictionary
- **PT** - Positive tests
- **NT** - Negative tests
35 changes: 19 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.1</version>
</dependency>

<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.4</version>
</dependency>


<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
Expand All @@ -39,12 +33,27 @@
<version>25.0.1</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>6.0.1</version>
<scope>test</scope>
</dependency>



</dependencies>

<build>

<plugins>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.4</version>
</plugin>


<plugin>
<groupId>org.openjfx</groupId>
Expand Down Expand Up @@ -78,13 +87,7 @@
</configuration>
</plugin>

<plugin>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>6.0.1</version>
</plugin>

</plugins>
</plugins>
</build>

</project>
</project>
202 changes: 141 additions & 61 deletions src/main/java/edu/ntnu/idi/idatt/Exchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,73 +9,153 @@
import java.math.BigDecimal;
import java.util.*;

/**
* Exchange class
*
* <p>
* Class that keeps the 'stock game' gameloop.
* Contains methods for managing game states aswell as performing
* all functionality.
* </p>
*
*/
public class Exchange {

//TODO: JavaDocs, Write over functions mby

private final String name;
private int week;
private HashMap<String, Stock> stockMap = new HashMap<>();
private Random random = new Random();

public Exchange(String name, List<Stock> stocks) {
this.name = name;
this.week = 1;

for(Stock stock : stocks){
stockMap.put(stock.getSymbol(), stock);
}

}

public String getName() {
return name;
}

public int getWeek() {
return week;
}

public boolean hasStock(String symbol) {
return stockMap.containsKey(symbol);
private final String name;
private int week;
private HashMap<String, Stock> stockMap = new HashMap<>();
private Random random = new Random();

/**
* Constructor for Exchange class
*
* @param name - Name of the current stock Exchange
* @param stocks - List of aviable stocks for this exchange.
*/
public Exchange(String name, List<Stock> stocks) {
this.name = name;
this.week = 1;

for (Stock stock : stocks) {
stockMap.put(stock.getSymbol(), stock);
}

public Stock getStock(String symbol) {
if(this.hasStock(symbol)){
stockMap.get(symbol);
}
return null; //TODO: Exception
}

/**
*
* Getters
*
* @return - their corresponding variables.
*/

public String getName() {
return name;
}

public int getWeek() {
return week;
}

/**
* Method for checking if a specific stock exists in the exchange.
*
* @param symbol - String symbol of a specific stock.
* @return - true/false if the exchange has the specific stock.
*/
public boolean hasStock(String symbol) {
return stockMap.containsKey(symbol);
}

/**
* Getter for a specific stock.
*
* @param symbol - String symbol of a specific stock.
* @return - The found stock if existant.
* @throws IllegalArgumentException if invalid symbol given.
*/
public Stock getStock(String symbol) {
if (this.hasStock(symbol)) {
return stockMap.get(symbol);
}

public List<Stock> findStocks(String searchTerm) {
ArrayList<Stock> stocksFound = new ArrayList<>();

for(Stock stock : stockMap.values()) {
if(stock.getCompany().contains(searchTerm) || stock.getSymbol().contains(searchTerm)){
stocksFound.add(stock);
}
}
return stocksFound;
throw new IllegalArgumentException("This stock doesn't exist in [" + name + "] exchange.");
}

/**
* Method for searching after stocks.
*
* @param searchTerm - String or character sequence of corporation name /
* corresponding symbol.
* @return - List of found stocks.
*/
public List<Stock> findStocks(String searchTerm) {
ArrayList<Stock> stocksFound = new ArrayList<>();

for (Stock stock : stockMap.values()) {
if (stock.getCompany().contains(searchTerm) || stock.getSymbol().contains(searchTerm)) {
stocksFound.add(stock);
}
}

public Transaction buy(String symbol, BigDecimal quantity, Player player) {
Share share = new Share(getStock(symbol), quantity, BigDecimal.valueOf(random.nextDouble()));
Purchase purchase = new Purchase(share, this.week);
purchase.commit(player);
return player.getTransactionArchive().getPurchases(this.week).getLast();
return stocksFound;
}

/**
* Method to allow a player to buy a stock.
*
* <p>
* Executes a purchase for a player which executes all logic
* and management of money, portfolio and archive.
* </p>
*
* @see Purchase
*
* @param symbol - The symbol of the bought stock.
* @param quantity - The amount of a bought stock.
* @param player - which player did this event.
* @return The given transaction details. (Transaction).
* @see Transaction
*/
public Transaction buy(String symbol, BigDecimal quantity, Player player) {
Share share = new Share(getStock(symbol), quantity, BigDecimal.valueOf(random.nextDouble()));
Purchase purchase = new Purchase(share, this.week);
purchase.commit(player);
return player.getTransactionArchive().getPurchases(this.week).getLast();
}

/**
* Method to allow a player to sell a stock.
*
* <p>
* Executes a sale for a player which executes all logic
* and management of money, portfolio and archive.
* </p>
*
* @see Sale
*
* @param Share - The instance of the sold share.
* @param player - which player did this event.
* @return The given transaction details. (Transaction).
* @see Transaction
*/
public Transaction sell(Share share, Player player) {
Sale sale = new Sale(share, this.week);
sale.commit(player);
return player.getTransactionArchive().getSales(this.week).getLast();
}

/**
* Method to advance the gameloop.
*
* <p>
* Adds a new price to each of the stock array.
* </p>
*
* @see Stock
*/
public void advance() {
for (Stock stocks : stockMap.values()) {
stocks.addNewSalesPrice(BigDecimal.valueOf(random.nextDouble()));
}

public Transaction sell(Share share, Player player) {
Sale sale = new Sale(share, this.week);
sale.commit(player);
return player.getTransactionArchive().getSales(this.week).getLast();
}

public void advance() {
for(Stock stocks : stockMap.values()) {
stocks.addNewSalesPrice(BigDecimal.valueOf(random.nextDouble()));
}
}

}

}
2 changes: 1 addition & 1 deletion src/main/java/edu/ntnu/idi/idatt/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ static void main() {
System.out.println("Hello world!");
}

}
}
94 changes: 47 additions & 47 deletions src/main/java/edu/ntnu/idi/idatt/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,51 @@

public class Player {

private final String name;
private final BigDecimal startingMoney;
private BigDecimal money;
private Portfolio portfolio = new Portfolio();
private TransactionArchive transactionArchive = new TransactionArchive();

public Player(String name, BigDecimal startingMoney) {
this.name = name;
this.startingMoney = startingMoney;
this.money = this.startingMoney;
}

/**
* Getters
*
* @return - Their corresponding variables.
*/

public String getName() {
return name;
}

public BigDecimal getMoney() {
return money;
}

public Portfolio getPortfolio() {
return portfolio;
}

public TransactionArchive getTransactionArchive() {
return transactionArchive;
}

/**
* Setters for money
*
* @param amount - Amount to be changed correspondingly.
*/

public void addMoney(BigDecimal amount){
this.money = this.money.add(amount);
}

public void withdrawMoney(BigDecimal amount){
this.money = this.money.subtract(amount);
}
private final String name;
private final BigDecimal startingMoney;
private BigDecimal money;
private Portfolio portfolio = new Portfolio();
private TransactionArchive transactionArchive = new TransactionArchive();

public Player(String name, BigDecimal startingMoney) {
this.name = name;
this.startingMoney = startingMoney;
this.money = this.startingMoney;
}

/**
* Getters
*
* @return - Their corresponding variables.
*/

public String getName() {
return name;
}

public BigDecimal getMoney() {
return money;
}

public Portfolio getPortfolio() {
return portfolio;
}

public TransactionArchive getTransactionArchive() {
return transactionArchive;
}

/**
* Setters for money
*
* @param amount - Amount to be changed correspondingly.
*/

public void addMoney(BigDecimal amount) {
this.money = this.money.add(amount);
}

public void withdrawMoney(BigDecimal amount) {
this.money = this.money.subtract(amount);
}
}
Loading