Skip to content

Commit

Permalink
Merge pull request #197 from DextheChik3n/142-storage-error-handling-…
Browse files Browse the repository at this point in the history
…v2.0

142 storage error handling v2.0
  • Loading branch information
ziyi105 authored Nov 1, 2023
2 parents ab28b14 + 50efd76 commit 4702e0e
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 90 deletions.
23 changes: 9 additions & 14 deletions src/main/java/seedu/cafectrl/CafeCtrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
import seedu.cafectrl.parser.Parser;
import seedu.cafectrl.parser.ParserUtil;
import seedu.cafectrl.storage.Storage;
import seedu.cafectrl.ui.Messages;
import seedu.cafectrl.ui.Ui;

import java.io.IOException;

/**
* CafeCtrl application's entry point.
* Initializes the application and starts the interaction with the user.
Expand All @@ -29,18 +26,15 @@ public class CafeCtrl {
/**
* Private constructor for the CafeCtrl class, used for initializing the user interface and menu list.
*/
private CafeCtrl() throws IOException {

private CafeCtrl() {
this.ui = new Ui();
this.ui.showToUser(Messages.INITIALISE_STORAGE_MESSAGE);
this.storage = new Storage(this.ui);
this.currentDate = new CurrentDate();
this.sales = new Sales();
this.menu = this.storage.loadMenu();
this.pantry = this.storage.loadPantryStock(menu);
this.pantry = this.storage.loadPantryStock();
this.sales = this.storage.loadOrderList(menu);
currentDate = new CurrentDate();
}

private void setup() {
ui.showWelcome();
}

/**
Expand All @@ -49,7 +43,8 @@ private void setup() {
* <p> This method consistently receives user input, parses commands, and executes the respective command
* until the user enters a "bye" command, terminating the application.</p>
*/
private void run() throws IOException {
private void run() {
ui.showWelcome();
ui.printLine();
do {
try {
Expand All @@ -63,12 +58,12 @@ private void run() throws IOException {
ui.printLine();
}
} while (!command.isExit());

this.storage.saveAll(this.menu, this.sales, this.pantry);
}

public static void main(String[] args) throws IOException {
public static void main(String[] args) {
CafeCtrl cafeCtrl = new CafeCtrl();
cafeCtrl.setup();
cafeCtrl.run();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/cafectrl/storage/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static ArrayList<Ingredient> decodeIngredientData(String[] ingredientsSt
}

//@@author ziyi105
public static Pantry decodePantryStockData(ArrayList<String> encodedPantryStock, Menu menu) {
public static Pantry decodePantryStockData(ArrayList<String> encodedPantryStock) {
ArrayList<Ingredient> pantryStock = new ArrayList<>();

if (encodedPantryStock.isEmpty()) {
Expand Down
58 changes: 21 additions & 37 deletions src/main/java/seedu/cafectrl/storage/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;

//@@author DextheChik3n
/**
* Manage everything related to file such as writing, reading, opening and creating file
*/
Expand All @@ -23,66 +23,50 @@ public FileManager(Ui ui) {
this.ui = ui;
}

//@@author DextheChik3n
/**
* Reads the text file from the specified file path and stores each line in an ArrayList.
*
* @return ArrayList that consists of every text line in each element
* @throws FileNotFoundException if text file at the specified file path does not exist
*/
public ArrayList<String> readTextFile(String filePath) throws FileNotFoundException {
openTextFile(filePath);
String userWorkingDirectory = System.getProperty("user.dir");
Path tasksFilePath = Paths.get(userWorkingDirectory, filePath);
File textFile = new File(String.valueOf(tasksFilePath));

if (textFile.length() < 0) {
throw new FileNotFoundException();
}

Path dataFilePath = Paths.get(userWorkingDirectory, filePath);
File textFile = new File(String.valueOf(dataFilePath));
ArrayList<String> textLines = new ArrayList<>();
// todo Dexter: implement proper error handling here
try {
Scanner s = new Scanner(textFile);

while (s.hasNext()){
textLines.add(s.nextLine());
}
Scanner s = new Scanner(textFile);

s.close();
} catch (FileNotFoundException e) {
ui.showToUser(ErrorMessages.DATA_FILE_NOT_FOUND_MESSAGE);
while (s.hasNext()) {
textLines.add(s.nextLine());
}

s.close();

return textLines;
}

//@@author DextheChik3n
/**
* Handles opening and creating (if needed) the text file and folder
* @param filePath the file path that is passed in main
* @return the file path of where the data is stored
* @throws IOException if an I/O error occurred while creating the text file
* Checks if the text file and folder exists in the user's system and creates them (if needed)
* @param filePath the specified path location of the file
*/
public String openTextFile(String filePath) {
public void checkFileExists(String filePath) throws IOException {
String userWorkingDirectory = System.getProperty("user.dir");
Path dataFilePath = Paths.get(userWorkingDirectory, filePath);
Path dataFolderPath = dataFilePath.getParent();
File textFile = new File(String.valueOf(dataFilePath));
File folder = new File(String.valueOf(dataFolderPath));

//Check if data folder exists
if (!Files.exists(dataFolderPath)) {
folder.mkdir();
ui.showToUser(ErrorMessages.DATA_FOLDER_NOT_FOUND_MESSAGE);
ui.showToUser(ErrorMessages.DATA_FOLDER_NOT_FOUND_MESSAGE, System.lineSeparator());
}

//Check if the file at the specified file path exists
if (!Files.exists(dataFilePath)) {
try {
textFile.createNewFile();
} catch (Exception e) {
ui.showToUser(ErrorMessages.DATA_FILE_NOT_FOUND_MESSAGE);
}
textFile.createNewFile();
}

return dataFilePath.toString();
}

/**
Expand All @@ -94,15 +78,14 @@ public String openTextFile(String filePath) {
* @throws IOException If I/O operations are interrupted.
*/
public void overwriteFile(String filePath, ArrayList<String> listOfTextToAdd) throws IOException {
String openFilePath = openTextFile(filePath);
FileWriter fw = new FileWriter(openFilePath);
checkFileExists(filePath);
FileWriter fw = new FileWriter(filePath);
for (String line : listOfTextToAdd) {
fw.write(line);
}
fw.close();
}

//@@author DextheChik3n
/**
* Writes text to the text file at the specified file path.
* Will overwrite all text in text file.
Expand All @@ -112,12 +95,12 @@ public void overwriteFile(String filePath, ArrayList<String> listOfTextToAdd) th
* @throws IOException If I/O operations are interrupted.
*/
public void overwriteFile(String filePath, String textToAdd) throws IOException {
checkFileExists(filePath);
FileWriter fw = new FileWriter(filePath);
fw.write(textToAdd);
fw.close();
}

//@@author DextheChik3n
/**
* Appends text to the text file at the specified file path.
* Will add text to text file.
Expand All @@ -127,6 +110,7 @@ public void overwriteFile(String filePath, String textToAdd) throws IOException
* @throws IOException If I/O operations are interrupted.
*/
public void appendToFile(String filePath, String textToAdd) throws IOException {
checkFileExists(filePath);
FileWriter fw = new FileWriter(filePath, true);
fw.write(textToAdd);
fw.close();
Expand Down
58 changes: 37 additions & 21 deletions src/main/java/seedu/cafectrl/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import seedu.cafectrl.data.Menu;
import seedu.cafectrl.data.Pantry;
import seedu.cafectrl.data.Sales;
import seedu.cafectrl.ui.ErrorMessages;
import seedu.cafectrl.ui.Ui;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

Expand All @@ -14,22 +16,27 @@
*/
public class Storage {
protected FileManager fileManager;
protected Ui ui;

public Storage (Ui ui) {
this.fileManager = new FileManager(ui);
this.ui = ui;
}

//@@author ShaniceTang
/**
* Loads menu data from a text file, decodes it, and returns it as a Menu object.
*
* @return A Menu object containing data from the file.
* @throws IOException if the file is not found in the specified file path.
*/
public Menu loadMenu() throws IOException {
fileManager.openTextFile(FilePath.MENU_FILE_PATH);
ArrayList<String> encodedMenu = fileManager.readTextFile(FilePath.MENU_FILE_PATH);
return Decoder.decodeMenuData(encodedMenu);
public Menu loadMenu() {
try {
ArrayList<String> encodedMenu = fileManager.readTextFile(FilePath.MENU_FILE_PATH);
return Decoder.decodeMenuData(encodedMenu);
} catch (FileNotFoundException e) {
ui.showToUser(ErrorMessages.MENU_FILE_NOT_FOUND_MESSAGE, System.lineSeparator());
return new Menu();
}
}

/**
Expand All @@ -47,10 +54,14 @@ private void saveMenu(Menu menu) throws IOException {
* Read and decode pantryStock data from text file and pass it to the menu
* @return pantryStock with data from the file
*/
public Pantry loadPantryStock(Menu menu) throws IOException {
ArrayList<String> encodedPantryStock = this.fileManager.readTextFile(FilePath.PANTRY_STOCK_FILE_PATH);
return Decoder.decodePantryStockData(encodedPantryStock, menu);
//return new Pantry(ui);
public Pantry loadPantryStock() {
try {
ArrayList<String> encodedPantryStock = this.fileManager.readTextFile(FilePath.PANTRY_STOCK_FILE_PATH);
return Decoder.decodePantryStockData(encodedPantryStock);
} catch (FileNotFoundException e) {
ui.showToUser(ErrorMessages.PANTRY_FILE_NOT_FOUND_MESSAGE, System.lineSeparator());
return new Pantry(ui);
}
}

/**
Expand All @@ -67,12 +78,15 @@ private void savePantryStock(Pantry pantry) throws IOException {
* 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 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);
public Sales loadOrderList(Menu menu) {
try {
ArrayList<String> encodedOrderList = fileManager.readTextFile(FilePath.ORDERS_FILE_PATH);
return Decoder.decodeSales(encodedOrderList, menu);
} catch (FileNotFoundException e) {
ui.showToUser(ErrorMessages.ORDER_LIST_FILE_NOT_FOUND_MESSAGE, System.lineSeparator());
return new Sales();
}
}

/**
Expand All @@ -84,21 +98,23 @@ public Sales loadOrderList(Menu menu) throws IOException {
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 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 {
saveMenu(menu);
saveOrderList(sales);
saveMenu(menu);
savePantryStock(pantry);
public void saveAll(Menu menu, Sales sales, Pantry pantry) {
try {
saveMenu(menu);
saveOrderList(sales);
saveMenu(menu);
savePantryStock(pantry);
} catch (IOException e) {
ui.showToUser(e.getMessage());
}
}

}
8 changes: 6 additions & 2 deletions src/main/java/seedu/cafectrl/ui/ErrorMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ public class ErrorMessages {
public static final String INVALID_ADD_ORDER_FORMAT_MESSAGE = "Error: Incorrect format for the add order command.";
public static final String DATA_FOLDER_NOT_FOUND_MESSAGE = "Data Folder was not found!\nIt's ok... "
+ "a new data folder has been created.";
public static final String DATA_FILE_NOT_FOUND_MESSAGE = "text file was not found!\nIt's ok... "
+ "a new data file has been created.";
public static final String DISH_NOT_FOUND = "I'm sorry, but it appears that dish is so exclusive "
+ "it hasn't even made it to our menu yet!";
public static final String ERROR_IN_PANTRY_STOCK_DATA = "Error in pantry stock data file! Skipping this "
+ "particular ingredient!";
public static final String UNIT_NOT_MATCHING = "Sorry, you have used a different unit for this ingredient!";
public static final String MENU_FILE_NOT_FOUND_MESSAGE = "Menu data was not found!\n"
+ "No worries, new menu has been created";
public static final String PANTRY_FILE_NOT_FOUND_MESSAGE = "Pantry stock data was not found!\n"
+ "No worries, new pantry has been created";
public static final String ORDER_LIST_FILE_NOT_FOUND_MESSAGE = "Order list data was not found!\n"
+ "No worries, new order list has been created";
public static final String INVALID_SHOW_SALE_DAY_FORMAT_MESSAGE = "Error: Incorrect format for the show_sale "
+ "command.\n";
public static final String INVALID_DAY_FORMAT = "Sorry, please enter a valid integer for the 'day' field!";
Expand Down
12 changes: 0 additions & 12 deletions src/test/java/seedu/cafectrl/DukeTest.java

This file was deleted.

19 changes: 16 additions & 3 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
...Downloading data...
Data Folder was not found!
It's ok... a new data folder has been created.
Menu data was not found!
No worries, new menu has been created


Pantry stock data was not found!
No worries, new pantry has been created


Order list data was not found!
No worries, new order list has been created


Hello! Welcome to CafeCTRL!
-----------------------------------------------------
> -----------------------------------------------------
Goodbye <3 Have a great day ahead!
-----------------------------------------------------
Data Folder was not found!
It's ok... a new data folder has been created.


0 comments on commit 4702e0e

Please sign in to comment.