示例#1
0
        private string logPath  = @"C:\Catering\log.txt";            // File path for log file

        public void LoadCateringItems(Catering catering)             // Reads from csv file into cateringitems list
        {
            using (StreamReader reader = new StreamReader(filePath))
            {
                while (!reader.EndOfStream)
                {
                    string   line        = reader.ReadLine();
                    string[] type        = line.Split("|"); // Splits each line from csv into an array on "|"
                    string   priceString = type[2];
                    decimal  price       = decimal.Parse(priceString);

                    CateringItem Item = new CateringItem(type[1], type[3], type[0], price); // Uses array indexes to grab specific values
                    catering.Add(Item);
                }
            }
        }
        private string filePath = @"C:\Catering"; // You will likely need to create this folder on your machine

        /// <summary>
        /// Read from the Catering System file. File is a .csv with a pipe delimiter.
        /// </summary>
        /// <param name="catering"></param>
        public void ReadingCateringInventory(Catering catering)
        {
            using (StreamReader reader = new StreamReader(Path.Combine(filePath, "cateringsystem.csv")))
            {
                while (!reader.EndOfStream)
                {
                    // Reads line from file.
                    string line = reader.ReadLine();

                    // Split line into an array via the pipe symbol.
                    string[] parts = line.Split("|");

                    // Declare parts.
                    string  productCode = parts[0];
                    string  product     = parts[1];
                    decimal price       = decimal.Parse(parts[2]);
                    string  productType = parts[3];

                    // We want the internal symbol for productType to be replaced with something a human can read.
                    switch (productType)
                    {
                    case "B":
                        productType = "Beverage";
                        break;

                    case "E":
                        productType = "Entree";
                        break;

                    case "D":
                        productType = "Dessert";
                        break;

                    case "A":
                        productType = "Appetizer";
                        break;
                    }

                    // Create new instance of CateringItem.
                    CateringItem cateringItem = new CateringItem(productCode, product, price, productType);

                    // Add cateringItem to list within catering.
                    catering.Add(cateringItem);
                }
            }
        }