In: Computer Science
Create a new Java project, name it “Quiz02”. (This is a JAVA Programming question, thumbs up quick and correct answer)
1) Create a java class Traveler that has four data members, String name, Date bDate, int id and int noOfDependens.
Generate the following:
1- Getters and Setters.
2- Four parametrize constructor to initialize the four data members above.
3- toString() method.
Create a java class Flight that has four data members, String number, String destination, int capacity, and ArrayList travelers.
Note1 - travelers will be used to track the No. of travelers so far on the selected flight.
Note2 - No. of travelers = No. of dependents + 1;
Generate the following: 1- Getters and Setters.
2- Three parametrize constructor to initialize the number, destination and capacity.
3- toString() method.
4- Method public void addTraveler(Traveler traveler) that will do the following:
a. It will calculate the total No. of travelers so far.
b. It will calculate the total availability = capacity - No. of travelers.
c. If the availability is equal to OR more than the requested tickets (traveler + his noOfDependens) then, accept this reservation and add the traveler to the flight ArrayList, otherwise, reject the reservation by printing the following message “This reservation is NOT accepted. Total No. of requested tickets are: X while the availability is: Y”
d. Sample for your output in case the reservation is accepted is listed below.
Create a java class AppSystem for testing, and include the main method. Then create the following:
Traveler("Traveler1", (17, 5, 1950), 1950517 , 7); Traveler("Traveler2", (2, 12, 1975), 1975122 , 5); Traveler("Traveler3", (13, 3, 1981), 1981313 , 3); Traveler("Traveler4", (9, 10, 1979), 1979109 , 4); Traveler("Traveler5", (25, 5, 1963), 1963525 , 6); Flight("QAR246", "Kuwait", 70); Flight("Kuw579", "Doha", 60);
5- Add to the Flight QAR246 the first Traveler1 Sample for addTraveler() method after accepting the traveler Traveler1.
Flight capacity: 70
No. of current travelers: 0
Flight availability: 70
=======================================
Accepted reservation. Total No. of reserved tickets are: 8
6- Print the flight QAR246 details by calling the method toString(), sample for your method output listed below:
***** Flight details *****
==========================
Flight No.: QAR246
Flight destination: Kuwait
Flight Capacity: 70
List of travelers:
Traveler name: Traveler1
Birth Date: 17/ 5/1950
ID: 1950517
No. of Dependents: 7
Hello Buddy!
Here is the code for above question.
import java.util.Date;
import java.util.ArrayList;
class Traveler
{
String name;
int id;
int noOfDependens;
Date bDate;
Traveler(String name,int d,int m, int y,int id,int noOfDependens){
this.name = name;
this.id = id;
this.noOfDependens = noOfDependens;
this.bDate = new Date(y,m,d);
}
public String toString(){
return "Name: "+this.name+"\nid: "+this.id+"\n No of Dependens: "+this.noOfDependens+"\n Birth Date:"+this.bDate;
}
}
class Flight{
String number;
String destination;
int capacity;
ArrayList<Traveler> travelers = new ArrayList<Traveler>();
Flight(String number,String destination,int capacity){
this.number = number;
this.destination = destination;
this.capacity = capacity;
}
public String toString(){
String s = "******Flight Details*******\n==========================";
s += "\nFlight Number: "+number+"\nFlight Destination: "+destination+"\nFlight Capacity: "+capacity;
s +="\nList of Travelers:";
for(int i=0;i<travelers.size();i++) {
s+="\n------------\nTraveler Name:"+(travelers.get(i)).name +"\nBirth Date:"+(travelers.get(i)).bDate+"\nID:"+ (travelers.get(i)).id +"\nNo. of Dependents:"+(travelers.get(i)).noOfDependens;
}
return s;
}
public void addTraveler(Traveler traveler){
int total_travelers = 0;
for(int i=0;i<travelers.size();i++) {
total_travelers+=(travelers.get(i)).noOfDependens+1;
}
int total_availablity = this.capacity-total_travelers;
System.out.println("Flight capacity:"+capacity);
System.out.println("No of current travelers:"+total_travelers);
System.out.println("Flight availablity:"+total_availablity);
System.out.println("=======================================");
if(total_availablity>=traveler.noOfDependens+1){
travelers.add(traveler);
System.out.println("Accepted reservation. Total No of reserved tickets are:"+(traveler.noOfDependens+1));
}
else{
System.out.println("This reservation is not accepted. Total No. of requested tickets are:"+capacity+", while the availablity is:"+total_availablity);
}
}
}
public class Main
{
public static void main(String[] args) {
System.out.println("=======================================");
Traveler t1 = new Traveler("Traveler1", 17, 5, 1950 ,1950517 , 7);
Traveler t2 = new Traveler("Traveler2", 2, 12, 1975 ,1975122 , 5);
Traveler t3 = new Traveler("Traveler3", 13, 3, 1981 ,1981313 , 3);
Traveler t4 = new Traveler("Traveler4", 9, 10, 1979 ,1979109 , 4);
Traveler t5 = new Traveler("Traveler5", 25, 5, 1963 ,1963525 , 6);
Flight f1 = new Flight("QAR246", "Kuwait", 70);
Flight f2 = new Flight("Kuw579", "Doha", 60);
f1.addTraveler(t1);
System.out.println(f1.toString());
}
}
Thumbs Up, if it helps;)