//Save file public void Save(string path) { //Create a writer for the file at our path StreamWriter writer = File.CreateText(path); //Write Gold writer.WriteLine(Gold); //Itterate through players items and write them writer.WriteLine(Items.Length); foreach (Item itm in Items) { if (itm is Weapon) { Weapon temp = itm as Weapon; itm.GetName(); writer.WriteLine("weapon"); writer.WriteLine(itm.GetName()); writer.WriteLine(temp.GetDamage()); writer.WriteLine(itm.GetValue()); writer.WriteLine(itm.GetDescription()); } else { Potion temp = itm as Potion; writer.WriteLine("potion"); writer.WriteLine(itm.GetName()); writer.WriteLine(temp.GetHeal()); writer.WriteLine(itm.GetValue()); writer.WriteLine(itm.GetDescription()); } } //Close it writer.Close(); }
//create and add new item to shop public void AddItem() { Console.WriteLine("Super user activated"); Console.WriteLine("Enter a name"); newName = Console.ReadLine(); Console.WriteLine("Enter a value"); int number = -1; _choice = Console.ReadLine(); if (Int32.TryParse(_choice, out number) && number >= 0) { newValue = number; } else { Console.WriteLine("Invalid Input"); return; } Console.WriteLine("Enter a description"); newDescription = Console.ReadLine(); Console.WriteLine(""); Console.WriteLine("Item created successfuly"); Console.WriteLine(""); Console.WriteLine("Item Name: " + newName); Console.WriteLine("Item Value: " + newValue); Console.WriteLine("Item Description: " + newDescription); Console.WriteLine(""); Console.WriteLine("What kind of item has been created?"); Console.WriteLine("1: Weapon"); Console.WriteLine("2: Potion"); _choice = Console.ReadLine(); int number2 = -1; //add new weapon item if (_choice == "1") { Console.WriteLine("Enter a damage value"); _choice = Console.ReadLine(); if (Int32.TryParse(_choice, out number2) && number2 >= 0) { newDamage = number2; } else { Console.WriteLine("Invalid Input"); return; } Item newItem = new Weapon(newName, newDamage, newValue, newDescription); _Shop.Add(newItem); Console.WriteLine(newItem.GetName() + " has been added to the shop inventory"); } //add new potion item else if (_choice == "2") { Console.WriteLine("Enter a heal value"); _choice = Console.ReadLine(); if (Int32.TryParse(_choice, out number2) && number2 >= 0) { newHeal = number2; } else { Console.WriteLine("Invalid Input"); return; } Item newItem = new Potion(newName, newHeal, newValue, newDescription); _Shop.Add(newItem); Console.WriteLine(newItem.GetName() + " has been added to the shop inventory"); } else { Console.WriteLine("Invalid"); } }