Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab_class_04_Feb_ex5;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.time.*;
- import java.util.*;
- public class Devansh20BCE0410 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner ReadInt = new Scanner(System.in);
- Scanner ReadString = new Scanner(System.in);
- System.out.println("Input number of passengers:");
- int n = ReadInt.nextInt();
- Passenger p[] = new Passenger[n];
- for(int i = 0; i < n; i++) {
- p[i] = new Passenger();
- p[i].getPassengerInfo();
- }
- System.out.println("Enter Passenger code:");
- String c = ReadString.next();
- boolean flag = false;
- for(Passenger i : p) {
- if (c.equals(i.getCode())) {
- flag = true;
- i.showDetails();
- break;
- }
- }
- if (flag == false) {
- System.out.println("ERROR: Passenger Code Not Found!");
- }
- ReadInt.close();
- ReadString.close();
- }
- }
- class Passenger{
- String name;
- String code;
- int age;
- Date dob;
- String dest;
- String flight;
- float price;
- public void getPassengerInfo() {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter name:");
- this.name = sc.nextLine();
- System.out.println("Enter code:");
- this.code = sc.nextLine();
- System.out.println("Enter date of birth in dd/MM/yyyy format:");
- try {
- this.dob = new SimpleDateFormat("dd/MM/yyyy").parse(sc.nextLine());
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // Converts dob to LocalDate, then finds the age by finding the difference in the years
- this.age = Period.between(this.dob.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(), LocalDate.now()).getYears();
- System.out.println("Enter destination:");
- this.dest = sc.nextLine().toLowerCase();
- sc.close();
- setDetails();
- }
- public void setDetails() {
- switch(this.dest) {
- case "singapore":
- case "malaysia":
- case "taiwan":
- this.flight = "Boeing 703";
- this.price = 3500f;
- this.price += this.price*0.10;
- break;
- case "us":
- case "uk":
- case "canada":
- this.flight = "Lufthansa 303";
- this.price = 75000f;
- this.price += this.price*0.20;
- break;
- default:
- this.flight = "No flight booked";
- this.price = 0f;
- break;
- }
- System.out.println("DONE");
- showDetails();
- }
- public void showDetails() {
- System.out.println("Name: "+ this.name);
- System.out.println("DOB: " + this.dob);
- System.out.println("Age: " + this.age);
- System.out.println("Destination: " + this.dest);
- System.out.println("Flight: " + this.flight);
- System.out.println("Total Price: " + this.price);
- }
- public String getCode() {
- return this.code;
- }
- }
Add Comment
Please, Sign In to add comment