Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm trying to write code that will take the contents of a text file, make changes to only specific lines based on the contents of a list.

E.G. 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.

I'm new to coding, and I'm more than happy to scrap this code if it's not close.

Thank you.

What I have tried:

C#
<pre>
private void btnCheckout_Click(object sender, RoutedEventArgs e)
        {
List<string> replaceList = new List<string>();
            List<string> tempList = new List<string>();
            StreamReader sr = new StreamReader("items.txt");
            while (!sr.EndOfStream)
            {
                tempList.Add(sr.ReadLine());
                string content = sr.ReadLine();
                foreach(string i in cartList)
                {
                    if (i == content)
                    {
                        string[] splitItem = content.Split(',');
                        decimal y = decimal.Parse(splitItem[2]) - 1;
                        string x = (splitItem[0] + ", " + splitItem[1] + ", " + y + ", " + splitItem[3]);
                        replaceList.Add(x);
                    }
                   foreach (string t in tempList)
                    {
                        if (t == i)
                        {
                            tempList.Remove(t);
                            break;
                        }
                    }
                }                
            }
            sr.Close();          

           
            foreach (string x in tempList)
            {
                replaceList.Add(x);
            }

            File.Delete("items.txt");

            StreamWriter sw = new StreamWriter("items.txt");
            foreach (string r in replaceList)
            {
            sw.WriteLine(r);
            }
            sw.Close();
}
Posted
Updated 18-Oct-21 20:32pm
Comments
BillWoodruff 19-Oct-21 3:10am    
What happens when you run your code ? Errors ? Unexpected results ?

Are you the person who creates the data/text file ?

Why aren't you using a database ?

1 solution

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:
C#
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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900