Question #53153

Take input for boarding place,arriving place and date
Date validation for 120 days(if date is more than 120 days you are not able to book the ticket).if days are less than 120 days then ask for the count of passengers
Take passengers age and gender
If age is less than 13 you have to pay half of the price of the ticket.And if age is greater than 59 you have to pay 60% of the ticket price
Then display final amount of the ticket,date,passengers(male,female and child count with age),ticket booking confirmation message

Expert's answer

Answer on Question #53153 - Programming / Java | JSP | JSF

Take input for boarding place, arriving place and date

Date validation for 120 days (if date is more than 120 days you are not able to book the ticket). If days are less than 120 days then ask for the count of passengers

Take passengers age and gender

If age is less than 13 you have to pay half of the price of the ticket. And if age is greater than 59 you have to pay 60% of the ticket price

Then display final amount of the ticket, date, passengers (male, female and child count with age), ticket booking confirmation message

Answer


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        final int DATE_VALIDATION = 120;
        final int CHILD_AGE = 13;
        final int RETIREMENT_AGE = 59;
        String boardingPlace, arrivingPlace;
        Date now, date = null;
        int passCount;
        List<Passenger> passengers = new ArrayList<>();
        int age;
        String gender;
        double ticketPrice, ticketAmount = 0;
        int childCount = 0;
    }
}
Random rnd = new Random();
Scanner input = new Scanner(System.in);
// Take input for boarding place, arriving place and date
System.out.print("Enter boarding place: ");
boardingPlace = input.nextLine();
System.out.print("Enter arriving place: ");
arrivingPlace = input.nextLine();
System.out.print("Date (dd.mm.yyyy): ");
String dateString = input.nextLine();
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
try {
    date = format.parse(dateString);
} catch (ParseException e) {
    e.printStackTrace();
}
// today's date
now = new Date();
// Difference between two dates in days
long difference = date.getTime() - now.getTime();
long days = difference / (24 * 60 * 60 * 1000);
if (days > DATE_VALIDATION) {
    System.out.println("You are not able to book the ticket!");
    return;
}
// ask for the count of passengers
System.out.print("Enter count of passengers: ");
passCount = Integer.parseInt(input.nextLine());
// Generate random ticket price
ticketPrice = 100 + 900 * rnd.nextDouble();
// Take passengers age and gender
for (int i = 0; i < passCount; i++) {
    System.out.print("Passenger #" + (i + 1) + ": Enter age: ");
    age = Integer.parseInt(input.nextLine());
    if (age < CHILD_AGE)
        ticketAmount += 0.5 * ticketPrice;
    else if (age > RETIREMENT_AGE)
        ticketAmount += 0.6 * ticketPrice;
    else ticketAmount += ticketPrice;
    System.out.print("Passenger #" + (i + 1) + ": Enter gender (M/F): ");
    gender = input.nextLine();
// Add passenger to list
    passengers.add(new Passenger(age, gender));
}
// Display final amount of the ticket
System.out.printf("Final amount of the ticket: %.2f\n", ticketAmount);
// Display date
System.out.println("Booking date: " + date);
// Display passengers (male, female and child count with age)
System.out.println("Passengers:");
for (int i = 0; i < passengers.size(); i++) {
    System.out.println("Passenger #" + (i + 1) + ". " + passengers.get(i));
    if (passengers.get(i).getAge() < CHILD_AGE)
        childCount++;
}
System.out.println("Child count: " + childCount);
// Display ticket booking confirmation message
System.out.println("Your ticket is registered.");
}
}
public class Passenger {
    private int age;
    private String gender;
    public Passenger(int age, String gender) {
        super();
        this.age = age;
        this.gender = gender;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    @Override
    public String toString() {
        return "Age: " + age + ", gender: " + gender;
    }
}
https://www.AssignmentExpert.com

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!

LATEST TUTORIALS
APPROVED BY CLIENTS