This assignment is to demonstrate the marshalling of a set of related objects by
ID: 3684793 • Letter: T
Question
This assignment is to demonstrate the marshalling of a set of related objects by overriding the Object toString method. Select a small number of related classes from your travel reservation model (Provided below) and implement the classes, attributes, and setters and getters in Java. Insure the classes are related to on another with appropriate associations (example "customer" inherits from "user" and "has a" travel reservation. Override the toString method to write the state of the object collection out to the console (or file if you choose) in an appropriate readable format. Develop a test "main" method to create an instance of these objects appropriately linked, and write them out to test the toString method's association following capability. Please use the Java programming language
Suggestions: use User, Customer, Travel Reservation, Flight Reservation, Car reservation class.
Explanation / Answer
Required solution:
public class TestMethod {
public static void main( String[] args )
{
//create the customer object which is sub class of user
Customer customer = new Customer();
customer.setFirstName("Raghu");
customer.setLastName("K");
customer.setUserId("100UID");
customer.setCustomerID("101CID");
//demonstartes toString method
System.out.println(customer);
//create the Car CarReservation which is sub class of TravelReservation
CarReservation reservation = new CarReservation();
reservation.setConfirmationNumber("12");
reservation.setStartDate("12-4-2016");
reservation.setEndDate("18-4-2016");
reservation.setStartingMilage("100");
reservation.setEndingMilage("92");
reservation.setPrice("10000USD");
reservation.setStatus("BOooked");
//demonstartes toString method
System.out.println(reservation);
}
}
//Required classes
class User {
String firstName;
String lastName;
String userId;
String password;
String[] addresses;
public String getFirstName()
{
return firstName;
}
public void setFirstName( String firstName )
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName( String lastName )
{
this.lastName = lastName;
}
public String getUserId()
{
return userId;
}
public void setUserId( String userId )
{
this.userId = userId;
}
public String getPassword()
{
return password;
}
public void setPassword( String password )
{
this.password = password;
}
public String[] getAddresses()
{
return addresses;
}
public void setAddresses( String[] addresses )
{
this.addresses = addresses;
}
@Override
public String toString()
{
return "First name:" + firstName + ",LastName:" + lastName + ",UserId:" + userId;
}
}
class Customer extends User {
String customerID;
String[] creditCardNums;
public String getCustomerID()
{
return customerID;
}
public void setCustomerID( String customerID )
{
this.customerID = customerID;
}
@Override
public String toString()
{
return super.toString() + "," + "CustomerID:" + customerID;
}
}
class TravelReservation {
String confirmationNumber;
String price;
String startDate;
String endDate;
String status;
public String getConfirmationNumber()
{
return confirmationNumber;
}
public void setConfirmationNumber( String confirmationNumber )
{
this.confirmationNumber = confirmationNumber;
}
public String getPrice()
{
return price;
}
public void setPrice( String price )
{
this.price = price;
}
public String getStartDate()
{
return startDate;
}
public void setStartDate( String startDate )
{
this.startDate = startDate;
}
public String getEndDate()
{
return endDate;
}
public void setEndDate( String endDate )
{
this.endDate = endDate;
}
public String getStatus()
{
return status;
}
public void setStatus( String status )
{
this.status = status;
}
@Override
public String toString()
{
return "Confirmation Number:" + confirmationNumber + ",price=" + price + ",start date:" + startDate
+ ",end date:" + endDate + ",status:" + status;
}
}
class CarReservation extends TravelReservation {
String startingMilage;
String endingMilage;
public String getStartingMilage()
{
return startingMilage;
}
public void setStartingMilage( String startingMilage )
{
this.startingMilage = startingMilage;
}
public String getEndingMilage()
{
return endingMilage;
}
public void setEndingMilage( String endingMilage )
{
this.endingMilage = endingMilage;
}
@Override
public String toString()
{
return super.toString() + ",starting mileage:" + startingMilage + ",ending mileage"" + endingMilage;
}
}
class FlightReservation extends TravelReservation {
String flightStatus;
public String getFlightStatus()
{
return flightStatus;
}
public void setFlightStatus( String flightStatus )
{
this.flightStatus = flightStatus;
}
@Override
public String toString()
{
return super.toString() + ",Flight Status:" + flightStatus;
}
}
Sample output:
First name:Raghu,LastName:K,UserId:100UID,CustomerID:101CConfirmation Number:12,price=10000USD,start date:12-4-2016,end date:18-4-2016,status:BOooked,starting mileage:100,ending mileage:92
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.