0

How can I create a new instance of Boeing737 and use it later in the program. For example I want to be able to create 5 Boeings, do I have to define them like

Boeing737 boeing1 =  new Boeing737(name: "Boeing737" + Console.ReadLine(),fuel: int.Parse(Console.ReadLine()) , tons: 0);

Boeing737 boeing2 =  new Boeing737(name: "Boeing737" + Console.ReadLine(),fuel: int.Parse(Console.ReadLine()) , tons: 0);

and so on... Or is there easier way? Other question, to WHAT can I assign all the properties of boeing1 for example?

Here is my current code:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Insert the type of boeing that u are using");
        Boeing737 boeing = new Boeing737(name: "Boeing737" + Console.ReadLine(),fuel: int.Parse(Console.ReadLine()) , tons: 0);
        Console.WriteLine("{0} has {1} tons of fuel and weights {2}", boeing.Name, boeing.Fuel, boeing.Tons);
        Console.ReadKey();

    }
}

public class Planes
{
    public Planes(string name, int fuel, int tons)
    {
        Name = name;
        Fuel = fuel;
        Tons = tons;
    }
    public int Tons;
    public int Fuel;
    public string Name { private set; get; }
}
class Boeing737 : Planes
{
    public Boeing737(string name, int fuel, int tons) : base(name, fuel, tons)
    {
        Tons = 700;
    }
}

}

5
  • 5
    Learn about arrays and loops.. Commented Dec 7, 2017 at 21:02
  • 6
    I woudl also recommend that you NOT call Console.ReadLine() within the constructor (let alone twice). Make that a separate call, capture the output, and use that in the constructor. Commented Dec 7, 2017 at 21:04
  • I would recommend that you google and read the C# Basics Tutorial focus on variables, classes, encapsulation, properties Commented Dec 7, 2017 at 21:10
  • 2
    I'd suggest also looking into proper grammar. Hate to be that guy, but even on a school assignment, they're likely to knock you for boeing that u are using Commented Dec 7, 2017 at 21:10
  • 1
    I don't get the down votes here, it's a perfectly valid question, there is some effort shown by the OP, there are some questions that could have been investigated better maybe, but there is absolutely nothing bad about it (even if it feels like it is a schoolwork question, it shows some effort done by the OP) Commented Dec 7, 2017 at 21:10

4 Answers 4

2

Well, let's start by improving your code a little bit:

// the class represents a single object, give it a
// singular name
public class Plane
{
    // get before set, it's not mandatory but give yourself
    // some basic coding rules to improve code maintainability
    // and readability
    // avoid public members, implementing properties is always
    // a good choice for future extensions and manipulations
    public int Fuel { get; private set; }
    public int Tons { get; private set; }
    public string Name { get; private set; }

    public Plane(string name, int fuel, int tons)
    {
        Name = name;
        Fuel = fuel;
        Tons = tons;
    }
}

// if your inheritance stops here, you could set a
// sealed attribute
public sealed class Boeing737 : Plane
{
    // no need to set the property twice, you are already
    // calling the base constructor, pass him the fixed
    // tons value of 700...
    public Boeing737(string name, int fuel) : base(name, fuel, 700)
    {
    }
}

Now, concerning instantiation, go for a the generic List<T> type, which is very easy to manage and will expand itself when you add more objects:

List<Boeing737> boeings = new List<Boeing737>
{
    new Boeing737("A", 5),
    new Boeing737("B", 5),
    new Boeing737("C", 5),
    new Boeing737("D", 5)
};

If you want to create a List that can contain different type of planes, stick to the upper level type:

List<Plane> planes = new List<Plane>
{
    new Boeing737("A", 5),
    new Boeing737("B", 5),
    new Boeing737("C", 5),
    new Boeing737("D", 5),
    new AirplaneX("D", 10, 350)
};

List can also be used together with LINQ to facilitate its manipulation and filtering (more info here). For example, sort by weight:

var planesSortedTons = planes.OrderBy(x => x.Tons).ToList();

Select only the planes with Fuel > 10:

var planesFuel10 = planes.Where(x => x.Fuel > 10).ToList();

On a side note, if you want to fill a huge list of data through console input, you need to build an infinite loop (for example a while (true)) and populate list by addition:

static void Main(string[] args)
{
    List<Boeing737> boeings = new List<Boeing737>();

    String input;

    while (true)
    {
        Consol.WriteLine("Enter name:");
        input = Console.ReadLine();

        if (input.ToLowerInvariant() == "stop")
            break;

        String name = input.Trim();

        Consol.WriteLine("Enter fuel:");
        input = Console.ReadLine();

        if (input.ToLowerInvariant() == "stop")
            break;

        Int32 fuel;

        try
        {
            fuel = Int32.Parse(input.Trim());
        }
        catch
        {
            Console.WriteLine("Wrong input, stopping!");
            break;
        }

        boeings.Add(new Boeing737(name, fuel));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I would create a list of Boeing737 and I would not take the straight input from the console.

List<Boeing737> boeingList = new List<Boeing737>();
boeingList.add(new Boeing737() { param=value...});

Then later on you can access them by index, name, loop through them, etc.

I would also look into Linq

Comments

0

Use array initialization:

var boeings = new []
  {
    new Boeing737("name", 123, 1000),
    new Boeing737("name", 123, 1000),
    new Boeing737("name", 123, 1000),
    new Boeing737("name", 123, 1000),
  };

4 Comments

Thank you. How can I sort them by .Fuel?
using linq is pretty simple var boeings = new [] { new Boeing737("name", 123, 1000), new Boeing737("name", 123, 1000), new Boeing737("name", 123, 1000), new Boeing737("name", 123, 1000), }.OrderBy(boeing => boeing.Fuel); Use an extra .ToArray() or .ToList() if you want something different than an IEnumerable
Thank you very much and now I need only one thing to know. How can I call the third sorted boeing's name?
You mean acessing the name of third element in the collection? string boeingName = boeings[2].Name; Just make sure you used .ToArray() or ToList() after using the OrderBy
0

What about using C# Arrays.

namespace ConsoleApps_examples { class Program { static void Main(string[] args) { //Console.WriteLine("Insert the type of boeing that u are using");

        //string sname = Console.ReadLine();
        //int ifuel = int.Parse(Console.ReadLine());

        Console.WriteLine(" here are 5 different type of boeing:");

        string sname = "";
        int ifuel = 0;

        int i;
        int[] fuellist = new int[5] { 99, 98, 92, 97, 95 };
        var nameslist = new string[5]  { "XXA1", "XXA2", "XXA3","XXA4","XXA5"};

        //var arr3 = new string[] { "one", "two", "three" };

        for (i = 0; i < 5; i++)
        {
            ifuel = fuellist[i];
            sname = nameslist[i];
            Boeing737 boeing = new Boeing737(name: "Boeing737" + sname, fuel: ifuel, tons: 0);
            Console.WriteLine("{0} has {1} tons of fuel and weights {2}", boeing.Name, boeing.Fuel, boeing.Tons);

        }

        //Boeing737 boeing = new Boeing737(name: "Boeing737" + sname, fuel: ifuel, tons: 0);
        Console.ReadKey();

    }    


}


public class Planes
{
    public Planes(string name, int fuel, int tons)
    {
        Name = name;
        Fuel = fuel;
        Tons = tons;
    }
    public int Tons;
    public int Fuel;
    public string Name { private set; get; }
}
class Boeing737 : Planes
{
    public Boeing737(string name, int fuel, int tons) : base(name, fuel, tons)
    {
        Tons = 700;
    }
}

}

this is the output: List of 5 types of Boeing planes:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.