I have an array called passengers , lets say it has 10 places. I also have a class for new passenger which gonna have the age of the passenger. I want to add a new passenger to the array by his age. This passenger is gonna be created by its class through calling the constructor. Lets say I add only ONE passenger by calling the method and 9 places are EMPTY. The NEXT time a call the method, I want to add a new passenger in the next place in the passengers array, So every time I want to add a new passenger, i call the passenger method, and it will be saved in the next empty place in array. until it is full. A message will say it is full and no passenger can go on.
My problem with my code is that i have to enter all passengers at once. but i want to enter only one passenger every time I call the method.
public void Add_Passengers ()
{
Console.WriteLine("How old are the passengers?");
for (int i = 0; i < passengers.Length; i++)
{
int age = Int32.Parse(Console.ReadLine());
passengers[i] = new Passenger(age);
}
}
/// <summary>
/// Passengers array
/// </summary>
public Passenger[] Passengers = new Passenger[10];
public class Passenger
{
public int Age { get; set; }
public Passenger(int age)
{
Age = age;
}
}
public void AddPassenger()
{
// Get the number of passengers
int passengerCount = Passengers.Count(p => p != null);
if (passengerCount == Passengers.Length)
Console.WriteLine("Maximum number of passengers");
else
{
Console.WriteLine("How old are the passengers?");
int age = int.Parse(Console.ReadLine());
// Add passenger
Passengers[passengerCount] = new Passenger(age);
}
}
You can try:
public void AddPassengers(Passenger[] passengers)
{
int i = Array.IndexOf(passengers, null);
if (i < 0)
{
Console.WriteLine("The array is full.");
return;
}
Console.WriteLine("How old is the passenger?");
int age = Int32.Parse(Console.ReadLine());
passengers[i] = new Passenger(age);
}
Hope this helps.
public void Add_Passengers()
{
PassengerClass PassengerClass = new PassengerClass();
Console.WriteLine("How old are the passengers?");
PassengerClass.AddPassanger(Int32.Parse(Console.ReadLine()));
}
public class PassengerClass
{
public List<int> passangerList;
public PassengerClass()
{
if (passangerList == null)
passangerList = new List<int>();
}
public void AddPassanger(int age)
{
passangerList.Add(age);
}
}
I would go with the class solution:
public class Passanger {
public int age;
public Passanger(int age)
{
this.age = age;
}
}
public class Passangers
{
private List<Passanger> passangers;
private int limit;
public bool ready { get; private set; }
public Passangers(int capacity)
{
passangers = new List<Passanger>(capacity);
limit = capacity;
ready = false;
}
public void addPassanger(Passanger passanger)
{
passangers.Add(passanger);
if(passangers.Count == limit)
{
ready = true;
}
}
public List<Passanger> getPassangers()
{
passangers.Sort((p1, p2) => p1.age.CompareTo(p2.age));
return passangers;
}
}
Rather than iterating the array each time, you can keep an additional field tracking the index and increment it within the add operation.
private Passenger[] passengers = new Passenger[10];
private int _count = 0;
public void Add_Passengers()
{
// validate overflow. >= not strictly necessary but a better safeguard than ==
if (_count >= passengers.Length)
throw new InvalidOperationException("Array is filled");
Console.WriteLine("How old are the passengers?");
// validate input
if (!int.TryParse(Console.ReadLine(), out int age))
throw new InvalidOperationException("Not an integer");
// Postfix increment returns original value
passengers[_count++] = new Passenger(age);
}
Obviously you should handle the error cases as you see fit. For demonstration I'm simply throwing an InvalidOperationException
.
Edit: I just noticed you asked explicitly to display an Error message. That could look like this:
private Passenger[] passengers = new Passenger[10];
private int _count = 0;
public void Add_Passengers()
{
// validate overflow. >= not strictly necessary but a better safeguard than ==
if (_count >= passengers.Length)
{
Console.WriteLine("Error: No more passengers are allowed. Size limit reached.");
return;
}
Console.WriteLine("How old are the passengers?");
// validate input
if (!int.TryParse(Console.ReadLine(), out int age))
{
Console.WriteLine("Error: Value is not an integer");
return;
}
// Postfix increment returns original value
passengers[_count++] = new Passenger(age);
}
A List<Passenger>
is probably a better option. Then you don't need to track the index or worry about the size overflowing. Note that neither solution is thread-safe, but that's a different question.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.