Пример #1
0
 public Catalogue(IDebugLogger logger)
 {
     debugLogger      = logger;
     cataloguePath    = Directory.GetCurrentDirectory() + @"\catalogues\";
     catalogueChoices = Directory.GetFiles(cataloguePath);
     debugLogger.LogMessage("Found " + catalogueChoices.Length + " catalogue choices.");
     foreach (string s in catalogueChoices)
     {
         debugLogger.LogMessage("Catalogue name " + s);
     }
     GetNewCatalogue(catalogueChoices[0]);
 }
Пример #2
0
        public Dictionary <string, Item> GetNewCatalogue(string catalogueName)
        {
            ItemCatalogue = new Dictionary <string, Item>();

            // Set current catalogue path here so we can resupply items from the chosen catalogue.
            path = catalogueName;

            if (FileExists(path))
            {
                debugLogger.LogMessage("Generating a new catalogue from " + path);
                using (var reader = new StreamReader(path))
                {
                    while (!reader.EndOfStream)
                    {
                        // Read the items.csv file for items
                        var line = reader.ReadLine();
                        if (line == null)
                        {
                            continue;
                        }
                        var values = line.Split(',');

                        // Create item and add it to catalogue
                        KeyValuePair <string, Item> itemKeyValue = GenerateItem(values);

                        // Add key-value pair with ItemID as the key and the item as the value
                        ItemCatalogue.Add(itemKeyValue.Key, itemKeyValue.Value);
                    }
                }
            }
            debugLogger.LogMessage("Catalogue from file '" + path + "' generated with " + ItemCatalogue.Count + " items.");
            return(ItemCatalogue);
        }
Пример #3
0
        private void HandleCatalogueMenu()
        {
            string[] catalogues = machine.GetCatalogueChoices();
            Console.WriteLine("Available catalogues: ");
            string curCatalogue = machine.GetCurrentCataloguePath();

            for (int i = 0; i < catalogues.Length; i++)
            {
                if (curCatalogue.Equals(catalogues[i]))
                {
                    Console.WriteLine(i + 1 + "." + Path.GetFileNameWithoutExtension(catalogues[i]) + "\t[CURRENT]");
                }
                else
                {
                    Console.WriteLine(i + 1 + "." + Path.GetFileNameWithoutExtension(catalogues[i]));
                }
            }

            bool success = false;

            while (!success)
            {
                // Get user input
                Console.WriteLine("\n");
                string choice = Console.ReadLine();

                if (!int.TryParse(choice, out int catalogueNo))
                {
                    Console.WriteLine("ERROR: Please input a proper number!");
                }
                else
                {
                    catalogueNo -= 1;
                    if (catalogueNo < 0 || catalogueNo > catalogues.Length - 1)
                    {
                        Console.WriteLine("ERROR: Selection outside of possible catalogues!");
                    }
                    else
                    {
                        // All good, change catalogue!
                        logger.LogMessage("User switched catalogue from " + machine.GetCurrentCataloguePath() + "to" + catalogues[catalogueNo]);
                        Console.WriteLine("Switching catalogue to " + Path.GetFileNameWithoutExtension(catalogues[catalogueNo]) + "!");
                        machine.ChangeCatalogue(catalogues[catalogueNo]);
                        success = true;
                    }
                }
            }
        }
Пример #4
0
        public void TurnMachineOn()
        {
            FillCatalogue();

            logger.LogMessage("====== Machine turned on " + DateTime.Now + "======");

            // Create and show menu for interaction
            VendingMenu menu = new VendingMenu(this, logger);

            menu.ShowMenu();
        }