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

Question #318079

ticket booking programming;--Developer needs to input the following from the console using programming to book ticket for a passenger:

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

-pnr no:a string field for pnr no with a default 7 digit number.

-travel date:a string field to enter the journey date in format(dd/mm/yyyy).

-source:a string field to capture the source airport.

-destination:a string field to capture the destination airport.

-status:a string field to select among following values new/confirm/hold.

-seat preference:a string field to select among following values,middle,aisle.

-meal preference:a string field to select among following values.

**once all fields are taken input,booking details need to be inserted in array/list/appropriate collection.after successful booking,a booking acknowledgment message need to be displayed on the console as "ticket booked successfully,Happy Journey...".


1
Expert's answer
2022-03-25T08:40:58-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 "Passenger{" +
                "passengerID='" + passengerID + "\'\n" +
                ", pnrNo='" + pnrNo + "\'\n" +
                ", source='" + source + "\'\n" +
                ", destination='" + destination + "\'\n" +
                ", status='" + status + "\'\n" +
                ", seatPreference='" + seatPreference + "\'\n" +
                ", mealPreference='" + mealPreference + "\'\n" +
                ", travelDate=" + travelDate +
                '}';
    }
}

public class Main {


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

        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(list.get(0));
    }
}

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