Ejemplo n.º 1
0
 public void PurchaseAProductTransaction(VendingMachineItem productPurchased, string productSlot)
 {
     try
     {
         using (StreamWriter sw = new StreamWriter(fullPath, true))
         {
             sw.Write(DateTime.UtcNow);
             sw.Write($"     {productPurchased.ProductName} was purchased from {productSlot} slot for ${productPurchased.ProductPrice}.  There are now {productPurchased.ProductQuantity} remaining.");
             sw.WriteLine();
         }
     }
     catch (IOException e)
     {
         Console.WriteLine("Error writing the file");
         Console.WriteLine(e.Message);
     }
 }
        private Dictionary <string, VendingMachineItem> ReadStockFromFile()
        {
            Dictionary <string, VendingMachineItem> output = new Dictionary <string, VendingMachineItem>();

            using (StreamReader sr = new StreamReader(@"C:\VendFiles\vendingmachine.csv"))
            {
                while (!sr.EndOfStream)
                {
                    string             line           = sr.ReadLine();
                    string[]           splitLine      = line.Split(new char[] { '|' });
                    VendingMachineItem itemBeingAdded = new VendingMachineItem();
                    itemBeingAdded.Slot     = splitLine[0];
                    itemBeingAdded.ItemName = splitLine[1];
                    itemBeingAdded.Price    = decimal.Parse(splitLine[2]);
                    itemBeingAdded.Stock    = 5; //Establishes each item at an initial stock of 5
                    output.Add(itemBeingAdded.Slot, itemBeingAdded);
                }
            }
            return(output);
        }