Skip to content

Commit

Permalink
Merge pull request #2 from danieskj/pawel-sapula/testing-exceptions
Browse files Browse the repository at this point in the history
Tests finished
  • Loading branch information
danieskj authored Feb 18, 2026
2 parents 22ee5e8 + 27f5b4a commit 1cfcd0a
Show file tree
Hide file tree
Showing 25 changed files with 1,368 additions and 510 deletions.
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

0 comments on commit 1cfcd0a

Please sign in to comment.