Quote:
I have a .txt of products with the format: ID, name, quantity, $price
I then have a list (cartList) that contains the items the user has added to the cart.
I want to make it so on buttonClick, the text file is edited so that all products that were in the cartList have 1 less quantity.
So, you have to have a Product class and a list of products, instead of list of strings.
For eaxmple:
class Product
{
private int id = 0;
private string name = string.Empty;
private double price = .0;
public Product(int _id, string _name, double _price)
{
id = _id;
name = _name;
price = _price;
}
public int ID
{
get => id;
set => id = value;
}
public string Name
{
get => name;
set => name = value;
}
public double Price
{
get => price;
set => price = value;
}
}
Then, you have to use that class.
List<Product> products = new List<Product>();
products.Add(new Product(1, "Apple", 5.55));
products.Add(new Product(2, "Orange", 6.11));
products.Add(new Product(3, "Coconut", 7.33));
Console.WriteLine($"Quantity: {products.Count()}");
Console.WriteLine($"Total price: {products.Sum(x=>x.Price)}");
Whenever you add/remove product to the list of products, you can easily calculate a quantity, sum, etc.
To be able to put a list of products to the text file (and then reverse the process), please, read about
Serialization[
^].
Good luck!