Thread View: aus.computers.java
1 messages
1 total messages
Started by Mike
Thu, 04 May 2000 00:00
Object serialization and deserialization of Airplane ?
Author: Mike
Date: Thu, 04 May 2000 00:00
Date: Thu, 04 May 2000 00:00
303 lines
7677 bytes
7677 bytes
Their are a few classes: Airplane, SeatingClass, Seat, and Passenger. Pretty
much everything else seemed to be attributes of these four classes.
Airplane knows it two seating classes and provides upper level assistance
for the assignment of seats and printing of the passenger manifest.
SeatingClass creates and manages Seats. It can find an unassigned seat of
the supplied seating preference and collaborates in printing the manifest.
Seat knows its occupant of Passenger. It provides a reserve() method for
establishing this relationship along with the price charged for the booking.
The date of the booking is assumed to be the current date. It collaborates
in printing the manifest by providing a toString() implementation
appropriate to the task.
Passenger is the occupant. It knows first and last name and provides a
toString() method that can be used for adding this detail to the manifest.
These four classes with initial implementation appear below. The final class
is the TicketAgent which is where most of my implementation remains to be
done. The text prompts, gathering and parsing user input, and acting upon
the input need to be added. Object serialization and deserialization becomes
fairly simple because all the objects are related to Airplane:
Airplane -- has 2 -- SeatingClass -- has many -- Seat -- has 1 -- Passenger
All the classes implement Serializable so all I have to do is serialize
Airplane to a file and i am done. Serialization takes care of traversing the
network of objects and getting them all written. I think Date and String are
the only jdk objects I used and these both implement Serializable.
Deserialize from the file when the program starts and can work with the
objects in memory.
Note that creating a new Airplane creates all the necessary objects except
the Passenger objects. These need to be created just prior to reserving a
seat as shown in TicketAgent.reserveSeat().
I hope that I can get some assistance.
---------- Airplane.java ----------
import java.io.Serializable;
public class Airplane implements Serializable
private SeatingClass firstClass;
private SeatingClass economyClass;
public Airplane()
// create seating classes of fixed size
economyClass = new SeatingClass(10, 6);
firstClass = new SeatingClass(5, 4);
}
public Seat assignSeat(int seatingClass, int preference)
if (seatingClass == SeatingClass.FIRST)
return firstClass.assignSeat(preference);
} else if (seatingClass == SeatingClass.ECONOMY)
return economyClass.assignSeat(preference);
} else
return null;
}
}
public void printManifest()
System.out.println("First class:");
firstClass.printManifest();
System.out.println("Economy class:");
economyClass.printManifest();
}
}
---------- SeatingClass.java ----------
import java.io.Serializable;
public class SeatingClass implements Serializable
public static final int FIRST = 1;
public static final int ECONOMY = 2;
private Seat[][] seats;
public SeatingClass(int rows, int seatsPerRow)
// Technically, we should require seatsPerRow to be even.
// Assume that it is for this assignment
seats = new Seat[rows][seatsPerRow];
for (int i = 0; i < rows; ++i)
seats[i][0] = new Seat(Seat.WINDOW);
for (int j = 1; j < seatsPerRow - 1; ++j)
boolean aisle = (j == (seatsPerRow / 2 - 1) || j == (seatsPerRow / 2 + 1));
seats[i][j] = new Seat(aisle ? Seat.AISLE : Seat.CENTER);
}
seats[i][seatsPerRow - 1] = new Seat(Seat.WINDOW);
}
}
public Seat assignSeat(int preference)
for (int i = 0; i < seats.length; ++i)
for (int j = 0; j < seats[0].length; ++j)
Seat seat = seats[i][j];
if (seat.isPosition(preference) && seat.isReserved() == false)
// Found an unoccupied seat matching the stated preference
return seats[i][j];
}
}
}
// No qualifying unoccupied seats found
return null;
}
public void printManifest()
for (int i = 0; i < seats.length; ++i)
for (int j = 0; j < seats[0].length; ++j)
Seat seat = seats[i][j];
// This needs a lot of work!!
System.out.println(i + "-" + j + ": " + seat.toString());
}
}
}
}
---------- Seat.java ----------
import java.io.Serializable;
import java.util.Date;
public class Seat implements Serializable
// Seating positions
public final static int WINDOW = 1;
public final static int CENTER = 2;
public final static int AISLE = 3;
private Passenger passenger;
private Date bookingDate;
private float ticketPrice;
private int position;
public Seat(int position)
this.position = position;
}
public boolean isReserved()
return passenger != null;
}
public void reserve(Passenger passenger, float price)
this.passenger = passenger;
ticketPrice = price;
bookingDate = new Date();
}
public Passenger getPassenger()
return passenger;
}
public Date getBookingDate()
return bookingDate;
}
public float getTicketPrice()
return ticketPrice;
}
public boolean isPosition(int preference)
return position == preference;
}
public String toString()
if (isReserved() == false)
return "unoccupied";
}
return passenger.toString();
}
}
---------- Passenger.java ----------
import java.io.Serializable;
public class Passenger implements Serializable
private String lastName;
private String firstName;
public Passenger(String firstName, String lastName)
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName()
return firstName;
}
public String getLastName()
return lastName;
}
public String toString()
return lastName + ", " + firstName;
}
}
---------- TicketAgent.java ----------
public class TicketAgent
Airplane airplane;
public TicketAgent()
airplane = new Airplane();
}
public void selectFlight(String[] args)
// Deserialize airplane from serialized file
// Maybe take name of file from args?
// Sets the airplane instance variable
}
public void interact()
// Loop asking for next choice: reserve seat, print manifest, quit
// Method doesn't return until quit is chosen
System.out.println("What do you want to do today?");
printManifest();
}
public void updateFlight(String[] args)
// Serialize airplane to file
// Maybe take name of file from args?
}
public void reserveSeat()
int seatingClass = SeatingClass.FIRST;
int seatingPreference = Seat.AISLE;
// Ask for class and seating preference
Seat assignment = airplane.assignSeat(seatingClass, seatingPreference);
if (assignment == null)
// apologize because no seats matching the preference
} else
// ask for passenger name
// set the price for the seat?
// reserve this seat
Passenger passenger = new Passenger("John", "Doe");
float price = 1000.0f;
assignment.reserve(passenger, price);
}
}
public void printManifest()
airplane.printManifest();
}
public static void main(String[] args)
TicketAgent agent = new TicketAgent();
agent.selectFlight(args);
agent.interact();
agent.updateFlight(args);
}
}
---------- end ----------
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads