Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the following: The int planTrip(int numPassengers, String tripType, Date date, int comfortLevel) method of Planner plans an trip of a given type with the

Complete the following:

The int planTrip(int numPassengers, String tripType, Date date, int comfortLevel) method of Planner plans an trip of a given type with the specified number of passengers on the specified date. Steps include: a. Determine the set of buses that the promoter can afford b. Find the lowest cost Bus from the set of affordable buses that i. can hold the number of passengers and ii. is available c. If there is at least one Bus that meets the requirements i. Create an instance of a Trip ii. Call the reserve method of the Bus and capture the return value in an integer iii. If the return value is greater than or equal to 0, 1. Call the setBus method of the trip to the selected bus 2. Pay for the selected bus 3. Return the return value as the result of planTrip d. If plans for the trip did not work out, return -1

look at this bus class code and utilize the methods in order to complete the int planTrip question here it is to be clear i need you to complete the int PlanTrip i have my attempted below the bus class so you can use that to guide you please and thanks

import java.util.ArrayList;

public class Bus {

private String name;

private int size;

private int basePrice;

private ArrayList approvedTrips;

private int level; // 1,2,3 for low,medium, high repectively;

private int id;

private static int nextId=0;

private Ministry mny;

protected String tripTypes;

private int getNextId(){

return nextId++;

}

public Bus(){

approvedTrips=new ArrayList();

}

public Bus( String name, int size, int basePrice, int lev, Ministry mny) {

approvedTrips=new ArrayList();

this.name = name;

this.size =size;

this.basePrice = basePrice;

this.level = lev;

this.id = getNextId();

this.mny = mny;

tripTypes = "BASICTRANSPORT";

}

public boolean available(Date date){

boolean retval = true;

for (Trip t:approvedTrips)

if (t.getDate().getDay() == date.getDay())

retval = false;

return retval;

}

public int getBasePrice(){

return basePrice;

}

public int getId(){

return id;

}

public String getName(){

return name;

}

public double getSize(){

return size;

}

public String toString(){

return name;

}

public boolean isSuitable(String type){

return tripTypes.contains(type);

}

public int getEstimate(String type, int numPassengers, int comfortLevel){

return basePrice;

}

public boolean canHold(int numPassengers, int comfortLevel){

int capacity = (int)(size / mny.getSeparation(comfortLevel));

return numPassengers <=capacity;

}

public void promoteTrips(){

System.out.println();

System.out.println("TRIPS ON " +getName());

System.out.println("===================");

for (Trip t:approvedTrips)

System.out.println(t);

}

public int reserve(Trip trip, Ministry mny){

int retval = -1;

ApprovalRequest ar = new ApprovalRequest(trip, this);

int result = mny.checkApproval(ar);

if (result >=0){

int est = getEstimate(trip.getType(), trip.getNumPeople(), trip.getComfortLevel());

if (trip.getPlanner().getBudget() >= getEstimate(trip.getType(), trip.getNumPeople(), trip.getComfortLevel())){

approvedTrips.add(trip);

trip.setBus(this);

retval = result;

}

}

return retval;

}

}

attempted code planner code

import java.util.ArrayList;

public class Planner

{

private String name;

private double budget;

private Ministry mny;

private Bus[] buses;

private ArrayList possibleBuses;

public Planner(String name, double budget, Ministry mny, Bus[] buses)

{

this.name = name;

this.budget = budget;

this.mny = mny;

this.buses = buses;

possibleBuses = new ArrayList();

}

public String getName(){

return name;

}

public double getBudget(){

return budget;

}

public int planTrip(int numPassengers, String tripType, Date date, int comfortLevel) {

int retval = -1;

possibleBuses.clear();

for (Bus bus : buses ) {

if (bus.getEstimate(tripType, numPassengers, comfortLevel) <= budget && bus.canHold(numPassengers, comfortLevel)) {

possibleBuses.add(bus);

//System.out.println(possibleBuses.size() +" affordable buses ");

}

}

//populate possible buses by adding busses that are suitable,

// can hold the passengers at the comfortlevel,and can be afforded

if (possibleBuses.size() > 0) {

Bus minBus = null;

for (Bus bus : possibleBuses) {

if (bus.available(date) && (minBus == null || bus.getEstimate(tripType, numPassengers, comfortLevel) < minBus.getEstimate(tripType, numPassengers, comfortLevel)) && bus.isSuitable(tripType)) {

minBus = bus;

}

}

/*

//find the suitable bus with minimum price and assign to minbus`

*/

if (minBus == null) {

System.out.println("No suitable bus is available");

}

else{

//create a trp with the type, number of passengers and the comfortlevel

// attempt to get get an approval id by calling the reserve method of the bus

// if the id >=0

if (minBus != null){

Trip trip = new Trip(tripType, numPassengers, date, comfortLevel,this);

int reserved = minBus.reserve(trip, mny);

if (reserved >= 0){

trip.setBus(minBus);

payFor(minBus,tripType, numPassengers, comfortLevel);

retval = reserved;

System.out.println(name + " successfully reserved " + trip);

}

}

}

}

else

System.out.println(name + " cannot afford to pay for any suitable buses");

return retval;

}

public void payFor(Bus bus, String tripType, int numPassengers, int comfortLevel){

budget-=bus.getEstimate(tripType, numPassengers, comfortLevel);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

ISBN: 321321367, 978-0321321367

More Books

Students also viewed these Databases questions

Question

Question Can a Keogh plan fund be reached by the owners creditors?

Answered: 1 week ago

Question

Question What happens to my plan if I die?

Answered: 1 week ago