【问题标题】:OOP Airline ReservationOOP 航空公司预订
【发布时间】:2015-04-08 03:05:23
【问题描述】:

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

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

EX.

姓名:约翰·史密斯 舱位:经济 座位偏好:[C]进入

结果:

第 3 排座位 B 姓名约翰·史密斯

我的问题是如何创建具有适当座位安排的第一和经济构造函数?我会使用数组列表吗?还是每排有座位和座位的二维数组?还是完全不同的东西?

谢谢!

【问题讨论】:

  • 这归结为需求,就个人而言,我会使用列表(如果需要的话),因为它更容易管理。我可能很想创建一个描述每排座位数的 Row 类并将这些添加到列表中。例如,您可以(技术上)有 3 个列表,左、中、右列
  • 不是真正的面向对象问题。你所需要的只是一系列座位!
  • @JamesAnderson 你真正需要的是4*2 + 6*20 bits。

标签: java oop


【解决方案1】:

从 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&lt;Row&gt; 的类 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 ){
        // ...
    }
}

【讨论】:

    猜你喜欢
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 2015-04-28
    相关资源
    最近更新 更多