Exercise: Warehouse Stock Sync
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 theAddStockAsyncmethod.Read the contents of the existing JSON file at
filePath.Deserialize the JSON string into an
InventoryItemobject.Increase the
StockQuantityof the retrieved item by thequantityToAddparameter.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 usingFile.ReadAllTextAsyncand print it to the console.In
Program.cs, call the utility method to add 50 units.In
Program.cs, read the updated JSON file usingFile.ReadAllTextAsyncand print it to the console to verify the change.
Constraints
Use
File.ReadAllTextAsyncandFile.WriteAllTextAsyncto perform non-blocking file I/O operations.Use
JsonSerializer.Deserialize<T>andJsonSerializer.Serializefrom theSystem.Text.Jsonnamespace.Create a
JsonSerializerOptionsobject and setWriteIndented = trueto 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
Deserializecan technically return null, you can use the null-conditional operator or a standardifcheck before addingquantityToAddto the stock quantity.Pass your configured
JsonSerializerOptionsinstance as the second argument toJsonSerializer.Serialize()to ensure the output is indented.Don't forget to
awaityour utility method call and your file read operations inProgram.cs.
Exercise: Warehouse Stock Sync
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 theAddStockAsyncmethod.Read the contents of the existing JSON file at
filePath.Deserialize the JSON string into an
InventoryItemobject.Increase the
StockQuantityof the retrieved item by thequantityToAddparameter.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 usingFile.ReadAllTextAsyncand print it to the console.In
Program.cs, call the utility method to add 50 units.In
Program.cs, read the updated JSON file usingFile.ReadAllTextAsyncand print it to the console to verify the change.
Constraints
Use
File.ReadAllTextAsyncandFile.WriteAllTextAsyncto perform non-blocking file I/O operations.Use
JsonSerializer.Deserialize<T>andJsonSerializer.Serializefrom theSystem.Text.Jsonnamespace.Create a
JsonSerializerOptionsobject and setWriteIndented = trueto 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
Deserializecan technically return null, you can use the null-conditional operator or a standardifcheck before addingquantityToAddto the stock quantity.Pass your configured
JsonSerializerOptionsinstance as the second argument toJsonSerializer.Serialize()to ensure the output is indented.Don't forget to
awaityour utility method call and your file read operations inProgram.cs.