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);
}
}
}