Program
import java.util.ArrayList;
import java.util.Iterator;
import java.io.*;
class Route{
String routeId;
String routeOrigin;
String routeDestination;
String routeDate;
}
class Flight{
String flightId;
String flightName;
String flightOrigin;
String flightDestination;
String flightDate;
ArrayList<Route> routeArray=new ArrayList<Route>(10);
}
class FlightManagement{
ArrayList<Flight> flightArray=new ArrayList<Flight>(10);
Route route;
public void addFlight(Flight newFlight){
flightArray.add(newFlight);
}
public void listFlight(Flight newFlight){
System.out.println("\nFlight Details \n————————\n");
System.out.println("Flight ID : " + newFlight.flightId);
System.out.println("Flight Name : " + newFlight.flightName);
System.out.println("Flight Origin : " + newFlight.flightOrigin);
System.out.println("Flight Destination : " + newFlight.flightDestination);
System.out.println("Flight Date : " + newFlight.flightDate);
System.out.println("\nRoute Details \n————————\n");
System.out.println("ID\t\tOrigin\t\tDestn\t\tDate\n");
for(int i=0;i<newFlight.routeArray.size();i++){
route=newFlight.routeArray.get(i);
System.out.println(route.routeId +"\t\t" + route.routeOrigin +"\t\t" + route.routeDestination +"\t\t" + route.routeDate);
}
}
public Flight findFlight(String newFlightId){
int flag=0;
for(int i=0;i<flightArray.size();i++){
if(flightArray.get(i).flightId.equals(newFlightId)){
Flight newFlight=flightArray.get(i);
System.out.println("Flight ID : " + newFlight.flightId);
System.out.println("Flight Name : " + newFlight.flightName);
System.out.println("Flight Origin : " + newFlight.flightOrigin);
System.out.println("Flight Destination : " + newFlight.flightDestination);
System.out.println("Flight Date : " + newFlight.flightDate);
System.out.println("\nRoute Details \n————————\n");
System.out.println("ID\t\tOrigin\t\tDestn\t\tDate\n");
for(int j=0;j<newFlight.routeArray.size();j++){
route=newFlight.routeArray.get(j);
System.out.println(route.routeId +"\t\t" + route.routeOrigin +"\t\t" + route.routeDestination +"\t\t" + route.routeDate);
}
flag=1;
return newFlight;
}
}
if(flag==0){
System.out.println("\nNo Flight Details Found !!!\n");
}
return null;
}
public void removeFlight(String newFlightId){
int flag=0;
for(int i=0;i<flightArray.size();i++){
if(flightArray.get(i).flightId.equals(newFlightId)){
flightArray.remove(i);
System.out.println("\nFlight Details Deleted Successfully !!!\n");
flag=1;
break;
}
}
if(flag==0){
System.out.println("\nNo Flight Details Found !!!\n");
}
}
public boolean updateFlight(Flight newFlight){
System.out.println(flightArray.indexOf(newFlight));
return true;
}
}
class Main{
static void enterFlightDetails(Flight flight) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("\nEnter Flight Details");
System.out.println("————————\n");
System.out.print("Enter Flight ID : ");
flight.flightId=br.readLine();
System.out.print("Enter Flight Name : ");
flight.flightName=br.readLine();
System.out.print("Enter Flight Origin : ");
flight.flightOrigin=br.readLine();
System.out.print("Enter Flight Destination : ");
flight.flightDestination=br.readLine();
System.out.print("Enter Flight Date : ");
flight.flightDate=br.readLine();
}
static void enterRouteDetails(Flight flight) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String choice;
Route route;
do{
route=new Route();
System.out.println("\nEnter Route Details");
System.out.println("————————\n");
System.out.print("Enter Route ID : ");
route.routeId=br.readLine();
System.out.print("Enter Route Origin : ");
route.routeOrigin=br.readLine();
System.out.print("Enter Route Destination : ");
route.routeDestination=br.readLine();
System.out.print("Enter Route Date : ");
route.routeDate=br.readLine();
flight.routeArray.add(route);
System.out.print("\nDo you want to enter more routes (y/n) ? : ");
choice=br.readLine();
}while(choice.equals("y"));
}
public static void main(String args[]) throws IOException{
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
FlightManagement flightMngt=new FlightManagement();
Flight flight=new Flight();
int choice;
String newFlightId;
do{
System.out.println("\n*********** CHOICE ***********\n");
System.out.println("1. ADD FLIGHT");
System.out.println("2. LIST FLIGHT");
System.out.println("3. FIND FLIGHT");
System.out.println("4. UPDATE FLIGHT");
System.out.println("5. REMOVE FLIGHT");
System.out.println("6. EXIT");
System.out.println("\n******************************\n");
System.out.print("Enter your choice : ");
choice=Integer.parseInt(br1.readLine());
switch(choice){
case 1:
flight=new Flight();
enterFlightDetails(flight);
enterRouteDetails(flight);
flightMngt.addFlight(flight);
break;
case 2:
for(int i=0;i<flightMngt.flightArray.size();i++){
flightMngt.listFlight(flightMngt.flightArray.get(i));
}
if(flightMngt.flightArray.size()==0){
System.out.println("\nNo Flight Details Exists !!!\n");
}
break;
case 3:
System.out.print("\nEnter Flight ID : ");
newFlightId=br1.readLine();
System.out.println("\nFlight Details \n————————\n");
flightMngt.findFlight(newFlightId);
break;
case 4:
System.out.print("\nEnter Flight ID : ");
newFlightId=br1.readLine();
System.out.println("\nOld Flight Details \n————————\n");
flight=flightMngt.findFlight(newFlightId);
flight.routeArray.clear();
enterFlightDetails(flight);
enterRouteDetails(flight);
boolean status=flightMngt.updateFlight(flight);
if(status==true){
System.out.println("\nFlight Details Updated !!!\n");
}
break;
case 5:
System.out.print("\nEnter Flight ID : ");
newFlightId=br1.readLine();
flightMngt.removeFlight(newFlightId);
break;
case 6:
System.out.println("\nThank you for using this program.. Bye\n");
System.exit(-1);
default:
System.out.println("\nPlease enter correct choice.. !!\n");
break;
}
}while(choice!=6);
}
}
Output
D:\a5518\Assignment 8>javac FlightManagement.java
D:\a5518\Assignment 8>java Main
*********** CHOICE ***********
1. ADD FLIGHT
2. LIST FLIGHT
3. FIND FLIGHT
4. UPDATE FLIGHT
5. REMOVE FLIGHT
6. EXIT
******************************
Enter your choice : 1
Enter Flight Details
————————
Enter Flight ID : 1
Enter Flight Name : Spicejet
Enter Flight Origin : Kochi
Enter Flight Destination : Dubai
Enter Flight Date : 12-04-2013
Enter Route Details
————————
Enter Route ID : 10
Enter Route Origin : Kochi
Enter Route Destination : Bangalore
Enter Route Date : 12-04-2013
Do you want to enter more routes (y/n) ? : y
Enter Route Details
————————
Enter Route ID : 11
Enter Route Origin : Bangalore
Enter Route Destination : Dubai
Enter Route Date : 12-04-2013
Do you want to enter more routes (y/n) ? : n
*********** CHOICE ***********
1. ADD FLIGHT
2. LIST FLIGHT
3. FIND FLIGHT
4. UPDATE FLIGHT
5. REMOVE FLIGHT
6. EXIT
******************************
Enter your choice : 2
Flight Details
————————
Flight ID : 1
Flight Name : Spicejet
Flight Origin : Kochi
Flight Destination : Dubai
Flight Date : 12-04-2013
Route Details
————————
ID Origin Destn Date
10 Kochi Bangalore 12-04-2013
11 Bangalore Dubai 12-04-2013
*********** CHOICE ***********
1. ADD FLIGHT
2. LIST FLIGHT
3. FIND FLIGHT
4. UPDATE FLIGHT
5. REMOVE FLIGHT
6. EXIT
******************************
Enter your choice : 3
Enter Flight ID : 1
Flight Details
————————
Flight ID : 1
Flight Name : Spicejet
Flight Origin : Kochi
Flight Destination : Dubai
Flight Date : 12-04-2013
Route Details
————————
ID Origin Destn Date
10 Kochi Bangalore 12-04-2013
11 Bangalore Dubai 12-04-2013
*********** CHOICE ***********
1. ADD FLIGHT
2. LIST FLIGHT
3. FIND FLIGHT
4. UPDATE FLIGHT
5. REMOVE FLIGHT
6. EXIT
******************************
Enter your choice : 6
Thank you for using this program.. Bye
D:\a5518\Assignment 8>