Search code examples
c#winformsfilefor-loopinventory

How to read file info with inventory data in Windows Forms


I'm currently working on an Inventory app assignment on Windows Forms C# My main Form displays 3 options which are:

  1. Register a product
  2. Purchase a product and
  3. Exit

For options 1 and 3 I already have what I need. However, for option 2, which is where the user should be able to purchase products already registered, I don't know how to "look for the product" in the files where it's saved.

The file stores the products info like this: (showing the name of the product, the quantity, the price how many pieces have been sold, from top to bottom)

Chair

100
10
0

Mouse

95
15
5

Laptop

50
13
4

I did this before in Console Application, but I didn't store info in Files, I did it with arrays and simply used a "for" cycle to find the product I needed and from there I could do the rest...

I was told in class that I needed to read the file line by line until I find the product I need and turn it into a variable? How can I do that in a Forms platform?


Solution

  • I was told in class that I needed to read the file line by line until I find the product I need and turn it into a variable? How can I do that in a Forms platform?

    Assuming the file format is strongly consistent, something like this should work:

        //A class to hold the individual pieces of data
        public class Item
        {
            public string Name = "";
            public int Qty = 0;
            public double Price = 0;
            public int QtySold = 0;
        }
        public Item FindItem(string filename, string itemname)
        {
            //An object of type Item that will hold the specific values
            Item output = new Item();
            //The using block handles automatic disposal of the streamreader
            using(StreamReader sr = new StreamReader(filename))
            {
                //Read the file until the end is reached
                while(!sr.EndOfStream)
                {
                    //Check the string from the file against the item name you're
                    //looking for.
                    string temp = sr.ReadLine().Trim();
                    if(temp == itemname)
                    {
                        //Once we find it, throw away the empty line and start
                        //assigning the data to the output object.
                        sr.ReadLine();
                        output.Name = temp;
                        output.Qty = int.Parse(sr.ReadLine());
                        output.Price = double.Parse(sr.ReadLine());
                        output.QtySold = int.Parse(sr.ReadLine());
                        //Since we found the item we're looking, there's no need
                        //to keep looping
                        break;
                    }
                }
    
            }
            //The file is closed and output will either have real data or an empty
            //name and the rest all 0's
            return output;
        }