public Item CreateItem(string[] args) { string typeOfItem = args[0]; Item newlyCreatedItem = null; try { switch (typeOfItem) { case "ArmorRepairKit": newlyCreatedItem = new ArmorRepairKit(); return(newlyCreatedItem); case "HealthPotion": newlyCreatedItem = new HealthPotion(); return(newlyCreatedItem); case "PoisonPotion": newlyCreatedItem = new PoisonPotion(); return(newlyCreatedItem); default: throw new ArgumentException($"Invalid item \"{typeOfItem}\"!"); } } catch (InvalidOperationException oe) { throw new InvalidOperationException(oe.Message); } }
public string AddItemToPool(string[] args) { var itemName = args[0]; Item item; switch (itemName) { case "HealthPotion": item = new HealthPotion(); break; case "PoisonPotion": item = new PoisonPotion(); break; case "ArmorRepairKit": item = new ArmorRepairKit(); break; default: throw new ArgumentException($"Invalid item \"{itemName}\""); } itemPool.Push(item); return($"{itemName} added to pool!"); }
public Item CreateItem(string itemName) { if (itemName != "ArmorRepairKit" && itemName != "HealthPotion" && itemName != "PoisonPotion") { throw new ArgumentException(string.Format(ExceptionMessages.InvalidItemException, itemName)); } Item item = null; if (itemName == "ArmorRepairKit") { item = new ArmorRepairKit(); } else if (itemName == "HealthPotion") { item = new HealthPotion(); } else if (itemName == "PoisonPotion") { item = new PoisonPotion(); } else { throw new ArgumentException(string.Format(ExceptionMessages.InvalidItemException, itemName)); } return(item); }
public string AddItemToPool(string[] args) { if (args[0] != nameof(ArmorRepairKit) && args[0] != nameof(PoisonPotion) && args[0] != nameof(HealthPotion)) { throw new ArgumentException($"Invalid item \"{args[0]}\"!"); } Item temp = null; switch (args[0]) { case nameof(ArmorRepairKit): temp = new ArmorRepairKit(); break; case nameof(PoisonPotion): temp = new PoisonPotion(); break; case nameof(HealthPotion): temp = new HealthPotion(); break; } itemPool.Push(temp); return($"{args[0]} added to pool!"); }
public string AddItemToPool(string[] args) { string itemName = args[0]; Item item; if (itemName == "ArmorRepairKit") { item = new ArmorRepairKit(); } else if (itemName == "HealthPotion") { item = new HealthPotion(); } else if (itemName == "PoisonPotion") { item = new PoisonPotion(); } else { throw new ArgumentException("Invalid item \"{ name }\"!"); } itemsPool.Push(item); return($"{itemName} added to pool."); }
public Item CreateItem(string type) { Item item = null; if (type == "ArmorRepairKit") { item = new ArmorRepairKit(); return(item); } else if (type == "HealthPotions") { item = new HealthPotion(); return(item); } else if (type == "PosionPotion") { item = new PoisonPotion(); return(item); } throw new ArgumentException(string.Format(ErrorMessages.InvalidItem, type)); }
public Item CreateItem(string itemName) { Item item; switch (itemName) { case "HealthPotion": item = new HealthPotion(); break; case "PoisonPotion": item = new PoisonPotion(); break; case "ArmorRepairKit": item = new ArmorRepairKit(); break; default: throw new ArgumentException(string.Format(OutputMessages.IvalidItemType, itemName)); } return(item); //Second way - reflection: //var type = this.GetType() // .Assembly // .GetTypes() // .FirstOrDefault(x => x.Name == itemName); //if(type == null || itemName == "Item" || String.IsNullOrWhiteSpace(itemName)) //{ // throw new ArgumentException(string.Format(OutputMessages.IvalidItemType, itemName)); //} //try //{ // var item = (Item)Activator.CreateInstance(type, new object[] { }); // return item; //} //catch (Exception ex) //{ // throw new InvalidOperationException(ex.Message); //} }
public Item CreateItem(string name) { Item item = null; switch (name) { case "HealthPotion": item = new HealthPotion(); break; case "PoisonPotion": item = new PoisonPotion(); break; case "ArmorRepairKit": item = new ArmorRepairKit(); break; default: throw new ArgumentException($"Invalid item \"{name}\"!"); } return(item); }
public Item CreateItem(string itemType) { Item item = null; switch (itemType) { case "HealthPotion": return(item = new HealthPotion()); case "PoisonPotion": return(item = new PoisonPotion()); case "ArmorRepairKit": return(item = new ArmorRepairKit()); default: throw new ArgumentException(string.Format(OutputMessages.InvalidItemType), itemType); } }
public Item ProduceItem(string itemType) { Item item = null; switch (itemType) { case "ArmorRepairKit": item = new ArmorRepairKit(); break; case "HealthPotion": item = new HealthPotion(); break; case "PoisonPotion": item = new PoisonPotion(); break; } return(item); }
public Item CreateItem(string itemName) { Item item; if (itemName == "HealthPotion") { item = new HealthPotion(); } else if (itemName == "PoisonPotion") { item = new PoisonPotion(); } else if (itemName == "ArmorRepairKit") { item = new ArmorRepairKit(); } else { throw new ArgumentException($"Invalid item \"{itemName}\"!"); } return(item); }
public Item createItem(string name) { if (name == "HealthPotion") { HealthPotion healthPotion = new HealthPotion(); return(healthPotion); } else if (name == "PoisonPotion") { PoisonPotion poisonPotion = new PoisonPotion(); return(poisonPotion); } else if (name == "ArmorRepairKit") { ArmorRepairKit armorRepairKit = new ArmorRepairKit(); return(armorRepairKit); } else { throw new ArgumentException($"Invalid item \"{name}\"!"); } }
public Item CreateItem(string[] args) { string itemName = args[0]; Item item = null; if (itemName == "ArmorRepairKit") { item = new ArmorRepairKit(); return(item); } else if (itemName == "HealthPotion") { item = new HealthPotion(); return(item); } else if (itemName == "PoisonPotion") { item = new PoisonPotion(); return(item); } throw new ArgumentException($"Invalid item \"{ itemName }\""); }
public Item CreateItem(string itemName) { Item item = null; switch (itemName) { case "ArmorRepairKit": item = new ArmorRepairKit(); break; case "HealthPotion": item = new HealthPotion(); break; case "PoisonPotion": item = new PoisonPotion(); break; default: throw new ArgumentException(string.Format(Messages.InvalidItemName, itemName)); } return(item); }
public Item CreateItem(string nameParam) { var name = nameParam; if (name != "HealthPotion" && name != "PoisonPotion" && name != "ArmorRepairKit") { throw new ArgumentException(/*string.Format(Inputs.InvalidItemType, name)*/ $"Invalid item \"{name}\"!"); } if (name == "HealthPotion") { var healthPotion = new HealthPotion(); return(healthPotion); } else if (name == "PoisonPotion") { var poisonPotion = new PoisonPotion(); return(poisonPotion); } else { var armorRepairKit = new ArmorRepairKit(); return(armorRepairKit); } }
public Item CreateItem(string name) { Item currentItem = null; switch (name) { case "ArmorRepairKit": currentItem = new ArmorRepairKit(); break; case "HealthPotion": currentItem = new HealthPotion(); break; case "PoisonPotion": currentItem = new PoisonPotion(); break; default: Error.InvalidItemType(name); break; } return(currentItem); }