-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from solvena/mappe_del2
Mappe del2 to main
- Loading branch information
Showing
30 changed files
with
236 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| package Model; | ||
| import java.math.BigDecimal; | ||
|
|
||
| public class Player { | ||
|
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/main/java/Purchase.java → src/main/java/Model/Purchase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
|
|
||
1 change: 1 addition & 0 deletions
1
src/main/java/PurchaseCalculator.java → src/main/java/Model/PurchaseCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
|
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
|
|
||
1 change: 1 addition & 0 deletions
1
src/main/java/SaleCalculator.java → src/main/java/Model/SaleCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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{ | ||
|
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| package Model; | ||
| import java.math.BigDecimal; | ||
|
|
||
| public class Share { | ||
|
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/main/java/Transaction.java → src/main/java/Model/Transaction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
1 change: 1 addition & 0 deletions
1
src/main/java/TransactionArchive.java → src/main/java/Model/TransactionArchive.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
1 change: 1 addition & 0 deletions
1
src/main/java/TransactionCalculator.java → ...ain/java/Model/TransactionCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| package Model; | ||
| import java.math.BigDecimal; | ||
|
|
||
| public interface TransactionCalculator { | ||
|
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
|
|
||
|
|
||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.