Question: There is some problem with the FlightManagement.java and assignment8.java file which gives a null pointer exception and fails the test cases please fix it so
There is some problem with the FlightManagement.java and assignment8.java file which gives a null pointer exception and fails the test cases please fix it so that the test cases pass
Assignment8.java
import java.io.*;
public class Assignment8 { public static void main (String[] args) { char input1; String airlines, depCity, depTime, arrCity, arrTime; String flightNumStr, depYearStr, depMonthStr, depDateStr, arrYearStr, arrMonthStr, arrDateStr;; int flightNum, depYear, depMonth, depDate, arrYear, arrMonth, arrDate; boolean operation = false; int operation2 = 0; String line; String filename;
// create a FlightManagement object. This is used throughout this class. FlightManagement manage1 = null;
try { // print out the menu printMenu();
// create a BufferedReader object to read input from a keyboard InputStreamReader isr = new InputStreamReader (System.in); BufferedReader stdin = new BufferedReader (isr);
System.out.print("Please enter a maximum number of flights "); String maxStr = stdin.readLine().trim(); //read in the max number as a string int max = Integer.parseInt(maxStr); manage1 = new FlightManagement(max);
do { System.out.print("What action would you like to perform? "); line = stdin.readLine().trim(); //read a line input1 = line.charAt(0); input1 = Character.toUpperCase(input1);
if (line.length() == 1) //check if a user entered only one character { switch (input1) {
case 'A': //Add Flight System.out.print("Please enter a flight to add: "); System.out.print("Please enter its airlines to add: "); airlines = stdin.readLine().trim(); System.out.print("Please enter its flight number to add: "); flightNumStr = stdin.readLine().trim(); flightNum = Integer.parseInt(flightNumStr);
System.out.print("Please enter its departure city to add: "); depCity = stdin.readLine().trim(); System.out.print("Please enter its departure year to add: "); depYearStr = stdin.readLine().trim(); depYear = Integer.parseInt(depYearStr); System.out.print("Please enter its departure month to add: "); depMonthStr = stdin.readLine().trim(); depMonth = Integer.parseInt(depMonthStr); System.out.print("Please enter its departure date to add: "); depDateStr = stdin.readLine().trim(); depDate = Integer.parseInt(depDateStr); System.out.print("Please enter its departure time to add: "); depTime = stdin.readLine().trim();
System.out.print("Please enter its arrival city to add: "); arrCity = stdin.readLine().trim(); System.out.print("Please enter its arrival year to add: "); arrYearStr = stdin.readLine().trim(); arrYear = Integer.parseInt(arrYearStr); System.out.print("Please enter its arrival month to add: "); arrMonthStr = stdin.readLine().trim(); arrMonth = Integer.parseInt(arrMonthStr); System.out.print("Please enter its arrival date to add: "); arrDateStr = stdin.readLine().trim(); arrDate = Integer.parseInt(arrDateStr); System.out.print("Please enter its arrival time to add: "); arrTime = stdin.readLine().trim();
operation = manage1.addFlight(airlines, flightNum, depCity, depYear, depMonth, depDate, depTime, arrCity, arrYear, arrMonth, arrDate, arrTime); if (operation == true) System.out.print("flight added "); else System.out.print("flight not added "); break;
case 'C': //Create a new flight management System.out.print("Please enter a new maximum number of flights: "); maxStr = stdin.readLine().trim(); //read in the max number as a string max = Integer.parseInt(maxStr); manage1 = new FlightManagement(max); break;
case 'D': //Search by flight number System.out.print("Please enter Airlines to search: "); airlines = stdin.readLine().trim(); System.out.print("Please enter flight number to search: "); flightNumStr = stdin.readLine().trim(); flightNum = Integer.parseInt(flightNumStr); operation2=manage1.flightNumberExists(airlines, flightNum);
if (operation2 > -1) System.out.print("flight number " + airlines + flightNum + " found "); else System.out.print("flight number " + airlines + flightNum + " not found "); break;
case 'E': //Search by departure System.out.print("Please enter departure city to search: "); depCity = stdin.readLine().trim(); System.out.print("Please enter its departure year to search: "); depYearStr = stdin.readLine().trim(); depYear = Integer.parseInt(depYearStr); System.out.print("Please enter its departure month to search: "); depMonthStr = stdin.readLine().trim(); depMonth = Integer.parseInt(depMonthStr); System.out.print("Please enter its departure date to search: "); depDateStr = stdin.readLine().trim(); depDate = Integer.parseInt(depDateStr); System.out.print("Please enter its departure time to search: "); depTime = stdin.readLine().trim(); operation2=manage1.departureExists(depCity, depYear, depMonth, depDate, depTime);
if (operation2 > -1) { System.out.print("flight departure " + depCity + "," + depMonth + "-" + depDate + "-" + depYear +"," + depTime + " found "); } else { System.out.print("flight departure " + depCity + "," + depMonth + "-" + depDate + "-" + depYear +"," + depTime + " not found ");
} break;
case 'L': //List flights System.out.print(manage1.listFlights()); break;
case 'O': // Sort by flight numbers manage1.sortByFlightNumber(); System.out.print("sorted by flight numbers "); break;
case 'P': // Sort by departure information manage1.sortByDeparture(); System.out.print("sorted by departures "); break;
case 'Q': //Quit break;
case 'R': //Remove by flight number System.out.print("Please enter Airlines to remove: "); airlines = stdin.readLine().trim(); System.out.print("Please enter flight number to remove: "); flightNumStr = stdin.readLine().trim(); flightNum = Integer.parseInt(flightNumStr); operation=manage1.removeFlightNumber(airlines, flightNum); if (operation == true) System.out.print("flight number " + airlines + flightNum + " removed "); else System.out.print("flight number " + airlines + flightNum + " not found "); break;
case 'T': //Close FlightManagement manage1.closeFlightManagement(); System.out.print("flight management system closed "); break; case 'U': //Write Text to a File System.out.print("Please enter a file name to write: "); filename = stdin.readLine().trim(); /************************************************************************************ *** ADD your code to write a text (string) to the specified file. Catch exceptions. ************************************************************************************/ break; case 'V': //Read Text from a File System.out.print("Please enter a file name to read: "); filename = stdin.readLine().trim(); /************************************************************************************ *** ADD your code to read a text (string) from the specified file. Catch exceptions. ************************************************************************************/ break; case 'W': //Serialize FlightManagement to a File System.out.print("Please enter a file name to write: "); filename = stdin.readLine().trim(); /************************************************************************************ *** ADD your code to write the flight management object to the specified file. Catch exceptions. ************************************************************************************/ break; case 'X': //Deserialize FlightManagement from a File System.out.print("Please enter a file name to read: "); filename = stdin.readLine().trim(); /************************************************************************************ *** ADD your code to read a flight management object from the specified file. Catch exception. ***********************************************************************************/ break; case '?': //Display Menu printMenu(); break; default: System.out.print("Unknown action "); break; } } else { System.out.print("Unknown action "); } } while (input1 != 'Q' || line.length() != 1); } catch (IOException exception) { System.out.print("IO Exception "); } }
/** The method printMenu displays the menu to a user **/ public static void printMenu() { System.out.print("Choice\t\tAction " + "------\t\t------ " + "A\t\tAdd Flight " + "C\t\tCreate FlightManagement " + "D\t\tSearch by Flight Number " + "E\t\tSearch by Departure " + "L\t\tList Flights " + "O\t\tSort by Flight Number " + "P\t\tSort by Departure " + "Q\t\tQuit " + "R\t\tRemove by Flight Number " + "T\t\tClose FlightManagement " + "U\t\tWrite Text to File " + "V\t\tRead Text from File " + "W\t\tSerialize FlightManagement to File " + "X\t\tDeserialize FlightManagement from File " + "?\t\tDisplay Help "); } } // end of Assignment8 class
FlightManagement.java
import java.io.*;
public class FlightManagement implements Serializable{ private Flight[] flightList; private int currentFlightsCount; private int maxSize;
public FlightManagement(int maximumSize){ maxSize = maximumSize; flightList = new Flight[maxSize]; for (int i=0; i public int flightNumberExists(String airlines, int flightNumber){ int val=-1; if (flightList.length<2){ return val; } else{ for (int i=0;i public int departureExists(String city, int year, int month, int day, String time){ int value = 0; boolean a,b,c,d,e; for (int i=0;i public boolean addFlight(String airlines,int flightNum, String depCity,int depYear,int depMonth,int depDate,String depTime, String arrCity,int arrYear,int arrMonth,int arrDate,String arrTime){ int numCheck, depCheck; numCheck = flightNumberExists(airlines, flightNum); depCheck = departureExists(depCity,depYear,depMonth,depDate,depTime); if (numCheck>0 || depCheck>0 || currentFlightsCount<=maxSize){ Flight flight = new Flight(); flight.setAirlines(airlines); flight.setFlightNum(flightNum); flight.setDeparture(depCity,depYear,depMonth,depDate,depTime); flight.setArrival(arrCity,arrYear,arrMonth,arrDate,arrTime); currentFlightsCount++; return true; } else{ return false; } } public boolean removeFlightNumber(String airlines, int flightNumber){ int index = flightNumberExists(airlines, flightNumber); if (index>0){ return true; } else{ return false; } } public void sortByFlightNumber(){ Sorts.sort(flightList,currentFlightsCount,new FlightNumberComparator()); } public void sortByDeparture(){ Sorts.sort(flightList, currentFlightsCount, new DepartureComparator()); } public String listFlights(){ String returnString = ""; if (flightList.length==0){ return " no flight "; } else{ for (int i=0;i Flight.java import java.io.*; public class Flight implements Serializable { private String airlines; private int flightNum; private Schedule departure; private Schedule arrival; //Constructor method to initialize each variable. public Flight() { airlines = new String("?"); flightNum = 0; departure = new Schedule(); arrival = new Schedule(); } //Accessor methods public String getAirlines() { return airlines; } public int getFlightNum() { return flightNum; } public Schedule getDeparture() { return departure; } public Schedule getArrival() { return arrival; } //Mutator methods public void setAirlines(String airlinesName ) { airlines = airlinesName; } public void setFlightNum(int fNumber) { flightNum = fNumber; } public void setDeparture(String someCity, int someYear, int someMonth, int someDate, String someTime) { departure.setCity(someCity); departure.setYear(someYear); departure.setMonth(someMonth); departure.setDate(someDate); departure.setTime(someTime); } public void setArrival(String someCity, int someYear, int someMonth, int someDate, String someTime) { arrival.setCity(someCity); arrival.setYear(someYear); arrival.setMonth(someMonth); arrival.setDate(someDate); arrival.setTime(someTime); } /************************************* Define a copy method that copies every member variable value from "other" parameter to their corresponding variable of the object itself using "this" reference **************************************/ public void copy(Flight other) { this.airlines=other.getAirlines(); this.flightNum=other.getFlightNum(); } //This method returns a String containing attribute(variable) values //of a flight. public String toString() { String result = " Airlines:\t" + airlines + " " + "Number:\t\t" + flightNum + " " + "Departure:\t" + departure.toString() + " " + "Arrival:\t" + arrival.toString() + " "; result += " "; return result; } } Schedule.java import java.io.*; public class Schedule implements Serializable { private String city; private int year; private int month; private int date; private String time; //Constructor method to initialize intance variables. public Schedule() { city = new String("?"); time= new String("?"); year = 0; month = 0; date = 0; } //Accessor methods public String getCity() { return city; } public String getTime() { return time; } public int getYear() { return year; } public int getMonth() { return month; } public int getDate() { return date; } //Modifier methods public void setCity(String place) { city = place; } public void setTime(String someTime) { time = someTime; } public void setDate(int someDate) { date = someDate; } public void setYear(int someYear) { year = someYear; } public void setMonth(int someMonth) { month = someMonth; } /************************************* Define a copy method that copies every member variable value from "other" parameter to their corresponding variable of the object itself using "this" reference **************************************/ public void copy(Schedule other) { this.city=other.getCity(); this.year=other.getYear(); this.month=other.getMonth(); this.date=other.getDate(); this.time=other.getTime(); //To be defined } //This method return a string containing the attribute information in //departure or arrival. public String toString() { String result; result = city + "," + month + "-" + date + "-" + year + "," + time; return result; } } FlightNumberComparator.java import java.util.*; public class FlightNumberComparator implements Comparator Sorts.java import java.util.*; public class Sorts{ public static void sort(Flight[] flightList,int size, Comparator Flight temp = flightList[j]; flightList[j]=flightList[j-1]; flightList[j-1] = temp; } } } } DepartureComparator.java import java.util.*; public class DepartureComparator implements Comparator
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
