Example #1
0
        /// <summary>
        /// Re-stocks the machine via the re-stock file.
        /// </summary>
        public void ReStock()
        {
            string currDirectory  = Environment.CurrentDirectory;
            string stockInputFile = "vendingmachine.csv";
            string fullPath       = Path.Combine(currDirectory, stockInputFile);

            if (File.Exists(fullPath))
            {
                try
                {
                    using (StreamReader sr = new StreamReader(fullPath))
                    {
                        Slots = new List <Slot>();

                        while (!sr.EndOfStream)
                        {
                            // Get the line
                            string   line     = sr.ReadLine();
                            string[] splitStr = line.Split("|", StringSplitOptions.RemoveEmptyEntries);

                            if (splitStr.Length == 4)
                            {
                                // Get the row char
                                char row = splitStr[0].ToUpper().Substring(0, 1)[0];

                                // Get the valid column int
                                int column = -1;
                                if (GeneralAssistance.CheckIfIntNumber(splitStr[0].Substring(1)))
                                {
                                    column = int.Parse(splitStr[0].Substring(1));
                                }
                                else
                                {
                                    AuditLog.Log("ERROR: Re-stock file column was not a number!\t" + line);
                                    continue;
                                }

                                bool notValid = false;
                                foreach (Slot slot in Slots)
                                {
                                    if (slot.Row == row && slot.Column == column)
                                    {
                                        AuditLog.Log("ERROR: Re-stock file contains a product with the same location as another!\t" + line);
                                        notValid = true;
                                        break;
                                    }
                                }
                                if (notValid)
                                {
                                    continue;
                                }

                                // Get product name
                                string name = splitStr[1];

                                // Get the valid price decimal
                                decimal price = -1;
                                if (GeneralAssistance.CheckIfFloatingPointNumber(splitStr[2]))
                                {
                                    price = Decimal.Parse(splitStr[2]);
                                }
                                else
                                {
                                    AuditLog.Log("ERROR: Re-stock file price was not a valid floating point number!\t" + line);
                                    continue;
                                }

                                // Determine Product type
                                switch (splitStr[3].ToLower())
                                {
                                case "chip":
                                    Slots.Add(new Slot(row, column, new Chip(name, price)));
                                    Slots[Slots.Count - 1].Add(5);
                                    break;

                                case "candy":
                                    Slots.Add(new Slot(row, column, new Candy(name, price)));
                                    Slots[Slots.Count - 1].Add(5);
                                    break;

                                case "drink":
                                    Slots.Add(new Slot(row, column, new Drink(name, price)));
                                    Slots[Slots.Count - 1].Add(5);
                                    break;

                                case "gum":
                                    Slots.Add(new Slot(row, column, new Gum(name, price)));
                                    Slots[Slots.Count - 1].Add(5);
                                    break;

                                default:
                                    AuditLog.Log("ERROR: Re-stock file Product type was not a valid type!\t" + line);
                                    break;
                                }
                            }
                            else
                            {
                                AuditLog.Log("ERROR: Re-stock file line does not have the right amount of peices!\t" + line);
                            }
                        }
                    }
                }
                catch (IOException e)
                {
                    AuditLog.Log("ERROR: Ran into a IOException.\t" + e.Message);
                }
            }
            else
            {
                AuditLog.Log("ERROR: Re-stock file not found!");
            }
        }