Menu
Coddy logo textTech

Recap - Inventory Update

Part of the Logic & Flow section of Coddy's Dart journey — lesson 32 of 65.

challenge icon

Challenge

Easy

Create 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:

  1. Read a string input representing the warehouse name
  2. Read multiple pairs of inputs representing product names and their current stock quantities (the input will end when you receive "inventory_done")
  3. Read a string input representing a product to check for existence
  4. Read a string input representing a new product to add with default stock
  5. Read a string input representing a product to restock
  6. Read a string input representing the additional quantity to add to the restocked product
  7. Create a Map to store product names as keys and their stock quantities as values
  8. Use containsKey() to check if the specified product exists in inventory
  9. Use putIfAbsent() to add the new product with a default stock of 50 units
  10. Update the stock quantity of the restocked product by adding the additional quantity
  11. 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 completed

If 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 completed

If 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 completed

Your 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