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

حساب التكلفة الإجمالية

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

challenge icon

التحدي

سهل

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

  1. استخدام خريطة (Map) كتالوج المنتجات الحالية وقائمة (List) عربة التسوق المملوءة من الإعداد السابق
  2. قراءة مدخلات نصية متعددة تمثل العناصر التي يريد العملاء إضافتها إلى عربتهم (ستنتهي المدخلات عندما تتلقى "cart_done")
  3. لكل عنصر، تحقق مما إذا كان موجوداً في كتالوج المنتجات باستخدام containsKey()
  4. إذا كان العنصر موجوداً، أضفه إلى قائمة عربة التسوق باستخدام طريقة add()
  5. احسب التكلفة الإجمالية من خلال المرور عبر قائمة العربة والبحث عن سعر كل عنصر في خريطة المنتجات
  6. اجمع كل الأسعار للحصول على التكلفة الإجمالية النهائية
  7. اعرض تقرير حساب العربة بالتنسيق الدقيق الموضح أدناه

على سبيل المثال، إذا كان المتجر يحتوي على منتجات "Apple" بسعر 1.50، و "Banana" بسعر 0.80، و "Orange" بسعر 2.00، و "Milk" بسعر 3.25، وأراد العملاء إضافة "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

إذا كان المتجر يحتوي على منتجات "Laptop" بسعر 999.99، و "Mouse" بسعر 25.50، و "Keyboard" بسعر 75.00، وأراد العملاء إضافة "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

إذا كان المتجر يحتوي على منتجات "Novel" بسعر 12.99، و "Magazine" بسعر 4.50، و "Textbook" بسعر 89.00، وأراد العملاء إضافة "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

يجب أن يقوم برنامجك بالمرور عبر كل عنصر في قائمة عربة التسوق، والبحث عن سعره في خريطة كتالوج المنتجات، وإضافة هذا السعر إلى المجموع الجاري. استخدم حلقة تكرار (loop) لمعالجة كل عنصر في العربة وتجميع التكلفة الإجمالية. اعرض السعر الفردي لكل عنصر خلال مرحلة حساب التكلفة. قم بتنسيق جميع الأسعار لتظهر بمنزلتين عشريتين بالضبط. سيكون تنسيق الإدخال هو: أسماء العناصر المراد إضافتها إلى العربة، وتنتهي بـ "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");
}

جميع دروس Logic & Flow