Search code examples
c#listclassoopinventory

Adding Objects to List and summing up the total weight


I have a problem with a Task for my school. We have to create an Inventory Class and fill it with Objects. The Inventory should have a maximum capacity. The second Task is to write a method that sums up the total weight (float) of my objects. I dont know how to get objects in the inventory.. Please help!! Here my code: Class:

public abstract class Item1 { //abstract BASE
    string label;
    float weight;
    protected Item1 (string l, float w) {
        label = l;
        weight = w;
    }
    public string Label() { return label; }
    public float Weight() { return weight; }
}

public abstract class Equipment : Item1 { //abstact
    float tear;
    protected Equipment (string l, float w, float t) : base (l,w) {
        tear = t;
    }   
    public float Tear() { return tear; }
}

public abstract class Goods : Item1 { //abstact
    float uselevel;
    protected Goods (string l, float w, float ul) : base (l,w) {
        uselevel = ul;      
    }
    public float Uselevel() { return uselevel; }
}

class Sword : Equipment { //concrete
    public float damage;
    public Sword (string l, float w, float t, float d) : base (l,w,t) {
        damage = d;
    }
    public override string ToString ()
    {
        return "Item: " + Label () +" +" + damage + " damage" ;
    }
}

class Shield : Equipment { //concrete
    public float block;
    public Shield (string l, float w, float t, float b) : base (l,w,t) {
        block = b;
    }
    public override string ToString ()
    {
        return "Item: " + Label () +" +" + block + " defence" ;
    }
}

class HP : Goods { //concrete
    public float heal;
    public HP (string l, float w, float ul, float h) : base (l,w,ul) {
        heal = h;
    }
    public override string ToString ()
    {
        return "Item: " + Label () +" +" + heal + " health" ;
    }
}

class MP : Goods { //concrete
    public float mana;
        public MP (string l, float w, float ul, float m) : base (l,w,ul) {
        mana = m;
    }
        public override string ToString ()
    {
        return "Item: " + Label () +" +" + mana + " mana" ;
    }
}





Program:


public class Build : MonoBehaviour {

    void Start() {  
}

    void Update () {

        if(Input.GetKey(KeyCode.F)) {

            //Inventory.items.Add( new Sword("Lucifers Blade",12,15,2130) );
            //Inventory.items.Add( new HP("Healpotion",1,2,850) );
            //Inventory.items.Add( new MP("Manapotion",1,2,1300) );
            //Sword sword = new Sword("Lucifers Blade",12,15,1337);
            //print (sword.Label() + " weight: " + sword.Weight() + " KG - Level: "+ sword.Tear() + " DMG: " + sword.damage );

        }

        if(Input.GetKey(KeyCode.D)) {

            MP mana = new MP("Manapotion",12,15,1337);
            print ("You used: " + mana.Label() + "(Weight: " + mana.Weight() + " kg) Level: " + mana.Uselevel() + " + " + mana.mana + " Mana.");

        }
    }

}


Inventory: 

using System.Collections.Generic;

public class Inventory {
    string name;
    public static List<Item1> items;
    public Inventory(string n) {
        name = n;
        items = new List<Item1>();
    }
    public void Add(Item1 item) {
        items.Add (item);
        items.Add( new Sword("Lucifers Blade",12,15,2130) );
    }
    public void Remove(Item1 item)  {
        items.Remove (item);
    }
    public override string ToString (){
        string s = name + " contains ";
        foreach (Item1 item in items)
            s += item + ", ";
        return s;
    }

}

Solution

  • You are using Inventory as a static class, you need to create an instance of it.

    Inventory myInventory = new Inventory("Inventory");
    

    Then to add items

    myInventory.items.Add( new MP("Manapotion",1,2,1300) );
    

    Finally, you can use Linq to work out the weight

    float weight = myInventory.items.Sum(item => item.weight);
    

    Also, it seems strange that the list is static. Is this intended?