I am trying to make a simple program that has the user buy items, sell items, and list their items. Just trying to practice what I have learned thus far. I have made a class to buy items, and a class to list the items. The problem I am encountering is when the "buy" class is done running and has stored the items in a playersInventory array, that data in the heap gets erased before the "list" class can list what the player has bought. Any help with this would be much appreciated, as well as any and all critique on my code.
Program Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BuySell_Gear
{
class Program
{
static void Main(string[] args)
{
MainMenu mainMenu = new MainMenu();
mainMenu.MenuScreen();
}
}
}
MainMenu struct
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BuySell_Gear
{
struct MainMenu
{
static int MainMenuMethod()
{
Console.Clear();
Console.WriteLine("Select what you would like to do:");
Console.WriteLine("1. Buy");
Console.WriteLine("2. Sell");
Console.WriteLine("3. List");
Console.WriteLine("4. Exit");
return Convert.ToInt32(Console.ReadLine());
}
public void MenuScreen()
{
int menuSelection = 0;
while (menuSelection != 4)
{
menuSelection = MainMenuMethod();
Buy buy = new Buy();
List list = new List();
if (menuSelection == 1)
{
buy.BuyStuff();
}
else if (menuSelection == 2)
{
break;
}
else if (menuSelection == 3)
{
list.GenerateList();
}
else if (menuSelection == 4)
{
break;
}
else
{
Console.Clear();
Console.WriteLine("\t\t***Invalid Entry***");
Console.ReadLine();
}
}
}
}
}
Buy class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BuySell_Gear
{
class Buy
{
private string[] itemsForSale = new string[] { "M-16", "Grenade", "Flak Jacket", "Kevlar", "K-Bar" };
private int[] pricesForItems = new int[] { 300, 90, 100, 50, 20 };
private string[] playersInventory = new string[5]; //Player can only carry 5 items
private int[] playersInventoryIndex = new int[5];
private int playersMoney = 500;
private int playersSelection = 0;
private int placeItemInIndexOfPlayersInventory = 0;
//Begin Constructors
public string[] PlayersInventory
{
get { return playersInventory; }
set { playersInventory = value; }
}
public int[] PricesForItems
{
get { return pricesForItems; }
}
public int[] PlayersInventoryIndex
{
get { return playersInventoryIndex; }
set { playersInventoryIndex = value; }
}
public int PlayersMoney
{
get { return playersMoney; }
set { playersMoney = value; }
}
private int PlayersSelection
{
get { return playersSelection; }
set { playersSelection = value; }
}
//Begin Methods:
public void BuyStuff()
{
while (PlayersSelection != 21)
{
Console.Clear();
Console.WriteLine("What would you like to buy:(type 22 to exit)");
int list = 1;
for (int index = 0; index < itemsForSale.Length; index++)
{
Console.WriteLine(list + ". " + itemsForSale[index] + " costs $" + pricesForItems[index]);
++list;
}
playersSelection = Convert.ToInt32(Console.ReadLine()) - 1;
if (PlayersSelection == 21)
{
break;
}
else if (PlayersInventory.Length == placeItemInIndexOfPlayersInventory)
{
Console.Clear();
Console.WriteLine("You cannot carry any more objects");
Console.ReadLine();
break;
}
else if (PlayersMoney < PricesForItems[playersSelection])
{
Console.Clear();
Console.WriteLine("\nYou do not have enough money");
Console.ReadLine();
continue;
}
else
{
PlayersInventory[placeItemInIndexOfPlayersInventory] = itemsForSale[playersSelection];
PlayersInventoryIndex[placeItemInIndexOfPlayersInventory] = PlayersSelection;
++placeItemInIndexOfPlayersInventory;
PlayersMoney -= PricesForItems[playersSelection];
Console.WriteLine("You just bought a/n " + itemsForSale[playersSelection] + " for $" + pricesForItems[playersSelection]);
Console.WriteLine("You have $" + PlayersMoney + " left.");
Console.ReadLine();
}
}
}
}
}
List Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BuySell_Gear
{
class List
{
public void GenerateList()
{
Buy buy = new Buy();
Console.Clear();
int list = 1;
for (int index = 0; index < buy.PlayersInventory.Length; index++)
{
if (buy.PlayersInventory[index] == null)
{
Console.WriteLine(list + ". empty");
++list;
continue;
}
else
{
Console.WriteLine(list + ". " + buy.PlayersInventory[index]);
++list;
}
}
Console.ReadLine();
}
}
}