Question: package mutu.utils; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Comparator; import java.util.Date; import java.util.List; import java.util.Scanner; import mutu.models.CsvDBObject; import mutu.models.Stocks; import mutu.models.StockTransaction; /**
package mutu.utils;
import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Comparator; import java.util.Date; import java.util.List; import java.util.Scanner; import mutu.models.CsvDBObject; import mutu.models.Stocks; import mutu.models.StockTransaction;
/** * * @author Cyrus Cheng */ public class DummyDB { private long _queueId; private final String _stocksFile, _transFile; private final ArrayList _stockDB; private final ArrayList _unmatchedTrans, _settledTrans; /* 1. For this question, not write the code but a setup. Create the Persistence Unit and assige "StockTransation" class as entity to that Persistence Unit. Add corresponding library */ /* 2. Define the entity manager object here */ /* 3. Write a function to initialize and assign an entity manager the the variable in question-2. Call it at the end of contructor */ /* 4. Change the function writeCSV(), so that apart from write CSV, it will also save the StockTransaction object to DB * Beware, to avoid overwrite the existing records, CRETE A NEW StockTransaction OBJECT EACH TIME WHEN CALL SAVE() */ private void writeCSV(StockTransaction _obj, String _filePath){ try(FileWriter pw = new FileWriter(_filePath, true)){ pw.write((this.prepareFile(_filePath) ? "" : " ") + _obj.toCsvString()); } catch (IOException ioe){ System.out.println("Error occur in writeCSV(" + _filePath + ") - " + ioe.getMessage()); }//end of try-catch block }//end of writeCSV() /** * DummyDB constructor to load CSV file content into in-memory list * @param stocksFile the full file path of stocks CSV file * @param transFile the full file path of transaction CSV file */ public DummyDB(String stocksFile, String transFile){ //init and assign local variable this._queueId = 0; this._stockDB = new ArrayList<>(); this._unmatchedTrans = new ArrayList<>(); this._stocksFile = stocksFile; this._transFile = transFile; this._settledTrans = new ArrayList<>();
//prepare load CSV to in-memory List prepareStockDB(); prepareTransactionFile(); /* 3. call the method */ }//end of constructor()