Menu
Coddy logo textTech

최종 영수증 표시하기

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

challenge icon

챌린지

쉬움

모든 거래 내역을 전문적인 형식으로 표시하는 종합적인 최종 영수증을 생성하여 쇼핑카트 계산기 프로젝트를 완성하는 프로그램을 작성하세요. 이전 레슨의 할인 시스템을 기반으로, 프로그램은 다음을 수행해야 합니다:

  1. 이전 설정의 기존 제품 카탈로그 Map과 쇼핑카트 기능을 사용합니다.
  2. 고객이 카트에 추가하려는 품목을 나타내는 여러 문자열 입력을 읽습니다 (입력은 "cart_done"을 받으면 종료됩니다).
  3. 각 품목에 대해 containsKey()를 사용하여 제품 카탈로그에 존재하는지 확인하고 유효한 품목을 카트에 추가합니다.
  4. 카트에 담긴 모든 품목 가격을 합산하여 소계(subtotal)를 계산합니다.
  5. 할인 임계값(threshold amount)을 나타내는 문자열 입력을 읽습니다.
  6. 적용할 할인율(percentage)을 나타내는 문자열 입력을 읽습니다.
  7. 소계가 임계값 이상인 경우에만 할인을 적용합니다.
  8. 할인 금액과 할인이 적용된 최종 합계를 계산합니다.
  9. 고객 이름을 나타내는 문자열 입력을 읽습니다.
  10. 거래 ID(transaction ID)를 나타내는 문자열 입력을 읽습니다.
  11. 아래에 표시된 것과 정확히 일치하는 형식으로 모든 거래 내역이 포함된 전체 영수증을 생성하고 표시합니다.

예를 들어, 상점에 1.50"Apple", 0.80"Banana", 2.00"Orange", 3.25"Milk" 제품이 있고, 고객이 "Apple", "Orange", "Milk", "Apple"을 추가하고 싶어하며, 할인 임계값이 5.00, 할인율이 10, 고객 이름이 "John Smith", 거래 ID가 "TXN001"인 경우 프로그램은 다음과 같이 출력해야 합니다:

Shopping Cart Operations:
Adding 'Apple': Item added to cart successfully
Adding 'Orange': Item added to cart successfully
Adding 'Milk': Item added to cart successfully
Adding 'Apple': Item added to cart successfully
Cart Contents: [Apple, Orange, Milk, Apple]
Cost Calculation:
Apple: $1.50
Orange: $2.00
Milk: $3.25
Apple: $1.50
Subtotal: $8.25
Discount Analysis:
Threshold: $5.00
Discount Rate: 10%
Subtotal ($8.25) meets threshold requirement
Discount Applied: $0.83
Final Total: $7.42
========================================
           FINAL RECEIPT
========================================
Transaction ID: TXN001
Customer: John Smith
Date: 2024-01-15
Time: 14:30:25
----------------------------------------
ITEMS PURCHASED:
Apple                           $1.50
Orange                          $2.00
Milk                            $3.25
Apple                           $1.50
----------------------------------------
Subtotal:                       $8.25
Discount (10%):                -$0.83
----------------------------------------
TOTAL:                          $7.42
----------------------------------------
Payment Method: Cash
Items Count: 4
Unique Products: 3
Thank you for shopping with us!
========================================

상점에 999.99"Laptop", 25.50"Mouse", 75.00"Keyboard" 제품이 있고, 고객이 "Mouse", "Keyboard"를 추가하고 싶어하며, 할인 임계값이 150.00, 할인율이 15, 고객 이름이 "Sarah Johnson", 거래 ID가 "TXN002"인 경우 프로그램은 다음과 같이 출력해야 합니다:

Shopping Cart Operations:
Adding 'Mouse': Item added to cart successfully
Adding 'Keyboard': Item added to cart successfully
Cart Contents: [Mouse, Keyboard]
Cost Calculation:
Mouse: $25.50
Keyboard: $75.00
Subtotal: $100.50
Discount Analysis:
Threshold: $150.00
Discount Rate: 15%
Subtotal ($100.50) does not meet threshold requirement
Discount Applied: $0.00
Final Total: $100.50
========================================
           FINAL RECEIPT
========================================
Transaction ID: TXN002
Customer: Sarah Johnson
Date: 2024-01-15
Time: 14:30:25
----------------------------------------
ITEMS PURCHASED:
Mouse                          $25.50
Keyboard                       $75.00
----------------------------------------
Subtotal:                     $100.50
Discount (15%):                $0.00
----------------------------------------
TOTAL:                        $100.50
----------------------------------------
Payment Method: Cash
Items Count: 2
Unique Products: 2
Thank you for shopping with us!
========================================

상점에 12.99"Novel", 4.50"Magazine", 89.00"Textbook" 제품이 있고, 고객이 "Novel", "Textbook", "Magazine", "Novel"을 추가하고 싶어하며, 할인 임계값이 75.00, 할인율이 20, 고객 이름이 "Mike Davis", 거래 ID가 "TXN003"인 경우 프로그램은 다음과 같이 출력해야 합니다:

