I'm not sure what is going on here:
Code:
int[][] Airplane = new int[rows][seats];
You have declared a variable called Airplane that is a two-dimensional array. You seem to be confusing that with your class name. Also, the constructor you wrote is not a match for the Airplane class itself. It looks like it all fell apart when you tried to add the arrays.
Going back a step and taking the arrays out of the equation, your Airplane class has two properties: rows and seats. This might be a good starting point if each row had the same number of seats:
Code:
Airplane airplane = new Airplane(15, 6);
Unfortunately you have to deal with first class and economy, which have different numbers of seats in their respective rows. So maybe your Airplane class is actually a Section class?
Code:
Airplane airplane = new Airplane();
airplane.firstClass = new Section(5, 4);
airplane.economy = new Section(15, 6);
If you took this approach, you still need a method to work out whether seats are aisle, middle or window, based on the number of seats per row and the seat number. It doesn't feel like a good model to me, and that makes programming it too hard.
I see a SeatType enum and a Seat object:
Code:
public enum SeatType {
WINDOW, MIDDLE, AISLE
}
public class Seat {
private SeatType type;
private boolean reserved;
...
}
Probably also a Row type and a Section enum, leading to your Airplane:
Code:
public enum Section {
FIRST, ECONOMY
}
public class Row {
private List<Seat> seats = new ArrayList<>();
private Section section;
...
}
public class Airplane {
private List<Row> rows = new ArrayList<>();
...
}
You'd construct rows for first class with 4 seats, rows for economy with 6 seats, then add them to the Airplane*. Then probably a SeatAllocationService** that you call from your main program. The main program deals with the input and output.
The seat allocation is then just a matter of iterating over the rows looking for matches and updating the reserved flag. You'd probably end up with a method on the row to test whether availability exists on that row for a given request and another to reserve seats in the row.
Code:
public class SeatAllocationService {
/**
* Allocate seats on an airplane in a preferred section, with a number of places and seating preference
* @return true if seats could be allocated according to preference, otherwise false.
*/
public boolean allocateSeats(Airplane airplane, Section section, int places, SeatType seatTypePreference) {
for (Row row : airplane.rowsMatchingSection(section) {
if (row.hasAvailability(places, seatTypePreference) {
...
return true;
}
}
return false;
}
}
I haven't thought it all the way through and there's probably a few twists and turns to negotiate before it would but that would be the approach I would take, i.e. model the problem more closely than just an Airplane class and a two-dimensional array. There's plenty of aggregation going on here: airplane has rows, rows have seats.
*Bonus points for using the builder design pattern to create the airplane instances and **programming to an interface on the service class.
Bookmarks