Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Implement following java classes with appropriate constructor(s). Each class should override the toString method. in Java

Write TestCabAppointment,java class where you will instantiate new
CabAppointment objects and
read data from RandomAccessFile and create CabAppointment objects and
save them in RandomAccessFile
You may use FixedLengthStringIO,java class, ICabAppointmentRecord.java
interface.
Complete the ReadWriteRandomAccessFile.java



  1. CabAppointment.java (this class extends Appointment class)
  2. private int id;
  3. private Address startAddress;
  4. private Address destinationAddress;
  5. private String terminal;
  6. private String description;


  1. Appointment.java
  2. private Customer customer;
  3. private long appointmentStartDateStamp;
  4. private long appointmentEndDateStamp;
  5. private float payment;
  6. private String paymentMethod;


  1. Customer.java
  2. private String name;
  3. private long phone;
  4. private String email;
  5. private Address address;


  1. Address.java
  2. private int number;
  3. private String street;
  4. private String city;
  5. private String State;
  6. private long zip;


  1. // Complete the following class
  2. public class ReadWriteRandomAccessFile {
  3.  
  4. public static CabAppointment read(RandomAccessFile raf) throws
  5. IOException {
  6. CabAppointment cabApointment = new CabAppointment();
  7. // read fields from the file and popuplate the cabApointment object
  8. cabApointment.setId(raf.readInt());
  9. String name =
  10. FixedLengthStringIO.readFixedLengthString(ICabAppointmentRecord.NAME_LENGTH,
  11. raf);
  12. cabApointment.getCustomer().setName(name);
  13. -------------------------------------------------------
  14. -------------------------------------------------------
  15. -------------------------------------------------------
  16.  
  17. }
  18.  
  19. public static void write(CabAppointment cabApointment, RandomAccessFile
  20. raf) throws IOException {
  21. // write fields from cabApointment object to random access file
  22. raf.writeInt(cabApointment.getId());
  23. FixedLengthStringIO.writeFixedLengthString(cabApointment.getCustomer().getName(),
  24. ICabAppointmentRecord.NAME_LENGTH, raf);
  25. -------------------------------------------------------
  26. -------------------------------------------------------
  27. -------------------------------------------------------
  28.  
  29. }
  30. ===========================================================================================================
  31.  
  32. public interface ICabAppointmentRecord {
  33. String CabAppointmentFile = "CabAppointmentFile.dat";
  34. int NUMBER_LENGTH = 4;
  35. int STREET_LENGTH = 15;
  36. int CITY_LENGTH = 15;
  37. int STATE_LENGTH = 15;
  38. int ZIP_LENGTH = 8;
  39. int ADDRESS_LENGTH = NUMBER_LENGTH + ZIP_LENGTH + (STREET_LENGTH +
  40. CITY_LENGTH + STATE_LENGTH) * 2;
  41. int NAME_LENGTH = 15;
  42. int PHONE_LENGTH = 8;
  43. int EMAIL_LENGTH = 30;
  44. int CUSTOMER_LENGTH = PHONE_LENGTH + (NAME_LENGTH + EMAIL_LENGTH) * 2
  45. + ADDRESS_LENGTH;
  46.  
  47.  
  48. int ID_LENGTH = 4;
  49. int APPOINTMENT_START_DATE_LENGTH = 8;
  50. int APPOINTMENT_END_DATE_LENGTH = 8;
  51. int PAYMENT_LENGTH = 4;
  52. int PAYMENT_METHOD_LENGTH = 12;
  53. int APPOINTMENT_LENGTH = CUSTOMER_LENGTH + ID_LENGTH +
  54. APPOINTMENT_START_DATE_LENGTH +
  55. APPOINTMENT_END_DATE_LENGTH +
  56. PAYMENT_LENGTH + (PAYMENT_METHOD_LENGTH * 2);
  57.  
  58. int TERMINAL_LENGTH = 15;
  59. int DESCRIPTION_LENGTH = 30;
  60.  
  61. int SIZE_OF_CAB_APPOINTMENT = APPOINTMENT_LENGTH + (ADDRESS_LENGTH *
  62. 2) + (TERMINAL_LENGTH +
  63. DESCRIPTION_LENGTH) * 2;
  64. }


  1. import java.io.DataInput;
  2. import java.io.DataOutput;
  3. import java.io.IOException;
  4.  
  5. public class FixedLengthStringIO {
  6.  
  7. /**
  8. * Read fixed number of characters from a DataInput stream
  9. */
  10. public static String readFixedLengthString(int size, DataInput in)
  11. throws IOException {
  12. // Declare an array of characters
  13. char[] chars = new char[size];
  14.  
  15. // Read fixed number of characters to the array
  16. for (int i = 0; i < size; i++) {
  17. chars[i] = in.readChar();
  18. }
  19.  
  20. return new String(chars);
  21. }
  22.  
  23. /**
  24. * Write fixed number of characters to a DataOutput stream
  25. */
  26. public static void writeFixedLengthString(String s, int size,
  27. DataOutput out) throws IOException {
  28. if (s == null || s.length() <= 1) {
  29. s = "NA";
  30. }
  31.  
  32. char[] chars = new char[size];
  33. try {
  34. // Fill in string with characters
  35. s.getChars(0, Math.min( s.length(), size), chars, 0);
  36. } catch (Exception e) {
  37. System.out.println(e);
  38. }
  39. // Fill in blank characters in the rest of the array
  40. for (int i = Math.min(s.length(), size); i < chars.length; i++) {
  41. chars[i] = ' ';
  42. }
  43.  
  44. // Create and write a new string padded with blank characters
  45. out.writeChars(new String(chars));
  46. }
  47. }
  48.