Skip to content

Commit

Permalink
Merge pull request #53 from imanamirshah/feature-AddCommand
Browse files Browse the repository at this point in the history
Add additional attributes to Item class and parameters to add methods
  • Loading branch information
nkotaa authored Mar 17, 2024
2 parents 924e756 + a0dfa4f commit 1b6bf8c
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 28 deletions.
42 changes: 40 additions & 2 deletions src/main/java/seedu/binbash/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@
public class Item {
private final String itemName;
private final String itemDescription;
private final int itemQuantity;
private final String itemExpirationDate;
private final double itemSalePrice;
private final double itemCostPrice;

public Item(String itemName, String itemDescription) {


public Item(String itemName, String itemDescription, int itemQuantity, String itemExpirationDate,
double itemSalePrice, double itemCostPrice) {
this.itemName = itemName;
this.itemDescription = itemDescription;
this.itemQuantity = itemQuantity;
this.itemExpirationDate = itemExpirationDate;
this.itemSalePrice = itemSalePrice;
this.itemCostPrice = itemCostPrice;
}

public String getItemName() {
Expand All @@ -17,8 +28,35 @@ public String getItemDescription() {
return itemDescription;
}

public int getItemQuantity() {
return itemQuantity;
}
public String getItemExpirationDate() {
return itemExpirationDate;
}

public double getItemSalePrice() {
return itemSalePrice;
}

public double getItemCostPrice() {
return itemCostPrice;
}

@Override
public String toString() {
return itemName + ": " + itemDescription;
return String.format("%s" + System.lineSeparator() +
"\tdescription: %s" + System.lineSeparator() +
"\tquantity: %d" + System.lineSeparator() +
"\texpiry date: %s" + System.lineSeparator() +
"\tsale price: $%.2f" + System.lineSeparator() +
"\tcost price: $%.2f",
itemName,
itemDescription,
itemQuantity,
itemExpirationDate,
itemSalePrice,
itemCostPrice
);
}
}
11 changes: 6 additions & 5 deletions src/main/java/seedu/binbash/ItemList.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ public int getItemCount() {
return itemList.size();
}

public String addItem(String itemName, String itemDescription) {
Item item = new Item(itemName, itemDescription);
public String addItem(String itemName, String itemDescription, int itemQuantity, String itemExpirationDate,
double itemSalePrice, double itemCostPrice) {
Item item = new Item(itemName, itemDescription, itemQuantity, itemExpirationDate, itemSalePrice, itemCostPrice);

itemList.add(item);

String output = "Noted! I have added the following item into your inventory:"
+ String.format("\t%s", item);
String output = "Noted! I have added the following item into your inventory:\n"
+ "\n" + item;
return output;
}

Expand Down Expand Up @@ -69,7 +70,7 @@ public String printList(List<Item> itemList) {
String output = "";

for (Item item: itemList) {
output += item.toString() + System.lineSeparator();
output += item.toString() + System.lineSeparator() + System.lineSeparator();
}

return output;
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/seedu/binbash/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ private Command parseAddCommand(String userInput) {
if (matcher.matches()) {
String itemName = matcher.group("itemName");
String itemDescription = matcher.group("itemDescription");
return new AddCommand(itemList, itemName, itemDescription);
int itemQuantity = Integer.parseInt(matcher.group("itemQuantity"));
String itemExpirationDate = matcher.group("itemExpirationDate");
double itemSalePrice = Double.parseDouble(matcher.group("itemSalePrice"));
double itemCostPrice = Double.parseDouble(matcher.group("itemCostPrice"));

return new AddCommand(itemList, itemName, itemDescription, itemQuantity, itemExpirationDate, itemSalePrice,
itemCostPrice);
} else {
return null;
}
Expand Down
30 changes: 23 additions & 7 deletions src/main/java/seedu/binbash/command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,42 @@
import seedu.binbash.ItemList;

public class AddCommand extends Command {
public static final Pattern COMMAND_FORMAT = Pattern.compile(
"add\\s+n/(?<itemName>.+?)\\s+d/(?<itemDescription>.+)"
);

public static final Pattern COMMAND_FORMAT =
Pattern.compile("add\\s+n/(?<itemName>.+?)\\s+d/(?<itemDescription>.+)\\s+q/(?<itemQuantity>.+)\\s"
+ "+e/(?<itemExpirationDate>.+)+s/(?<itemSalePrice>.+)+c/(?<itemCostPrice>.+)");
private final String itemName;
private final String itemDescription;
private final int itemQuantity;
private final String itemExpirationDate;
private final double itemSalePrice;
private final double itemCostPrice;

public AddCommand(ItemList itemList, String itemName, String itemDescription) {
public AddCommand(ItemList itemList, String itemName, String itemDescription, int itemQuantity,
String itemExpirationDate, double itemSalePrice, double itemCostPrice) {
super(itemList);
this.itemName = itemName;
this.itemDescription = itemDescription;
this.itemQuantity = itemQuantity;
this.itemExpirationDate = itemExpirationDate;
this.itemSalePrice = itemSalePrice;
this.itemCostPrice = itemCostPrice;

commandLogger.fine(String.format(
"Creating Add Command... ItemName: %s , ItemDescription: %s",
"Creating Add Command... itemName: %s, itemDescription: %s, itemQuantity: %d, itemExpirationDate: %s"
+ "itemSalePrice: %f, itemCostPrice: %f",
itemName,
itemDescription
itemDescription,
itemQuantity,
itemExpirationDate,
itemSalePrice,
itemCostPrice
));
}

@Override
public String execute() {
return itemList.addItem(itemName, itemDescription);
return itemList.addItem(itemName, itemDescription, itemQuantity, itemExpirationDate,
itemSalePrice, itemCostPrice);
}
}
37 changes: 29 additions & 8 deletions src/test/java/seedu/binbash/ItemListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class ItemListTest {
@Test
void deleteItem_oneItemInItemList_noItemInItemList() {
ItemList itemList = new ItemList();
itemList.addItem("testItem", "A test item");
itemList.addItem("testItem", "A test item", 2,
"3", 4.00, 5.00);

itemList.deleteItem(1);

Expand All @@ -20,32 +21,52 @@ void deleteItem_oneItemInItemList_noItemInItemList() {
void addItem_noItemInItemList_oneItemInItemList() {
ItemList itemList = new ItemList();

itemList.addItem("testItem", "A test item");
itemList.addItem("testItem", "A test item", 2,
"3", 4.00, 5.00);
assertEquals(1, itemList.getItemCount());
}

@Test
void addItem_itemNameAndDescription_correctItemNameAndDescription() {
void addItem_itemInputs_correctItemParameters() {
ItemList itemList = new ItemList();

itemList.addItem("testItem", "A test item");
itemList.addItem("testItem", "A test item", 2,
"3", 4.00, 5.00);
Item item = itemList.getItemList().get(0);

assertEquals(item.getItemName(), "testItem");
assertEquals(item.getItemDescription(), "A test item");
assertEquals(item.getItemQuantity(), 2);
assertEquals(item.getItemExpirationDate(), "3");
assertEquals(item.getItemSalePrice(), 4.00);
assertEquals(item.getItemCostPrice(), 5.00);
}

@Test
void printList_twoItemsInItemList_correctPrintFormatForBothItems() {
ItemList itemList = new ItemList();

itemList.addItem("testItem", "1");
itemList.addItem("testItem", "2");
itemList.addItem("testItem1", "Test item 1", 2,
"3", 4.00, 5.00);
itemList.addItem("testItem2", "Test item 2", 6,
"7", 8.00, 9.00);

String actualOutput = itemList.printList(itemList.getItemList());

String expectedOutput = "testItem: 1" + System.lineSeparator() +
"testItem: 2" + System.lineSeparator();
String expectedOutput = "testItem1" + System.lineSeparator() +
"\tdescription: Test item 1" + System.lineSeparator() +
"\tquantity: 2" + System.lineSeparator() +
"\texpiry date: 3" + System.lineSeparator() +
"\tsale price: $4.00" + System.lineSeparator() +
"\tcost price: $5.00" + System.lineSeparator() +
System.lineSeparator() +
"testItem2" + System.lineSeparator() +
"\tdescription: Test item 2" + System.lineSeparator() +
"\tquantity: 6" + System.lineSeparator() +
"\texpiry date: 7" + System.lineSeparator() +
"\tsale price: $8.00" + System.lineSeparator() +
"\tcost price: $9.00" + System.lineSeparator() +
System.lineSeparator();

assertEquals(expectedOutput, actualOutput);
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/seedu/binbash/command/AddCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class AddCommandTest {
@Test
void execute_item_oneItemInItemList() {
ItemList itemList = new ItemList();
AddCommand addCommand = new AddCommand(itemList, "testItem", "A test item");
AddCommand addCommand = new AddCommand(itemList, "testItem", "A test item", 2,
"3", 4.00, 5.00);

addCommand.execute();
assertEquals(1, itemList.getItemCount());
Expand Down
22 changes: 18 additions & 4 deletions src/test/java/seedu/binbash/command/ListCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,29 @@ class ListCommandTest {
void execute_listCommandWithTwoItemsInItemList_correctPrintFormatForBothItems() {
ItemList itemList = new ItemList();

itemList.addItem("testItem", "1");
itemList.addItem("testItem", "2");
itemList.addItem("testItem1", "Test item 1", 2,
"3", 4.00, 5.00);
itemList.addItem("testItem2", "Test item 2", 6,
"7", 8.00, 9.00);

ListCommand listCommand = new ListCommand(itemList);

String actualOutput = listCommand.execute();

String expectedOutput = "testItem: 1" + System.lineSeparator() +
"testItem: 2" + System.lineSeparator();
String expectedOutput = "testItem1" + System.lineSeparator() +
"\tdescription: Test item 1" + System.lineSeparator() +
"\tquantity: 2" + System.lineSeparator() +
"\texpiry date: 3" + System.lineSeparator() +
"\tsale price: $4.00" + System.lineSeparator() +
"\tcost price: $5.00" + System.lineSeparator() +
System.lineSeparator() +
"testItem2" + System.lineSeparator() +
"\tdescription: Test item 2" + System.lineSeparator() +
"\tquantity: 6" + System.lineSeparator() +
"\texpiry date: 7" + System.lineSeparator() +
"\tsale price: $8.00" + System.lineSeparator() +
"\tcost price: $9.00" + System.lineSeparator() +
System.lineSeparator();

assertEquals(expectedOutput, actualOutput);
}
Expand Down

0 comments on commit 1b6bf8c

Please sign in to comment.