Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualKhaiy

Posted 22 March 2013 - 04:59 PM

If I were to use a Dictionary to set the item and price, then store what the user selects into a List, would I be able to reference the item back to the Dictionary if the user wants to sell that item back for what he paid?


I'm imagining the dictionary as a persistent variable that is referenced as needed for buying and selling, and inventory is stored separately. So the Buy interface doesn't add or remove an item from the dictionary but only accesses it to get the price for that item. Inventories, like a store's current stock, would be something different (potentially another dictionary with the item as a key and the number in stock as the value, or any other arrangement you like).

If the item/price dictionary variable is named priceList, the Buy interface would get the price for itemX by accessing priceList[itemX].

When the player buys an item, you deduct int price = priceList[itemX] from their current money supply and add the item to their inventory (player.Inventory.Add(the item), if the inventory is a list). To sell the item back, the player's money supply is credited with priceList[itemX] dollars, and the item is removed from the player's inventory (player.Inventory.Remove(the item)).

Does that help? I'm back on my phone, so again my post quality is taking a hit.

 

EDIT: Home again. Here's a quick sample of what the code might look like if you have a dictionary for storing price information, a list for inventory, a dictionary for store stock, and items represented only as strings:

 

 
// A class defining a player:
 
public partial class Player
{
     // Fields
     public List<string> inventory;
 
     int money;
}
 
// A class defining a store
 
public partial class Store
{
     // Fields
     public Dictionary<string, int> priceList;
 
     public Dictionary<string, int> itemStock;
 
     // Methods
     public void DisplayStock()
     {
          // Code to display the items for sale
          foreach (string s in itemStock.Keys)
          {
               Console.WriteLine("{0} costs ${1}.", s, priceList[s]);
          }          
     }
 
     public void BuyItem(Player p, string itemPurchased)
     {
          if (p.money >= priceList[itemPurchased])
          {
               p.money -= priceList[itemPurchased];
               p.inventory.Add(itemPurchased);
               itemStock[itemPurchased]--;
          }
 
          else
          {
               Console.WriteLine("You don't have enough money.");
          }
     }
 
     public void SellItem(Player p, string itemSold)
     {
          p.inventory.Remove(itemSold);
          p.money += priceList[itemSold];
          itemStock[itemPurchased]++;
     }
}
 

#1Khaiy

Posted 22 March 2013 - 12:26 PM

If I were to use a Dictionary to set the item and price, then store what the user selects into a List, would I be able to reference the item back to the Dictionary if the user wants to sell that item back for what he paid?


I'm imagining the dictionary as a persistent variable that is referenced as needed for buying and selling, and inventory is stored separately. So the Buy interface doesn't add or remove an item from the dictionary but only accesses it to get the price for that item. Inventories, like a store's current stock, would be something different (potentially another dictionary with the item as a key and the number in stock as the value, or any other arrangement you like).

If the item/price dictionary variable is named priceList, the Buy interface would get the price for itemX by accessing priceList[itemX].

When the player buys an item, you deduct int price = priceList[itemX] from their current money supply and add the item to their inventory (player.Inventory.Add(the item), if the inventory is a list). To sell the item back, the player's money supply is credited with priceList[itemX] dollars, and the item is removed from the player's inventory (player.Inventory.Remove(the item)).

Does that help? I'm back on my phone, so again my post quality is taking a hit.

PARTNERS