using System.Collections.Generic; using UnityEngine; using System; using UnityEngine.InputSystem; [Serializable] public class Basket { //Variables private float currentValue; //Holds basket current value; private Dictionary<int, int> basketItems = new Dictionary<int, int>(); //Holds the current products the customer already has and it's quantity //Just for Debugging purposes [Header("Basket Items")] [SerializeField] private List<BasketItem> items = new List<BasketItem>(); [Serializable] private struct BasketItem { [SerializeField] public int productID; [SerializeField] public int amount; public BasketItem(int productID, int amount) { this.productID = productID; this.amount = amount; } } //Properties public float CurrentValue => currentValue; //Constructor public Basket() { } //Gets the product value private float GetProductValue(int id) => ShopManager.Catalog.ContainsKey(id) == true ? ShopManager.Catalog[id] : 0; //Adds an Item to the basket public void AddItem(int id, int quantity) { // Update or add to dictionary if (basketItems.ContainsKey(id)) { basketItems[id] += quantity; // Note: Changed from = to += to accumulate quantity } else { basketItems[id] = quantity; } // Update debug list bool foundInList = false; for (int i = 0; i < items.Count; i++) { if (items[i].productID == id) { BasketItem item = items[i]; item.amount = basketItems[id]; // Update with current quantity items[i] = item; foundInList = true; break; } } if (!foundInList) { items.Add(new BasketItem(id, basketItems[id])); } float product_total = GetProductValue(id) * quantity; currentValue += product_total; } }