Question: Part 3 - Developing Class Train Section C 1. Method issueTicket() has a single boolean parameter and returns a boolean. If the parameter is true,

Part 3 - Developing Class Train Section C 1. Method issueTicket() has a single boolean parameter and returns a boolean. If the parameter is true, the method attempts to issue a ticket for a seat in a business-class car; if false, it attempts to issue a ticket for a seat in an economy-class car. This method will attempt to book a seat in the first car of the appropriate type, but if a seat is not available it will attempt to book a seat in the second car of the appropriate type, and so on. This method must ensure that a request to issue a ticket in a business-class car will never result in a seat being booked in an economy-class car, and vice-versa. If a ticket is successfully issued, the method returns true; otherwise, it returns false. This method must NOT call the seats() method in class Car (that method is reserved for use by test cases). Hint: recall that class Car has a method called bookNextSeat().

Part 4 - Developing Class Train Section D 1. Method cancelTicket() has two parameters, an integer car id and an integer seat number, and returns a boolean. If the car id and seat number are valid, and if the specified seat in the specified car has been booked, this method cancels the booking for that seat and returns true. If the car id or seat number are not valid, or if the specified seat has not been booked, the method returns false. This method must NOT call the seats() method in class Car (that method is reserved for use by test cases). Hint: recall that class Car has a method called cancelSeat().

public class Train { /** The cars in this train. */ private ArrayList cars; /** * Constructs an empty train; i.e., one that has no cars. */ public Train() { cars = new ArrayList(0); } /** * Adds the specified car to this train. */ public void addCar(Car car) { cars.add(car); } /** * Returns this trains's list of cars. This method is intended for * testing purposes only, and should not be called by other objects, * as it may be removed from the final version of this class. * * @return A list of the cars in the train */ public ArrayList cars() { return cars; } /** * Attempts to issue a ticket for a business class seat or an * economy class seat, as specified by the method's argument. * It will attempt to book a seat in the first car of the appropriate * type, but if a seat is not available it will attempt to book a seat * in the second car of the appropriate type, and so on. * A request to issue a ticket in a business-class car will never result * in a seat being reserved in an economy-class car, and vice-versa. * Returns true if successful, false otherwise. */ public boolean issueTicket(boolean businessClassSeat) { return false; } /** * Cancels the ticket for the specified seat in the specified car. * Returns true if successful, false otherwise. */ public boolean cancelTicket(int carId, int seatNo) { return false; } }

public class Car { /** This car's identifier. */ private int id; /** * true == this car is a business-class car, * false == this car is an economy-class car. */ private boolean businessClass; /** The cost of a business class seat, in dollars. */ public static final double BUSINESS_SEAT_COST = 125.0; /** The cost of an economy class seat, in dollars. */ public static final double ECONOMY_SEAT_COST = 50.0; /** The number of seats in a business class car. */ public static final int BUSINESS_SEATS = 30; /** The number of seats in an economy class car. */ public static final int ECONOMY_SEATS = 40; /** The list of this car's seats. */ private Seat[] seats; /** * Constructs a new Car object with the specified id. * If parameter isBusinessClass is true, the car is a business-class * car. If parameter isBusinessClass is false, the car is an * economy-class car. */ public Car(int carId, boolean isBusinessClass) {id = carId; businessClass = isBusinessClass; int Count = 1; if (businessClass == true) { seats = new Seat[BUSINESS_SEATS]; for(int i = 0; i < seats.length; i++) { seats[i] = new Seat(Count, BUSINESS_SEAT_COST); Count++; } } else { seats = new Seat[ECONOMY_SEATS]; for(int i = 0; i < seats.length; i++) { seats[i] = new Seat(Count, ECONOMY_SEAT_COST); Count++; } } }

/** * Returns this car's list of seats. This method is intended for * testing purposes only, and should not be called by other objects, * as it may be removed from the final version of this class. * * @return The seats in this car, an array of Seat objects. */ public Seat[] seats() { return seats; } /** * Returns true if this is a business-class car, * false if this is an economy-class car. */ public boolean isBusinessClass() { if(businessClass == true) { return true; } return false; } /** * Returns the id of this car. */ public int id() { return id; } /** * This method is called when the specified seat has been booked, * to print a ticket for that seat. * * @param seatNo The integer identifier of the seat. */ private void printTicket(int seatNo) { System.out.println("Car ID: " + id); System.out.println("Seat number: " + seatNo); System.out.println("Price: "); if (businessClass) { System.out.println(BUSINESS_SEAT_COST); } else { System.out.println(ECONOMY_SEAT_COST); } } /** * Attempts to book a seat. If successful, this method prints a * ticket and returns true. * If no seats are available, this method returns false. */ public boolean bookNextSeat() { for(int i = 0; i < seats.length; i++) { if(seats[i].isBooked() == false) { seats[i].book(); printTicket(seats[i].number()); return true; } } return false; }

/** * Cancels the booking for the specified seat, which must be between * 1 and the maximum number of seats in the car. * If the seat number is valid and if the seat has been reserved, this * method cancels the booking for that seat and returns true. * If the seat number is not valid, this method returns false. * If the seat number is valid, but the seat has not been reserved, * this method returns false. */ public boolean cancelSeat(int seatNo) { if(seatNo < 1 || seatNo > seats.length) { return false; } if(seats[seatNo-1].isBooked() == false) { return false; } seats[seatNo-1].cancelBooking(); return true; } }

public class Seat { private int number; // the seat's number private boolean booked; // has this seat has been reserved? private double price; // the cost of a ticket for this seat, in dollars

/** * Constructs a new Seat with the specified seat number and * ticket price. */ public Seat(int seatNo, double cost) { booked = false; number = seatNo; price = cost; }

/** * Returns the cost of purchasing a ticket for this Seat. */ public double price() { return price; }

/** * Returns this seat's number. */ public int number() { return number; }

/** * Returns true if someone has purchased a ticket * for this this Seat. */ public boolean isBooked() { return booked; }

/** * If this seat is available, books it and returns true. * If the seat is not available, returns false. */ public boolean book() { if (!booked) { booked = true; return true; } return false; }

/** * If this seat is booked, cancels the booking and returns true. * If the seat was not booked, returns false. */ public boolean cancelBooking() { if (booked) { booked = false; return true; } return false; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!