Shopping Cart Operations:
Adding 'Novel': Item added to cart successfully
Adding 'Textbook': Item added to cart successfully
Adding 'Magazine': Item added to cart successfully
Adding 'Novel': Item added to cart successfully
Cart Contents: [Novel, Textbook, Magazine, Novel]
Cost Calculation:
Novel: $12.99
Textbook: $89.00
Magazine: $4.50
Novel: $12.99
Subtotal: $119.48
Discount Analysis:
Threshold: $75.00
Discount Rate: 20%
Subtotal ($119.48) meets threshold requirement
Discount Applied: $23.90
Final Total: $95.58
========================================
           FINAL RECEIPT
========================================
Transaction ID: TXN003
Customer: Mike Davis
Date: 2024-01-15
Time: 14:30:25
----------------------------------------
ITEMS PURCHASED:
Novel                          $12.99
Textbook                       $89.00
Magazine                        $4.50
Novel                          $12.99
----------------------------------------
Subtotal:                     $119.48
Discount (20%):               -$23.90
----------------------------------------
TOTAL:                         $95.58
----------------------------------------
Payment Method: Cash
Items Count: 4
Unique Products: 3
Thank you for shopping with us!
========================================

프로그램은 적절한 정렬과 간격을 사용하여 최종 영수증의 형식을 맞춰야 합니다. 문자열 보간법(string interpolation)을 사용하여 고객 정보, 품목별 구매 내역, 가격 계산 및 요약 통계를 포함한 모든 거래 내역을 표시하세요. 영수증에는 날짜(2024-01-15), 시간(14:30:25), 결제 방법(Cash)에 대한 고정된 값이 포함되어야 합니다. 모든 화폐 가치는 소수점 이하 2자리까지 정확하게 표시되도록 형식을 지정하세요. 카트 길이를 사용하여 품목 수(items count)를 계산하고, 카트를 Set으로 변환하여 고유 제품 수(unique products)를 계산하세요. 입력 형식은 "cart_done"으로 끝나는 카트에 추가할 품목 이름들, 이어서 할인 임계값, 할인율, 고객 이름, 거래 ID 순서입니다(모두 문자열로 입력됨).

직접 해보기

import 'dart:io';

void main() {
  // Product catalog
  Map<String, double> products = {
    '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
  };
  
  List<String> cart = [];
  int itemsAdded = 0;
  int itemsNotFound = 0;
  
  print('Shopping Cart Operations:');
  
  // Read items until "cart_done"
  while (true) {
    String? item = stdin.readLineSync();
    if (item == 'cart_done') break;
    
    if (products.containsKey(item)) {
      cart.add(item!);
      print("Adding '$item': Item added to cart successfully");
      itemsAdded++;
    } else {
      print("Adding '$item': Item not found in catalog");
      itemsNotFound++;
    }
  }
  
  print('Cart Contents: $cart');
  
  // Calculate subtotal
  print('Cost Calculation:');
  double subtotal = 0.0;
  for (String item in cart) {
    double price = products[item]!;
    print('$item: \$${price.toStringAsFixed(2)}');
    subtotal += price;
  }
  print('Subtotal: \$${subtotal.toStringAsFixed(2)}');
  
  // Read discount threshold and percentage
  String? thresholdInput = stdin.readLineSync();
  String? discountPercentageInput = stdin.readLineSync();
  
  double threshold = double.parse(thresholdInput!);
  int discountPercentage = int.parse(discountPercentageInput!);
  
  // Discount analysis
  print('Discount Analysis:');
  print('Threshold: \$${threshold.toStringAsFixed(2)}');
  print('Discount Rate: $discountPercentage%');
  
  double discountAmount = 0.0;
  double finalTotal = subtotal;
  String discountStatus = 'No discount applied';
  
  if (subtotal >= threshold) {
    print('Subtotal (\$${subtotal.toStringAsFixed(2)}) meets threshold requirement');
    discountAmount = subtotal * (discountPercentage / 100);
    finalTotal = subtotal - discountAmount;
    discountStatus = '$discountPercentage% discount applied';
  } else {
    print('Subtotal (\$${subtotal.toStringAsFixed(2)}) does not meet threshold requirement');
  }
  
  print('Discount Applied: \$${discountAmount.toStringAsFixed(2)}');
  print('Final Total: \$${finalTotal.toStringAsFixed(2)}');
  
  // Cart summary
  Set<String> uniqueProducts = cart.toSet();
  print('Cart Summary:');
  print('Total items in cart: ${cart.length}');
  print('Unique products in cart: ${uniqueProducts.length}');
  print('Items successfully added: $itemsAdded');
  print('Items not found: $itemsNotFound');
  print('Discount Status: $discountStatus');
  print('Cart Status: Ready for checkout');
}

로직 & 흐름의 모든 레슨