I'm currently working on my college project and having a problem while using an array of classes. The problem is that when I try to call a list of the data held in the classes it shows a repeated list of the last item in the array. I'll post a few parts of the program and attach a copy of the code. It's no where near complete and comments are patchy.
This is the class
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]
public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]flight
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]{
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] flightNo; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//holds unique number to identify flights
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] departureAirport; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//hold the departure airport
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] arrivalAirport; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//holds the arrival airport
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] flightStatus; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//holds status of flight
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//diplays the details held about the flight
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] displayFlightDetails()
{
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Flight Number: \t\t\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + flightNo);
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Departure Airport \t\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + departureAirport);
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Arrival Airport \t\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + arrivalAirport);
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Flight Status \t\t\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + flightStatus);
}
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//creates new flight
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] CreateNewFlight([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] counter)
{
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].flightNo = counter; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//defines the unique flight number
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Flight Number: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].flightNo);
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Write([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Please Enter Departure Airport: - "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].departureAirport = [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ReadLine();
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Write([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Please enter Arrival Airport: - "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].arrivalAirport = [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ReadLine();
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].flightStatus = 1;
}
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] checkNumber()
{
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].flightNo;
}
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] checkStatus()
{
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].flightStatus;
}
}
[/SIZE]
My decleration of the class:
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af] flight[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][] flights = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]flight[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][20]; [/SIZE]
This is where the problem arises
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//booking seat
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Clear();
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Booking A Seat"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine();
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Avaliable Flights"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Number\tDeparts from\tArrives at\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][] avaliableFlights = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][20];
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] avaliableFlightsCounter = 0;
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i = 0; i < flightCounter; i++)
{
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (flights[i].checkStatus() == 1)
{
//This is where I try to output the data
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine(flights[i].flightNo + [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + flights[i].departureAirport + [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + flights[i].arrivalAirport);
avaliableFlights[avaliableFlightsCounter] = flightCounter;
}
}
[/SIZE]
Hope to hear back from someone soon.


Sign In
Create Account


Back to top









