Skip to content

Commit

Permalink
Merge pull request #63 from fxe025/master
Browse files Browse the repository at this point in the history
Edit readFromFile
  • Loading branch information
fxe025 committed Mar 27, 2024
2 parents a24caac + 3e86d57 commit 952cb25
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 22 deletions.
3 changes: 3 additions & 0 deletions src/main/java/seedu/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import command.ExitCommand;
import exceptions.CommandFormatException;
import parser.Parser;
import storage.Storage;
import ui.TextUi;

import itemlist.Itemlist;
Expand All @@ -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");
}
Expand Down
65 changes: 48 additions & 17 deletions src/main/java/storage/Storage.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storage;

import item.Item;
import itemlist.Itemlist;

import java.io.File;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -62,31 +63,70 @@ 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<Item> 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);
}

public static void overwriteFile(ArrayList<Item> 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);
Expand All @@ -95,13 +135,4 @@ public static void overwriteFile(ArrayList<Item> 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.");
}
}
}
9 changes: 4 additions & 5 deletions src/test/java/storage/StorageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -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 {
Expand Down

0 comments on commit 952cb25

Please sign in to comment.