package reservation.system.functions;
import reservation.system.Flight;
import reservation.system.Pos;
import reservation.system.Person;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.File;
import java.util.Vector;
/**
* This class hold Store structure. This object allows to save in a file the different command generating the content of the database.
* @author Texier Mathieu and Frederic Bidon
*/
public class Store_alternatif extends Functions{
/**Name of the file where we want to save the data.*/
/**
* Verify invariants :
* <PRE>
* - Argument not null
* - The number of argument equals 1
* - File's name doesn't exist
* - The file can be created
* </PRE>
* @throws Exception if the file exists or cannot be created.
*/
CheckFlightListNotEmpty ();
ArgumentIsValid (arg, 1, 1);
if (!datafile.exists ()) {
if (!datafile.createNewFile ())
throw new Exception ("Cannot create the file: " + datafile.
getPath ());
} else
throw new Exception ("file " + datafile.
getPath () + " already exist");
this.datafile = datafile;
}
/**
* Display the usage for the command <CODE>Save</code>.
* @return the usage.
*/
static public String usage
() {
String usage
= " Usage command Save: save <fileName>\n\r";
usage += " - The <fileName> is the path to the file to be save \n\r";
usage +=" The file extention is .rcm .\n";
return usage;
}
/**
* Store in a file the different command generating the content of the database.
* @throws Exception if the file is not saved.
* @return a confimation's message if the file is saved.
*/
String[] listFlight
= fs.
getFlightList ();
Integer[] listBooking
= fs.
getBookingList ();
for(int i = 0; i < listFlight.length; i++) {
Flight flight = fs.selectFlight (listFlight[i]);
String flightName
= flight.
getFlightName ();
Pos dim = flight.getDimension ();
short row = dim.getRow ();
short col = dim.getCol ();
String create
= "create " + flightName
+ " " + row
+ " " + col
;
data.println (create);
}
for (int j = 0; j < fs.getBookingNumberMax (); j++) {
listPerson = fs.identify (listBooking[j].intValue ());
Flight flightPerson = null;
Person personName = null;
for (int k = 0; k < listPerson.size (); k++) {
personName = (Person)listPerson.get (k);
Passengers += personName.getPersonName () + " ";
flightPerson = personName.getFlight ();
}
String reserve
= "reserve " + flightPerson.
getFlightName () + Passengers
;
data.println (reserve);
}
data.close ();
return "File saved";
}
}