Skip to content

Commit

Permalink
Merge pull request #170 from NaychiMin/148_encoding_decoding_orders_v2.0
Browse files Browse the repository at this point in the history
Task 148, Encoding and Decoding of Sales object v2.0
  • Loading branch information
ziyi105 authored Oct 31, 2023
2 parents b76a27a + 2b98bd6 commit 132908e
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 83 deletions.
6 changes: 3 additions & 3 deletions src/main/java/seedu/cafectrl/CafeCtrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import seedu.cafectrl.ui.Messages;
import seedu.cafectrl.ui.Ui;

import java.io.FileNotFoundException;
import java.io.IOException;

/**
Expand All @@ -29,12 +28,13 @@ public class CafeCtrl {
/**
* Private constructor for the CafeCtrl class, used for initializing the user interface and menu list.
*/
private CafeCtrl() throws FileNotFoundException {
private CafeCtrl() throws IOException {
this.ui = new Ui();
this.ui.showToUser(Messages.INITIALISE_STORAGE_MESSAGE);
this.storage = new Storage(this.ui);
this.menu = this.storage.loadMenu();
this.pantry = this.storage.loadPantryStock();
this.sales = this.storage.loadOrderList(menu);
currentDate = new CurrentDate();
this.sales = new Sales();
}
Expand Down Expand Up @@ -62,7 +62,7 @@ private void run() throws IOException {
ui.printLine();
}
} while (!command.isExit());
this.storage.saveAll(this.menu, this.sales, this.pantry);
//this.storage.saveAll(this.menu, this.sales, this.pantry);
}

