In: Computer Science
Exercise3
Given the following classes:
The Holiday class has:
The TravelAgent class has:
(RunTravelAgent.java
public class RunTravelAgent { public static void main(String[] args) { Holiday h1 = new Holiday("Bermuda", 2, 800); Holiday h2 = new Holiday("Hull", 14, 8); Holiday h3 = new Holiday("Los Angeles", 12, 2100); TravelAgent t1 = new TravelAgent("CheapAsChips", "MA99 1CU"); t1.addHoliday(h1); t1.addHoliday(h2); t1.addHoliday(h3); TravelAgent t2 = new TravelAgent("Shoe String Tours", "CO33 2DX"); System.out.println(t1); System.out.printf("h3 Duration=%s days & Cost=$%s\n", h3.getDuration(), h3.getCost()); System.out.printf("t2 %s %s\n", t2.getName(), t2.getPostcode()); } }
)
Implement these two classes. You are supplied with a test file called RunTravelAgent.java which should produce this output:
CheapAsChips at MA99 1CU
Holiday{destination=Bermuda, duration=2 days, cost=$800}
Holiday{destination=Hull, duration=14 days, cost=$8}
Holiday{destination=Los Angeles, duration=12 days, cost=$2100}
h3 Duration=$12 & Cost =$2100
t2 Shoe String Tours CO33 2DX
Hi,
Please find below code as per your requirement.
I didn't modify RunTravelAgent.java class so not posting again.
Let me know if you have any concern/doubt in this answer via comments.
Hope this answer helps you.
Thanks.
/*************************JAVA CODE**************************/
/**************Holiday.java***************/
public class Holiday {
//instance variables
private String destination;
private int duration;
private int cost;
/**
* This is default constructor which construct Holiday
object with default values.
*/
public Holiday() {
}
/**
* Parameterized constructor which takes 3 parameter to
construct Holiday object
* @param destination
* @param duration
* @param cost
*/
public Holiday(String destination, int duration, int
cost) {
this.destination =
destination;
this.duration = duration;
this.cost = cost;
}
/**
* @return the destination
*/
public String getDestination() {
return destination;
}
/**
* @param destination the destination to set
*/
public void setDestination(String destination) {
this.destination =
destination;
}
/**
* @return the duration
*/
public int getDuration() {
return duration;
}
/**
* @param duration the duration to set
*/
public void setDuration(int duration) {
this.duration = duration;
}
/**
* @return the cost
*/
public int getCost() {
return cost;
}
/**
* @param cost the cost to set
*/
public void setCost(int cost) {
this.cost = cost;
}
/**
* This method returns String representation of Holiday
object
*/
@Override
public String toString() {
return "Holiday {destination=" +
destination + ", duration=" + duration + " days, cost=$" + cost +
"}";
}
}
/*************TravelAgent.java***************/
import java.util.ArrayList;
public class TravelAgent {
//instance variables
private String name;
private String postcode;
private ArrayList<Holiday> holidays;
/**
* Parameterized constructor which takes 2 parameter to
construct Travel Agent object
* @param name
* @param postcode
*/
public TravelAgent(String name, String postcode)
{
this.name = name;
this.postcode = postcode;
//initializing arraylist of
holiday
this.holidays = new
ArrayList<Holiday>();
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the postcode
*/
public String getPostcode() {
return postcode;
}
/**
* @param postcode the postcode to set
*/
public void setPostcode(String postcode) {
this.postcode = postcode;
}
/**
* This method adds holiday into the list of
holiday
* @param holiday
*/
public void addHoliday(Holiday holiday) {
this.holidays.add(holiday);
}
/**
* This method returns String representation of
TravelAgent including holiday
*/
@Override
public String toString() {
//StringBuilder used to make
mutable string
StringBuilder sb = new
StringBuilder();
sb.append(name + " at " +
postcode);
//iterating over all holiday
objects in list and used toString of holiday to get String
representation of Holiday object
for (Holiday holiday : holidays)
{
sb.append("\n"+holiday.toString());
}
return sb.toString();
}
}