I created a List<> of objects using info from productdetails.csv file and I have to update that List<> with info from salesdata.csv which I have currently stored in an array (5 int figures of quantities of sales) but I'm stumped as to how to do it. I added another constructor to the Product Class thinking that maybe I could create a new List<> of objects with the 'WeeklySales' property included... I have unfortunately not been able to find any enlightenment in other similar questions. Thanks in advance for any light you can shed on the subject.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace q6._2
{
class Program
{
static void Main(string[] args)
{
string fileName = GetDataDirectory() + "\\data\\salesdata.csv";
string[] salesDetails = File.ReadAllLines(fileName);
string[] salesCol = new string[2];
for (int i = 1; i < salesDetails.Length; i++)
{
string currentLine = salesDetails[i];
salesCol = currentLine.Split(',');
string salesQuant = salesCol[1];
}
Console.ReadLine();
}
public static string GetDataDirectory()
{
string path = Directory.GetCurrentDirectory();
char[] removeSequence = { 'b', 'i', 'n', '\\', 'D', 'e', 'b', 'u', 'g' };
string newPath = path.TrimEnd(removeSequence);
return newPath;
}
public static List<Product> ReadProductFile()
{
string fileName = GetDataDirectory() + "\\data\\productdetails.csv";
string[] productDetails = File.ReadAllLines(fileName);
string[] lineDetails = new string[5];
List<Product> productObj = new List<Product>();
for (int i = 1; i < productDetails.Length; i++)
{
lineDetails = productDetails[i].Split(',');
Product newProduct = new Product(lineDetails[0], lineDetails[1], lineDetails[2], Convert.ToDecimal(lineDetails[3]), Convert.ToInt32(lineDetails[4]));
productObj.Add(newProduct);
}
return productObj;
}
}
class Product
{
public string ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public int StockAvailable { get; set; }
public int WeeklySales { get; set; }
public Product(string id, string name, string description, decimal price, int stockavailable)
{
ID = id;
Name = name;
Description = description;
Price = price;
StockAvailable = stockavailable;
}
public Product(string id, string name, string description, decimal price, int stockavailable, int weeklysales)
{
ID = id;
Name = name;
Description = description;
Price = price;
StockAvailable = stockavailable;
WeeklySales = weeklysales;
}
}
}
Microsoft.VisualBasic.dlland usingMicrosoft.VisualBasic.FileIO.TextFieldParserso that you don't get messed up by escape chars and other CSV gotchas. Yes... it works fine from C#.