Menu
Coddy logo textTech

총 비용 계산하기

Coddy Dart 여정의 로직 & 흐름 섹션에 포함된 레슨 — 65개 중 35번째.

challenge icon

챌린지

쉬움

쇼핑 카트에 담긴 모든 품목의 총 비용을 계산하는 기능을 구현하여 쇼핑 카트 계산기 프로젝트를 이어가는 프로그램을 작성하세요. 이전 레슨의 카트 관리를 바탕으로, 프로그램은 다음을 수행해야 합니다:

  1. 이전 설정에서 사용한 기존 제품 카탈로그 Map과 데이터가 채워진 쇼핑 카트 List를 사용합니다.
  2. 고객이 카트에 추가하려는 품목을 나타내는 여러 문자열 입력을 읽습니다 (입력은 "cart_done"을 받으면 종료됩니다).
  3. 각 품목에 대해 containsKey()를 사용하여 제품 카탈로그에 존재하는지 확인합니다.
  4. 품목이 존재하면 add() 메서드를 사용하여 쇼핑 카트 List에 추가합니다.
  5. 카트 리스트를 반복(iterate)하면서 제품 맵에서 각 품목의 가격을 찾아 총 비용을 계산합니다.
  6. 모든 가격을 합산하여 최종 총 비용을 구합니다.
  7. 아래에 표시된 정확한 형식으로 카트 계산 보고서를 표시합니다.

예를 들어, 상점에 1.50 가격의 "Apple", 0.80 가격의 "Banana", 2.00 가격의 "Orange", 3.25 가격의 "Milk" 제품이 있고, 고객이 "Apple", "Banana", "Apple", "Milk"를 추가하려는 경우 프로그램은 다음과 같이 출력해야 합니다:

Shopping Cart Operations:
Adding 'Apple': Item added to cart successfully
Adding 'Banana': Item added to cart successfully
Adding 'Apple': Item added to cart successfully
Adding 'Milk': Item added to cart successfully
Cart Contents: [Apple, Banana, Apple, Milk]
Cost Calculation:
Apple: $1.50
Banana: $0.80
Apple: $1.50
Milk: $3.25
Total Cost: $7.05
Cart Summary:
Total items in cart: 4
Unique products in cart: 3
Items successfully added: 4
Items not found: 0
Cart Status: Ready for checkout

상점에 999.99 가격의 "Laptop", 25.50 가격의 "Mouse", 75.00 가격의 "Keyboard" 제품이 있고, 고객이 "Mouse", "Keyboard", "Mouse", "Monitor"를 추가하려는 경우 프로그램은 다음과 같이 출력해야 합니다:

Shopping Cart Operations:
Adding 'Mouse': Item added to cart successfully
Adding 'Keyboard': Item added to cart successfully
Adding 'Mouse': Item added to cart successfully
Adding 'Monitor': Item not available in store
Cart Contents: [Mouse, Keyboard, Mouse]
Cost Calculation:
Mouse: $25.50
Keyboard: $75.00
Mouse: $25.50
Total Cost: $126.00
Cart Summary:
Total items in cart: 3
Unique products in cart: 2
Items successfully added: 3
Items not found: 1
Cart Status: Ready for checkout

상점에 12.99 가격의 "Novel", 4.50 가격의 "Magazine", 89.00 가격의 "Textbook" 제품이 있고, 고객이 "Novel", "Magazine", "Novel", "Textbook", "Dictionary"를 추가하려는 경우 프로그램은 다음과 같이 출력해야 합니다:

Shopping Cart Operations:
Adding 'Novel': Item added to cart successfully
Adding 'Magazine': Item added to cart successfully
Adding 'Novel': Item added to cart successfully
Adding 'Textbook': Item added to cart successfully
Adding 'Dictionary': Item not available in store
Cart Contents: [Novel, Magazine, Novel, Textbook]
Cost Calculation:
Novel: $12.99
Magazine: $4.50
Novel: $12.99
Textbook: $89.00
Total Cost: $119.48
Cart Summary:
Total items in cart: 4
Unique products in cart: 3
Items successfully added: 4
Items not found: 1
Cart Status: Ready for checkout

프로그램은 쇼핑 카트 리스트의 각 품목을 반복하면서 제품 카탈로그 맵에서 해당 가격을 찾아 누적 합계에 더해야 합니다. 루프를 사용하여 각 카트 품목을 처리하고 총 비용을 누적하세요. 비용 계산 단계에서 각 품목의 개별 가격을 표시합니다. 모든 가격은 소수점 이하 2자리까지 정확하게 표시되도록 형식을 지정하세요. 입력 형식은 카트에 추가할 품목 이름들이며, "cart_done"으로 끝납니다.

직접 해보기

import 'dart:io';

void main() {
  // Product catalog with items and prices
  Map<String, double> productCatalog = {
    'Apple': 1.50,
    'Banana': 0.80,
    'Orange': 2.00,
    'Milk': 3.25,
    'Laptop': 999.99,
    'Mouse': 25.50,
    'Keyboard': 75.00,
    'Novel': 12.99,
    'Magazine': 4.50,
    'Textbook': 89.00
  };
  
  // Shopping cart to store selected items
  List<String> shoppingCart = [];
  
  // Counters for tracking operations
  int successfulAdditions = 0;
  int itemsNotFound = 0;
  
  print("Shopping Cart Operations:");
  
  // Read items until "cart_done" is received
  String? item;
  while ((item = stdin.readLineSync()) != "cart_done") {
    if (item != null) {
      if (productCatalog.containsKey(item)) {
        shoppingCart.add(item);
        successfulAdditions++;
        print("Adding '$item': Item added to cart successfully");
      } else {
        itemsNotFound++;
        print("Adding '$item': Item not available in store");
      }
    }
  }
  
  // Display cart contents
  print("Cart Contents: $shoppingCart");
  
  // Calculate unique products
  Set<String> uniqueProducts = shoppingCart.toSet();
  
  // Display cart summary
  print("Cart Summary:");
  print("Total items in cart: ${shoppingCart.length}");
  print("Unique products in cart: ${uniqueProducts.length}");
  print("Items successfully added: $successfulAdditions");
  print("Items not found: $itemsNotFound");
  print("Cart Status: Ready for checkout");
}

로직 & 흐름의 모든 레슨