RenT-Boi
RenT-Boi

Reputation: 1

Printing some data

I have been trying to print the information about whether the equipment is mobile or immobile, but for some reason it isn't showing any output.

public class Equipment
{
    public string name;
    public string description;
    public string type;
    public int distance_moved = 0;
    public int wheels = 0;
    public int weight = 0;
    public int maintenance_cost;
    public Equipment(string n, string d, string t, int w, int wt)
    {
        n = name;
        d = description;
        t = type;
        w = wheels;
        wt = weight;
    }

    public void Move_By(int d)
    {
        distance_moved = distance_moved + d;
        if (this.type == "Mobile")
        {
            maintenance_cost = wheels * distance_moved;
        }
        else maintenance_cost = weight * distance_moved;
    }
} 
class Program
{
    static void Main(string[] args)
    {
        var Equipment_list = new List<Equipment>()
        {
            new Equipment("Bulldozer", "Light Vehicle", "Mobile",4,0),
            new Equipment("Box", "Huge Box", "ImMobile",0,10),
            new Equipment("Drill Machine", "Light Machine", "ImMobile",0,20),
            new Equipment("Truck", "Heavy Vehicle", "Mobile",4,0)
        };
        var Mobile_Equipment = from Mobile in Equipment_list
            where Mobile.type == "Mobile"
            orderby Mobile.name
            select Mobile;
        foreach (var Mobile in Mobile_Equipment)
        {
            Console.WriteLine("{0} is a Mobile Equipment", Mobile.name);
        }
        Console.ReadKey();
    }
}

Upvotes: 0

Views: 50

Answers (2)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131403

The assignments in Equipment's constructor are in the wrong order. It's not a good idea to use public fields either. Fields are implementation details, even if they are public.

The class should look like this:

    public class Equipment
    {
        public string Name {get;set;}
        public string Description {get;set;}
        public string Type {get;set;}
        public int Distance_moved {get;set;}= 0;
        public int Wheels {get;set;} =0;
        public int Weight {get;set;} =0;
        public int Maintenance_cost {get;set;} =0;
        public Equipment(string name, string description, string type, 
                         int wheels, int weight)
        {
            Name = name;
            Description = description;
            Type = type;
            Wheels = wheels;
            Weight = weight;
        }

Upvotes: 0

itsme86
itsme86

Reputation: 19496

Your Equipment constructor is doing all of its assignments backwards. You're assigning the default class member values to the method arguments. It should be like this instead:

public Equipment(string n, string d, string t, int w, int wt)
{
    name = n;
    description = d;
    type = t;
    wheels = w;
    weight = wt;
}

Upvotes: 3

Related Questions