복습 - 인벤토리 업데이트
Coddy Dart 여정의 로직 & 흐름 섹션에 포함된 레슨 — 65개 중 32번째.
챌린지
쉬움제품 재고 수준을 추적하고, 기존 품목을 확인하며, 새 제품을 안전하게 추가하고, 수량을 업데이트하여 창고 재고 시스템을 관리하는 프로그램을 작성하세요. 프로그램의 요구 사항은 다음과 같습니다:
- 창고 이름을 나타내는 문자열 입력을 읽습니다.
- 제품 이름과 현재 재고 수량을 나타내는 여러 쌍의 입력을 읽습니다 (입력은
"inventory_done"을 받을 때 종료됩니다). - 존재 여부를 확인할 제품을 나타내는 문자열 입력을 읽습니다.
- 기본 재고와 함께 추가할 새 제품을 나타내는 문자열 입력을 읽습니다.
- 재입고할 제품을 나타내는 문자열 입력을 읽습니다.
- 재입고할 제품에 추가할 수량을 나타내는 문자열 입력을 읽습니다.
- 제품 이름을 키(key)로, 재고 수량을 값(value)으로 저장하는 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()를 사용하세요
// 기본 재고 50으로 새 제품을 추가하려면 putIfAbsent()를 사용하세요
// 재고 수량을 업데이트하고 재고 통계를 계산하세요
// 재고 관리 보고서 출력
print("Warehouse: $warehouseName");
// 여기에 출력 문을 추가하세요
}로직 & 흐름의 모든 레슨
1고급 리스트 조작
리스트 속성: first 및 last리스트 상태: isEmpty 및 isNotEmpty리스트 뒤집기리스트에 추가: insert리스트 요소 제거: removeWhere리스트에서 찾기: indexOf리스트 정렬리스트 섞기요약 - 리스트 정리 도구4고급 Map 조작
Map 순회하기키와 값 확인하기Map 속성: keys 및 values조건부 추가: putIfAbsentMap에서 항목 삭제하기중첩된 Map복습 - 인벤토리 업데이트