Untitled

 avatar
unknown
plain_text
25 days ago
4.4 kB
2
Indexable
using System;
using System.Collections.Generic;

namespace ShoppingCartApp
{
    class Product
    {
        public string Name { get; set; }
        public decimal Price { get; set; }
        public int Quantity { get; set; }

        public Product(string name, decimal price, int quantity)
        {
            Name = name;
            Price = price;
            Quantity = quantity;
        }
    }

    class ShoppingCart
    {
        private List<Product> products = new List<Product>();
        private const int QuantityLimit = 10;

        public event EventHandler LimitExceeded;

        public void AddToCart(Product product)
        {
            products.Add(product);
            Console.WriteLine($"{product.Name} daemata kalatahsi.");

            if (GetTotalQuantity() > QuantityLimit)
            {
                OnLimitExceeded();
            }
        }

        public decimal GetTotalCost()
        {
            decimal totalCost = 0;
            foreach (var product in products)
            {
                totalCost += product.Price * product.Quantity;
            }
            return totalCost;
        }

        public int GetTotalQuantity()
        {
            int totalQuantity = 0;
            foreach (var product in products)
            {
                totalQuantity += product.Quantity;
            }
            return totalQuantity;
        }

        protected virtual void OnLimitExceeded()
        {
            LimitExceeded?.Invoke(this, EventArgs.Empty);
        }

        public void Checkout()
        {
            if (GetTotalQuantity() > QuantityLimit)
            {
                Console.WriteLine("ver gaagrdzelebt sanam ar daasrulebt proces.");
            }
            else
            {
                products.Clear();
                Console.WriteLine("shekveta dadasturebuilia!");
            }
        }

        public void ShowCartDetails()
        {
            Console.WriteLine("\nkalatis detalebi:");
            foreach (var product in products)
            {
                Console.WriteLine($"{product.Name} - fasi: {product.Price}, raodenoba: {product.Quantity}");
            }
            Console.WriteLine($"sruli fasi: {GetTotalCost()}");
            Console.WriteLine($"sruli raodenoba: {GetTotalQuantity()}\n");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ShoppingCart cart = new ShoppingCart();
            cart.LimitExceeded += Cart_LimitExceeded;

            bool running = true;

            while (running)
            {
                Console.WriteLine("1. daamatet kalatashi");
                Console.WriteLine("2. kalatis detalebis naxva");
                Console.WriteLine("3. shekveta");
                Console.WriteLine("4. gamosvla");
                Console.Write("airchie: ");
                string input = Console.ReadLine();

                switch (input)
                {
                    case "1":
                        Console.Write("Enter product name: ");
                        string name = Console.ReadLine();
                        Console.Write("Enter product price: ");
                        decimal price = decimal.Parse(Console.ReadLine());
                        Console.Write("Enter product quantity: ");
                        int quantity = int.Parse(Console.ReadLine());

                        Product product = new Product(name, price, quantity);
                        cart.AddToCart(product);
                        break;

                    case "2":
                        cart.ShowCartDetails();
                        break;

                    case "3":
                        cart.Checkout();
                        break;

                    case "4":
                        running = false;
                        break;

                    default:
                        Console.WriteLine("Invalid option. Please try again.");
                        break;
                }
            }
        }

        private static void Cart_LimitExceeded(object sender, EventArgs e)
        {
            Console.WriteLine("\nWarning: You cannot proceed until you have completed the order.");
        }
    }
}
Leave a Comment