Menu
Coddy logo textTech
flag Ar iconالعربيةdown icon

عرض الإيصال النهائي

جزء من قسم Logic & Flow في رحلة Dart على Coddy — الدرس 37 من 65.

challenge icon

التحدي

سهل

قم بإنشاء برنامج يكمل مشروع حاسبة عربة التسوق من خلال إنشاء إيصال نهائي شامل يعرض جميع تفاصيل المعاملة بتنسيق احترافي. بناءً على نظام الخصم في الدرس السابق، يجب على برنامجك القيام بما يلي:

  1. استخدام خريطة (Map) كتالوج المنتجات الحالية ووظيفة عربة التسوق من الإعداد السابق
  2. قراءة مدخلات نصية متعددة تمثل العناصر التي يريد العملاء إضافتها إلى عربة التسوق الخاصة بهم (سينتهي الإدخال عندما تتلقى "cart_done")
  3. لكل عنصر، تحقق مما إذا كان موجوداً في كتالوج المنتجات باستخدام containsKey() وأضف العناصر الصالحة إلى العربة
  4. حساب المجموع الفرعي (subtotal) عن طريق جمع أسعار جميع العناصر في العربة
  5. قراءة مدخل نصي يمثل قيمة حد الخصم (discount threshold)
  6. قراءة مدخل نصي يمثل نسبة الخصم المئوية المراد تطبيقها
  7. تطبيق الخصم فقط إذا كان المجموع الفرعي يساوي أو يتجاوز قيمة الحد
  8. حساب قيمة الخصم والإجمالي النهائي بعد تطبيق الخصم
  9. قراءة مدخل نصي يمثل اسم العميل
  10. قراءة مدخل نصي يمثل معرف المعاملة (transaction ID)
  11. إنشاء وعرض إيصال كامل مع جميع تفاصيل المعاملة بالتنسيق الدقيق الموضح أدناه

على سبيل المثال، إذا كان المتجر يحتوي على منتجات "Apple" بسعر 1.50، و "Banana" بسعر 0.80، و "Orange" بسعر 2.00، و "Milk" بسعر 3.25، وأراد العملاء إضافة "Apple"، و "Orange"، و "Milk"، و "Apple"، وكان حد الخصم هو 5.00، ونسبة الخصم هي 10، واسم العميل هو "John Smith"، ومعرف المعاملة هو "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"، ومعرف المعاملة هو "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"، ومعرف المعاملة هو "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). قم بتنسيق جميع القيم النقدية لتظهر منزلتين عشريتين بالضبط. احسب عدد العناصر باستخدام طول العربة والمنتجات الفريدة عن طريق تحويل العربة إلى مجموعة (Set). سيكون تنسيق الإدخال هو: أسماء العناصر المراد إضافتها إلى العربة تنتهي بـ "cart_done"، متبوعة بمبلغ حد الخصم، ونسبة الخصم، واسم العميل، ومعرف المعاملة (جميعها كنصوص).

جرّب بنفسك

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');
}

جميع دروس Logic & Flow