can someone help with fixing my code? Assignment8.java import java.io.*; public
ID: 3864576 • Letter: C
Question
can someone help with fixing my code?
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;
BufferedWriter bw = null;
FileWriter fw = null;
BufferedReader br =null;
FileReader fr = null;
// 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();
System.out.println("Please enter a string to write in the file: ");
String textToWrite = stdin.readLine().trim();
try {
fw = new FileWriter(filename);
bw = new BufferedWriter(fw);
bw.write(textToWrite);
} catch (IOException e) {
e.printStackTrace();
}
break;
case 'V': //Read Text from a File
System.out.print("Please enter a file name to read: ");
filename = stdin.readLine().trim();
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(filename));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
}
break;
case 'W': //Serialize FlightManagement to a File
System.out.print("Please enter a file name to write: ");
filename = stdin.readLine().trim();
try {
FileOutputStream fileOut =
new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(manage1);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in "+filename);
}catch(IOException i) {
i.printStackTrace();
}
break;
case 'X': //Deserialize FlightManagement from a File
System.out.print("Please enter a file name to read: ");
filename = stdin.readLine().trim();
FlightManagement mSer = null;
try {
FileInputStream fileIn = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(fileIn);
mSer = (FlightManagement) in.readObject();
in.close();
fileIn.close();
}catch(IOException i) {
i.printStackTrace();
return;
}catch(ClassNotFoundException c) {
System.out.println("FlightManagement class not found");
c.printStackTrace();
return;
}
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 Action " +
"------ ------ " +
"A Add Flight " +
"C Create FlightManagement " +
"D Search by Flight Number " +
"E Search by Departure " +
"L List Flights " +
"O Sort by Flight Number " +
"P Sort by Departure " +
"Q Quit " +
"R Remove by Flight Number " +
"T Close FlightManagement " +
"U Write Text to File " +
"V Read Text from File " +
"W Serialize FlightManagement to File " +
"X Deserialize FlightManagement from File " +
"? Display Help ");
}
} // end of Assignment8 class
Schedule.java
import java.io.Serializable;
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.city;
this.year = other.year;
this.month = other.month;
this.date = other.date;
this.time = other.time;
}
//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.Comparator;
public class FlightNumberComparator implements Comparator<Flight> {
public int compare(Flight first, Flight second) {
int retVal;
retVal= first.getAirlines().compareTo(second.getAirlines());
if(retVal==0){
String flytNum1 = String.valueOf(first.getFlightNum());
String flytNum2 = String.valueOf(second.getFlightNum());
retVal= flytNum1.compareTo(flytNum2);
}
else{
return retVal;
}
return retVal;
}
}
FlightManagement.java
import java.io.Serializable;
public class FlightManagement implements Serializable {
private Flight[] flightList;
private int currentFlightsCount;
private int maxSize;
public FlightManagement(int maximumsize){
this.maxSize =maximumsize;
flightList = new Flight[maxSize];
for(int i=0; i<this.maxSize ;i++){
flightList[i] = null;
}
currentFlightsCount =0;
}
public int flightNumberExists(String airlines, int flightNumber){
for(int i=0; i<this.maxSize ;i++){
Flight currFlight = this.flightList[i];
if(currFlight.getAirlines().equalsIgnoreCase(airlines) && currFlight.getFlightNum() == flightNumber){
return i;
}
}
return -1;
}
public int departureExists(String city, int year, int month, int date, String time){
for(int i=0; i<this.maxSize ;i++){
Flight currFlight = this.flightList[i];
if(null!=currFlight){
if(city.equalsIgnoreCase(currFlight.getDeparture().getCity())
&& year == currFlight.getDeparture().getYear()
&& month == currFlight.getDeparture().getMonth()
&& date== currFlight.getDeparture().getDate()
&& time.equalsIgnoreCase(currFlight.getDeparture().getTime())){
return i;
}
}
}
return -1;
}
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){
Flight flight = new Flight();
flight.setAirlines(airlines);
flight.setFlightNum(flightNum);
flight.setDeparture(depCity, depYear, depMonth, depDate, depTime);
flight.setArrival(arrCity, arrYear, arrMonth, arrDate, arrTime);
if(departureExists(depCity,depYear,depMonth,depDate,depTime)<0){
if(this.currentFlightsCount != this.maxSize){
flightList[currentFlightsCount] = flight;
this.currentFlightsCount++;
return true;
}
}
return false;
}
boolean removeFlightNumber(String airlines, int flightNumber){
for(int i=0; i<this.maxSize ;i++){
Flight currFlight = this.flightList[i];
if(currFlight.getAirlines().equalsIgnoreCase(airlines) && currFlight.getFlightNum() == flightNumber){
flightList[i] = null;
return true;
}
}
return false;
}
public String listFlights(){
if(currentFlightsCount>0){
for(int i=0; i<this.maxSize ;i++){
Flight currFlight = this.flightList[i];
return currFlight.toString();
}
}
else{
return " no flight ";
}
}
public void closeFlightManagement(){
for(int i=0; i<this.maxSize ;i++){
flightList[i] = null;
}
currentFlightsCount =0;
}
public void sortByFlightNumber(){
Sorts.sort(flightList, maxSize, new FlightNumberComparator());
}
public void sortByDeparture(){
Sorts.sort(flightList, maxSize, new DepartureComparator());
}
public Flight[] getFlightList() {
return flightList;
}
public void setFlightList(Flight[] flightList) {
this.flightList = flightList;
}
public int getCurrentFlightsCount() {
return currentFlightsCount;
}
public void setCurrentFlightsCount(int currentFlightsCount) {
this.currentFlightsCount = currentFlightsCount;
}
public int getMaxSize() {
return maxSize;
}
public void setMaxSize(int maxSize) {
this.maxSize = maxSize;
}
}
Flight.java
import java.io.Serializable;
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.airlines;
this.flightNum = other.flightNum;
this.departure = other.departure;
this.arrival = other.arrival;
}
//This method returns a String containing attribute(variable) values
//of a flight.
public String toString()
{
String result = " Airlines: " + airlines + " " +
"Number: " + flightNum + " " +
"Departure: " + departure.toString() + " " +
"Arrival: " + arrival.toString() + " ";
result += " ";
return result;
}
}
Schedule.java
import java.io.Serializable;
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.city;
this.year = other.year;
this.month = other.month;
this.date = other.date;
this.time = other.time;
}
//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.Comparator;
public class FlightNumberComparator implements Comparator<Flight> {
public int compare(Flight first, Flight second) {
int retVal;
retVal= first.getAirlines().compareTo(second.getAirlines());
if(retVal==0){
String flytNum1 = String.valueOf(first.getFlightNum());
String flytNum2 = String.valueOf(second.getFlightNum());
retVal= flytNum1.compareTo(flytNum2);
}
else{
return retVal;
}
return retVal;
}
}
DepartureComparator.java
import java.util.Comparator;
public class DepartureComparator implements Comparator<Flight> {
public DepartureComparator() {
// TODO Auto-generated constructor stub
}
public int compare(Flight first, Flight second) {
int retVal;
int retVal2;
int retVal3;
int retVal4;
int retVal5;
retVal= first.getDeparture().getCity().compareTo(second.getDeparture().getCity());
if(retVal==0){
String yr1 = String.valueOf(first.getDeparture().getYear());
String yr2 = String.valueOf(second.getDeparture().getYear());
retVal2= yr1.compareTo(yr2);
if(retVal2==0){
String month1 = String.valueOf(first.getDeparture().getMonth());
String month2 = String.valueOf(second.getDeparture().getMonth());
retVal3= month1.compareTo(month2);
if(retVal3 ==0){
String date1 = String.valueOf(first.getDeparture().getDate());
String date2 = String.valueOf(second.getDeparture().getDate());
retVal4= date1.compareTo(date2);
if(retVal4 == 0){
String time1 = String.valueOf(first.getDeparture().getTime());
String time2 = String.valueOf(second.getDeparture().getTime());
return ( time1.compareTo(time2));
}
else{
return retVal4;
}
}
else{
return retVal3;
}
}
else{
return retVal2;
}
}
else{
return retVal;
}
}
}
Sorts.java
import java.util.Comparator;
public class Sorts {
public static void sort(Flight[] flightList, int size, Comparator<Flight> comparator){
Arrays.sort(flightList, comparator);
//comparator.compare(o1, o2)
}
}
Explanation / Answer
there were 2 compilation errors.
1. Sorts.java
Arrays util file was not imported.
2. FlightManagement.java
in listFlights() method.
you had return statements inside if-else blocks. if method has a return type defined, value should be returned outside the if-else block.
i have updated both files. and copying the content of these 2 files only.
-----------------------------------------------------------------------------------------
FlightManagement.java
import java.io.Serializable;
public class FlightManagement implements Serializable {
private Flight[] flightList;
private int currentFlightsCount;
private int maxSize;
public FlightManagement(int maximumsize){
this.maxSize =maximumsize;
flightList = new Flight[maxSize];
for(int i=0; i<this.maxSize ;i++){
flightList[i] = null;
}
currentFlightsCount =0;
}
public int flightNumberExists(String airlines, int flightNumber){
for(int i=0; i<this.maxSize ;i++){
Flight currFlight = this.flightList[i];
if(currFlight.getAirlines().equalsIgnoreCase(airlines) && currFlight.getFlightNum() == flightNumber){
return i;
}
}
return -1;
}
public int departureExists(String city, int year, int month, int date, String time){
for(int i=0; i<this.maxSize ;i++){
Flight currFlight = this.flightList[i];
if(null!=currFlight){
if(city.equalsIgnoreCase(currFlight.getDeparture().getCity())
&& year == currFlight.getDeparture().getYear()
&& month == currFlight.getDeparture().getMonth()
&& date== currFlight.getDeparture().getDate()
&& time.equalsIgnoreCase(currFlight.getDeparture().getTime())){
return i;
}
}
}
return -1;
}
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){
Flight flight = new Flight();
flight.setAirlines(airlines);
flight.setFlightNum(flightNum);
flight.setDeparture(depCity, depYear, depMonth, depDate, depTime);
flight.setArrival(arrCity, arrYear, arrMonth, arrDate, arrTime);
if(departureExists(depCity,depYear,depMonth,depDate,depTime)<0){
if(this.currentFlightsCount != this.maxSize){
flightList[currentFlightsCount] = flight;
this.currentFlightsCount++;
return true;
}
}
return false;
}
boolean removeFlightNumber(String airlines, int flightNumber){
for(int i=0; i<this.maxSize ;i++){
Flight currFlight = this.flightList[i];
if(currFlight.getAirlines().equalsIgnoreCase(airlines) && currFlight.getFlightNum() == flightNumber){
flightList[i] = null;
return true;
}
}
return false;
}
public String listFlights(){
String str="";
if(currentFlightsCount>0){
for(int i=0; i<this.maxSize ;i++){
Flight currFlight = this.flightList[i];
str = currFlight.toString();
}
}
else{
str = " no flight ";
}
return str;
}
public void closeFlightManagement(){
for(int i=0; i<this.maxSize ;i++){
flightList[i] = null;
}
currentFlightsCount =0;
}
public void sortByFlightNumber(){
Sorts.sort(flightList, maxSize, new FlightNumberComparator());
}
public void sortByDeparture(){
Sorts.sort(flightList, maxSize, new DepartureComparator());
}
public Flight[] getFlightList() {
return flightList;
}
public void setFlightList(Flight[] flightList) {
this.flightList = flightList;
}
public int getCurrentFlightsCount() {
return currentFlightsCount;
}
public void setCurrentFlightsCount(int currentFlightsCount) {
this.currentFlightsCount = currentFlightsCount;
}
public int getMaxSize() {
return maxSize;
}
public void setMaxSize(int maxSize) {
this.maxSize = maxSize;
}
}
-----------------------------------------------------------------------------------
Sorts.java
import java.util.Arrays;
import java.util.Comparator;
public class Sorts {
public static void sort(Flight[] flightList, int size, Comparator<Flight> comparator){
Arrays.sort(flightList, comparator);
//comparator.compare(o1, o2)
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.