-
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.
Added StockFileHandler class and temporary aksjer.csv file
Added aksjer.csv file for testing purposes, will be deleted later
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
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,50 @@ | ||
| import java.nio.file.*; | ||
| import java.io.IOException; | ||
| import java.math.BigDecimal; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| 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
| 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 |