/*
* Reload.java
* The package functions contains the different class executing the different functions of the FlightSystem Reservation.
*/
package reservation.system.functions;
import reservation.system.FlightSystem;
import reservation.system.Flight;
import reservation.system.Pos;
import reservation.system.Person;
import java.io.*;
import java.util.Vector;
/**
* This class hold Reload structure. This object allows to load the data previous saved in a file.
* @author Texier Mathieu and Frederic Bidon
*/
public class Reload extends Functions {
/**Name of the file to reload*/
/**
* Verify invariants :
* <PRE>
* - Argument not null
* - The number of argument equals 1
* - File's name exists
* - The file can be read
* </PRE>
* @throws Exception if the file doesn't exist or cannot read.
*/
ArgumentIsValid (arg, 1, 1);
if (!datafile.exists () || !datafile.canRead () ) {
throw new Exception ("file " + datafile.
getPath () + " does not exist or can not be read");
} else this.datafile = datafile;
}
/**
* Display the usage for the command <CODE>reload</code>.
* @return the usage.
*/
static public String usage
() {
String usage
= " Usage command Reload: reload <fileName>\n\r";
usage += " - The <fileName> is the path to the file to be reloaded \n\r";
usage +=" The file extention is .out .\n";
return usage;
}
/**
* Reload the data previous stored in a file.
* @throws Exception if the file is not reloaded.
* @return a confimation's message if the data are reloaded.
*/
String reString
= "FlightSystem reloaded";
Object obj
= fin.
readObject ();
if (obj instanceof FlightSystem) {
FlightSystem fsExtracted = (FlightSystem) obj;
String[] listFlight
= fsExtracted.
getFlightList ();
int decount = listFlight.length;
for(int i = 0; i < listFlight.length; i++) {
Flight flight = fsExtracted.selectFlight (listFlight[i]);
decount -= fs.create (flight)? 1 : 0;
}
if (decount != 0)
throw new Exception ("Flight list partialy reloaded: " + decount
+ "flight skiped");
Integer[] listBooking
= fsExtracted.
getBookingList ();
for (int j = 0; j < fsExtracted.getBookingNumberMax (); j++) {
Vector listPerson
= fsExtracted.
identify (listBooking
[j
].
intValue ());
decount += listPerson.size();
for (int k = 0; k < listPerson.size (); k++) {
Person person = (Person) listPerson.get (k);
decount -= fs.reserve (person);
}
}
if (decount != 0)
throw new Exception ("Person list partialy reloaded: " + decount
+ "person skiped");
fin.close ();
}
return reString;
}
}