From a5543ecb7244ea74089db3d82e3f992dc880c500 Mon Sep 17 00:00:00 2001 From: fxe025 Date: Wed, 27 Mar 2024 16:25:17 +0800 Subject: [PATCH 1/9] Edit readFromFile() --- src/main/java/seedu/duke/Duke.java | 2 + src/main/java/storage/Storage.java | 57 +++++++++++++++++--------- src/test/java/storage/StorageTest.java | 5 +-- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/main/java/seedu/duke/Duke.java b/src/main/java/seedu/duke/Duke.java index 8d14fe25b9..02ddb90af3 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; @@ -23,6 +24,7 @@ public static void main(String[] args) throws IOException, CommandFormatExceptio } public void run() throws IOException, CommandFormatException { + Storage.readFromFile("./StockMasterData.txt"); ui.showWelcomeMessage("1.0", "./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 ab202853fd..175862c424 100644 --- a/src/main/java/storage/Storage.java +++ b/src/main/java/storage/Storage.java @@ -1,6 +1,10 @@ package storage; +import command.Command; +import exceptions.CommandFormatException; import item.Item; +import itemlist.Itemlist; +import parser.Parser; import java.io.File; import java.io.FileNotFoundException; @@ -17,9 +21,6 @@ public class Storage { private static final String FILENAME = "./StockMasterData.txt"; - private static File stockMaster; - - /** * Write contents to the file. * @@ -62,11 +63,36 @@ 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) { + Parser parser = new Parser(); 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 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 { + commandName = keyCommand; + } + } + Item toAdd = new Item(commandName, Integer.parseInt(commandQty), commandUom, commandCat); + Itemlist.addItem(toAdd); } } catch(FileNotFoundException e){ System.out.println("File does not exist."); @@ -76,8 +102,9 @@ public static void readFromFile(File fileName) { 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() + "\n"; + String descriptionAdded = (items.size()) + "." + " | " + lastItem.getItemName() + + " | " + "Qty: " + lastItem.getQuantity() + " " + lastItem.getUom() + " | " + "Cat: " + + lastItem.getCategory() + "\n"; updateFile(descriptionAdded, ifAppend); } @@ -85,8 +112,9 @@ 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() + "\n"; + String descriptionAdded = (index + 1) + "." + " | " + items.get(index).getItemName() + + " | " + "Qty: " + items.get(index).getQuantity() + " " + items.get(index).getUom() + + " | " + "Cat: " + items.get(index).getCategory() + "\n"; if (index == 0) { updateFile(descriptionAdded, ifAppend); } else { @@ -94,13 +122,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..6dddf25857 100644 --- a/src/test/java/storage/StorageTest.java +++ b/src/test/java/storage/StorageTest.java @@ -1,5 +1,6 @@ package storage; +import itemlist.Itemlist; import org.junit.jupiter.api.Test; import java.io.File; @@ -18,11 +19,9 @@ public void readFromFile_fileNotFound() { System.setOut(new PrintStream(outputStream)); String directory = "./testFile.txt"; - File testFile = new File(directory); try { Storage.writeToFile(directory, "Created", 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."); From 0386314f5569b65c1547881e50093a6bd443ca60 Mon Sep 17 00:00:00 2001 From: fxe025 Date: Wed, 27 Mar 2024 18:24:00 +0800 Subject: [PATCH 2/9] remove StockMasterData --- StockMasterData.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 StockMasterData.txt diff --git a/StockMasterData.txt b/StockMasterData.txt deleted file mode 100644 index e69de29bb2..0000000000 From 1f2069ecda7d95a65a437df24c1776b833caf801 Mon Sep 17 00:00:00 2001 From: fxe025 Date: Wed, 27 Mar 2024 19:54:27 +0800 Subject: [PATCH 3/9] Remove unnecessary import --- src/main/java/storage/Storage.java | 2 -- src/test/java/storage/StorageTest.java | 1 - 2 files changed, 3 deletions(-) diff --git a/src/main/java/storage/Storage.java b/src/main/java/storage/Storage.java index 536f2965b9..d647d87de3 100644 --- a/src/main/java/storage/Storage.java +++ b/src/main/java/storage/Storage.java @@ -2,7 +2,6 @@ import item.Item; import itemlist.Itemlist; -import parser.Parser; import java.io.File; import java.io.FileNotFoundException; @@ -10,7 +9,6 @@ import java.util.Scanner; import java.io.FileWriter; import java.io.IOException; -import java.util.regex.Pattern; /** * Represents an add command where a new task is added to the existing list of task. diff --git a/src/test/java/storage/StorageTest.java b/src/test/java/storage/StorageTest.java index 6dddf25857..6ec4dc4f46 100644 --- a/src/test/java/storage/StorageTest.java +++ b/src/test/java/storage/StorageTest.java @@ -1,6 +1,5 @@ package storage; -import itemlist.Itemlist; import org.junit.jupiter.api.Test; import java.io.File; From 07ed12b12e57e1bf87d59730720049668fba1454 Mon Sep 17 00:00:00 2001 From: fxe025 Date: Wed, 27 Mar 2024 20:07:52 +0800 Subject: [PATCH 4/9] Fix StorageTest methods --- src/test/java/storage/StorageTest.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/test/java/storage/StorageTest.java b/src/test/java/storage/StorageTest.java index 6ec4dc4f46..e8b57a402b 100644 --- a/src/test/java/storage/StorageTest.java +++ b/src/test/java/storage/StorageTest.java @@ -16,10 +16,11 @@ 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(directory); assertEquals("File does not exist." + System.lineSeparator(), outputStream.toString()); } catch (IOException e) { @@ -29,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 { From 0fe724319f26371b6c89b1332a37a823eba90500 Mon Sep 17 00:00:00 2001 From: fxe025 Date: Wed, 27 Mar 2024 20:17:13 +0800 Subject: [PATCH 5/9] Fix I/O redirection test --- StockMasterData.txt | 1 + src/main/java/seedu/duke/Duke.java | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 StockMasterData.txt diff --git a/StockMasterData.txt b/StockMasterData.txt new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/StockMasterData.txt @@ -0,0 +1 @@ + diff --git a/src/main/java/seedu/duke/Duke.java b/src/main/java/seedu/duke/Duke.java index ebdbc34c65..196a5b308f 100644 --- a/src/main/java/seedu/duke/Duke.java +++ b/src/main/java/seedu/duke/Duke.java @@ -26,7 +26,6 @@ public static void main(String[] args) throws IOException, CommandFormatExceptio public void run() throws IOException, CommandFormatException { ui.showWelcomeMessage("StockMaster v2.0", "./StockMasterData.txt"); Storage.readFromFile("./StockMasterData.txt"); - ui.showWelcomeMessage("1.0", "./StockMasterData.txt"); this.normalOperation(); ui.showGoodByeMessage("./StockMasterData.txt"); } From 8d0a2dbdf9e6288647125af717427858380af578 Mon Sep 17 00:00:00 2001 From: fxe025 Date: Wed, 27 Mar 2024 20:39:47 +0800 Subject: [PATCH 6/9] Remove unused method --- src/main/java/storage/Storage.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/main/java/storage/Storage.java b/src/main/java/storage/Storage.java index d647d87de3..2977eb3d87 100644 --- a/src/main/java/storage/Storage.java +++ b/src/main/java/storage/Storage.java @@ -135,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."); - } - } } From 187669c35374c1c84ad2b45675d0e6fbadd0997b Mon Sep 17 00:00:00 2001 From: fxe025 Date: Wed, 27 Mar 2024 20:45:00 +0800 Subject: [PATCH 7/9] no message --- src/main/java/storage/Storage.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/storage/Storage.java b/src/main/java/storage/Storage.java index 2977eb3d87..0f0f8d0b18 100644 --- a/src/main/java/storage/Storage.java +++ b/src/main/java/storage/Storage.java @@ -103,9 +103,9 @@ else if (keyCommand.contains("SellPrice: $")) { Itemlist.addItem(toAdd); } } catch(FileNotFoundException e) { - System.out.println("File does not exist."); + //System.out.println("File does not exist."); } catch(NumberFormatException e) { - System.out.println("Invalid numbers found."); + //System.out.println("Invalid numbers found."); } } From dda12ddb43f866cdc1525ad626f73187d5e7b4f5 Mon Sep 17 00:00:00 2001 From: fxe025 Date: Wed, 27 Mar 2024 20:53:42 +0800 Subject: [PATCH 8/9] no message --- src/main/java/storage/Storage.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/storage/Storage.java b/src/main/java/storage/Storage.java index 0f0f8d0b18..2977eb3d87 100644 --- a/src/main/java/storage/Storage.java +++ b/src/main/java/storage/Storage.java @@ -103,9 +103,9 @@ else if (keyCommand.contains("SellPrice: $")) { Itemlist.addItem(toAdd); } } catch(FileNotFoundException e) { - //System.out.println("File does not exist."); + System.out.println("File does not exist."); } catch(NumberFormatException e) { - //System.out.println("Invalid numbers found."); + System.out.println("Invalid numbers found."); } } From 3e86d57f252eb3f33d3250314f417d037a048b6b Mon Sep 17 00:00:00 2001 From: fxe025 Date: Wed, 27 Mar 2024 21:00:22 +0800 Subject: [PATCH 9/9] Fix I/O redirection errored test --- src/main/java/seedu/duke/Duke.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/seedu/duke/Duke.java b/src/main/java/seedu/duke/Duke.java index 196a5b308f..a440aca2e2 100644 --- a/src/main/java/seedu/duke/Duke.java +++ b/src/main/java/seedu/duke/Duke.java @@ -25,6 +25,7 @@ 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");