Exercise: Warehouse Stock Sync

Build a utility method to read, deserialize, update, serialize, and write JSON inventory data using asynchronous file operations.

Problem statement

A warehouse manages its inventory using a JSON configuration file. When a new shipment arrives, the automated system must use a utility method to read the current stock from the disk, increase the item's quantity, and save the updated inventory back to the disk in a clean, human-readable format.

Task requirements

  • In InventoryManager.cs, implement the AddStockAsync method.

  • Read the contents of the existing JSON file at filePath.

  • Deserialize the JSON string into an InventoryItem object.

  • Increase the StockQuantity of the retrieved item by the quantityToAdd parameter.

  • Serialize the updated object back into a JSON string, formatted with indentation (pretty-printing).

  • Overwrite the original file with the newly formatted JSON string.

  • Print “Inventory updated successfully.” to the console.

  • In Program.cs, read the initial JSON file using File.ReadAllTextAsync and print it to the console.

  • In Program.cs, call the utility method to add 50 units.

  • In Program.cs, read the updated JSON file using File.ReadAllTextAsync and print it to the console to verify the change.

Constraints

  • Use File.ReadAllTextAsync and File.WriteAllTextAsync to perform non-blocking file I/O operations.

  • Use JsonSerializer.Deserialize<T> and JsonSerializer.Serialize from the System.Text.Json namespace.

  • Create a JsonSerializerOptions object and set WriteIndented = true to format the output.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Make sure to use the generic version of the deserializer: JsonSerializer.Deserialize<InventoryItem>(jsonString).

  • Because Deserialize can technically return null, you can use the null-conditional operator or a standard if check before adding quantityToAdd to the stock quantity.

  • Pass your configured JsonSerializerOptions instance as the second argument to JsonSerializer.Serialize() to ensure the output is indented.

  • Don't forget to await your utility method call and your file read operations in Program.cs.

Exercise: Warehouse Stock Sync

Build a utility method to read, deserialize, update, serialize, and write JSON inventory data using asynchronous file operations.

Problem statement

A warehouse manages its inventory using a JSON configuration file. When a new shipment arrives, the automated system must use a utility method to read the current stock from the disk, increase the item's quantity, and save the updated inventory back to the disk in a clean, human-readable format.

Task requirements

  • In InventoryManager.cs, implement the AddStockAsync method.

  • Read the contents of the existing JSON file at filePath.

  • Deserialize the JSON string into an InventoryItem object.

  • Increase the StockQuantity of the retrieved item by the quantityToAdd parameter.

  • Serialize the updated object back into a JSON string, formatted with indentation (pretty-printing).

  • Overwrite the original file with the newly formatted JSON string.

  • Print “Inventory updated successfully.” to the console.

  • In Program.cs, read the initial JSON file using File.ReadAllTextAsync and print it to the console.

  • In Program.cs, call the utility method to add 50 units.

  • In Program.cs, read the updated JSON file using File.ReadAllTextAsync and print it to the console to verify the change.

Constraints

  • Use File.ReadAllTextAsync and File.WriteAllTextAsync to perform non-blocking file I/O operations.

  • Use JsonSerializer.Deserialize<T> and JsonSerializer.Serialize from the System.Text.Json namespace.

  • Create a JsonSerializerOptions object and set WriteIndented = true to format the output.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Make sure to use the generic version of the deserializer: JsonSerializer.Deserialize<InventoryItem>(jsonString).

  • Because Deserialize can technically return null, you can use the null-conditional operator or a standard if check before adding quantityToAdd to the stock quantity.

  • Pass your configured JsonSerializerOptions instance as the second argument to JsonSerializer.Serialize() to ensure the output is indented.

  • Don't forget to await your utility method call and your file read operations in Program.cs.

C# 14.0
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
namespace CodingExercise;
public static class InventoryManager
{
public static async Task AddStockAsync(string filePath, int quantityToAdd)
{
// Add your code below this line
}
}