coffeebeforecode

Java class 4 Feb Q1

Feb 3rd, 2022 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. package lab_class_04_Feb_ex5;
  2.  
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.time.*;
  6. import java.util.*;
  7.  
  8. public class Devansh20BCE0410 {
  9.  
  10.     public static void main(String[] args) {
  11.         // TODO Auto-generated method stub
  12.         Scanner ReadInt = new Scanner(System.in);
  13.         Scanner ReadString = new Scanner(System.in);
  14.         System.out.println("Input number of passengers:");
  15.         int n = ReadInt.nextInt();
  16.         Passenger p[] = new Passenger[n];
  17.         for(int i = 0; i < n; i++) {
  18.             p[i] = new Passenger();
  19.             p[i].getPassengerInfo();
  20.         }
  21.        
  22.         System.out.println("Enter Passenger code:");
  23.         String c = ReadString.next();
  24.        
  25.         boolean flag = false;
  26.         for(Passenger i : p) {
  27.             if (c.equals(i.getCode())) {
  28.                 flag = true;
  29.                 i.showDetails();
  30.                 break;
  31.             }
  32.         }
  33.        
  34.         if (flag == false) {
  35.             System.out.println("ERROR: Passenger Code Not Found!");
  36.         }
  37.        
  38.         ReadInt.close();
  39.         ReadString.close();
  40.     }
  41.  
  42. }
  43.  
  44. class Passenger{
  45.     String name;
  46.     String code;
  47.     int age;
  48.     Date dob;
  49.     String dest;
  50.     String flight;
  51.     float price;
  52.    
  53.     public void getPassengerInfo() {
  54.         Scanner sc = new Scanner(System.in);
  55.         System.out.println("Enter name:");
  56.         this.name = sc.nextLine();
  57.         System.out.println("Enter code:");
  58.         this.code = sc.nextLine();
  59.         System.out.println("Enter date of birth in dd/MM/yyyy format:");
  60.         try {
  61.             this.dob = new SimpleDateFormat("dd/MM/yyyy").parse(sc.nextLine());
  62.         } catch (ParseException e) {
  63.             // TODO Auto-generated catch block
  64.             e.printStackTrace();
  65.         }
  66.        
  67.         // Converts dob to LocalDate, then finds the age by finding the difference in the years
  68.         this.age = Period.between(this.dob.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(), LocalDate.now()).getYears();
  69.        
  70.         System.out.println("Enter destination:");
  71.         this.dest = sc.nextLine().toLowerCase();
  72.         sc.close();
  73.         setDetails();
  74.     }
  75.    
  76.     public void setDetails() {
  77.         switch(this.dest) {
  78.         case "singapore":
  79.         case "malaysia":
  80.         case "taiwan":
  81.             this.flight = "Boeing 703";
  82.             this.price = 3500f;
  83.             this.price += this.price*0.10;
  84.             break;
  85.         case "us":
  86.         case "uk":
  87.         case "canada":
  88.             this.flight = "Lufthansa 303";
  89.             this.price = 75000f;
  90.             this.price += this.price*0.20;
  91.             break;
  92.         default:
  93.             this.flight = "No flight booked";
  94.             this.price = 0f;
  95.             break;
  96.         }
  97.         System.out.println("DONE");
  98.         showDetails();
  99.     }
  100.    
  101.     public void showDetails() {
  102.         System.out.println("Name: "+ this.name);
  103.         System.out.println("DOB: " + this.dob);
  104.         System.out.println("Age: " + this.age);
  105.         System.out.println("Destination: " + this.dest);
  106.         System.out.println("Flight: " + this.flight);
  107.         System.out.println("Total Price: " + this.price);
  108.     }
  109.    
  110.     public String getCode() {
  111.         return this.code;
  112.     }
  113. }
Add Comment
Please, Sign In to add comment