Home > other >  Adding a product list file into a C# collection
Adding a product list file into a C# collection

Time:03-15

So I'm trying to add a made up product list into my program for an assignment. In my prototype I had the list hard-coded in as below

public static List<Product> Products = new List<Product>()
        {  
            new Product(1001, "Apple", 49, 43, new DateTime(2022, 01, 25)),
            new Product(1002, "Banana", 60, 32, new DateTime(2022, 01, 25)),
            new Product(1003, "Grapes", 99, 15, new DateTime(2022, 01, 28)),
            new Product(1004, "Melon", 110, 14, new DateTime(2022, 02, 01)),
            new Product(1005, "Strawberries", 119, 22, new DateTime(2022, 01, 28)),
            new Product(1006, "Orange", 45, 31, new DateTime(2022, 01, 25)),
            new Product(1007, "Pineapple", 125, 9, new DateTime(2022, 02, 01))
        }

I've now tried to convert this to using files instead. The info I have in the file is as below:

1001,"Apple",49,43,new DataTime(2022, 01, 25)
1002,"Banana",60,32,new DataTime(2022, 01, 25)
1003,"Grapes",99,15,new DataTime(2022, 01, 28)
1004,"Melon",110,14,new DataTime(2022, 02, 01)
1005,"Strawberries",119,22,new DataTime(2022, 01, 28)
1006,"Orange",45,31,new DataTime(2022, 01, 25)
1007,"Pineapple",125,9,new DataTime(2022, 02, 01)

How can I have the program read this products.txt file and create the list of products itself? Apologies I'm fairly new to File.IO.

Many thanks

CodePudding user response:

There might be multiple ways to do this, if you insist on using a text file, for example, I would recommend removing the new DateTime(x,y,z) from the end of the line, and replace it directly with x,y,z, and also remove the quotations around the name, unless needed like this.

Then you can read all the lines of the file, and create a new instance of your object while looping through the lines. Let's assume we saved the file as products.txt, your code will look like this:

public List<Product> GetProductsFromFile()
{
    List<Product> products = new List<Product>();

    string[] allLines = File.ReadAllLines("products.txt");
    foreach (var line in allLines)
    {
        string[] values = line.Split(",");
        products.Add(new Product(int.Parse(values[0]), values[1], int.Parse(values[2]), int.Parse(values[3]), new DateTime(int.Parse(values[4]), int.Parse(values[5]), int.Parse(values[6]))));
    }

    return products;
}

There might be a cleaner way to do that with a text file, but I hope you can see how ugly the code becomes like this. Also note that I haven't taken care of any errors in inputs, so you'll still need more work to finalize this function into something you can really use.

Instead, I would recommend using a JSON file, which can be directly deserialized into your list without the need of parsing it ourselves...

I don't know the Product class you have, but I wrote it like this:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int FirstInteger { get; set; }
    public int SecondInteger { get; set; }
    public DateTime DateOfSomething { get; set; }
}

And the file that you'll use will be like this:

[{
    "Id": 1001,
    "Name": "Apple",
    "FirstInteger": 49,
    "SecondInteger": 43,
    "DateOfSomething": "2022-01-25T00:00:00"
},
{
    "Id": 1002,
    "Name": "Banana",
    "FirstInteger": 60,
    "SecondInteger": 32,
    "DateOfSomething": "2022-01-25T00:00:00"
},
{
    "Id": 1003,
    "Name": "Grapes",
    "FirstInteger": 99,
    "SecondInteger": 15,
    "DateOfSomething": "2022-01-28T00:00:00"
},
{
    "Id": 1004,
    "Name": "Melon",
    "FirstInteger": 110,
    "SecondInteger": 14,
    "DateOfSomething": "2022-02-01T00:00:00"
},
{
    "Id": 1005,
    "Name": "Strawberries",
    "FirstInteger": 119,
    "SecondInteger": 22,
    "DateOfSomething": "2022-01-28T00:00:00"
},
{
    "Id": 1006,
    "Name": "Orange",
    "FirstInteger": 45,
    "SecondInteger": 31,
    "DateOfSomething": "2022-01-25T00:00:00"
},
{
    "Id": 1007,
    "Name": "Pineapple",
    "FirstInteger": 125,
    "SecondInteger": 9,
    "DateOfSomething": "2022-02-01T00:00:00"
}]

Let's assume this file name is jsonFile.json, the way to load it as your list will be as simple as this:

List<Product> productsFromFile = JsonConvert.DeserializeObject<List<Product>>(File.ReadAllText("jsonFile.json"));

CodePudding user response:

You don't want new DataTime or even new DateTime in you input file. Convert to

1001,"Apple",49,43,2022, 01, 25
1002,"Banana",60,32,2022, 01, 25
1003,"Grapes",99,15,2022, 01, 28
1004,"Melon",110,14,2022, 02, 01
1005,"Strawberries",119,22,2022, 01, 28
1006,"Orange",45,31,2022, 01, 25
1007,"Pineapple",125,9,2022, 02, 01

Then use one of the many CVS (Comma-Separated Values) implementations that are available in nuget.org. Your dates will be split into year, month and day, so you need to new up a DateTime after reading.

  • Related