Skip to content

Commit

Permalink
Merge pull request #78 from Fureimi/edit-function-branch
Browse files Browse the repository at this point in the history
Edit function branch
  • Loading branch information
Fureimi committed Mar 31, 2024
2 parents e72b867 + 882b2ef commit 2db3412
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 26 deletions.
3 changes: 0 additions & 3 deletions StockMasterData.txt
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@

1. | potatoes | Qty: 491 pieces | Cat: food | BuyPrice: $0.5 | SellPrice: $1.2
2. | cherries | Qty: 999 pieces | Cat: fruit | BuyPrice: $0.2 | SellPrice: $0.5
52 changes: 46 additions & 6 deletions src/main/java/command/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@
public class EditCommand extends Command{

protected String itemName;

protected String newItemName;
protected int newQuantity;
protected String newUom;
protected String newCategory;
protected float newBuyPrice;
protected float newSellPrice;

public EditCommand(String itemName, int newQuantity) {
public EditCommand(String itemName, String newItemName, int newQuantity, String newUom, String newCategory,
float newBuyPrice, float newSellPrice) {
this.itemName = itemName;
this.newItemName = newItemName;
this.newQuantity = newQuantity;
this.newUom = newUom;
this.newCategory = newCategory;
this.newBuyPrice = newBuyPrice;
this.newSellPrice = newSellPrice;
}

@Override
Expand All @@ -21,17 +31,47 @@ public void execute() {
for (Item item : Itemlist.getItems()) {
if (item.getItemName().equals(itemName) || item.getItemName().toLowerCase().equals(itemName)) {
index = Itemlist.getItems().indexOf(item);
item = Itemlist.getItems().get(index);
break;
}
}
if (index == -1) {
//throw exception;
System.out.println("item not found!");
ui.TextUi.replyToUser("item not found!");
} else {
ui.TextUi.showEditMessage(itemName, Itemlist.getItem(index).getQuantity(), newQuantity );
Itemlist.editQuantity(index, newQuantity);
Item item = Itemlist.getItem(index);
String itemName = item.getItemName();
ui.TextUi.replyToUser("\n" +
"Changed: ");
if (!newItemName.equals("NA")) {
ui.TextUi.showEditMessage(itemName, "newItemName", itemName, newItemName);
item.setItemName(newItemName);
}
if (newQuantity != -1) {
ui.TextUi.showEditMessage(itemName, "newQuantity", String.valueOf(item.getQuantity()),
String.valueOf(newQuantity));
item.setQuantity(newQuantity);
}
if (!newUom.equals("NA")) {
ui.TextUi.showEditMessage(itemName, "newUom", item.getUom(), newUom);
item.setUom(newUom);
}
if (!newCategory.equals("NA")) {
ui.TextUi.showEditMessage(itemName, "newCategory", String.valueOf(item.getCategory()),
String.valueOf(newCategory));
item.setCategory(newCategory);
}
if (newBuyPrice != -1) {
ui.TextUi.showEditMessage(itemName, "newBuyPrice", String.valueOf(item.getBuyPrice()),
String.valueOf(newBuyPrice));
item.setBuyPrice(newBuyPrice);
}
if (newSellPrice != -1) {
ui.TextUi.showEditMessage(itemName, "newSellPrice", String.valueOf(item.getSellPrice()),
String.valueOf(newSellPrice));
item.setSellPrice(newSellPrice);
}
}
ui.TextUi.replyToUser("");
Storage.overwriteFile(Itemlist.getItems());
}
}
7 changes: 5 additions & 2 deletions src/main/java/common/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ public class Messages {
"'add [ITEM_NAME] qty/[QUANTITY_OF_ITEM] /[UNIT_OF_MEASUREMENT] cat/[CATEGORY] " +
"buy/[BUY_PRICE] sell/[SELL_PRICE]'";
public static final String INVALID_DELETE_FORMAT ="Invalid command format. Please use format: 'del [ITEM_NAME]'";
public static final String INVALID_EDIT_FORMAT ="Invalid command format. Please use format: " +
"'edit [ITEM_NAME] qty/[NEW_QUANTITY]'";
public static final String INVALID_EDIT_FORMAT ="Invalid edit command format. Please use format: " +
"'edit [ITEM_NAME] name/[NEW_NAME] qty/[NEW_QUANTITY] uom/[NEW_UOM] cat/[NEW_CATEGORY] " +
"buy/[NEW_BUY_PRICE] sell/[NEW_SELL_PRICE]'\n" + "You can edit at least 1 parameter up to all available" +
" parameters. For example, if you only wish to update buy and sell price, you can input:\n" +
"'edit [ITEM_NAME] buy/[NEW_BUY_PRICE] sell/[NEW_SELL_PRICE]'";
public static final String INVALID_SELL_FORMAT ="Invalid command format. Please use format: " +
"'sell [ITEM_NAME] qty/[SELL_QUANTITY] price/[SELL_PRICE]'";
public static final String INVALID_SELL_PRICE ="Price cannot be negative!";
Expand Down
30 changes: 26 additions & 4 deletions src/main/java/item/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
import itemlist.Itemlist;

public class Item {
private final String itemName;
private String itemName;
private int quantity;
private final String uom;
private final String category;
private String uom;
private String category;
private float buyPrice;
private float sellPrice;
private boolean isOOS;


public Item(String name, int quantity, String uom, String category, float buyPrice, float sellPrice) {
this.itemName = name;
this.quantity = quantity;
assert quantity>= 0 : "Quantity should not be negative.";
this.uom = uom;
if (category.isEmpty()) {
this.category = "NA";
Expand All @@ -38,13 +38,26 @@ public String getCategory() {
return this.category;
}
}

public void setCategory(String newCategory) {
this.category = newCategory;
}
public String getItemName() {
return this.itemName;
}

public void setItemName(String newName) {
this.itemName = newName;
}

public String getUom() {
return uom;
}

public void setUom(String newUom) {
this.uom = newUom;
}

public int getQuantity() {
return this.quantity;
}
Expand All @@ -56,10 +69,19 @@ public void setQuantity(int newQuantity) {
public float getBuyPrice() {
return buyPrice;
}

public void setBuyPrice(float newBuyPrice) {
this.buyPrice = newBuyPrice;
}

public float getSellPrice() {
return sellPrice;
}

public void setSellPrice(float newSellPrice) {
this.sellPrice = newSellPrice;
}

public void markOOS() {
this.isOOS = true;
}
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public class Parser {
Pattern.compile("del (?<itemName>[^/]+)");

public static final Pattern EDIT_COMMAND_FORMAT =
Pattern.compile("edit (?<itemName>[^/]+) qty/(?<newQuantity>\\d+)");
Pattern.compile("edit (?<itemName>[^/]+)" +
"(?:\\s+(name/(?<newItemName>[^/]+)|qty/(?<newQuantity>\\d+)|uom/(?<newUom>[^/]+)|" +
"cat/(?<newCategory>[^/]+)|buy/(?<newBuyPrice>\\d*\\.?\\d+)|sell/(?<newSellPrice>\\d*\\.?\\d+)))+");

public static final Pattern SELL_COMMAND_FORMAT =
Pattern.compile("sell (?<itemName>[^/]+) qty/(?<sellQuantity>\\d+)(?: price/(?<sellPrice>[^/]+))?");
Expand Down Expand Up @@ -139,15 +141,27 @@ private Command prepareDelete(String args) throws CommandFormatException{

private Command prepareEdit(String args) throws CommandFormatException{
final Matcher matcher = EDIT_COMMAND_FORMAT.matcher(args.trim());
// Validate arg string format
if (!matcher.matches()) {
throw new CommandFormatException(CommandType.EDIT);
}
int newQuantity = Integer.parseInt(matcher.group("newQuantity"));
assert newQuantity >= 0 : "New quantity should not be negative.";
String itemName = matcher.group("itemName");
String newItemName = matcher.group("newItemName") != null ? matcher.group("newItemName") : "NA";
int newQuantity = matcher.group("newQuantity") != null ?
Integer.parseInt(matcher.group("newQuantity")) : -1;
String newUom = matcher.group("newUom") != null ? matcher.group("newUom") : "NA";
String newCategory = matcher.group("newCategory") != null ? matcher.group("newCategory") : "NA";
float newBuyPrice = matcher.group("newBuyPrice") != null ?
Float.parseFloat(matcher.group("newBuyPrice")) : -1;
float newSellPrice = matcher.group("newSellPrice") != null ?
Float.parseFloat(matcher.group("newSellPrice")) : -1;
return new EditCommand(
matcher.group("itemName"),
newQuantity
itemName,
newItemName,
newQuantity,
newUom,
newCategory,
newBuyPrice,
newSellPrice
);
}

Expand Down
31 changes: 26 additions & 5 deletions src/main/java/ui/TextUi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Scanner;

import item.Item;

public class TextUi {
Expand Down Expand Up @@ -70,7 +71,8 @@ public static <T> void showInventoryList(ArrayList<T> arrayList) {
}
}
}
public static void showCategoryList (ArrayList<Item> arrayList, String category) {

public static void showCategoryList(ArrayList<Item> arrayList, String category) {
if (arrayList.isEmpty()) {
replyToUser("There is nothing here! Time to spend some money and stock em up!");
} else {
Expand All @@ -89,10 +91,29 @@ public static void showCategoryList (ArrayList<Item> arrayList, String category)
}
}

public static void showEditMessage(String item, int oldQuantity, int newQuantity) {
replyToUser("\n" +
"Changed quantity of " + item + " from " + oldQuantity + " to " + newQuantity
);
public static void showEditMessage(String item, String editedParameter, String oldParameter, String newParameter) {
switch (editedParameter) {
case "newItemName":
replyToUser("Name of " + item + " from " + oldParameter + " to " + newParameter);
break;
case "newQuantity":
replyToUser("Quantity of " + item + " from " + oldParameter + " to " + newParameter);
break;
case "newUom":
replyToUser("Unit of Measurement of " + item + " from " + oldParameter + " to " + newParameter);
break;
case "newCategory":
replyToUser("Category of " + item + " from " + oldParameter + " to " + newParameter);
break;
case "newBuyPrice":
replyToUser("Buy Price of " + item + " from " + oldParameter + " to " + newParameter);
break;
case "newSellPrice":
replyToUser("Sell Price of " + item + " from " + oldParameter + " to " + newParameter);
break;
default:
break;
}
}

public static void showSellMessage(String item, int sellQuantity, int remainingQuantity, float sellPrice) {
Expand Down

0 comments on commit 2db3412

Please sign in to comment.