Skip to content

Commit

Permalink
add buy and sell prices to add function
Browse files Browse the repository at this point in the history
  • Loading branch information
Joellimjr committed Mar 27, 2024
1 parent a248960 commit 63f9470
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
2 changes: 2 additions & 0 deletions StockMasterData.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
0 | apple | 5
0 | apple | 5
20 changes: 14 additions & 6 deletions src/main/java/command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,44 @@ public class AddCommand extends Command {
protected int quantity;
protected String uom;
protected String category = "NA";
protected int buyPrice;
protected int sellPrice;
private final Item toAdd;

public AddCommand(String itemName, int quantity, String uom, String category) {
public AddCommand(String itemName, int quantity, String uom, String category, int buyPrice, int sellPrice) {
this.itemName = itemName;
this.quantity = quantity;
this.uom = uom;
this.category = category;
this.toAdd = new Item(itemName, quantity, uom, category);
this.buyPrice = buyPrice;
this.sellPrice = sellPrice;
this.toAdd = new Item(itemName, quantity, uom, category, buyPrice, sellPrice);
}

public String getItemName() {
return itemName;
}

public int getQuantity() {
return quantity;
}

public String getCategory() {
return category;
}

public String getUom() {
return uom;
}
public int getBuyPrice() {
return buyPrice;
}
public int getSellPrice() {
return sellPrice;
}

@Override
public void execute() {
Itemlist.addItem(toAdd);
System.out.print(MESSAGE_SUCCESS + getItemName() + " (Qty: " + getQuantity() + " " + getUom() + ")");
System.out.print(MESSAGE_SUCCESS + getItemName() + " (Qty: " + getQuantity() + getUom() +
", Buy: $" + getBuyPrice() + ", Sell: $" + getSellPrice() + ")");
Storage.addToFile(Itemlist.getItems(), true);
if (!category.equals("NA")) {
System.out.println(" to " + getCategory());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/common/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class Messages {
public static final String INVALID_COMMAND = "Invalid command detected. Type 'help' for list of valid commands";
public static final String HELP = "Commands: ....";
public static final String INVALID_ADD_FORMAT ="Invalid command format. Please use format: " +
"'add [ITEM_NAME] qty/[QUANTITY_OF_ITEM] /[UNIT_OF_MEASUREMENT] cat/[CATEGORY]'";
"'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]'";
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/item/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ public class Item {
public static int numberOfItems;
private final String itemName;
private int quantity;
private String uom;
private String category;
private final String uom;
private final String category;
private int buyPrice;
private int sellPrice;
private boolean isOOS;


public Item(String name, int quantity, String uom, String category) {
public Item(String name, int quantity, String uom, String category, int buyPrice, int sellPrice) {
this.itemName = name;
this.quantity = quantity;
this.uom = uom;
Expand All @@ -18,6 +20,8 @@ public Item(String name, int quantity, String uom, String category) {
} else {
this.category = category;
}
this.buyPrice = buyPrice;
this.sellPrice = sellPrice;
if (quantity == 0) {
this.isOOS = true;
} else {
Expand Down Expand Up @@ -48,6 +52,13 @@ public void setQuantity(int newQuantity) {
this.quantity = newQuantity;
}

public int getBuyPrice() {
return buyPrice;
}
public int getSellPrice() {
return sellPrice;
}

public void markOOS() {
this.isOOS = true;
}
Expand All @@ -58,6 +69,7 @@ public void unmarkOOS() {

public String toString() {
String categoryString = (getCategory() != null) ? " to " + getCategory() : ""; // Check if category is null
return (getItemName() + " (Qty " + getQuantity() + " " + getUom() + ")" + categoryString);
return (getItemName() + " (Qty " + getQuantity() + getUom() +
", Buy: $" + getBuyPrice() + ", Sell: $" + getSellPrice() + ")" + categoryString);
}
}
9 changes: 7 additions & 2 deletions src/main/java/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

public class Parser {
public static final Pattern ADD_COMMAND_FORMAT =
Pattern.compile("add (?<itemName>[^/]+) qty/(?<quantity>\\d+) /(?<uom>[^/]+)(?: cat/(?<category>[^/]+))?");
Pattern.compile("add (?<itemName>[^/]+) qty/(?<quantity>\\d+) /(?<uom>[^/]+)(?: " +
"cat/(?<category>[^/]+))? buy/(?<buyPrice>\\d+) sell/(?<sellPrice>\\d+)");


public static final Pattern DELETE_COMMAND_FORMAT =
Expand Down Expand Up @@ -88,12 +89,16 @@ private Command prepareAdd(String args) throws CommandFormatException{
}
String category = matcher.group("category") != null ? matcher.group("category") : "NA";
int quantity = Integer.parseInt(matcher.group("quantity"));
int buyPrice = Integer.parseInt(matcher.group("buyPrice"));
int sellPrice = Integer.parseInt(matcher.group("sellPrice"));
assert quantity >= 0 : "Quantity should not be negative.";
return new AddCommand(
matcher.group("itemName"),
quantity,
matcher.group("uom"),
category
category,
buyPrice,
sellPrice
);
}

Expand Down

0 comments on commit 63f9470

Please sign in to comment.