// Helper method to request stock. private void requestStock(Dictionary <string, int> storeStockMap, int restockThreshold, string stockId) { int stockLevel; // Gets the item if (storeStockMap.TryGetValue(stockId, out stockLevel)) { // If the user doesn't want to restock, the method returns. if (checkIfRestock(stockLevel, restockThreshold) == false) { return; } int quantity = Utility.getStockAmount(); // This declaration and the try catch block open the file. Dictionary <string, StockRequest> requests; try { requests = JsonUtil.getStockRequests(); } catch (FileNotFoundException) { // If the file isn't found, this method cannot run. Utility.displayError("A file necessary for that operation could not be found."); return; } catch (JsonReaderException) { // If the Json is corrupted, this method cannot run. Utility.displayError("A file necessary for that operation could not be read."); return; } // This section of code checks whether an id is being used, and if it is, increments it, // until a unused value is reached. int requestId = 1; while (requests.ContainsKey($"{requestId}")) { requestId++; } // Creates a new request, and adds it to the requests file, and saves. StockRequest request = new StockRequest($"{requestId}", franchiseID, stockId, quantity); requests.Add(request.RequestID, request); JsonUtil.saveStockRequests(requests); Console.Clear(); } else { // A valid stock Id was not passed in. Utility.displayError("Error - That is not a valid stock ID"); } }
// Helper method to check if the owner has enough stock. private bool checkOwnerStock(Dictionary <string, Stock> stockMap, StockRequest request) { Stock item; if (stockMap.TryGetValue(request.ProductID, out item)) { if (item.StockLevel >= request.Quantity) { // If the owner has enough stock, true is returned. return(true); } } return(false); }