コード例 #1
0
        //Sets inventory from provided csv file
        public void GetInventory()
        {
            using (StreamReader sr = new StreamReader("C:\\Users\\Student\\workspace\\week-4-pair-exercises-c-team-6\\19_Capstone\\dotnet\\vendingmachine.csv"))
            {
                while (!sr.EndOfStream)                    //read to the end of the csv
                {
                    string   lines     = sr.ReadLine();    //reading lines of the csv
                    string[] temparray = lines.Split('|'); //splitting to an array

                    if (temparray.Contains("Chip"))
                    {
                        Chips chips = new Chips(temparray[1], decimal.Parse(temparray[2]));
                        InventoryList.Add(temparray[0], chips);
                    }
                    if (temparray.Contains("Drink"))
                    {
                        Drinks drinks = new Drinks(temparray[1], decimal.Parse(temparray[2]));
                        InventoryList.Add(temparray[0], drinks);
                    }
                    if (temparray.Contains("Candy"))
                    {
                        Candy candy = new Candy(temparray[1], decimal.Parse(temparray[2]));
                        InventoryList.Add(temparray[0], candy);
                    }
                    if (temparray.Contains("Gum"))
                    {
                        Gum gum = new Gum(temparray[1], decimal.Parse(temparray[2]));
                        InventoryList.Add(temparray[0], gum);
                    }

                    //adding the array to our list
                }
            }
        }
コード例 #2
0
        public Item Clone()
        {
            Item result = null;

            if (this is Gum)
            {
                result            = new Gum(ProductName, Price);
                result.AmountLeft = AmountLeft;
            }
            else if (this is Drink)
            {
                result            = new Drink(ProductName, Price);
                result.AmountLeft = AmountLeft;
            }
            else if (this is Candy)
            {
                result            = new Candy(ProductName, Price);
                result.AmountLeft = AmountLeft;
            }
            else if (this is Chip)
            {
                result            = new Chip(ProductName, Price);
                result.AmountLeft = AmountLeft;
            }
            return(result);
        }
