回顾:库存更新
本课属于 Coddy Dart 学习旅程的“逻辑与流程控制”单元。第 32 课,共 65 课。
挑战
简单创建一个程序,通过跟踪产品库存、检查商品是否已存在、安全地添加新产品并更新数量,来管理仓库库存系统。你的程序应当:
- 读取一个表示仓库名称的字符串输入
- 读取多组输入,每组表示产品名称和当前库存数量(收到
"inventory_done"时结束输入) - 读取一个表示要检查是否存在的产品的字符串输入
- 读取一个表示要添加的新产品的字符串输入,新产品使用默认库存
- 读取一个表示要补货的产品的字符串输入
- 读取一个表示要添加到补货产品中的额外数量的字符串输入
- 创建一个 Map,用产品名称作为键,用库存数量作为值
- 使用
containsKey()检查指定产品是否存在于库存中 - 使用
putIfAbsent()添加新产品,并将默认库存设为 50 件 - 将额外数量加到补货产品的库存数量中,以更新库存
- 按照下方显示的确切格式打印库存管理报告
例如,仓库名称为 "Central Warehouse",库存中有 "Laptops",数量为 25 件;有 "Mice",数量为 150 件;有 "Keyboards",数量为 75 件;要检查的产品是 "Laptops",要添加的新产品是 "Monitors",要补货的产品是 "Mice",额外数量为 30,那么你的程序应输出:
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如果仓库名称为 "North Storage",库存中有 "Tablets",数量为 40 件;有 "Headphones",数量为 80 件;要检查的产品是 "Smartphones",要添加的新产品是 "Tablets",要补货的产品是 "Headphones",额外数量为 20,那么你的程序应输出:
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如果仓库名称为 "South Depot",库存中有 "Printers",数量为 15 件;有 "Scanners",数量为 35 件;有 "Cables",数量为 200 件;要检查的产品是 "Scanners",要添加的新产品是 "Webcams",要补货的产品是 "Printers",额外数量为 45,那么你的程序应输出:
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你的程序必须使用 containsKey() 验证产品是否存在于库存中,使用 putIfAbsent() 安全地添加新产品,并将默认库存设为 50 件,还要直接更新 Map 中的值以增加库存数量。库存低于 30 件的产品计为低库存商品。将最终库存中的所有数量相加,计算库存总件数。输入格式为:仓库名称,接着是交替出现的产品名称和库存数量(以字符串形式输入,需要转换为整数),以 "inventory_done" 结束,随后是要检查的产品、要添加的新产品、要补货的产品和额外数量。
亲手试试
import 'dart:io';
void main() {
// 读取仓库名称
String? warehouseName = stdin.readLineSync();
// 读取库存商品,直到输入 "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;
}
// 读取要检查的商品
String? productToCheck = stdin.readLineSync();
// 读取要添加的新商品
String? newProduct = stdin.readLineSync();
// 读取要补货的商品
String? productToRestock = stdin.readLineSync();
// 读取追加数量
String? additionalQuantityStr = stdin.readLineSync();
int additionalQuantity = int.parse(additionalQuantityStr!);
// TODO: 在下方编写你的代码
// 使用 containsKey() 检查商品是否存在
// 使用 putIfAbsent() 添加新商品,默认库存为 50
// 更新库存数量并计算库存统计数据
// 打印库存管理报告
print("Warehouse: $warehouseName");
// 在这里添加你的输出语句
}逻辑与流程控制中的所有课
自己练习: Dart 在线编译器