Recap - Inventory Update
Part of the Logic & Flow section of Coddy's Dart journey — lesson 32 of 65.
Challenge
EasyCreate a program that manages a warehouse inventory system by tracking product stock levels, checking for existing items, adding new products safely, and updating quantities. Your program should:
- Read a string input representing the warehouse name
- Read multiple pairs of inputs representing product names and their current stock quantities (the input will end when you receive
"inventory_done") - Read a string input representing a product to check for existence
- Read a string input representing a new product to add with default stock
- Read a string input representing a product to restock
- Read a string input representing the additional quantity to add to the restocked product
- Create a Map to store product names as keys and their stock quantities as values
- Use
containsKey()to check if the specified product exists in inventory - Use
putIfAbsent()to add the new product with a default stock of 50 units - Update the stock quantity of the restocked product by adding the additional quantity
- Print the inventory management report in the exact format shown below
For example, if the warehouse name is "Central Warehouse", the inventory includes "Laptops" with 25 units, "Mice" with 150 units, "Keyboards" with 75 units, the product to check is "Laptops", the new product to add is "Monitors", the product to restock is "Mice", and the additional quantity is 30, your program should output:
Warehouse: Central Warehouse
Initial Inventory: {Laptops: 25, Mice: 150, Keyboards: 75}
Inventory Operations:
Checking for 'Laptops': Product found in inventory
Adding 'Monitors': New product added with default stock 50
Restocking 'Mice': Updated quantity from 150 to 180
Final Inventory: {Laptops: 25, Mice: 180, Keyboards: 75, Monitors: 50}
Inventory Summary:
Total products: 4
Total stock units: 330
Low stock items (below 30): 1
Products restocked: 1
Status: Inventory management completedIf the warehouse name is "North Storage", the inventory includes "Tablets" with 40 units, "Headphones" with 80 units, the product to check is "Smartphones", the new product to add is "Tablets", the product to restock is "Headphones", and the additional quantity is 20, your program should output:
Warehouse: North Storage
Initial Inventory: {Tablets: 40, Headphones: 80}
Inventory Operations:
Checking for 'Smartphones': Product not found in inventory
Adding 'Tablets': Product already exists, keeping stock 40
Restocking 'Headphones': Updated quantity from 80 to 100
Final Inventory: {Tablets: 40, Headphones: 100}
Inventory Summary:
Total products: 2
Total stock units: 140
Low stock items (below 30): 0
Products restocked: 1
Status: Inventory management completedIf the warehouse name is "South Depot", the inventory includes "Printers" with 15 units, "Scanners" with 35 units, "Cables" with 200 units, the product to check is "Scanners", the new product to add is "Webcams", the product to restock is "Printers", and the additional quantity is 45, your program should output:
Warehouse: South Depot
Initial Inventory: {Printers: 15, Scanners: 35, Cables: 200}
Inventory Operations:
Checking for 'Scanners': Product found in inventory
Adding 'Webcams': New product added with default stock 50
Restocking 'Printers': Updated quantity from 15 to 60
Final Inventory: {Printers: 60, Scanners: 35, Cables: 200, Webcams: 50}
Inventory Summary:
Total products: 4
Total stock units: 345
Low stock items (below 30): 0
Products restocked: 1
Status: Inventory management completedYour program must use containsKey() to verify if the product exists in the inventory, putIfAbsent() to safely add new products with a default stock of 50 units, and direct map value updates to increase stock quantities. Count low stock items as those with quantities below 30 units. Calculate the total stock units by summing all quantities in the final inventory. The input format will be: warehouse name, then alternating product names and stock quantities (as strings that need to be converted to integers), ending with "inventory_done", followed by the product to check, new product to add, product to restock, and additional quantity.
Try it yourself
import 'dart:io';
void main() {
// Read warehouse name
String? warehouseName = stdin.readLineSync();
// Read inventory items until "inventory_done"
Map<String, int> inventory = {};
while (true) {
String? input = stdin.readLineSync();
if (input == "inventory_done") break;
String productName = input!;
String? quantityStr = stdin.readLineSync();
int quantity = int.parse(quantityStr!);
inventory[productName] = quantity;
}
// Read product to check
String? productToCheck = stdin.readLineSync();
// Read new product to add
String? newProduct = stdin.readLineSync();
// Read product to restock
String? productToRestock = stdin.readLineSync();
// Read additional quantity
String? additionalQuantityStr = stdin.readLineSync();
int additionalQuantity = int.parse(additionalQuantityStr!);
// TODO: Write your code below
// Use containsKey() to check if products exist
// Use putIfAbsent() to add new products with default stock 50
// Update stock quantities and calculate inventory statistics
// Print the inventory management report
print("Warehouse: $warehouseName");
// Add your output statements here
}All lessons in Logic & Flow
1Advanced List Manipulation
List Properties: first & lastList State: isEmpty & isNotEmpReversing a ListAdding to a List: insertList Removal: removeWhereFinding in a List: indexOfSorting a ListShuffling a ListRecap - List Organizer4Advanced Map Manipulation
Iterating Over a MapChecking for Keys and ValuesMap Properties: keys & valuesConditional Add: putIfAbsentRemoving Entries from a MapNested MapsRecap - Inventory Update2Functional List Operations
Transforming with 'map'Filtering with 'where'Using '.toList()'Checking Conditions with 'any'Conditions with 'every'Finding with 'firstWhere'Recap - Data Filtering5Project: Shopping Cart Calc
Project SetupAdding Items to the Cart3Sets
What is a Set?Creating a SetAdding and Removing from SetsChecking for Elements in a SetConverting a List to a SetSet UnionSet IntersectionSet DifferenceRecap - Unique Guest List