Menu
Coddy logo textTech

Sepete Ürün Ekleme

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

challenge icon

Görev

Kolay

Bir alışveriş sepetine ürün ekleme işlevini gerçekleştiren bir program oluşturun. Programınız, kodun içine yazılmış bir ürün kataloğu Map'i ve boş bir alışveriş sepeti List'i kullanmalı, ardından kullanıcıdan gelen ürün girişlerini işlemelidir.

Aşağıdaki ürün kataloğunu doğrudan kodunuzda tanımlayın:

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

Programınız şunları yapmalıdır:

  1. Yukarıda tanımlanan ürün kataloğu Map'ini ve boş bir 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" ifadesini aldığınızda sona erecektir)
  3. Her ü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. Ürün mevcut değilse, atlayın ve işleme devam edin
  6. Başarılı eklemeleri ve başarısız denemeleri takip edin
  7. Sepet yönetimi raporunu tam olarak aşağıda gösterilen formatta görüntüleyin

Örneğin, müşteriler "Apple", "Bread", "Banana", "Apple", "Juice" eklemek isterse, programınız şu çıktıyı vermelidir:

Shopping Cart Operations:
Adding 'Apple': Item added to cart successfully
Adding 'Bread': Item not available in store
Adding 'Banana': Item added to cart successfully
Adding 'Apple': Item added to cart successfully
Adding 'Juice': Item not available in store
Cart Contents: [Apple, Banana, Apple]
Cart Summary:
Total items in cart: 3
Unique products in cart: 2
Items successfully added: 3
Items not found: 2
Cart Status: Ready for checkout

Eğer müşteriler "Mouse", "Monitor", "Keyboard", "Mouse" eklemek isterse, programınız şu çıktıyı vermelidir:

Shopping Cart Operations:
Adding 'Mouse': Item added to cart successfully
Adding 'Monitor': Item not available in store
Adding 'Keyboard': Item added to cart successfully
Adding 'Mouse': Item added to cart successfully
Cart Contents: [Mouse, Keyboard, Mouse]
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

Eğer müşteriler "Novel", "Dictionary", "Magazine", "Novel", "Comic", "Textbook" eklemek isterse, programınız şu çıktıyı vermelidir:

Shopping Cart Operations:
Adding 'Novel': Item added to cart successfully
Adding 'Dictionary': Item not available in store
Adding 'Magazine': Item added to cart successfully
Adding 'Novel': Item added to cart successfully
Adding 'Comic': Item not available in store
Adding 'Textbook': Item added to cart successfully
Cart Contents: [Novel, Magazine, Novel, Textbook]
Cart Summary:
Total items in cart: 4
Unique products in cart: 3
Items successfully added: 4
Items not found: 2
Cart Status: Ready for checkout

Programınız, her bir ürünü sepete eklemeden önce ürün kataloğunda mevcut olduğunu doğrulamak için containsKey() kullanmalıdır. Geçerli ürünleri alışveriş sepeti List'ine eklemek için add() yöntemini kullanın. Sepetteki toplam ürün sayısını length özelliğini kullanarak sayın ve sepeti bir Set'e dönüştürüp uzunluğunu kontrol ederek benzersiz ürünleri belirleyin. Hem başarılı eklemeleri hem de katalogda bulunmayan ürünleri takip edin. Giriş formatı şu şekilde olacaktır: sepete eklenecek ürün adları, "cart_done" ile biter.

Kendin dene

import 'dart:io';

void main() {
  // Read store name
  String? storeName = stdin.readLineSync();
  
  // Initialize data structures
  Map<String, double> products = {};
  List<String> shoppingCart = [];
  
  // Read products until "products_done"
  while (true) {
    String? input = stdin.readLineSync();
    if (input == "products_done") {
      break;
    }
    String productName = input!;
    String? priceInput = stdin.readLineSync();
    double price = double.parse(priceInput!);
    
    // Add product to map
    products[productName] = price;
  }
  
  // Calculate statistics
  double totalPrice = 0;
  double maxPrice = 0;
  double minPrice = double.infinity;
  String mostExpensiveItem = '';
  String cheapestItem = '';
  
  for (String productName in products.keys) {
    double price = products[productName]!;
    totalPrice += price;
    
    if (price > maxPrice) {
      maxPrice = price;
      mostExpensiveItem = productName;
    }
    
    if (price < minPrice) {
      minPrice = price;
      cheapestItem = productName;
    }
  }
  
  double averagePrice = totalPrice / products.length;
  
  // Print the store setup report
  print('Store: $storeName');
  print('Product Catalog Setup:');
  print('Available Products: $products');
  print('Shopping Cart Initialized: $shoppingCart');
  print('System Status:');
  print('Total products available: ${products.length}');
  print('Cart status: Empty');
  print('Most expensive item: $mostExpensiveItem (\$${maxPrice.toStringAsFixed(2)})');
  print('Cheapest item: $cheapestItem (\$${minPrice.toStringAsFixed(2)})');
  print('Average product price: \$${averagePrice.toStringAsFixed(2)}');
  print('Setup Status: Shopping cart system ready for customers');
}

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