Question Clearing array and calcing array?

Netsid

Member
Joined
Feb 10, 2020
Messages
21
Programming Experience
Beginner
Im doing a school project and i have managed to come so far but now im stuck again i can´t figure out this last 2 things. This is my first project in C#, well programming at all. What im trying to do is get the methods for "emptying bus" and "calcpass" to work properly but im stuck right here. Line 42 and 59 is starting points, i really apriciate any help i can get! =)

thebus:
using System;

namespace ConsoleApp8
{
    class Program
    {
        static void Main(string[] args)     //main method, runs Bus
        {   //visuals
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.SetWindowSize(63, 39);
                                                //runs bus class
            var runbus = new Bus();
            runbus.Run();
        }
    }
    class Bus
    {
        private const int V = 25;                            //variable for max passengers
        private int Y = 0;                                  //variable for keeping track of passengers
        private Passenger[] passengers = new Passenger[V]; // Initializing array of passengers

        public void addpass()                            //method for adding passengers
        {
            if (Y < 25)                                  //if statement informs if bus is full
            {
                Console.WriteLine("Type name of the passenger and press [Enter]");
                string nname = Console.ReadLine();
                Console.WriteLine("Type age of the passenger and press [Enter]");
                double nage = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Type sex m/f of the passenger and press [Enter]");
                string nsex = Console.ReadLine();
                passengers[Y] = (nname, nage, nsex);
                Y++;
            }
            else
            {
                Console.WriteLine("The bus is full");
            }

        }
        public void emptybus()                        //method for emptying bus XXXXXX not working
        {
            if (Y > 0)
            {
                Array.Clear(passengers, 0, Y);
                Console.WriteLine("Press any key to continue");
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine("Bus is empty");
                Console.WriteLine("Press any key to continue");
                Console.ReadKey();
            }

        }
        //  public double calc_age() =>  / Bus.Y;
        public void calcpass()                           //method for calculating passenger average age XXXXXXX not working
        {
            double totage = 0;
            for (double i = 0; i < passengers.Length; i++)
            {
                Console.WriteLine(passengers[Y]);
                totage = passengers[Y].age;
                double midage = (double)totage / passengers.Length;

                Console.WriteLine($"midage is {midage}");
                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();
                this.Run();
            }
        }
        public void showlist()                          //method with forloop to show passenger list
        {
            for (int i = 0; i < Y; i++)
            {
                Console.WriteLine($"Name: {passengers[i].name} Age: {passengers[i].age} Sex: {passengers[i].sex}");
            }
            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
        }
        public void Run()
        {
            int choice = 0;
            var _exiter = new Exit();                     //variable for starting class for exiting program 
            do                                            //do-while to loop menu
            {
                Console.Clear();                         //keeps menu clean
                Console.WriteLine("" + Y + " passengers on bus.");
                Console.WriteLine("Welcome, what do you need to do?");
                Console.WriteLine("Please choose a number from the menu and press enter.");
                Console.WriteLine("1. Add passenger");
                Console.WriteLine("2. Empty bus");
                Console.WriteLine("3. Show passenger list");
                Console.WriteLine("4. Calculate average age");
                Console.WriteLine("5. Close Program");
                try                                       // try block to catch bad input
                {
                    choice = int.Parse(Console.ReadLine());
                }
                catch
                {
                    Console.WriteLine("Please choose 1-4");
                }                                        //switch statement menu
                switch (choice)
                {
                    case 1:
                        this.addpass();
                        break;
                    case 2:
                        this.emptybus();
                        break;
                    case 3:
                        this.showlist();
                        break;
                    case 4:
                        this.calcpass();
                        break;
                    case 5:
                        _exiter.Run();
                        break;
                    default:
                        Console.WriteLine("Try again.");
                        break;
                }
            } while (choice != 0);
        }
    }
    class Passenger                                    //passenger class containing variabels, parameters and constructor
    {
        public string name;
        public double age;
        public string sex;

        public Passenger(string name, double age, string sex)          //constructor passenger
        {
            this.name = name;
            this.age = age;
            this.sex = sex;
        }
        public double getage()                                  //method to return age
        {
            return age;
        }

                                                            //Tuple constructor for returning values
        public static implicit operator Passenger((string nname, double nage, string nsex) v)
        {
            return new Passenger(v.nname, v.nage, v.nsex);
        }

    }
    class Exit                                   //Class for closing program
    {
        public void Run()
        {
            Console.Clear();
            Console.Write("Press any key to exit.");
            Console.ReadKey();
            Environment.Exit(0);
        }
    }
}
 
Anyway, it seems that the better data structure for the task at hand would be a List<Passenger> instead of an array of Passenger, but I assume that the teacher for this class has a method to his/her madness.

As an aside, did you really need this:
C#:
//Tuple constructor for returning values
public static implicit operator Passenger((string nname, double nage, string nsex) v)
{
    return new Passenger(v.nname, v.nage, v.nsex);
}
just so that you could have this line of code:
C#:
passengers[Y] = (nname, nage, nsex);
when that could have simply have been written as:
C#:
passengers[Y] = new Passenger(nname, nage, nsex);
The code is no less readable.

Yes, there are places where tuples are useful (and not to mention cool), but in this case it seemed like the feature is being used just for the sake of using the feature.
 
88 to 97 needs to be moved before line 87. There is no point in those lines rewriting. In fact, there is no point in that loop.

You would need the setter. Since the setter just ignores any values set to it, you might as well delete line 34.

@Skydiver at a re-glance, I think that was one of the reasons i left it there, as so the OP could ascertain and best decide what to do with it.
 
I hear what your saying about editing your original post thats kinda obvius when i think about it! I tried rewriting the Y variable with the "Y = Get_PassengerCount();" and now the array.clear method works properly! I know the calculation for calcage looks wierd asf its cuz i just put something there atm so i could run the program without commenting out any parts.
Really apreciate the help guys! thanks a lot =)
 
Back
Top Bottom