-
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 #21 from danieskj/file-handeling
Finished issue file-handeling
- Loading branch information
Showing
5 changed files
with
261 additions
and
17 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
100 changes: 100 additions & 0 deletions
100
src/main/java/edu/ntnu/idi/idatt/file/ExchangeLoader.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 |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package edu.ntnu.idi.idatt.file; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.BufferedWriter; | ||
| import java.io.File; | ||
| import java.io.FileReader; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.math.BigDecimal; | ||
| import java.net.URL; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import edu.ntnu.idi.idatt.marked.Stock; | ||
|
|
||
| public class ExchangeLoader { | ||
|
|
||
| private final File file; | ||
|
|
||
| /** | ||
| * Constructors for ExchangeLoader | ||
| * | ||
| * <p> | ||
| * Utilizes method overloading for different types | ||
| * of path formatting. | ||
| * </p> | ||
| * | ||
| * @throws IllegalArgumentException if specified path doesn't exist. | ||
| */ | ||
| protected ExchangeLoader(String path) { | ||
| file = new File(path); | ||
| if (!file.exists()) { | ||
| throw new IllegalArgumentException("File at this path doesn't exist!"); | ||
| } | ||
| } | ||
|
|
||
| protected ExchangeLoader(URL path) { | ||
| file = new File(path.toString()); | ||
| if (!file.exists()) { | ||
| throw new IllegalArgumentException("File at this path doesn't exist!"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Method for loading from stocks from file | ||
| * | ||
| * @return List<Stock> of loaded stocks. | ||
| * @throws IOException on BufferedReader error | ||
| */ | ||
| protected List<Stock> load() throws IOException { | ||
|
|
||
| ArrayList<Stock> stocks = new ArrayList<>(); | ||
|
|
||
| try (BufferedReader reader = new BufferedReader(new FileReader(file))) { | ||
| List<String> stockStringList = new ArrayList<>(reader.readAllLines()); | ||
|
|
||
| // Remove comments | ||
| stockStringList.removeIf(s -> s.isBlank()); | ||
| stockStringList.removeIf(s -> s.startsWith("#")); | ||
|
|
||
| for (String stockString : stockStringList) { | ||
| String[] stockValues = stockString.split(","); | ||
|
|
||
| // TODO: Loading all historical prices not the recent, saved one. | ||
| Stock stock = new Stock(stockValues[0], stockValues[1], List.of(new BigDecimal(stockValues[2]))); | ||
| stocks.add(stock); | ||
| } | ||
|
|
||
| } catch (IOException e) { | ||
| throw new IOException("File loading failed!"); | ||
| } | ||
|
|
||
| return stocks; | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Method for saving stocks to file. | ||
| * | ||
| * @param stocks The destined list to be saved. | ||
| * @throws IOException on BufferedWriter error. | ||
| */ | ||
| protected void save(List<Stock> stocks) throws IOException { | ||
|
|
||
| try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { | ||
|
|
||
| for (Stock stock : stocks) { | ||
| String data = stock.getSymbol() + "," + stock.getCompany() + "," | ||
| + stock.getHistoricalPrices().getLast().toString(); | ||
| writer.write(data); | ||
| writer.newLine(); | ||
| } | ||
|
|
||
| } catch (IOException e) { | ||
| throw new IOException("File saving failed!"); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
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
103 changes: 103 additions & 0 deletions
103
src/test/java/edu/ntnu/idi/idatt/file/ExchangeLoaderTest.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 |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package edu.ntnu.idi.idatt.file; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.math.BigDecimal; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.StandardCopyOption; | ||
| import java.util.List; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import edu.ntnu.idi.idatt.marked.Stock; | ||
|
|
||
| /** | ||
| * Test class for ExchangeLoader | ||
| * | ||
| * <p> | ||
| * Tests the loading and saving of stock data. | ||
| * </p> | ||
| */ | ||
| class ExchangeLoaderTest { | ||
|
|
||
| private ExchangeLoader loader; | ||
| private List<Stock> exampleStocks; | ||
|
|
||
| @BeforeEach | ||
| public void PT_setup() throws IOException { | ||
|
|
||
| InputStream is = getClass() | ||
| .getClassLoader() | ||
| .getResourceAsStream("stocks.csv"); | ||
|
|
||
| Path tempFile = Files.createTempFile("stocks", ".csv"); | ||
| Files.copy(is, tempFile, StandardCopyOption.REPLACE_EXISTING); | ||
|
|
||
| loader = new ExchangeLoader(tempFile.toFile().toPath().toString()); | ||
|
|
||
| Stock AAPL = new Stock("AAPL", "Apple Inc.", List.of(new BigDecimal("32"))); | ||
| Stock NVDA = new Stock("NVDA", "NVIDIA", List.of(new BigDecimal("182.81"))); | ||
| Stock TSLA = new Stock("TSLA", "Tesla", List.of(new BigDecimal("417.44"))); | ||
| Stock AMD = new Stock("AMD", "Advanced Micro Devices", List.of(new BigDecimal("207.32"))); | ||
|
|
||
| exampleStocks = List.of(AAPL, NVDA, TSLA, AMD); | ||
| } | ||
|
|
||
| /** | ||
| * Positive test for loading/reading stocks | ||
| */ | ||
| @Test | ||
| void PT_load() { | ||
| List<Stock> stocks = null; | ||
| try { | ||
| stocks = loader.load(); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| assertEquals(4, stocks.size()); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Positive test for saving stocks. | ||
| */ | ||
| @Test | ||
| void PT_save() { | ||
| exampleStocks.get(3).addNewSalesPrice(new BigDecimal("99999")); | ||
|
|
||
| // Save | ||
|
|
||
| try { | ||
| loader.save(exampleStocks); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| // Try to read again | ||
| List<Stock> stocks = null; | ||
| try { | ||
| stocks = loader.load(); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| assertEquals(new BigDecimal("99999"), stocks.get(3).getSalesPrice()); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Negative tests for constructor | ||
| */ | ||
| @Test | ||
| void NT_IllegalArgumentException_Constructor() { | ||
| assertThrows(IllegalArgumentException.class, | ||
| () -> new ExchangeLoader("resources/notexistantfile.csv")); | ||
| } | ||
|
|
||
| } |
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,7 @@ | ||
| # Ticker,Name,Price | ||
| AAPL,Apple Inc,32 | ||
| NVDA,NVIDIA,182.81 | ||
| TSLA,Tesla,417.44 | ||
| AMD,Advanced Micro Devices,207.32 | ||
|
|
||
|
|