Skip to content

Commit

Permalink
Merge pull request #297 from Cazh1/Bug_fix_Day_not_set_if_null_order
Browse files Browse the repository at this point in the history
Bug fix day not set if null order
  • Loading branch information
ShaniceTang authored Nov 10, 2023
2 parents d8dd484 + 7893320 commit 9837d16
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class BuyIngredientCommand extends Command {
private ArrayList<Ingredient> ingredients;
private ArrayList<Ingredient> ingredientsToBePrinted = new ArrayList<>();
private String ingredientString = ""; // Used to store the message about the bought ingredients
private int FIRST_INDEX = 0;
private int finalIndex = 0;

/**
* Constructs a BuyIngredientCommand with the specified ingredients, user interface, and pantry.
Expand Down Expand Up @@ -67,7 +67,7 @@ private void addIngredient() {
ingredients.set(i, ingredient);
}

for (int i = ingredients.size() - ui.OFFSET_LIST_INDEX; i >= FIRST_INDEX; i--) {
for (int i = ingredients.size() - ui.OFFSET_LIST_INDEX; i >= finalIndex; i--) {
Ingredient ingredient = ingredients.get(i);
buildBuyIngredientMessage(ingredient);
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/seedu/cafectrl/data/Sales.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
*/
public class Sales {
private static ArrayList<OrderList> orderLists;
private static final int DAY_DISPLAY_OFFSET = 1;
private int daysAccounted;
private final int DAY_DISPLAY_OFFSET = 1;

public Sales() {
this.orderLists = new ArrayList<>();
Expand All @@ -21,7 +21,7 @@ public Sales() {

public Sales(ArrayList<OrderList> orderLists) {
this.orderLists = orderLists;
this.daysAccounted = 0;
this.daysAccounted = orderLists.size() - 1;
}

//TODO: @Zhong Heng, Remove this method if not used
Expand Down Expand Up @@ -63,12 +63,12 @@ public void printSales(Ui ui, Menu menu) {
ui.showToUser("No sales made.");
return;
}
ui.showSalesBottom();
//ui.showSalesBottom();
for (int day = 0; day < orderLists.size(); day++) {
OrderList orderList = orderLists.get(day);

if (orderList.isEmpty() || !orderList.hasCompletedOrders()) {
ui.showToUser("No sales for day " + (day + DAY_DISPLAY_OFFSET) + ".");
ui.showToUser("", "No sales for day " + (day + DAY_DISPLAY_OFFSET) + ".", "");
continue;
}

Expand Down
56 changes: 42 additions & 14 deletions src/main/java/seedu/cafectrl/storage/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import seedu.cafectrl.data.dish.Dish;
import seedu.cafectrl.data.dish.Ingredient;
import seedu.cafectrl.ui.ErrorMessages;
import seedu.cafectrl.ui.Messages;
import seedu.cafectrl.ui.Ui;

import java.util.ArrayList;
Expand Down Expand Up @@ -115,32 +116,59 @@ private static boolean isValidPantryStockFormat(String[] decodedPantryStock) {
* @return Sales object containing OrderList objects decoded from the provided strings.
*/
public static Sales decodeSales(ArrayList<String> textLines, Menu menu) {
boolean salesOrderTextTamperDetectionMessagePrinted = false;
ArrayList<OrderList> orderLists = new ArrayList<>();
if(textLines.isEmpty()) {
return new Sales();
}
//for each 'order' in text file
for (String line : textLines) {
String[] orderData = line.split(DIVIDER);
int day = Integer.parseInt(orderData[0].trim()) - 1;
String dishName = orderData[1].trim();
int quantity = Integer.parseInt(orderData[2].trim());
float totalOrderCost = Float.parseFloat(orderData[3].trim());
boolean isComplete = "true".equals(orderData[4].trim());
Dish dish = menu.getDishFromName(dishName);
if(dish == null) {
ui.showDecodedInvalidDish(dishName);
} else {
try {
String[] orderData = line.split(DIVIDER);
int day = Integer.parseInt(orderData[0].trim()) - 1;
String dishName = orderData[1].trim();
if (dishName.equals(Encoder.NULL_ORDER_DAY)) {
orderLists = fillOrderListSize(orderLists, day);
continue;
}
int quantity = Integer.parseInt(orderData[2].trim());
float totalOrderCost = Float.parseFloat(orderData[3].trim());
boolean isComplete = "true".equals(orderData[4].trim());
Dish dish = menu.getDishFromName(dishName);
if (dish == null) {
ui.showDecodedInvalidDish(dishName);
continue;
}
Order orderedDish = new Order(menu.getDishFromName(dishName), quantity, totalOrderCost, isComplete);
//increase size of orderLists if needed
//this can be used in the event that the text file's first order is not day 0
while (orderLists.size() <= day) {
orderLists.add(new OrderList());
}

orderLists = fillOrderListSize(orderLists, day);
orderLists.get(day).addOrder(orderedDish);
} catch (IndexOutOfBoundsException e) {
ui.showToUser(Messages.SALES_LAST_DAY_TEXT_TAMPERED, System.lineSeparator());
} catch (NumberFormatException e) {
if (!salesOrderTextTamperDetectionMessagePrinted) {
ui.showToUser(Messages.SALES_ORDER_TEXT_TAMPERED, System.lineSeparator());
salesOrderTextTamperDetectionMessagePrinted = true;
}
}
}
return new Sales(orderLists);
}

//@@author Cazh1
/**
* Increases the size of the orderlist when there is gap between the previous order and the next
*
* @param orderLists The current partially filled ArrayList of OrderList
* @param day The day of the next order
* @return orderLists after filling in the gaps
*/
private static ArrayList<OrderList> fillOrderListSize(ArrayList<OrderList> orderLists, int day) {
while (orderLists.size() <= day) {
orderLists.add(new OrderList());
}
return orderLists;
}

}
25 changes: 25 additions & 0 deletions src/main/java/seedu/cafectrl/storage/Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* making the data suitable for saving to a file.
*/
public class Encoder {
public static final String NULL_ORDER_DAY = "the last day has no orders but please account for it";
private static final String DIVIDER = " | ";
//@@author ShaniceTang
/**
Expand Down Expand Up @@ -105,6 +106,30 @@ public static ArrayList<String> encodeSales(Sales sales) {
orderString.append(System.lineSeparator());
encodedList.add(String.valueOf(orderString));
}
if (day == sales.getDaysAccounted()) {
encodedList = encodeLastSalesDay(encodedList, orderList, day);
}
}
return encodedList;
}

//@@author Cazh1
/**
* Checks if the last day accessed has valid orders added
*
* @param encodedList An ArrayList of strings representing the encoded sales data.
* @param orderList An ArrayList of Orders of the last day accessed
* @param day The last day accessed
* @return encodedList with specific String added at the end if no valid orders were detected
*/
private static ArrayList<String> encodeLastSalesDay(ArrayList<String> encodedList, OrderList orderList, int day) {
if (orderList.getSize() == 0) {
StringBuilder orderString = new StringBuilder();
//day of each orderList is index + 1
orderString.append((day + 1) + DIVIDER);
orderString.append(NULL_ORDER_DAY);
orderString.append(System.lineSeparator());
encodedList.add(String.valueOf(orderString));
}
return encodedList;
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/seedu/cafectrl/ui/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,13 @@ public class Messages {
+ "You might have tempered with the file and added in a non existing dish.\n"
+ "Don't worry :D , we will continue operations without ";

public static final String SALES_LAST_DAY_TEXT_TAMPERED = "Well, well, well! "
+ "It seems someone's been playing digital detective and tampered with the last line in the text file.\n"
+ "Unfortunately, any empty order lists after the latest valid order have been wiped from my memory banks."
+ " Poof!" ;

public static final String SALES_ORDER_TEXT_TAMPERED = "Well, well, well! "
+ "It seems someone's been playing digital detective and tampered with the orders in the text file.\n"
+ "Unfortunately, any such orders have been wiped from my memory banks. Poof!" ;

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public void execute_existingSales_listTotalSales() {
System.setOut(originalOut);

String expectedOutput = Messages.SHOW_SALES_END_CAP
+ Messages.SHOW_SALES_END_CAP
+ Messages.SHOW_SALES_DAY_PART_1
+ "1"
+ Messages.SHOW_SALES_DAY_PART_2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.jupiter.api.Test;
import seedu.cafectrl.data.Pantry;
import seedu.cafectrl.data.dish.Ingredient;
import seedu.cafectrl.ui.Messages;
import seedu.cafectrl.ui.Ui;

import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -32,16 +33,16 @@ void execute_printPantryStock() {
String actualOutput = baos.toString().trim();
System.setOut(originalOut);

String expectedOutput = "+-------------------------------------------------------+\n"
+ "| You have the following ingredients in pantry: |\n"
+ "+----------------------------------------+--------------+\n"
+ "| Ingredients | Qty |\n"
+ "+----------------------------------------+--------------+\n"
String expectedOutput = Messages.MENU_END_CAP
+ Messages.VIEW_STOCK_MESSAGE2
+ Messages.MENU_CORNER
+ Messages.VIEW_STOCK_TITLE_MESSAGE
+ Messages.MENU_CORNER
+ "| chicken | 500g |\n"
+ "| rice | 1000g |\n"
+ "+-------------------------------------------------------+\n";
+ Messages.MENU_END_CAP;

assertEquals(expectedOutput.trim().replaceAll("\\s+", " "),
actualOutput.trim().replaceAll("\\s+", " "));
assertEquals(expectedOutput.trim().replaceAll("\\s+", ""),
actualOutput.trim().replaceAll("\\s+", ""));
}
}

0 comments on commit 9837d16

Please sign in to comment.