Menu
Coddy logo textTech

Toplam Tutarı Hesaplama

Coddy'nin Dart Journey'sinin Mantık & Akış bölümünün bir parçası — ders 35 / 65.

challenge icon

Görev

Kolay

Alışveriş sepetindeki tüm ürünlerin toplam maliyetini hesaplama işlevini uygulayarak alışveriş sepeti hesaplayıcı projesine devam eden bir program oluşturun. Önceki dersin sepet yönetimini temel alarak programınız şunları yapmalıdır:

  1. Önceki kurulumdaki mevcut ürün kataloğu Map'ini ve doldurulmuş alışveriş sepeti List'ini kullanın
  2. Müşterilerin sepetlerine eklemek istedikleri ürünleri temsil eden birden fazla dize girişi okuyun (giriş "cart_done" aldığınızda sona erecektir)
  3. Her bir ürün için, containsKey() kullanarak ürün kataloğunda mevcut olup olmadığını kontrol edin
  4. Ürün mevcutsa, add() yöntemini kullanarak alışveriş sepeti List'ine ekleyin
  5. Sepet listesinde gezinerek ve ürün haritasında her bir ürünün fiyatını arayarak toplam maliyeti hesaplayın
  6. Nihai toplam maliyeti elde etmek için tüm fiyatları toplayın
  7. Sepet hesaplama raporunu tam olarak aşağıda gösterilen formatta görüntüleyin

Örneğin, mağazada 1.50 fiyatıyla "Apple", 0.80 fiyatıyla "Banana", 2.00 fiyatıyla "Orange", 3.25 fiyatıyla "Milk" ürünleri varsa ve müşteriler "Apple", "Banana", "Apple", "Milk" eklemek isterse, programınız şu çıktıyı vermelidir:

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

Mağazada 999.99 fiyatıyla "Laptop", 25.50 fiyatıyla "Mouse", 75.00 fiyatıyla "Keyboard" ürünleri varsa ve müşteriler "Mouse", "Keyboard", "Mouse", "Monitor" eklemek isterse, programınız şu çıktıyı vermelidir:

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

Mağazada 12.99 fiyatıyla "Novel", 4.50 fiyatıyla "Magazine", 89.00 fiyatıyla "Textbook" ürünleri varsa ve müşteriler "Novel", "Magazine", "Novel", "Textbook", "Dictionary" eklemek isterse, programınız şu çıktıyı vermelidir:

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

Programınız alışveriş sepeti listesindeki her bir öğe üzerinde döngü kurmalı, ürün kataloğu haritasında fiyatını aramalı ve bu fiyatı bir genel toplama eklemelidir. Her sepet öğesini işlemek ve toplam maliyeti biriktirmek için bir döngü kullanın. Maliyet hesaplama aşamasında her bir ürünün bireysel fiyatını görüntüleyin. Tüm fiyatları tam olarak 2 ondalık basamak gösterecek şekilde formatlayın. Giriş formatı şu şekilde olacaktır: sepete eklenecek ürün adları, "cart_done" ile biter.

Kendin dene

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

Mantık & Akış bölümündeki tüm dersler