Skip to content

Commit

Permalink
Merge pull request #4 from danieskj/dev
Browse files Browse the repository at this point in the history
Part 1 Done
  • Loading branch information
danieskj authored Mar 10, 2026
2 parents 248a8a6 + 18d72fd commit 473e022
Show file tree
Hide file tree
Showing 31 changed files with 1,664 additions and 19 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Java CI with Maven

on:
push:
branches: [ "dev" ]
pull_request:
branches: [ "dev" ]

jobs:
build:
runs-on: self-hosted

steps:
- uses: actions/checkout@v4
- name: Set up JDK 25
uses: actions/setup-java@v4
with:
java-version: '25'
distribution: 'temurin'
cache: 'maven'

- name: Build with Maven
run: mvn -B compile --file pom.xml

- name: Test with Maven
run: mvn -B test --file pom.xml

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
59 changes: 42 additions & 17 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,19 +33,56 @@
<version>25.0.1</version>
</dependency>

<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>25.0.1</version>
</dependency>

<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>25.0.1</version>
</dependency>

<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>25.0.1</version>
</dependency>

<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<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>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>edu.ntnu.idi.idatt.Main</mainClass>
<mainClass>edu.ntnu.idi.idatt.gui.javafx</mainClass>
</configuration>
</plugin>

Expand All @@ -78,13 +109,7 @@
</configuration>
</plugin>

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

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

</project>
</project>
161 changes: 161 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt/Exchange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package edu.ntnu.idi.idatt;

import edu.ntnu.idi.idatt.marked.Share;
import edu.ntnu.idi.idatt.marked.Stock;
import edu.ntnu.idi.idatt.transaction.Purchase;
import edu.ntnu.idi.idatt.transaction.Sale;
import edu.ntnu.idi.idatt.transaction.Transaction;

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 {

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);
}

}

/**
*
* 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);
}
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);
}
}
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()));
}
}

}
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!");
}

}
}
57 changes: 57 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package edu.ntnu.idi.idatt;

import edu.ntnu.idi.idatt.marked.Portfolio;
import edu.ntnu.idi.idatt.transaction.TransactionArchive;

import java.math.BigDecimal;

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);
}
}
Loading

0 comments on commit 473e022

Please sign in to comment.