Menu
Coddy logo textTech

最終レシートの表示

CoddyのDartジャーニー「論理とフロー」セクションの一部 — レッスン 37/65。

challenge icon

チャレンジ

簡単

すべての取引詳細をプロフェッショナルな形式で表示する包括的な最終レシートを生成することで、ショッピングカート計算機プロジェクトを完成させるプログラムを作成してください。前回のレッスンの割引システムを基に、プログラムは以下のことを行う必要があります:

  1. 前回のセットアップで作成した既存の製品カタログ Map とショッピングカート機能を使用する
  2. 顧客がカートに追加したいアイテムを表す複数の文字列入力を読み取る(入力は "cart_done" を受け取ったときに終了します)
  3. 各アイテムについて、containsKey() を使用して製品カタログに存在するかどうかを確認し、有効なアイテムをカートに追加する
  4. カート内のすべてのアイテムの価格を合計して小計を計算する
  5. 割引のしきい値額を表す文字列入力を読み取る
  6. 適用する割引率を表す文字列入力を読み取る
  7. 小計がしきい値額以上の場合にのみ割引を適用する
  8. 割引額と割引適用後の最終合計金額を計算する
  9. 顧客名を表す文字列入力を読み取る
  10. 取引IDを表す文字列入力を読み取る
  11. 以下に示す正確な形式で、すべての取引詳細を含む完全なレシートを生成して表示する

例えば、店に "Apple"1.50"Banana"0.80"Orange"2.00"Milk"3.25 の製品があり、顧客が "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!
========================================

店に "Laptop"999.99"Mouse"25.50"Keyboard"75.00 の製品があり、顧客が "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!
========================================

店に "Novel"12.99"Magazine"4.50"Textbook"89.00 の製品があり、顧客が "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!
========================================

プログラムは、適切な配置と間隔で最終レシートをフォーマットする必要があります。文字列補間を使用して、顧客情報、項目別の購入内容、価格計算、要約統計を含むすべての取引詳細を表示してください。レシートには、日付(2024-01-15)、時刻(14:30:25)、支払い方法(Cash)の固定値を含める必要があります。すべての通貨値は、小数点以下ちょうど2桁を表示するようにフォーマットしてください。アイテム数はカートの長さ(length)を使用して計算し、ユニークな製品数はカートを Set に変換して計算します。入力形式は、カートに追加するアイテム名("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');
}

論理とフローのすべてのレッスン