Answer to Question #318085 in Java | JSP | JSF for abcdefghijk

Question #318085

Bookong List Programming--developer needs to input the following information from the console using programming to view the booked ticket history for a passenger.

*passenger id: a 7 digit number for passenger id for whom booking is to be done.

*on submitting the passenger id,the console shows rows with columns populated from inserted array/list/appropriate collections; PNR NO|Travel date| source|destination|Seat preference|Meal preference


1
Expert's answer
2022-03-25T12:10:39-0400
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;


class Passenger{
    String passengerID, pnrNo, source, destination, status, seatPreference, mealPreference;
    LocalDate travelDate;

    Passenger(String passengerID, String pnrNo, LocalDate travelDate, String source, String destination, String status, String seatPreference, String mealPreference){
        this.passengerID = passengerID;
        this.pnrNo = pnrNo;
        this.source = source;
        this.destination = destination;
        this.status = status;
        this.seatPreference = seatPreference;
        this.mealPreference = mealPreference;
        this.travelDate = travelDate;
    }

    @Override
    public String toString() {
        return "| " + pnrNo + " | " + travelDate + " | " + source + " | " + destination  + " | " + seatPreference + " | " + mealPreference + " | ";
    }
}

public class Main {

    public static void findPassenger(List<Passenger> list, String findPas){
        Scanner in = new Scanner(System.in);

        for (Passenger obj : list) {
            if(findPas.equals(obj.passengerID)){
                System.out.print(obj.toString());
            }
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        List<Passenger> list = new ArrayList<>();
        Random rand = new Random();
        Main mai = new Main();

        System.out.print("Enter 7 digit number for passenger id: ");
        String passengerID = in.next();

        String pnrNo = "";

        for(int i = 0; i<7; i++){
            int a = rand.nextInt(10);
            String k = Integer.toString(a);
            pnrNo += k;

        }

        System.out.print("Enter the journey date in format(dd/mm/yyyy): ");
        String firstString = in.next();
        LocalDate travelDate = LocalDate.parse(firstString, formatter);

        System.out.print("Enter the place of departure: ");
        String source = in.next();

        System.out.print("Enter the place of arrival: ");
        String destination = in.next();

        System.out.print("Select among following values (new/confirm/hold): ");
        String status = in.next();

        System.out.print("Select among following values (middle,aisle): ");
        String noob = in.nextLine();
        String seatPreference = in.nextLine();

        System.out.print("Enter food preferences: ");
        String mealPreference = in.nextLine();

        list.add(new Passenger(passengerID, pnrNo, travelDate, source, destination, status, seatPreference, mealPreference));


        System.out.print("Enter 7 digit number for passenger id for search: ");
        String findPas = in.next();

        Main.findPassenger(list, findPas);

    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS