Java Q1 FlightSearchEngine MiniProject Create a FlightManage

Java:

Q1: [FlightSearchEngine Mini-Project] Create a FlightManager class. This class will store instances of the Flight class, and support generating a list of potential itineraries. It should contain one constructor, two instance variables (an array of Flights, and something that counts the number of flights in the system), and four methods (addFlight, increaseSize, findItineraries, shrinkItineraries).

default constructor: will set up the instances variables with zero flights.

addFlight(...): Adds a flight to the flights array. Uses the increaseSize() method if the array is full.

increaseSize(): Doubles the size of the flights array, while keeping whatever is already there. Make this a private method.

findItineraries(...): Searches the flights based on source airport, destination airport, and departure time, to find 1-flight and 2-flight itineraries meeting those criteria. When searching for 2-flight itineraries, checks that the flights have a connecting city, and that the first arrives in time for the second. Your method should create an array of potential itineraries that can be returned. Uses the shrinkItineraries() method to \"clean up\" up the potential itineraries array; see below. (If it helps, you may assume that this method will never find more than 100 itineraries.) Hint: use loops to search for all the flights in the flights variable, check if they meet the criteria, and then create a new Itinerary object if the criteria is met.

shrinkItineraries(...). Takes an array of itineraries, where some of the later indices are unused (null), and returns a new array of itineraries where there are no empty indices. Make this a private method.

Q2: [FlightSearchEngine Mini-Project] Create a FlightSearchEngine class. This class will set up a FlightManager object with flight data, ask the user for flight criteria, and then display the resulting flights (all and cheapest). It should contain one constructor, two instance variables (a FlightManager and a Scanner), and six methods (main, loadFights, runSearch, readAirport, findCheapestItinerary, displayItineraries).

default constructor: will set up the instance variables.

main(...): Entry point for your program that starts everything.

loadFights(): Loads sample flights into the FlightManager. Given in base file.

runSearch(): Performs a search. Prompts the user for source and destination cites, and ideal departure time (hour and minute). Cities should be read using the readAirport() method. The departure time represents the earliest time the user wants to leave. Using findItineraries(...) from FlightManager, generates an array of potential itineraries. If at least one itinerary is found, then display the number of results, all of the itineraries (using displayItineraries), and the cheapest route. If no itineraries are found, then displays \"No itineraries found.\" and finishes.

readAirport(): Prompts the user to select one of four cities (Phoenix, Los Angeles, Tokyo, San Francisco) by number, and returns an enumeration value that matches what the user entered. If the user gives something invalid, should prompt the user to make a new selection.

findCheapestItinerary(...): Searches an array of itineraries, and returns the itinerary with the cheapest total cost.

displayItineraries(...): Loops over an array of itineraries. For each itinerary, print its number (array index plus one), its toString, and two blank lines.

Solution

Flight.java


public class Flight {

   private Itinerary itr;
private int flightNumber;
   public int getFlightNumber() {
       return flightNumber;
   }
   public void setFlightNumber(int flightNumber) {
       this.flightNumber = flightNumber;
   }
   public Flight()
   {
       itr=null;
       flightNumber=0;
   }
   public Itinerary getItr() {
       return itr;
   }

   public void setItr(Itinerary itr) {
       this.itr = itr;
   }
  
}

Itinerary.java


public class Itinerary {
   private String source;
   private String destination;
private   String connCity;
   private String depTime;
   private int cost;
   public Itinerary(String source,String destination,String connCity,String depTime,int cost)
   {
       this.source=source;
       this.destination=destination;
       this.connCity=connCity;
       this.depTime=depTime;
       this.cost=cost;
   }
   public String getSource() {
       return source;
   }
   public void setSource(String source) {
       this.source = source;
   }
   public String getDestination() {
       return destination;
   }
   public void setDestination(String destination) {
       this.destination = destination;
   }
   public String getConnCity() {
       return connCity;
   }
   public void setConnCity(String connCity) {
       this.connCity = connCity;
   }
   public String getDepTime() {
       return depTime;
   }
   public void setDepTime(String depTime) {
       this.depTime = depTime;
   }
   public int getCost() {
       return cost;
   }
   public void setCost(int cost) {
       this.cost = cost;
   }
  
}

FlightManager.java

import java.util.Date;

public class FlightManager {
private Flight[] flights;
static int count=0;
   public FlightManager()
{
   flights=new Flight[0];
}
   public void addFlight(Flight fl)
   {
       if(count==0)
       {
           flights=new Flight[1];
           flights[0]=fl;
           count++;
       }
       else if(flights.length==count)
       {
           increaseSize();
           flights[count]=fl;
           count++;
       }
       else
       {
           flights[count]=fl;
           count++;
       }

   }
   private void increaseSize()
   {
       Flight[] fls=flights;
       flights=new Flight[flights.length*2];
       for(int i=0;i<fls.length;i++)
       {
           flights[i]=fls[i];
       }
      
   }
   public void findItineraries(String source,String destination,String depTime)
   {
       Itinerary[] itrArray=new Itinerary[100];
       Itinerary itr;
       int index=0;
       Date d=new Date();
       int hrsC=Integer.parseInt(d.toString().split(\" \")[3].split(\":\")[0]);
       int hrsF;
      
       for(int i=0;i<count;i++)
       {
           itr=flights[i].getItr();
           hrsF=Integer.parseInt(itr.getDepTime().split(\":\")[0]);
           if(hrsC<hrsF)
           if(itr.getSource().equals(source) && itr.getDestination().equals(destination))
           {
               itrArray[index]=itr;
               index++;
           }
       }
   }
   private void shrinkItineraries()
   {
      
   }
}

FlightSearchEngine.java

import java.util.Date;
import java.util.Scanner;

public class FlightSearchEngine {

   public static enum cities{Phoenix,LosAngeles,Tokyo,SanFrancisco}
   static FlightManager fm=new FlightManager();
   static Scanner read=new Scanner(System.in);
   public FlightSearchEngine()
   {
      
   }
   public static void main(String[] args) {
       // TODO Auto-generated method stub

   }
   public static void loadFlights()
   {
       Flight fl1=new Flight();
       fl1.setFlightNumber(111);
       fl1.setItr(new Itinerary(\"Phoenix\",\"Tokyo\",\"A\",\"7:10\",100));
       fm.addFlight(fl1);
       Flight fl2=new Flight();
       fl2.setFlightNumber(222);
       fl2.setItr(new Itinerary(\"Tokyo\",\"Phoenix\",\"B\",\"8:10\",150));
       fm.addFlight(fl2);
       Flight fl3=new Flight();
       fl3.setFlightNumber(333);
       fl3.setItr(new Itinerary(\"SanFrancisco\",\"LosAngeles\",\"C\",\"12:10\",200));
       fm.addFlight(fl3);
       Flight fl4=new Flight();
       fl4.setFlightNumber(444);
       fl4.setItr(new Itinerary(\"Phoenix\",\"Tokyo\",\"D\",\"6:10\",90));
       fm.addFlight(fl4);
   }
   public static void runSearch()
   {
       String source,destination,depTime;
       System.out.println(\"Source :\");
       source=readAirport();
       System.out.println(\"Destination :\");
       destination=readAirport();
       System.out.println(\"Enter dep time hh:mm :\");
       depTime=read.next();
       fm.findItineraries(source,destination,depTime);
      
   }
   public static String readAirport()
   {
       String city=\"\";
       int selection=0;
       boolean b=true;
       while(b)
       {
           System.out.println(\"Select city\");
           System.out.println(\"Enter 1 for Phoenix\");
           System.out.println(\"Enter 2 for LosAngeles\");
           System.out.println(\"Enter 3 for Tokyo\");
           System.out.println(\"Enter 4 for San Francisco\");
           selection=read.nextInt();
           if(selection>=1 && selection<=4)
           {
               city=cities.values()[selection-1].toString();
               b=false;
           }
           else
               System.out.println(\"Enter correct option\");  
       }
       return city;
   }
   public static void findCheapestItinerary()
   {
      
   }
   public static void displayItineraries()
   {
      
   }

}

Java: Q1: [FlightSearchEngine Mini-Project] Create a FlightManager class. This class will store instances of the Flight class, and support generating a list of
Java: Q1: [FlightSearchEngine Mini-Project] Create a FlightManager class. This class will store instances of the Flight class, and support generating a list of
Java: Q1: [FlightSearchEngine Mini-Project] Create a FlightManager class. This class will store instances of the Flight class, and support generating a list of
Java: Q1: [FlightSearchEngine Mini-Project] Create a FlightManager class. This class will store instances of the Flight class, and support generating a list of
Java: Q1: [FlightSearchEngine Mini-Project] Create a FlightManager class. This class will store instances of the Flight class, and support generating a list of

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site