Skip to content

Commit

Permalink
Merge pull request #5 from solvena/mappe_del2
Browse files Browse the repository at this point in the history
Mappe del2 to main
  • Loading branch information
solvena authored Apr 12, 2026
2 parents 1a1c43e + 4a03ca5 commit b87e793
Show file tree
Hide file tree
Showing 30 changed files with 236 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/main/java/Controller/StockFileHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package Controller;
import java.nio.file.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import Model.Stock;

public class StockFileHandler {

// lesing
public List<Stock> loadStocksFromFile(String filename) throws IOException {
List<Stock> stocks = new ArrayList<>();
List<String> lines = Files.readAllLines(Paths.get(filename));

for (String line : lines) {
line = line.trim();
if (line.isEmpty() || line.startsWith("#")) {
continue;
}

String[] parts = line.split(",");
if (parts.length == 3) {
String symbol = parts[0];
String name = parts[1];
BigDecimal price = new BigDecimal(parts[2]);

stocks.add(new Stock(symbol, name, price));
}
}
return stocks; // returnerer listen
}

// lagring
public void saveStocksToFile(String filename, List<Stock> stocks) throws IOException {
List<String> lines = new ArrayList<>();

// header
lines.add("# Ticker,Name,Price");

for (Stock stock : stocks) {
// formaterer hver aksje som "SYMBOL,NAME,PRICE"
String line = String.format("%s,%s,%s",
stock.getSymbol(),
stock.getCompany(),
stock.getSalesPrice().toString());
lines.add(line);
}

Files.write(Paths.get(filename), lines);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package Model;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -109,6 +110,17 @@ public void advance() {
}
}

public List<Stock> getGainers(int limit) { // viser "vinnerne"
return stockMap.values().stream()
.sorted((stock1, stock2) -> stock2.getLatestPriceChange().compareTo(stock1.getLatestPriceChange()))
.limit(limit)
.toList();
}


public List<Stock> getLosers(int limit) { // viser "taperne"
return stockMap.values().stream()
.sorted((stock1, stock2) -> stock1.getLatestPriceChange().compareTo(stock2.getLatestPriceChange()))
.limit(limit)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package Model;
import java.math.BigDecimal;

public class Player {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package Model;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -34,5 +36,14 @@ public List<Share> getShares(String symbol) {
public boolean contains(Share share) {
return shares.contains(share);
}

public BigDecimal getNetWorth() {
BigDecimal total = BigDecimal.ZERO;
for (Share share : shares) {
SaleCalculator calc = new SaleCalculator(share);
total = total.add(calc.calculateTotal());
}
return total;
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package Model;
import java.math.BigDecimal;

public class Purchase extends Transaction {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package Model;
import java.math.BigDecimal;

public class PurchaseCalculator implements TransactionCalculator {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/Sale.java → src/main/java/Model/Sale.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package Model;
import java.math.BigDecimal;

public class Sale extends Transaction {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package Model;
import java.math.BigDecimal;

public class SaleCalculator implements TransactionCalculator{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package Model;
import java.math.BigDecimal;

public class Share {
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/Stock.java → src/main/java/Model/Stock.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package Model;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -30,4 +31,28 @@ public BigDecimal getSalesPrice() {
public void addNewSalesPrice(BigDecimal price) {
prices.add(price);
}

public List<BigDecimal> getHistoricalPrices() {
return new ArrayList<>(prices); // returnerer en kopi for å beskytte selve listen
}

public BigDecimal getHighestPrice() {
return prices.stream()
.reduce(prices.get(0), BigDecimal::max);
}

public BigDecimal getLowestPrice() {
return prices.stream()
.reduce(prices.get(0), BigDecimal::min);
}

public BigDecimal getLatestPriceChange() {
if (prices.size() < 2) {
return BigDecimal.ZERO;
}

BigDecimal latest = prices.get(prices.size() - 1);
BigDecimal previous = prices.get(prices.size() - 2);
return latest.subtract(previous);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package Model;
public abstract class Transaction {
private Share share;
private int week;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package Model;
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package Model;
import java.math.BigDecimal;

public interface TransactionCalculator {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/aksjer.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Top 500 US Stocks by Market Cap
# Ticker,Name,Price
NVDA,Nvidia,191.27
AAPL,Apple Inc.,276.43
MSFT,Microsoft,404.68
4 changes: 4 additions & 0 deletions src/test/java/ExchangeTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import Model.Exchange;
import Model.Stock;

import static org.junit.jupiter.api.Assertions.*;

import java.math.BigDecimal;
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/PortfolioTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

import Model.Portfolio;
import Model.Share;
import Model.Stock;

import java.math.BigDecimal;

public class PortfolioTest {
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/PurchaseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.math.BigDecimal;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import Model.Player;
import Model.Purchase;
import Model.Share;
import Model.Stock;

public class PurchaseTest {
Stock stock;
Share share;
Purchase purchase;

@BeforeEach
void setUp() {
this.stock = new Stock("symbol", "company", new BigDecimal(100));
this.share = new Share(this.stock, new BigDecimal(20), new BigDecimal(50));
this.purchase = new Purchase(this.share, 18);
}

@Test
void commit_validPurchase_updatesPlayer() {

// Arrange
Player player = new Player("Jane", new BigDecimal(500000));
BigDecimal startingMoney = player.getMoney();

// Act
this.purchase.commit(player);

// Assert
assertEquals(1, startingMoney.subtract(player.getMoney()).signum());

}



}
62 changes: 62 additions & 0 deletions src/test/java/SaleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import Model.Player;
import Model.Sale;
import Model.Share;
import Model.Stock;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.math.BigDecimal;

public class SaleTest {
Stock stock;
Share share;
Sale sale;

@BeforeEach
void setUp(){
this.stock = new Stock("symbol", "company", new BigDecimal(100));
this.share = new Share(this.stock, new BigDecimal(20), new BigDecimal(80));
this.sale = new Sale(this.share, 18);
}

@Test
void commit_validSale_updatesPlayerState() {

// Arrange
Player player = new Player("Jane", new BigDecimal(500));
player.getPortfolio().addShare(this.share);

// Act
this.sale.commit(player);

// Assert
assertTrue(player.getTransactionArchive().getTransactions(this.sale.getWeek()).contains(this.sale));
assertFalse(player.getPortfolio().contains(this.share));
assertTrue(this.sale.isCommitted());
}

@Test
void commit_alreadyCommitted_noAction() {

// Arrange
Player player = new Player("Jane", new BigDecimal(500));
player.getPortfolio().addShare(this.share);
this.sale.commit(player);
BigDecimal moneyAfterFirstCommit = player.getMoney();

// Act
this.sale.commit(player);

// Assert
assertEquals(moneyAfterFirstCommit, player.getMoney());
assertEquals(1, player.getTransactionArchive().getTransactions(this.sale.getWeek()).size());
assertTrue(this.sale.isCommitted());
}


}
4 changes: 4 additions & 0 deletions src/test/java/ShareTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

import Model.Share;
import Model.Stock;

import java.math.BigDecimal;


Expand Down
3 changes: 3 additions & 0 deletions src/test/java/StockTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import org.junit.jupiter.api.Test;

import Model.Stock;

import static org.junit.jupiter.api.Assertions.*;

import java.math.BigDecimal;
Expand Down
Binary file removed target/classes/Exchange.class
Binary file not shown.
Binary file removed target/classes/Portfolio.class
Binary file not shown.
Binary file removed target/classes/PurchaseCalculator.class
Binary file not shown.
Binary file removed target/classes/Share.class
Binary file not shown.
Binary file removed target/classes/Stock.class
Binary file not shown.
Binary file removed target/classes/TransactionCalculator.class
Binary file not shown.
Binary file added target/test-classes/ExchangeTest.class
Binary file not shown.
Binary file added target/test-classes/PortfolioTest.class
Binary file not shown.
Binary file added target/test-classes/ShareTest.class
Binary file not shown.
Binary file added target/test-classes/StockTest.class
Binary file not shown.

0 comments on commit b87e793

Please sign in to comment.