Skip to content

Einar/write to file #149

Merged
merged 2 commits into from
May 26, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private List<String> getPositiveHeadlines() {
goodNews.add("Turns out the product of " + company + " is good for the environment.");

goodNews.add("Beaver population increased due to the efforts of " + company);
goodNews.add("Eminem dropped new hit single, namedropping " + company);
goodNews.add("Famous artist dropped new hit single, namedropping " + company);

goodNews.add("Founder of " + company + " seen feeding the homeless.");
goodNews.add(company + " got frontpage in new forbes magazine, "
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package edu.ntnu.idi.idatt2003.gruppe42.model;

import edu.ntnu.idi.idatt2003.gruppe42.model.exceptions.StockFileParseException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.*;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -134,6 +134,33 @@ public static List<Stock> readFromFile(final InputStream inputStream)
return stocks;
}

/**
* Method for writing stock information to file.
*
* @param path the relative path of the file being parsed (from source root)
* @param stocks list of stock objects to be transcribed to data file
* @throws IOException if file not found
*/
public static void writeToFile(Path path, List<Stock> stocks) throws IOException {

try (BufferedWriter writer = Files.newBufferedWriter(path)) {
writer.write("# S&P 500 Companies by Market Cap");
writer.newLine();
writer.write("# Ticker,Name,Price");
writer.newLine();

for (Stock stock : stocks) {
String line = String.format("%s,%s,%s",
stock.getSymbol(),
stock.getCompany(),
stock.getSalesPrice());

writer.write(line);
writer.newLine();
}
}
}

private static BigDecimal getBigDecimal(String rawPrice, int lineNumber, String trimmed)
throws StockFileParseException {
BigDecimal price;
Expand Down