Home > database >  How do I get rid of huge if elseif statements?
How do I get rid of huge if elseif statements?

Time:11-24

So, I am making a small text based game and this is part of a lootbox after beating a dungeon. It's for one of the 5 items you can possibly get out of the lootbox. It checks if inventoryslot 1 is free. If not, it checks if inventoryslot 2 is free... etc etc. This results in giant if elseif statements and makes the code really messy. Are there any alternatives I can use instead of this?

if (whatitem == 1)
{
    Console.WriteLine("You got "   item5   "(10%)");
    if (invitem1 == "")
    {
        invitem1 = item5;
        Console.Write("The item has been added to your inventory");
        Console.WriteLine(" Added to slot 1");
    }
    else if (invitem2 == "")
    {
        invitem2 = item5;
        Console.Write("The item has been added to your inventory");
        Console.WriteLine(" Added to slot 2");
    }
    else if (invitem3 == "")
    {
        invitem3 = item5;
        Console.Write("The item has been added to your inventory");
        Console.WriteLine(" Added to slot 3");
    }
    else if (invitem4 == "")
    {
        invitem4 = item5;
        Console.Write("The item has been added to your inventory");
        Console.WriteLine(" Added to slot 4");
    }
    else if (invitem5 == "")
    {
        invitem5 = item5;
        Console.Write("The item has been added to your inventory");
        Console.WriteLine(" Added to slot 5");
    }
    else if (invitem6 == "")
    {
        invitem6 = item5;
        Console.Write("The item has been added to your inventory");
        Console.WriteLine(" Added to slot 6");
    }
    else if (invitem7 == "")
    {
        invitem7 = item5;
        Console.Write("The item has been added to your inventory");
        Console.WriteLine(" Added to slot 7");
    }
    else
    {
        Console.WriteLine("No space, Reward deleted");
    }
    Console.WriteLine("Press ENTER to proceed");
}

I tried searching for solutions on google and so on, but I feel that this problem is very specific and I couldn't find a solid answer.

CodePudding user response:

You need to split it up and use the 'object oriented' part of C#. Something like below (with better outlining).

public class Program
{
   public static void Main(string[] args)
   {
       var inventory = new Inventory();
       var items = new List<Item>();
       
       foreach (var item in items)
       {
           inventory.AddItemToInventory(item).
       }
   }
}
    public class Item
    {
          public string Name {get;set: }
    }

public class Inventory
{
   public int Slots => 4

   public List<Item> SlotsInUse { get; set; } = new List<Item>();
    
   public bool AddItemToInventory(Item item)
   {
      if (SlotsInUse.Count() < Slots)
      {
         Console.WriteLine($"{item.Name} added to inventory.";
         SlotsInUse.Add(item);
      }
      else
      {
          Console.WriteLine("Your inventory is full!"
       }
}

CodePudding user response:

Similar to the other answer but with examples of adding, removing and displaying the inventory:

Inventory.cs

internal class Inventory
{
    private readonly List<string> _inventory;
    private readonly uint _maximumInventorySize;

    public Inventory(uint maximumSize)
    {
        if (maximumSize < 1)
            throw new ArgumentOutOfRangeException(nameof(maximumSize), "Inventory must hold at least one item");

        _maximumInventorySize = maximumSize;
        _inventory = new List<string>();
    }

    public bool AddItem(string itemName)
    {
        if (_inventory.Count == _maximumInventorySize)
            return false;

        _inventory.Add(itemName);
        return true;
    }

    public bool RemoveItem(string itemName)
    {
        if (!_inventory.Contains(itemName))
            return false;

        _inventory.Remove(itemName);
        return true;
    }

    public override string ToString()
    {
        return _inventory.Count == 0
            ? "Nothing"
            : string.Join(", ", _inventory);
    }
}

Program.cs

internal static class Program
{
    public static void Main()
    {
        const int inventorySize = 7;
        var inventory = new Inventory(inventorySize);

        // Display the inventory.
        Console.WriteLine($"Your inventory contains: {inventory}.");

        // Attempt to overfill the inventory to get the 'No space' message.
        for (var item = 0; item < inventorySize   1; item  )
        {
            var itemName = $"Item{item   1}";
            if (inventory.AddItem(itemName))
                Console.Write($"{itemName} has been added to your inventory\n");
            else
                Console.WriteLine($"No space for {itemName}, reward deleted");
        }

        // Display the inventory.
        Console.WriteLine($"Your inventory contains: {inventory}.");

        // Remove odd numbered inventory items.
        for (var item = 0; item < inventorySize; item  = 2)
        {
            var itemName = $"Item{item   1}";
            inventory.RemoveItem(itemName);
        }

        // Display the inventory.
        Console.WriteLine($"After removing odd numbered items, your inventory contains: {inventory}.");

        Console.WriteLine("Press ENTER to proceed");
        Console.ReadLine();
    }
}
  • Related