- CabAppointment.java (this class extends Appointment class)
- private int id;
- private Address startAddress;
- private Address destinationAddress;
- private String terminal;
- private String description;
- Appointment.java
- private Customer customer;
- private long appointmentStartDateStamp;
- private long appointmentEndDateStamp;
- private float payment;
- private String paymentMethod;
- Customer.java
- private String name;
- private long phone;
- private String email;
- private Address address;
- Address.java
- private int number;
- private String street;
- private String city;
- private String State;
- private long zip;
- // Complete the following class
- public class ReadWriteRandomAccessFile {
- public static CabAppointment read(RandomAccessFile raf) throws
- IOException {
- CabAppointment cabApointment = new CabAppointment();
- // read fields from the file and popuplate the cabApointment object
- cabApointment.setId(raf.readInt());
- String name =
- FixedLengthStringIO.readFixedLengthString(ICabAppointmentRecord.NAME_LENGTH,
- raf);
- cabApointment.getCustomer().setName(name);
- -------------------------------------------------------
- -------------------------------------------------------
- -------------------------------------------------------
- }
- public static void write(CabAppointment cabApointment, RandomAccessFile
- raf) throws IOException {
- // write fields from cabApointment object to random access file
- raf.writeInt(cabApointment.getId());
- FixedLengthStringIO.writeFixedLengthString(cabApointment.getCustomer().getName(),
- ICabAppointmentRecord.NAME_LENGTH, raf);
- -------------------------------------------------------
- -------------------------------------------------------
- -------------------------------------------------------
- }
- ===========================================================================================================
- public interface ICabAppointmentRecord {
- String CabAppointmentFile = "CabAppointmentFile.dat";
- int NUMBER_LENGTH = 4;
- int STREET_LENGTH = 15;
- int CITY_LENGTH = 15;
- int STATE_LENGTH = 15;
- int ZIP_LENGTH = 8;
- int ADDRESS_LENGTH = NUMBER_LENGTH + ZIP_LENGTH + (STREET_LENGTH +
- CITY_LENGTH + STATE_LENGTH) * 2;
- int NAME_LENGTH = 15;
- int PHONE_LENGTH = 8;
- int EMAIL_LENGTH = 30;
- int CUSTOMER_LENGTH = PHONE_LENGTH + (NAME_LENGTH + EMAIL_LENGTH) * 2
- + ADDRESS_LENGTH;
- int ID_LENGTH = 4;
- int APPOINTMENT_START_DATE_LENGTH = 8;
- int APPOINTMENT_END_DATE_LENGTH = 8;
- int PAYMENT_LENGTH = 4;
- int PAYMENT_METHOD_LENGTH = 12;
- int APPOINTMENT_LENGTH = CUSTOMER_LENGTH + ID_LENGTH +
- APPOINTMENT_START_DATE_LENGTH +
- APPOINTMENT_END_DATE_LENGTH +
- PAYMENT_LENGTH + (PAYMENT_METHOD_LENGTH * 2);
- int TERMINAL_LENGTH = 15;
- int DESCRIPTION_LENGTH = 30;
- int SIZE_OF_CAB_APPOINTMENT = APPOINTMENT_LENGTH + (ADDRESS_LENGTH *
- 2) + (TERMINAL_LENGTH +
- DESCRIPTION_LENGTH) * 2;
- }
- import java.io.DataInput;
- import java.io.DataOutput;
- import java.io.IOException;
- public class FixedLengthStringIO {
- /**
- * Read fixed number of characters from a DataInput stream
- */
- public static String readFixedLengthString(int size, DataInput in)
- throws IOException {
- // Declare an array of characters
- char[] chars = new char[size];
- // Read fixed number of characters to the array
- for (int i = 0; i < size; i++) {
- chars[i] = in.readChar();
- }
- return new String(chars);
- }
- /**
- * Write fixed number of characters to a DataOutput stream
- */
- public static void writeFixedLengthString(String s, int size,
- DataOutput out) throws IOException {
- if (s == null || s.length() <= 1) {
- s = "NA";
- }
- char[] chars = new char[size];
- try {
- // Fill in string with characters
- s.getChars(0, Math.min( s.length(), size), chars, 0);
- } catch (Exception e) {
- System.out.println(e);
- }
- // Fill in blank characters in the rest of the array
- for (int i = Math.min(s.length(), size); i < chars.length; i++) {
- chars[i] = ' ';
- }
- // Create and write a new string padded with blank characters
- out.writeChars(new String(chars));
- }
- }