Stevan
Stevan

Reputation: 11

How to save user choice into new array list

I’m sure I wasn’t clear enough in the title, clarifying everything here. I have a passenger booking a flight, I want to make a list in which the flights booked by the passenger will be stored.

This is the part of the code where the passenger completes the reservation, how do I make a new list and add a booked flight in it

@Override
public void payingFirstClassWithoutPromoCode(ArrayList<Flight> flightsList) {

    System.out.println("Confirm buying ticket: (yes or no)");
    String confirmation = scanner.nextLine();

    if (confirmation.equalsIgnoreCase("yes")) {
        if (selectedPassenger.getBalance() >= selectedFlight.getPriceForFirstClass()) {
            System.out.println("Successful reserved!");
            selectedPassenger.setBalance(selectedPassenger.getBalance() - selectedFlight.getPriceForFirstClass());
            System.out.println(selectedPassenger.getFirstName() + "'s new balance is: " + selectedPassenger.getBalance());
        } else {
            noFundsAvailable(flightsList);
        }
    } else if (confirmation.equalsIgnoreCase("no")) {
        cancellation(flightsList);
    } else {
        System.out.println("Wrong input!");
    }

}

Upvotes: 1

Views: 160

Answers (2)

Alexander Wiklund
Alexander Wiklund

Reputation: 145

You could design your Passenger class like this:

public class Passenger {
   private String name;
   private List<Flight> flights;

   //Getter and setter for name

   public Passenger(String name) {
      this.name = name;
      this.flights = new ArrayList<>();
   }

   public void addFlight(Flight flight) {
      flights.add(flight);
   }
}

On a side note, it might be worth refactoring your code. I note that you named your method payingFirstClassWithoutPromoCode(ArrayList<Flight> flightsList). I imagine you will have many more options, which means you will have to write multiple methods with very similar code.

It can be a good idea to make one method that can handle many different scenarios. For example, you can add the ticket type (first/business/economy class) as an parameter in your method: payingWithoutPromoCode(ArrayList<Flight> flightsList, String ticketType).

It means you have to rewrite your method a little bit. In my example, you probably need to rewrite the methods in your Flight class. Instead of selectedFlight.getPriceForFirstClass() you can do selectedFlight.getPrice(ticketType).

Upvotes: 0

Saravanan
Saravanan

Reputation: 911

I think you can add a Flight field in your Passenger class, then do selectedPassenger.setFlight(selectedFlight) Here's the passenger class

class Passenger{
   //other fields
   Flight flight;
   public void setFlight(Flight f){
    this.flight = f;
   }

If a passenger can have multiple flight, then declare a List in your passenger class, then do selectedPassenger.getFlights().add(selectedFlight);

class Passenger{
   //other fields
   List<Flight> flight;
   public Passenger(){
    //don't forget to initialize flight list
    flight = new ArrayList();
   }
   public List<Flight> getFlights(){
     return this.flight ;
   }

instead of fetching the whole list and adding it manually, you can have a method in your passenger class like

public void addFlight(Flight f){
  this.flight.add(f);
}

then you can do

selectedPassenger.addFlight(selectedFlight);

Upvotes: 1

Related Questions