Skip to content

Commit

Permalink
Added StockFileHandler class and temporary aksjer.csv file
Browse files Browse the repository at this point in the history
Added aksjer.csv file for testing purposes, will be deleted later
  • Loading branch information
elisab3 committed Mar 24, 2026
1 parent 4a105f8 commit 5a12603
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/main/java/StockFileHandler.java
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);
}
}
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

0 comments on commit 5a12603

Please sign in to comment.