Esempio n. 1
0
        public static IItem Parse(string[] input)
        {
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();

            for (int index = 3; index < input.Length; index++)
            {
                string[] splittedParams = input[index].Split('=');
                keyValuePairs[splittedParams[0]] = splittedParams[1];
            }

            string id = keyValuePairs["id"];
            string title = keyValuePairs["title"];
            decimal price = Decimal.Parse(keyValuePairs["price"]);
            string genre = keyValuePairs["genre"];

            IItem item;

            switch (input[1])
            {
                case "book":
                    string author = keyValuePairs["author"];

                    item = new Book(id, title, price, author, genre);
                    break;
                case "movie":
                    int length = int.Parse(keyValuePairs["length"]);

                    item = new Movie(id, title, price, length, genre);
                    break;
                case "game":
                    if (keyValuePairs.ContainsKey("ageRestriction"))
                    {
                        AgeRestriction restriction = (AgeRestriction)Enum.Parse(typeof(AgeRestriction), keyValuePairs["ageRestriction"]);
                        item = new Game(id, title, price, genre, restriction);
                    }
                    else
                    {
                        item = new Game(id, title, price, genre);
                    }
                    break;
                default:
                    throw new ArgumentException("There is no such item");
            }

            return item;
        }
        private void ExecuteSupplyCommand(string itemType, string paramsString, int quantity)
        {
            var itemParams = GetItemParams(paramsString);

            switch (itemType)
            {
                case "book":
                    {
                        string id = itemParams["id"];
                        string title = itemParams["title"];
                        string author = itemParams["author"];
                        decimal price = decimal.Parse(itemParams["price"]);
                        string genre = itemParams["genre"];

                        var book = new Book(id, title, price, author, genre);
                        this.AddToSupplies(book, quantity);
                        break;
                    }
                case "video":
                    {
                        string id = itemParams["id"];
                        string title = itemParams["title"];
                        decimal price = decimal.Parse(itemParams["price"]);
                        string genre = itemParams["genre"];
                        int length = int.Parse(itemParams["length"]);

                        var video = new Video(id, title, price, length, genre);
                        this.AddToSupplies(video, quantity);
                        break;
                    }
                case "game":
                    {
                        string id = itemParams["id"];
                        string title = itemParams["title"];
                        decimal price = decimal.Parse(itemParams["price"]);
                        string genre = itemParams["genre"];
                        AgeRestriction ageRestriction = ToEnum(itemParams["ageRestriction"]);

                        var game = new Game(id, title, price, genre, ageRestriction);
                        this.AddToSupplies(game, quantity);
                        break;
                    }
                default:
                    throw new ArgumentException("Invalid item type.");
            }
        }