Skip to content

Commit

Permalink
Merge pull request #149 from einaskoi/einar/writeToFile
Browse files Browse the repository at this point in the history
Einar/write to file
  • Loading branch information
einaskoi authored May 26, 2026
2 parents a393e2a + 1352063 commit 6c84687
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
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

0 comments on commit 6c84687

Please sign in to comment.