Exemplo n.º 1
0
        /// <summary>
        /// Reads in the sales report (if one currently exists) and saves each object into a list of SalesReportProducts for manipulation. If file doesn't exist then it will use the provided currStockedProducts;
        /// </summary>
        /// <param name="currStockedProducts">The products currently stocked.</param>
        public static void ReadIn(List <Product> currStockedProducts)
        {
            string currentDirectory = Environment.CurrentDirectory;
            string fileName         = "salesReport.txt";
            string fullPath         = Path.Combine(currentDirectory, fileName);

            if (File.Exists(fullPath))
            {
                try
                {
                    using (StreamReader sr = new StreamReader(fullPath))
                    {
                        while (!sr.EndOfStream)
                        {
                            string line = sr.ReadLine();
                            if (line == "")
                            {
                                break;
                            }
                            string[] splitStr = line.Split("|", StringSplitOptions.RemoveEmptyEntries);

                            if (GeneralAssistance.CheckIfIntNumber(splitStr[1]))
                            {
                                foreach (Product item in currStockedProducts)
                                {
                                    if (item.Name == splitStr[0])
                                    {
                                        products.Add(new SalesReportProduct(splitStr[0], int.Parse(splitStr[1]), item.Price));
                                    }
                                }
                            }
                        }
                    }
                }
                catch (IOException e)
                {
                    AuditLog.Log("ERROR: Ran into a IOException.\t" + e.Message);
                }
            }
            else
            {
                foreach (Product item in currStockedProducts)
                {
                    products.Add(new SalesReportProduct(item.Name, 0, item.Price));
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// The select product menu. This is a one pass menu.
        /// </summary>
        /// <returns>Returns the product purchased. Null if not successful.</returns>
        public Product SelectProductMenu()
        {
            Console.Write("\n\nEnter your selection (ex. \"A1\"): ");

            string loc = Console.ReadLine().ToUpper();

            while (loc == "")
            {
                loc = Console.ReadLine();
            }

            if (loc.Length >= 2)
            {
                char row = loc[0];

                string columnStr = loc.Substring(1);
                int    column    = -1;
                if (GeneralAssistance.CheckIfIntNumber(columnStr))
                {
                    column = int.Parse(columnStr);
                    if (column <= 0)
                    {
                        Console.WriteLine("Invalid slot.");
                        return(null);
                    }
                    else
                    {
                        return(inventory.TakeOne(row, column, transObj));
                    }
                }
                else
                {
                    Console.WriteLine("Invalid slot.");
                    return(null);
                }
            }

            Console.WriteLine("Invalid slot.");
            return(null);
        }
Exemplo n.º 3
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!");
            }
        }