2014-12-21 279 views
1

該程序旨在讀取.csv文件中的信息;然後使用該文件中的數據創建Product對象,然後將其存儲在列表中。從陣列創建對象列表

我的問題是,我不知道如何傳輸.csv文件中的數據,這些文件將按','拆分並存儲在數組中,並存儲到構造函數對象中。任何幫助將不勝感激。

.csv看起來是這樣的:

What the .csv Looks Like

這裏是我的代碼至今:

class Product 
{ 
    public string ID { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public string Price { get; set; } 
    public string StockAvailable { get; set; } 
    public string WeeklySales { get; set; } 

    // Constructor 
    public Product(string iD, string name, string Desc, string price, string StockAva, string weeklysales) 
    { 
     ID = iD; 
     Name = name; 
     Description = Desc; 
     Price = price; 
     StockAvailable = StockAva; 
     WeeklySales = weeklysales; 
    } 
} 

private static void ReadProductFile() 
{ 
    string productPath = GetDataDirectory("prod"); 

    string[] fileData = File.ReadAllLines(productPath); 

    string[] productDetails = new string[20]; 

    for (int i = 0; i < fileData.Length; i++) 
    { 
     productDetails = fileData[i].Split(','); 

     // I have no idea what do do next! 
    } 
} 
+1

.csv文件包含什麼內容?這是所有的構造函數參數嗎?你能給我們一個例子.csv文件嗎? –

+1

您的產品構造函數有6個字段,但您的產品詳細信息有20個數組?我不明白這一點? – SuncoastOwner

+0

對不起.csv看起來像這樣; http://tinypic.com/view.php?pic=xkrasi&s=8#.VJb_w_9_gJJ – Razis

回答

1
List<Product> myProducts = new List<Product>(); 

... 

for (int i = 0; i < fileData.Length; i++) 
{ 
    productDetails = fileData[i].Split(','); 

    var p = new Product(productDetails[0], 
         productDetails[1], 
         ... 
         productDetails[5])); 
    myProducts.Add(p); 
} 

正如其他人所說,Split(',')不是解析CSV文件中的最可靠的辦法 - 如果什麼產品說明包含一個,?使用適當的C# CSV parser將爲您解決這些問題。

+0

確實。我不認爲這是一個次要問題,順便說一句......它實際上是OP的 主要問題:*「我的問題是,我不知道如何將存儲在數組中的數據傳遞給構造函數對象「* – Heinzi

+0

我認爲Heinzi的回答幾乎是我所看到的。我的知識目前非常基礎。我給的信息是給我們完成的一個問題的一部分。使用Split(',')解析CSV文件的過程與我們目前所做的一樣先進。我們從來沒有涉及到列表,但講師把他們扔進來,讓我們想一點點我猜...並不是說他們看起來與arays等不同。 – Razis

+0

該程序將使用具有以下屬性的Product類 ID ,Name,Description,Price,StockAvailable,WeeklySales 該程序將通過首先閱讀產品信息的功能。然後該程序將根據此文件創建許多Product對象,並將這些產品存儲在產品列表中。 然後,程序將從銷售文件中讀取銷售信息並使用每週銷售數字更新產品列表。 最後,程序會將更新的信息寫入更新的產品詳細信息文件,以確保庫存值發生變化以反映銷售情況。 – Razis

0

這很簡單,但需要你事先知道字段在您的CSV順序文件。一旦你知道了,只需要讀取所有特定的字段並將它們發送到Product類的構造函數(幸好已經接受字段值)。

您應該爲此任務使用CSV閱讀器。這將使解析和讀取單個字段值變得更容易。在.NET類庫中有一個內置的CSV解析器。有關詳細信息和用法,請參閱this SO post

你的函數看起來像下面,如果您使用CSV解析器:

private static List<Product> ReadProductFile() 
{ 
    string productPath = GetDataDirectory("prod"); 

    if(!System.IO.File.Exists(productPath)) return null; 

    var lst = new List<Product>(); 

    TextFieldParser parser = new TextFieldParser(productPath); 
    parser.TextFieldType = FieldType.Delimited; 
    parser.SetDelimiters(","); 

    while (!parser.EndOfData) 
    { 
     string[] fields = parser.ReadFields(); 
     foreach (string field in fields) 
     { 
      try 
      { 
       //assuming that the order of fields in the CSV file is the same as the 
       //order of arguments of Product's constructor. 
       Product p = new Product(field[0], field[1], ...); 
       lst.Add(p); 
      } 
      catch(Exception ee) 
      { 
       //Log error somewhere and continue with the rest of the CSV 
      } 
     } 
    } 

    parser.Close(); 

    return lst; 
}