コード例 #3
0
        /// <summary>
        /// Stocks the vending machine with the items in the 'vendingmachine.csv' file.
        /// </summary>
        public void StockItems()
        {
            //read csv file and split by line
            string        filename = "vendingmachine.csv";
            List <string> allWords = new List <string>();

            using (StreamReader sr = new StreamReader(filename))
            {
                while (!sr.EndOfStream)
                {
                    string   line  = sr.ReadLine();
                    string[] words = line.Split("\n");
                    allWords.AddRange(words);
                }
            }
            //join lines with ',' and then split and create into array
            string newWordStr = string.Join(",", allWords);

            string[] splitByLine = newWordStr.Split(",");

            //loop through and assign array values to variables
            for (int i = 0; i < splitByLine.Length; i++)
            {
                string[] splitByPipe  = splitByLine[i].Split("|");
                string   slot         = splitByPipe[0];
                string   itemName     = splitByPipe[1];
                string   itemPrice    = splitByPipe[2];
                double   itemPriceInt = double.Parse(itemPrice);
                string   itemType     = splitByPipe[3];
                slotQuantities[slot] = 5;

                // make a new VendingItem of the proper type to hold the data
                switch (itemType)
                {
                case "Candy":
                    slotItems[slot] = new Candy(itemName, itemPriceInt);
                    break;

                case "Chip":
                    slotItems[slot] = new Chip(itemName, itemPriceInt);
                    break;

                case "Drink":
                    slotItems[slot] = new Drink(itemName, itemPriceInt);
                    break;

                case "Gum":
                    slotItems[slot] = new Gum(itemName, itemPriceInt);
                    break;

                default:
                    break;
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// stocks a machine with new items on creation
        /// </summary>
        public void StockMachine()
        {
            string directory = Environment.CurrentDirectory;
            string fileName  = @"\..\..\..\etc\vendingmachine.csv";
            string fullPath  = directory + fileName;

            using (StreamReader sr = new StreamReader(fullPath))
            {
                while (!sr.EndOfStream)
                {
                    string   fullLine = sr.ReadLine();
                    string   type     = "";
                    string[] lines    = fullLine.Split('|');
                    type = lines[3];
                    if (type == "Chip")
                    {
                        Chips chip = new Chips();
                        chip.Symbol   = lines[0];
                        chip.Name     = lines[1];
                        chip.Price    = decimal.Parse(lines[2]);
                        chip.Quantity = 5;
                        inventory.Add(chip);
                    }
                    else if (type == "Candy")
                    {
                        Candy candy = new Candy();
                        candy.Symbol   = lines[0];
                        candy.Name     = lines[1];
                        candy.Price    = decimal.Parse(lines[2]);
                        candy.Quantity = 5;
                        inventory.Add(candy);
                    }
                    else if (type == "Drink")
                    {
                        Drinks drink = new Drinks();
                        drink.Symbol   = lines[0];
                        drink.Name     = lines[1];
                        drink.Price    = decimal.Parse(lines[2]);
                        drink.Quantity = 5;
                        inventory.Add(drink);
                    }
                    else if (type == "Gum")
                    {
                        Gum gum = new Gum();
                        gum.Symbol   = lines[0];
                        gum.Name     = lines[1];
                        gum.Price    = decimal.Parse(lines[2]);
                        gum.Quantity = 5;
                        inventory.Add(gum);
                    }
                }
            }
        }
コード例 #5
0
        public VendingMachine(string filePath)
        {
            StoredMoney = 0.00M;
            bool fileExists = File.Exists(filePath);

            if (fileExists)
            {
                try
                {
                    using (StreamReader sr = new StreamReader(filePath))
                    {
                        //the input file fills our AllProducts dictionary
                        while (!sr.EndOfStream)
                        {
                            string line = sr.ReadLine();

                            string[] productProperties = line.Split("|");
                            string   slotLocation      = productProperties[0];
                            string   productName       = productProperties[1];
                            decimal  price             = decimal.Parse(productProperties[2]);
                            Product  currentProduct    = null;

                            if (productProperties[3].ToLower() == "candy")
                            {
                                currentProduct = new Candy(productName, price);
                            }
                            if (productProperties[3].ToLower() == "chip")
                            {
                                currentProduct = new Chip(productName, price);
                            }
                            if (productProperties[3].ToLower() == "drink")
                            {
                                currentProduct = new Drink(productName, price);
                            }
                            if (productProperties[3].ToLower() == "gum")
                            {
                                currentProduct = new Gum(productName, price);
                            }
                            AllProducts.Add(slotLocation, currentProduct);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("There was a problem running reading the file");
                    Console.WriteLine(e.Message);
                }
            }
            else
            {
                Console.WriteLine("File does not exist");
            }
        }
コード例 #6
0
        /// <summary>
        /// Reading inventory from vendingmachine.txt
        /// </summary>
        /// <param name="file"></param>
        public void CreateInventory(string file)
        {
            string directory = Environment.CurrentDirectory;
            string fullPath  = Path.Combine(directory, file);

            try
            {
                using (StreamReader sr = new StreamReader(fullPath))
                {
                    while (!sr.EndOfStream)
                    {
                        //Creating "information" array from each line which holds the slot number, name, price, and type
                        //of each item
                        string   line        = sr.ReadLine();
                        string[] information = line.Split("|");
                        string   slot        = information[0];
                        string   name        = information[1];
                        decimal  price       = decimal.Parse(information[2]);
                        string   type        = information[3];


                        if (type == "Chip")
                        {
                            Chip chip = new Chip(name, price);
                            Inventory.Add(information[0], chip);
                        }
                        else if (type == "Drink")
                        {
                            Drink drink = new Drink(name, price);
                            Inventory.Add(information[0], drink);
                        }
                        else if (type == "Candy")
                        {
                            Candy candy = new Candy(name, price);
                            Inventory.Add(information[0], candy);
                        }
                        else if (type == "Gum")
                        {
                            Gum gum = new Gum(name, price);
                            Inventory.Add(information[0], gum);
                        }
                    }
                }
            }

            catch (Exception e)
            {
                Console.WriteLine("Invalid path. Please try again.");
                Console.ReadKey();
            }
        }
コード例 #7
0
        public Inventory()
        {
            string path     = Directory.GetCurrentDirectory();
            string fileName = "vendingmachine.csv";

            try
            {
                using (StreamReader sr = new StreamReader(Path.Combine(path, fileName)))
                {
                    while (!sr.EndOfStream)
                    {
                        string    line    = sr.ReadLine();
                        string[]  words   = line.Split("|");
                        decimal   cost    = decimal.Parse(words[2]);
                        IVendable newItem = null;
                        currentInventory.Add(words[0], 5);
                        if (words[3] == "Chip")
                        {
                            newItem = new Chip(words[1], cost, words[0]);
                        }
                        if (words[3] == "Candy")
                        {
                            newItem = new Candy(words[1], cost, words[0]);
                        }
                        if (words[3] == "Drink")
                        {
                            newItem = new Drink(words[1], cost, words[0]);
                        }
                        if (words[3] == "Gum")
                        {
                            newItem = new Gum(words[1], cost, words[0]);
                        }
                        items.Add(words[0], newItem);
                    }
                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("File not found. Try again.");
            }
            catch (FormatException)
            {
                Console.WriteLine("Incorrect format for cost.");
            }
            catch (Exception)
            {
                Console.WriteLine("Could not load inventory.  Try again.");
            }
        }
コード例 #8
0
        //created a constructor that reads over the text file, and adds each line(producted) to the dictionary
        public VendingMachineDictionary()
        {
            string pathToVendingInventory = Path.GetFullPath(Environment.CurrentDirectory + @"\..\..\..\..");
            string nameOfInventoryFile    = "vendingmachine.csv";
            string fullPathToInventory    = Path.Combine(pathToVendingInventory, nameOfInventoryFile);

            try
            {
                using (StreamReader sr = new StreamReader(fullPathToInventory))
                {
                    // loops over the file
                    while (!sr.EndOfStream)
                    {
                        //stores the current line to a variable
                        string currentLine = sr.ReadLine();
                        // splits on the ("|") and creates an array
                        string[] arrayVendingItem = currentLine.Split("|");
                        //if the 3 index of the array is = to Chip
                        if (arrayVendingItem[3] == "Chip")
                        {
                            //create an instance of the Chip class and named it chip... then stored that item to the dictionary
                            Chip chip = new Chip(arrayVendingItem[1], decimal.Parse(arrayVendingItem[2]), 5);
                            VendingItemList.Add(arrayVendingItem[0], chip);
                        }
                        else if (arrayVendingItem[3] == "Candy")
                        {
                            Candy candy = new Candy(arrayVendingItem[1], decimal.Parse(arrayVendingItem[2]), 5);
                            VendingItemList.Add(arrayVendingItem[0], candy);
                        }
                        else if (arrayVendingItem[3] == "Gum")
                        {
                            Gum gum = new Gum(arrayVendingItem[1], decimal.Parse(arrayVendingItem[2]), 5);
                            VendingItemList.Add(arrayVendingItem[0], gum);
                        }
                        else
                        {
                            Drink drink = new Drink(arrayVendingItem[1], decimal.Parse(arrayVendingItem[2]), 5);
                            VendingItemList.Add(arrayVendingItem[0], drink);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Sorry, your file path was not found. Please try again.");
            }
        }
コード例 #9
0
 public void StartingInventory()
 {
     try
     {
         string inventoryPath = @"..\..\..\..\etc\VendingMachine.txt";
         //reads through entire file one line at a time.
         using (StreamReader sr = new StreamReader(inventoryPath))
         {
             while (!sr.EndOfStream)
             {
                 string line = sr.ReadLine();
                 //separates line by information
                 string[] inventoryDetails = line.Split("|");
                 if (inventoryDetails.Length != 4)
                 {
                     throw new FileException();
                 }
                 Product eachProduct = null;
                 if (inventoryDetails[3] == "Candy")
                 {
                     eachProduct = new Candy(inventoryDetails[1], decimal.Parse(inventoryDetails[2]));
                 }
                 else if (inventoryDetails[3] == "Gum")
                 {
                     eachProduct = new Gum(inventoryDetails[1], decimal.Parse(inventoryDetails[2]));
                 }
                 else if (inventoryDetails[3] == "Chip")
                 {
                     eachProduct = new Chip(inventoryDetails[1], decimal.Parse(inventoryDetails[2]));
                 }
                 else if (inventoryDetails[3] == "Drink")
                 {
                     eachProduct = new Drink(inventoryDetails[1], decimal.Parse(inventoryDetails[2]));
                 }
                 else
                 {
                     throw new FileException();
                 }
                 Inventory.Add(inventoryDetails[0], eachProduct);
             }
         }
     }
     catch (Exception a)
     {
         a.GetType();
     }
 }
コード例 #10
0
 //adds items to inventoryItems from file
 public void AddStartingInventory()
 {
     try
     {
         //Read inventory file from file provided
         using (StreamReader sr = new StreamReader(path))
         {
             while (!sr.EndOfStream) // read through input file
             {
                 string   item   = sr.ReadLine();
                 string[] fields = item.Split("|");
                 decimal  cost   = decimal.Parse(fields[2]);
                 string   slot   = fields[0];
                 string   name   = fields[1];
                 string   type   = fields[3];
                 Products p      = null;
                 //Creates a new object of candy, chips, drink or gum
                 if (type == ("Candy"))
                 {
                     p = new Candy(name, cost, slot, type);
                 }
                 else if (type == ("Chip"))
                 {
                     p = new Chips(name, cost, slot, type);
                 }
                 else if (type == ("Drink"))
                 {
                     p = new Drink(name, cost, slot, type);
                 }
                 else if (type == "Gum")
                 {
                     p = new Gum(name, cost, slot, type);
                 }
                 //adds object to inventory items list with key of slot and product with all information
                 inventoryItems.Add(p.ProductSlot, p);
             }
         }
     }
     catch (Exception)
     {
         Console.WriteLine($"ERROR! Invalid {path} path.");
     }
 }
コード例 #11
0
        /// <summary>
        /// Takes file path to read a file to set up the items
        /// in their slots on our vending machine.
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        private void ReadInventory(string filePath)
        {
            using (StreamReader sr = new StreamReader(filePath))
            {
                while (!sr.EndOfStream)
                {
                    string   itemString     = sr.ReadLine();
                    string[] itemProperties = itemString.Split("|");

                    string slot = itemProperties[0];
                    string type = itemProperties[3];

                    string  name  = itemProperties[1];
                    decimal price = decimal.Parse(itemProperties[2]);

                    IVendingMachineItem item = null;

                    if (type == CANDY)
                    {
                        item = new Candy(name, price, ITEMS_AT_START);
                    }
                    else if (type == CHIP)
                    {
                        item = new Chip(name, price, ITEMS_AT_START);
                    }
                    else if (type == DRINK)
                    {
                        item = new Drink(name, price, ITEMS_AT_START);
                    }
                    else if (type == GUM)
                    {
                        item = new Gum(name, price, ITEMS_AT_START);
                    }

                    Slots.Add(slot, item);
                }
            }
        }
コード例 #12
0
        public Dictionary <string, VendingItem> GetVendingItems()
        { //make it take a filepath
            Dictionary <string, VendingItem> VendingItemList = new Dictionary <string, VendingItem>();

            if (File.Exists(@"C:\Users\Student\git\c-module-1-capstone-team-8\19_Capstone\vendingmachine.csv"))
            {
                try
                {
                    using (StreamReader sr = new StreamReader(@"C:\Users\Student\git\c-module-1-capstone-team-8\19_Capstone\vendingmachine.csv"))
                    {
                        while (!sr.EndOfStream)
                        {
                            string line = sr.ReadLine();

                            string[] productDetails = line.Split("|");

                            string slotLocation = productDetails[0];
                            string productName  = productDetails[1];
                            string price        = productDetails[2];
                            string type         = productDetails[3];

                            if (!decimal.TryParse(productDetails[2], out decimal productPrice))
                            {
                                productPrice = 0M;
                            }

                            int itemsRemaining = 5;

                            VendingItem item;

                            switch (productDetails[3])
                            {
                            case "Chip":
                                item = new Chip(productName, productPrice, itemsRemaining);
                                break;

                            case "Drink":
                                item = new Beverage(productName, productPrice, itemsRemaining);
                                break;

                            case "Gum":
                                item = new Gum(productName, productPrice, itemsRemaining);
                                break;

                            case "Candy":
                                item = new Candy(productName, productPrice, itemsRemaining);
                                break;

                            default: throw new ArgumentOutOfRangeException();
                            }

                            VendingItemList.Add(productDetails[0], item);
                        }
                    }
                }
                catch
                {
                }
            }
            else
            {
                Console.WriteLine("The input file is missing. Is this vending machine even real? Are we even real?");
            }
            return(VendingItemList);
        }
コード例 #13
0
        public Dictionary <string, VendingItem> GetVendingItems()
        {
            Dictionary <string, VendingItem> items = new Dictionary <string, VendingItem>();

            string file = string.Empty;

            if (File.Exists("vendingmachine.csv"))
            {
                file = "vendingmachine.csv";

                try
                {
                    using (StreamReader sr = new StreamReader(file))
                    {
                        while (!sr.EndOfStream)
                        {
                            // Read the line
                            string line = sr.ReadLine();

                            string[] itemDetails = line.Split("|");

                            string itemName = itemDetails[Pos_ItemName];

                            if (!decimal.TryParse(itemDetails[Pos_ItemPrice], out decimal itemPrice))
                            {
                                itemPrice = 0M;
                            }

                            int itemsRemaining = 5;

                            VendingItem item;

                            switch (itemDetails[Pos_itemType])
                            {
                            case "Chip":
                                item = new Chip(itemName, itemPrice, itemsRemaining);
                                break;

                            case "Drink":
                                item = new Drink(itemName, itemPrice, itemsRemaining);
                                break;

                            case "Gum":
                                item = new Gum(itemName, itemPrice, itemsRemaining);
                                break;

                            case "Coke":
                                item = new Coke(itemName, itemPrice, itemsRemaining = 10);
                                break;

                            case "Meant":
                                item = new Meant(itemName, itemPrice, itemsRemaining = 15);
                                break;
                            //case "Water":
                            //    item = new Water(itemName, itemPrice, itemsRemaining);
                            //    break;

                            case "Snickers":
                                item = new Snickers(itemName, itemPrice, itemsRemaining = 7);
                                break;

                            default:
                                item = new Chip(itemName, itemPrice, itemsRemaining);
                                break;
                            }

                            items.Add(itemDetails[Pos_itemNumber], item);
                        }
                    }
                }
                catch
                {
                    Console.WriteLine("Error trying to open the input file.");
                }
            }
            else
            {
                Console.WriteLine("Input file is missing!! The vending machine will now self destruct.");
                items.Add("A1", new Drink("YOU BROKE IT!", 10000M, 5));
            }

            return(items);
        }
        public Dictionary <string, VendingItem> GetVendingItems()
        {
            Dictionary <string, VendingItem> VendingItemList = new Dictionary <string, VendingItem>();

            string currentDirectory = Directory.GetCurrentDirectory();
            string filePath         = Path.Combine(currentDirectory, "..\\..\\..\\..");

            Directory.SetCurrentDirectory(filePath);

            if (File.Exists("vendingmachine.csv"))
            {
                try
                {
                    using (StreamReader sr = new StreamReader("vendingmachine.csv"))
                    {
                        while (!sr.EndOfStream)
                        {
                            string line = sr.ReadLine();

                            string[] productDetails = line.Split("|");

                            string slotLocation = productDetails[0];
                            string productName  = productDetails[1];
                            string price        = productDetails[2];
                            string type         = productDetails[3];

                            if (!decimal.TryParse(productDetails[2], out decimal productPrice))
                            {
                                productPrice = 0M;
                            }

                            int itemsRemaining = 5;

                            VendingItem item;

                            switch (productDetails[3])
                            {
                            case "Chip":
                                item = new Chip(productName, productPrice, itemsRemaining);
                                break;

                            case "Drink":
                                item = new Beverage(productName, productPrice, itemsRemaining);
                                break;

                            case "Gum":
                                item = new Gum(productName, productPrice, itemsRemaining);
                                break;

                            case "Candy":
                                item = new Candy(productName, productPrice, itemsRemaining);
                                break;

                            default: throw new ArgumentOutOfRangeException();
                            }

                            VendingItemList.Add(productDetails[0], item);
                        }
                    }
                }
                catch
                {
                }
            }
            else
            {
                Console.WriteLine("The input file is missing. Is this vending machine even real? Are we even real?");
            }
            return(VendingItemList);
        }