gpt4 book ai didi

java - OOP 航空公司预订

转载 作者:行者123 更新时间:2023-12-01 11:56:47 25 4
gpt4 key购买 nike

我正在航空公司预订系统上创建 OOP。该机设有头等舱(2排,每排4个座位(座位为A B C D))和经济舱(20排,每排6个座位(座位为A B C D E F)。各排之间有过道分隔。

用户输入他的姓名、类(class)规范和座位偏好([W]indow、[A]isle、[C]enter),它会找到第一个可用的座位来放置用户。

EX。

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

结果:

第 3 排 B 座位姓名 John Smith

我的问题是如何创建具有适当座位安排的头等舱和经济舱构造机?我会使用数组列表吗?或者一个二维数组,其中有座位和每排座位?或者完全是别的什么?

谢谢!

最佳答案

从 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
}
}

创建填充其List<Row>的类Plane使用“商业”和“经济”,设置行号(确保省略行数字 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 ){
// ...
}
}

关于java - OOP 航空公司预订,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28391748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com