diff --git a/src/main/java/seedu/duke/Duke.java b/src/main/java/seedu/duke/Duke.java index c15aa8a17d..a440aca2e2 100644 --- a/src/main/java/seedu/duke/Duke.java +++ b/src/main/java/seedu/duke/Duke.java @@ -4,6 +4,7 @@ import command.ExitCommand; import exceptions.CommandFormatException; import parser.Parser; +import storage.Storage; import ui.TextUi; import itemlist.Itemlist; @@ -24,6 +25,8 @@ public static void main(String[] args) throws IOException, CommandFormatExceptio public void run() throws IOException, CommandFormatException { ui.showWelcomeMessage("StockMaster v2.0", "./StockMasterData.txt"); + Storage.updateFile("", true); + Storage.readFromFile("./StockMasterData.txt"); this.normalOperation(); ui.showGoodByeMessage("./StockMasterData.txt"); } diff --git a/src/main/java/storage/Storage.java b/src/main/java/storage/Storage.java index 91f0c29b45..2977eb3d87 100644 --- a/src/main/java/storage/Storage.java +++ b/src/main/java/storage/Storage.java @@ -1,6 +1,7 @@ package storage; import item.Item; +import itemlist.Itemlist; import java.io.File; import java.io.FileNotFoundException; @@ -62,22 +63,59 @@ public static File setFile() { * Add the identified tasks into a list of existing tasks. * */ - public static void readFromFile(File fileName) { + public static void readFromFile(String fileName) { try { - Scanner scanner = new Scanner(fileName); + Scanner scanner = new Scanner(new File(fileName)); while (scanner.hasNext()) { - String lineSkipped = scanner.nextLine(); + String fileLine = "add" + scanner.nextLine(); + String[] keyCommands = fileLine.split(" \\| "); + String commandQty = ""; + String commandCat = ""; + String commandUom = ""; + String commandBuy = ""; + String commandSell = ""; + String commandName = ""; + for (String keyCommand : keyCommands) { + if (keyCommand.contains(".")) { + //do nothing. + } + else if (keyCommand.contains("Qty: ")) { + String[] commandQtyUnit = keyCommand.replace("Qty: ", "").split(" "); + assert commandQtyUnit.length == 2 : "length not 2!"; + commandQty = commandQtyUnit[0]; + commandUom = commandQtyUnit[1]; + } + else if (keyCommand.contains("Cat: ")) { + commandCat = keyCommand.replace("Cat: ", ""); + } + else if (keyCommand.contains("BuyPrice: $")) { + commandBuy = keyCommand.replace("BuyPrice: $", ""); + } + else if (keyCommand.contains("SellPrice: $")) { + commandSell = keyCommand.replace("SellPrice: $", ""); + } + else { + commandName = keyCommand; + } + } + Item toAdd = new Item(commandName, Integer.parseInt(commandQty), commandUom, commandCat, + Integer.parseInt(commandBuy), Integer.parseInt(commandSell)); + Itemlist.addItem(toAdd); } - } catch(FileNotFoundException e){ + } catch(FileNotFoundException e) { System.out.println("File does not exist."); + } catch(NumberFormatException e) { + System.out.println("Invalid numbers found."); } } public static void addToFile(ArrayList items, boolean ifAppend) { assert items != null : "Items cannot be null."; Item lastItem = items.get(items.size() - 1); - String descriptionAdded = (items.size() - 1) + " | " + lastItem.getItemName() + " | " + lastItem.getQuantity() - + " | " + lastItem.getBuyPrice() + " | " + lastItem.getSellPrice() + "\n"; + String descriptionAdded = (items.size()) + "." + " | " + lastItem.getItemName() + + " | " + "Qty: " + lastItem.getQuantity() + " " + lastItem.getUom() + + " | " + "Cat: " + lastItem.getCategory() + " | " + "BuyPrice: $" + + lastItem.getBuyPrice() + " | " + "SellPrice: $" + lastItem.getSellPrice() + "\n"; updateFile(descriptionAdded, ifAppend); } @@ -85,8 +123,10 @@ public static void overwriteFile(ArrayList items, boolean ifAppend) { assert items != null : "Items cannot be null."; int length = items.size(); for (int index = 0; index < length; index++) { - String descriptionAdded = index + " | " + items.get(index).getItemName() + " | " + - items.get(index).getQuantity() + " | " + items.get(index).getBuyPrice() + " | " + + String descriptionAdded = (index + 1) + "." + " | " + items.get(index).getItemName() + + " | " + "Qty: " + items.get(index).getQuantity() + " " + items.get(index).getUom() + + " | " + "Cat: " + items.get(index).getCategory() + " | " + "BuyPrice: $" + + items.get(index).getBuyPrice() + " | " + "SellPrice: $" + items.get(index).getSellPrice() + "\n"; if (index == 0) { updateFile(descriptionAdded, ifAppend); @@ -95,13 +135,4 @@ public static void overwriteFile(ArrayList items, boolean ifAppend) { } } } - - public static void main (String[]args){ - stockMaster = setFile(); - try { - writeToFile(stockMaster.getPath(), "", true); - } catch (IOException e) { - System.out.println("File does not exist."); - } - } } diff --git a/src/test/java/storage/StorageTest.java b/src/test/java/storage/StorageTest.java index 2cefbdfd99..e8b57a402b 100644 --- a/src/test/java/storage/StorageTest.java +++ b/src/test/java/storage/StorageTest.java @@ -16,13 +16,12 @@ public class StorageTest { public void readFromFile_fileNotFound() { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(outputStream)); - - String directory = "./testFile.txt"; + String directory = "./testFile1.txt"; File testFile = new File(directory); try { - Storage.writeToFile(directory, "Created", true); + Storage.writeToFile(directory, "", true); testFile.delete(); - Storage.readFromFile(testFile); + Storage.readFromFile(directory); assertEquals("File does not exist." + System.lineSeparator(), outputStream.toString()); } catch (IOException e) { fail("failed to create a file."); @@ -31,7 +30,7 @@ public void readFromFile_fileNotFound() { @Test public void writeToFile_aLine_writeSuccessful() { - String directory = "./testFile.txt"; + String directory = "./testFile2.txt"; File testFile = new File(directory); String aLine = "A line"; try {