CSE205 Assignment #8 ASU According to the following code, please help me figure
ID: 3802115 • Letter: C
Question
CSE205 Assignment #8 ASU
According to the following code, please help me figure out the Code that needed to add in Assignment8 file for case U V W X!!!
Flight.java
package flight;
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.getAirlines();
this.flightNum = other.getFlightNum();
this.departure = other.getDeparture();
this.arrival = other.getArrival();
}
// 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;
}
} // end of Flight class
Schedule.java
package flight;
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.getCity();
this.time = other.getTime();
this.year = other.getYear();
this.month = other.getMonth();
this.date = other.getDate();
}
// 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;
}
}
DepartureComparator.java
package flight;
import java.util.Comparator;
public class DepartureComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Flight f1 = (Flight)o1;
Flight f2 = (Flight)o2;
Schedule s1 = f1.getDeparture();
Schedule s2 = f2.getDeparture();
int cityComp = s1.getCity().compareTo(s2.getCity());
if(cityComp == 0){
if(s1.getYear() < s2.getYear()){
return -1;
}
else if(s1.getYear() > s2.getYear()){
return 1;
}
else{
if(s1.getMonth() < s2.getMonth()){
return -1;
}
else if(s1.getMonth() > s2.getMonth()){
return 1;
}
else{
if(s1.getDate() < s2.getDate()){
return -1;
}
else if(s1.getDate() > s2.getDate()){
return 1;
}
else{
return s1.getTime().compareTo(s2.getTime());
}
}
}
}
return cityComp;
}
}
FlightnumberComparator.java
package flight;
import java.util.Comparator;
public class FlightNumberComparator implements Comparator {
@Override
public int compare(Object arg0, Object arg1) {
Flight f1 = (Flight) arg0;
Flight f2 = (Flight) arg1;
int comp = f1.getAirlines().compareTo(f2.getAirlines());
if(comp == 0){
if(f1.getFlightNum() < f2.getFlightNum()){
return -1;
}
else if(f1.getFlightNum() > f2.getFlightNum()){
return 1;
}
else{
return 0;
}
}
return comp;
}
}
Sorts.java
package flight;
import java.util.Comparator;
public class Sorts {
public static void sort(Flight[] flightList, int size, Comparator<Flight> comp) {
}
}
FlightManagement.java
package flight;
import java.io.Serializable;
import java.util.Arrays;
public class FlightManagement implements Serializable {
private Flight[] flightList;
private int currentFlightsCount;
private int maxSize;
public FlightManagement(int maxSize) {
this.maxSize = maxSize;
this.currentFlightsCount = 0;
flightList = new Flight[maxSize];
for (int i = 0; i < maxSize; i++) {
flightList[i] = null;
}
}
public int flightNumberExists(String airlines, int flightNumber) {
for (int i = 0; i < currentFlightsCount; i++) {
if (flightList[i].getAirlines().equals(airlines) && flightList[i].getFlightNum() == flightNumber) {
return i;
}
}
return -1;
}
public int departureExists(String city, int year, int month, int date, String time) {
Schedule departure;
for (int i = 0; i < currentFlightsCount; i++) {
departure = flightList[i].getDeparture();
if (departure.getCity().equals(city) && departure.getYear() == year && departure.getMonth() == month
&& departure.getDate() == date && departure.getTime().equals(time)) {
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) {
boolean result = true;
if (currentFlightsCount == maxSize || flightNumberExists(airlines, flightNum) != -1
|| departureExists(depCity, depYear, depMonth, depDate, depTime) != -1) {
result = false;
}
else{
Flight flight = new Flight();
flight.setAirlines(airlines);
flight.setFlightNum(flightNum);
flight.setDeparture(depCity, depYear, depMonth, depDate, depTime);
flight.setArrival(arrCity, arrYear, arrMonth, arrDate, arrTime);
flightList[currentFlightsCount++] = flight;
result = true;
}
return result;
}
public boolean removeFlightNumber(String airlines, int flightNumber) {
int index = flightNumberExists(airlines, flightNumber);
if(index !=-1){
for(int i=index;i<currentFlightsCount;i++){
flightList[i] = flightList[i+1];
}
currentFlightsCount--;
return true;
}
return false;
}
@SuppressWarnings("unchecked")
public void sortByFlightNumber() {
Arrays.sort(flightList, 0, currentFlightsCount, new FlightNumberComparator());
}
public void sortByDeparture() {
Arrays.sort(flightList, 0, currentFlightsCount, new DepartureComparator());
}
public String listFlights() {
if(currentFlightsCount == 0){
return " no flight ";
}
String newStr = "";
for(int i=0;i<currentFlightsCount;i++){
newStr += flightList[i].toString();
}
return newStr;
}
public void closeFlightManagement() {
for (int i = 0; i < currentFlightsCount; i++) {
flightList[i] = null;
}
this.currentFlightsCount = 0;
}
}
Assignment8.java
package flight;
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 bject 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 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 ");
}
}
Note: Please note that 4 methods are implemented. It is really not enough to implement all the mentioned methods within the given time. All other or implemented and working fine. Thanks for your understanding!!
Output:
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
Please enter a maximum number of flights
4
What action would you like to perform?
A
Please enter a flight to add:
Please enter its airlines to add:
AIR-ASIA
Please enter its flight number to add:
100
Please enter its departure city to add:
CHENNAI
Please enter its departure year to add:
2017
Please enter its departure month to add:
0321
Please enter its departure date to add:
21
Please enter its departure time to add:
123
Please enter its arrival city to add:
MUMBAI
Please enter its arrival year to add:
2017
Please enter its arrival month to add:
03
Please enter its arrival date to add:
23
Please enter its arrival time to add:
23:00
flight added
What action would you like to perform?
Explanation / Answer
Case 'U':
BufferedWriter bufferedWriter = null;
try{
System.out.print("Please enter a file name to write: ");
filename = stdin.readLine().trim();
string content= "Hi, this is an example of writing text into file";
if(!filename.exists())
{
filename.createNewFile();
}
Writer writer = new FileWriter(myFile);
bufferedWriter = new BufferedWriter(writer);
bufferedWriter.write(content);
}
catch (IOException e) {
e.printStackTrace();
}
finally{
try{
if(bufferedWriter != null) bufferedWriter.close();
} catch(Exception ex){
}
}
break;
Case 'V':
BufferedWriter bufferedWriter = null;
try{
System.out.print("Please enter a file name to read: ");
filename = stdin.readLine().trim();
FileReader reader=new FileReader(filename);
int character;
while ((character = reader.read()) != -1) {
System.out.print((char) character);
}
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
break;
Case 'W':
try{
System.out.print("Please enter a file name to write: ");
filename = stdin.readLine().trim();
FileOutputStream fileOut = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(emp);
out.close();
fileOut.close();
}
catch (IOException e) {
e.printStackTrace();
}
break;
Case 'X'
try{
System.out.print("Please enter a file name to read: ");
filename = stdin.readLine().trim();
FileInputStream fileIn = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(fileIn);
emp = (Employee) in.readObject();
in.close();
fileIn.close();
}
catch (IOException e) {
e.printStackTrace();
}
break;