Skip to content

Commit

Permalink
Merge pull request #82 from HengShuHong/master
Browse files Browse the repository at this point in the history
Fix readFile
  • Loading branch information
HengShuHong committed Mar 31, 2024
2 parents 2db3412 + 19e77f2 commit 83334a5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 2 additions & 0 deletions StockMasterData.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1. | apple iphone | Qty: 10 pieces | Cat: electronics | BuyPrice: $100.0 | SellPrice: $500.0
2. | apple | Qty: 50 pieces | Cat: fruits | BuyPrice: $4.0 | SellPrice: $5.0
10 changes: 6 additions & 4 deletions src/main/java/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public static void readFromFile(String fileName) {
try {
Scanner scanner = new Scanner(new File(fileName));
while (scanner.hasNext()) {
String fileLine = "add" + scanner.nextLine();
//String fileLine = "add" + scanner.nextLine();
String fileLine = scanner.nextLine();
String[] keyCommands = fileLine.split(" \\| ");
String commandQty = "";
String commandCat = "";
Expand All @@ -74,7 +75,7 @@ public static void readFromFile(String fileName) {
String commandSell = "";
String commandName = "";
for (String keyCommand : keyCommands) {
if (keyCommand.contains(".")) {
if (keyCommand.contains("add")) {
//do nothing.
} else if (keyCommand.contains("Qty: ")) {
String[] commandQtyUnit = keyCommand.replace("Qty: ", "").split(" ");
Expand All @@ -88,12 +89,13 @@ public static void readFromFile(String fileName) {
} else if (keyCommand.contains("SellPrice: $")) {
commandSell = keyCommand.replace("SellPrice: $", "");
} else {
commandName = keyCommand;
commandName = keyCommand.trim();
}
}
Item toAdd = new Item(commandName, Integer.parseInt(commandQty), commandUom, commandCat,
Integer.parseInt(commandBuy), Integer.parseInt(commandSell));
Float.parseFloat(commandBuy), Float.parseFloat(commandSell));
Itemlist.addItem(toAdd);

}
scanner.close();
} catch(FileNotFoundException e) {
Expand Down

0 comments on commit 83334a5

Please sign in to comment.