OOP航空公司预订

时间:2015-02-08 07:44:46

标签: java oop

我正在航空预订系统上创建OOP。飞机将拥有头等舱(2排,每排4个座位(座位为ABCD))和经济舱(20排,每排6个座位(座位为ABCDEF)。行由过道隔开。

用户将输入他的姓名,班级规格和座位偏好([W] indow,[A] isle,[C] enter),并且会找到第一个可供用户加入的座位。

EX。

姓名:约翰史密斯 类别:经济 座位偏好:[C]输入

结果:

第3行座位B名称John Smith

我的问题是如何通过适当的座位安排创建第一个和经济型施工人员?我会使用arraylist吗?或者每排有座位和座位的二维阵列?或完全不同的东西?

谢谢!

1 个答案:

答案 0 :(得分:1)

从座位类开始,具有属性位置:窗口,中心,过道和位置' 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 and 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 ){
        // ...
    }
}
相关问题