public static void main(String[] args) throws IOException {
Expand Down
28 changes: 22 additions & 6 deletions src/main/java/seedu/cafectrl/data/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ public Order(Dish orderedDish, int dishQty) {
this.dishQty = dishQty;
this.orderedDish = orderedDish;
this.ingredientList = setIngredientList();
this.totalOrderCost = getDishPrice();
this.totalOrderCost = totalOrderCost();
}

public Order(Dish orderedDish, int dishQty, float orderCost) {
this.dishQty = dishQty;
this.orderedDish = orderedDish;
this.ingredientList = setIngredientList();
this.totalOrderCost = orderCost;
}

@Override
public String toString() {
return "Order: " + orderedDish.getName() + " Quantity: "+ dishQty
+ "\nOrder Cost: $" + dollarValue.format(totalOrderCost);
return "Order: " + getDishName() + " Quantity: "+ dishQty
+ "\nIngredientList: " + ingredientList
+ "\nTotal Order Cost: $" + dollarValue.format(totalOrderCost);
}

/**
Expand All @@ -33,10 +41,10 @@ public String toString() {
*
* @return Total calculated cost
*/
private float getDishPrice() {

public float totalOrderCost() {
float dishCost = orderedDish.getPrice();
float totalOrderCost = dishCost * dishQty;
return totalOrderCost;
return dishCost * dishQty;
}

/**
Expand Down Expand Up @@ -72,4 +80,12 @@ public boolean getIsComplete() {
return isComplete;
}

public String getDishName() {
return orderedDish.getName();
}

public int getQuantity() {
return dishQty;
}

}
7 changes: 5 additions & 2 deletions src/main/java/seedu/cafectrl/data/OrderList.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public OrderList() {
this.orderList = new ArrayList<>();
this.totalOrderListCost = 0;
}
public OrderList(ArrayList<Order> orderList) {
this.orderList = orderList;
public OrderList(ArrayList<Order> decodedOrderList){
this.orderList = decodedOrderList;
this.totalOrderListCost = 0;
}

Expand All @@ -28,6 +28,9 @@ public Order getOrder(int orderID) {
public void removeOrder(int orderID) {
orderList.remove(orderID);
}
public boolean isEmpty() {
return orderList.isEmpty();
}

public void addOrder(Order order) {
orderList.add(order);
Expand Down
60 changes: 42 additions & 18 deletions src/main/java/seedu/cafectrl/storage/Decoder.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package seedu.cafectrl.storage;

import seedu.cafectrl.data.Menu;

import seedu.cafectrl.data.Pantry;
import seedu.cafectrl.data.dish.Dish;
import seedu.cafectrl.data.Order;
import seedu.cafectrl.data.OrderList;
import seedu.cafectrl.data.Menu;
import seedu.cafectrl.data.Sales;
import seedu.cafectrl.data.dish.Ingredient;
import seedu.cafectrl.ui.ErrorMessages;
import seedu.cafectrl.ui.Ui;

import java.util.ArrayList;

/**
* The Decoder class offers methods to interpret string representations from text files,
* decoding them into appropriate data structures. It includes methods to decode a Menu,
* Pantry stock, and OrderList, allowing retrieval of data stored in a file.
*/
public class Decoder {

private static final String DIVIDER = " | ";
private static final Ui ui = new Ui();

//@@author ziyi105
Expand Down Expand Up @@ -47,24 +57,38 @@ private static boolean isValidPantryStockFormat(String[] decodedPantryStock) {
return true;
}

//@@author Dexter
public Menu decodeMenuData(ArrayList<String> textLines) {
ArrayList<Dish> dishArrayList = new ArrayList<>();
for (String task : textLines) {
String[] splitTaskString = task.split(" \\| ");
String dishName = splitTaskString[0];
float dishPrice = Float.parseFloat(splitTaskString[1]);
String dishIngredient = splitTaskString[2];

try {
//todo: remove testing code
Dish dish = new Dish(dishName, dishPrice);
dishArrayList.add(dish);
} catch (Exception e) {
ui.showToUser(e.getMessage());
//@@NaychiMin
/**
* Decodes a list of order data and constructs a Sales object using an array of OrderList objects.
*
* @param textLines List of order strings in the format "dishName|quantity|totalOrderCost".
* @param menu Menu instance to retrieve Dish objects based on dishName.
* @return Sales object containing OrderList objects decoded from the provided strings.
*/
public static Sales decodeSales(ArrayList<String> textLines, Menu menu) {
ArrayList<OrderList> orderLists = new ArrayList<>();

//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());

Order orderedDish = new Order(menu.getDishFromName(dishName), quantity, totalOrderCost);

//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());
}
}

return new Menu(dishArrayList);
orderLists.get(day).addOrder(orderedDish);
}
return new Sales(orderLists);
}
//@@author

}
55 changes: 28 additions & 27 deletions src/main/java/seedu/cafectrl/storage/Encoder.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package seedu.cafectrl.storage;

import seedu.cafectrl.data.Menu;
import seedu.cafectrl.data.Order;
import seedu.cafectrl.data.OrderList;
import seedu.cafectrl.data.Pantry;
import seedu.cafectrl.data.Sales;
import seedu.cafectrl.data.dish.Ingredient;

import java.util.ArrayList;

public class Encoder {
private static final String DIVIDER = " | ";

//@@author ziyi105
public static ArrayList<String> encodePantryStock(Pantry pantry) {
Expand All @@ -22,34 +24,33 @@ public static ArrayList<String> encodePantryStock(Pantry pantry) {
return pantryStockInString;
}

//@@NaychiMin
/**
* Encodes a Sales object into a list of strings for storage.
* Each string represents an order, including day, dish name, quantity, and total cost.
*
* @param sales The Sales object to be encoded.
* @return An ArrayList of strings representing the encoded sales data.
*/
public static ArrayList<String> encodeSales(Sales sales) {
return null;
}

//@@author DextheChik3n
public static ArrayList<String> encodeMenu(Menu menu) {
/*
ArrayList<Dish> dishArrayList = menu.getMenuItemsList();
String tasksFilePathString = this.fileManager.openTextFile();
if (dishArrayList.isEmpty()) {
this.fileManager.overwriteFile(tasksFilePathString, ""); //overwrite text file to store empty text
}
//todo: remove testing code
dishArrayList.add(new Dish("test", (float) 1.2));
dishArrayList.add(new Dish("test", (float) 1.2));
//input arraylist data into text file
for (int i = 0; i < dishArrayList.size(); i++) {
String taskDataRow = "chicken rice | 5.00 | rice 50g";
if (i == 0) {
this.fileManager.overwriteFile(tasksFilePathString, taskDataRow);
} else {
this.fileManager.appendToFile(tasksFilePathString, taskDataRow);
ArrayList<String> encodedList = new ArrayList<>();
ArrayList<OrderList> orderLists = sales.getOrderLists();

for (int day = 0; day < orderLists.size(); day++) {
//get orderList for each day from list of sales
OrderList orderList = sales.getOrderList(day);
//get order from each orderList obtained
for (Order order : orderList.getOrderList()) {
StringBuilder orderString = new StringBuilder();
//day of each orderList is index + 1
orderString.append((day + 1) + DIVIDER);
orderString.append(order.getDishName() + DIVIDER);
orderString.append(order.getQuantity() + DIVIDER);
orderString.append(order.totalOrderCost());
encodedList.add(String.valueOf(orderString));
}
}
*/
return null;
return encodedList;
}
//@@author
}
52 changes: 25 additions & 27 deletions src/main/java/seedu/cafectrl/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,42 @@ public Pantry loadPantryStock() {
//return new Pantry(ui);
}

//@@NaychiMin
/**
* Read and decode order list data from text file and pass it to the menu
* @return orderList with data from the file
* Loads order lists from a text file, decodes it, and returns it as a Sales object.
*
* @return An OrderList object containing data from the file.
* @throws IOException if the file is not found in the specified file path.
*/
public Sales loadSales() {
// ArrayList<String> encodedOrderList = this.fileManager.readTextFile(FilePath.ORDERS_FILE_PATH);
// return Decoder.decodeOrderListData(encodedOrderList);
return new Sales();
public Sales loadOrderList(Menu menu) throws IOException {
fileManager.openTextFile(FilePath.ORDERS_FILE_PATH);
ArrayList<String> encodedOrderList = fileManager.readTextFile(FilePath.ORDERS_FILE_PATH);
return Decoder.decodeSales(encodedOrderList, menu);
}

/**
* Encodes the provided OrderList data from Sales object and writes it to a text file
*
* @param sales The Sales object containing the order to be saved to the file.
* @throws IOException if the file is not found in the specified file path.
*/
private void saveOrderList(Sales sales) throws IOException {
this.fileManager.overwriteFile(FilePath.ORDERS_FILE_PATH, Encoder.encodeSales(sales));
}
//@@author

//@@author ziyi105
/**
* Encode and write the data from menu, orderList and pantry to the respective text files
* @param menu menu from current session
* @param sales sales from current session
* @param sales sale object from current session
* @param pantry pantry from current session
* @throws IOException if the file is not found in the specified file path
*/
public void saveAll(Menu menu, Sales sales, Pantry pantry) throws IOException {
// to be uncommented when the following features are implemented
//saveMenu(menu);
//saveSales(sales);
saveMenu(menu);
saveOrderList(sales);
saveMenu(menu);
savePantryStock(pantry);
}

Expand All @@ -78,22 +92,6 @@ private void savePantryStock(Pantry pantry) throws IOException {
this.fileManager.overwriteFile(FilePath.PANTRY_STOCK_FILE_PATH, Encoder.encodePantryStock(pantry));
}

/**
* Encode and write the data from orderList to the text file
* @param sales
* @throws IOException if the file is not found in the specified file path
*/
private void saveSales(Sales sales) throws IOException {
this.fileManager.overwriteFile(FilePath.ORDERS_FILE_PATH, Encoder.encodeSales(sales));
}

/**
* Encode and write the data from menu to the text file
* @param menu menu from current session
* @throws IOException if the file is not found in the specified file path
*/
private void saveMenu(Menu menu) throws IOException {
this.fileManager.overwriteFile(FilePath.MENU_FILE_PATH, Encoder.encodeMenu(menu));
}
public void saveMenu(Menu menu) throws IOException {}

}

0 comments on commit 132908e

Please sign in to comment.