-
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.
Feat: Updated fileconverter and fileparser.
- Loading branch information
Showing
2 changed files
with
178 additions
and
12 deletions.
There are no files selected for viewing
60 changes: 55 additions & 5 deletions
60
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/FileConverter.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 |
|---|---|---|
| @@ -1,23 +1,73 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe; | ||
|
|
||
| import java.io.IOException; | ||
| import java.math.BigDecimal; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Converts stock objects to/from string format for file handling. | ||
| * | ||
| * <p style ="font-weight: bold;">Responsibilities:</p> | ||
| * <ul> | ||
| * <li>Turn a valid list of stock string elements to a | ||
| * list of {@link Stock} objects.</li> | ||
| * | ||
| * <li>Turn a list of stock objects to a | ||
| * list of string elements.</li> | ||
| * </ul> | ||
| * | ||
| * <p>Used with {@link FileParser}</p> | ||
| * | ||
| * @see FileParser | ||
| * @author tohja | ||
| * @version 1.0.0 | ||
| * | ||
| * */ | ||
| public class FileConverter { | ||
|
|
||
| public List<Stock> getStocksFromFile(FileParser fileParser) throws IOException { | ||
| List<String> strings = fileParser.readFile(); | ||
| /** | ||
| * Turns a list of valid string representations | ||
| * of stock objects to a list of stock objects. | ||
| * | ||
| * @param validStocks list of string elements properly | ||
| * representing stock objects. | ||
| * | ||
| * @return {@link List<Stock>} | ||
| * */ | ||
| public List<Stock> getStocksFromStrings(final List<String> validStocks) { | ||
| List<Stock> stocksFromFile = new ArrayList<>(); | ||
| List<String> stockSymbols = new ArrayList<>(); | ||
|
|
||
| strings.forEach(s -> { | ||
| validStocks.forEach(s -> { | ||
| String[] lineElements = s.split(","); | ||
| String stockSymbol = lineElements[0].trim(); | ||
| String stockName = lineElements[1].trim(); | ||
| BigDecimal stockPrice = new BigDecimal(lineElements[2].trim()); | ||
| stocksFromFile.add(new Stock(stockSymbol, stockName, stockPrice)); | ||
| // TODO: try-catch | ||
| Stock stockObject = new Stock(stockSymbol, stockName, stockPrice); | ||
| if (!stockSymbols.contains(stockSymbol)) { | ||
| stockSymbols.add(stockSymbol); | ||
| stocksFromFile.add(stockObject); | ||
| } | ||
| }); | ||
| return stocksFromFile; | ||
| } | ||
|
|
||
| /** | ||
| * Converts a list of stocks to string representations of that stock. | ||
| * | ||
| * <p>format: SYMBOL, NAME, PRICE</p> | ||
| * | ||
| * @param stocks a list of {@link Stock} objects. | ||
| * | ||
| * @return a list of string representation of the stock objects. | ||
| * */ | ||
| public List<String> stocksToStrings(final List<Stock> stocks) { | ||
| ArrayList<String> stringList = new ArrayList<>(); | ||
| stocks.forEach(s -> | ||
| stringList.add(s.getSymbol().trim() + "," + s.getCompany().trim() | ||
| + "," + s.getSalesPrice().toString()) | ||
| ); | ||
| return stringList; | ||
| } | ||
| } |
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