从 Seat 类开始,具有属性 Location:Window、Center、Aisle 和位置 'A'、'B'、...
public enum Location { WINDOW, CENTER, AISLE }
public class Seat {
private Location loc;
private char pos; // A, B, C...
public Seat( Location loc, char pos ){...}
//...
}
创建类Row,子类成Business和Economy:构造函数负责创建合适的席位。座位可能在列表中,并添加数字作为行的属性。
public abstract class Row {
private int number;
private List<Seat> seats = new ArrayList<>();
protected Row( int number ){ ... }
public void addSeat( Seat seat ){...}
public Seat findSeat( Location loc ){...}
}
public class Business extends Row {
public Business( int number ){
super( number );
addSeat( Location.WINDOW, 'A' ); // continue as required
}
}
public class Economy extends Row {
public Economy( int number ){
super( number );
addSeat( Location.WINDOW, 'A' );
addSeat( Location.CENTER, 'B' ); // continue as required
}
}
使用 Business 和 Economy 创建填充其 List<Row> 的类 Plane,设置行号(确保省略行 number 13)。
public class Plane {
private static final NUM_BUSINESS = 2;
private static final NUM_ECONOMY = 20;
private List<Row> rows = new ArrayList<>();
public Plane(){
int iRow = 1;
for( int i = 0; i < NUM_BUSINESS; ++i ){
rows.add( new Business( iRow++ ) );
}
// similar for Economy
}
public Seat findSeat( boolean business, Location loc ){
// ...
}
}