简体   繁体   English

为火车预订查找相邻座位

[英]Finding adjacent seating for a train reservation

I have constructed an object train that holds 2 ArrayList<Seating> for business class and economy class.我为商务舱和经济舱构建了一个包含2 ArrayList<Seating>的对象列车。

My Seating Object has an ArrayList of Seats我的座位对象有一个座位数组列表

List<Seating> business_class = new ArrayList<Seating>();
List<Seating> economy_class = new ArrayList<Seating>();
int bCount = 1;
int eCount = 10;
for(int i = 0; i < rowNumber; i++)
{
    business_class.get(i).addSeat(new Seats(Integer.toString(bCount), "A"));
    business_class.get(i).addSeat(new Seats(Integer.toString(bCount), "B"));
    business_class.get(i).addSeat(new Seats(Integer.toString(bCount), "C"));
    business_class.get(i).addSeat(new Seats(Integer.toString(bCount), "D"));
    business_class.get(i).addSeat(new Seats(Integer.toString(bCount), "E"));
    business_class.get(i).addSeat(new Seats(Integer.toString(bCount), "F"));

    economy_class.get(i).addSeat(new Seats(Integer.toString(eCount), "A"));
    economy_class.get(i).addSeat(new Seats(Integer.toString(eCount), "B"));
    economy_class.get(i).addSeat(new Seats(Integer.toString(eCount), "C"));
    economy_class.get(i).addSeat(new Seats(Integer.toString(eCount), "D"));
    economy_class.get(i).addSeat(new Seats(Integer.toString(eCount), "E"));
    economy_class.get(i).addSeat(new Seats(Integer.toString(eCount), "F"));
    bCount++;
    eCount++;
}


1A / 1B / 1C || 1D / 1E / 1F
2A / 2B / 2C || 2D / 2E / 2F

My java program can add either a single passenger or a group .我的 java 程序可以添加single passenger or a group For my group reservations, I have to seek the largest amount of available adjacent seating .对于我的团体预订,我必须寻求最大数量的可用adjacent seating I am stuck on this method.我坚持这种方法。 I have coded it already, but it is a mixture and if and else if statements taking into account all the possible combinations of seating.我已经对它进行了编码,但它是一个混合体,if 和 else if 语句考虑了所有可能的座位组合。 The method has over 600 lines of if-else statements... I am asking for a simpler and more effective way to find adjacent seating for groups.该方法有 600 多行 if-else 语句……我要求一种更简单、更有效的方法来为团体找到相邻的座位。

More detail on adjacent seating有关相邻座位的更多详细信息
For example, if all the seats were empty, then I'd have 6 adjacent seats.例如,如果所有座位都是空的,那么我会有 6 个相邻的座位。 In the case that I have 6 group members, I would fill the entire row.如果我有 6 个小组成员,我会填满整行。 In the case that I have 7 group members.在我有 7 个小组成员的情况下。 1 would be placed in the row before if any available or after if any available starting at (seatNumber)A -> (seatNumber)F 1 将放置在从 (seatNumber)A -> (seatNumber)F 开始的行之前(如果有)或之后(如果有)

If there are passengers located at 1A and 1F than I'd have 2 sets of 2 adjacent seats 1B / 1C and 1D / 1E.如果有乘客位于 1A 和 1F,那么我会有 2 组 2 个相邻的座位 1B / 1C 和 1D / 1E。
In the event that there is a group of 4, then those 4 will fill those seats.如果有 4 人一组,则这 4 人将填补这些席位。 In the event that there is a group of 3, then 1B / 1C ||如果有一组 3,则 1B / 1C || 1D will be filled. 1D 将被填充。

That is the logic of this particular adjacent seating method.这就是这种特殊的相邻座位方法的逻辑。 Business and Economy class have no relationships in seating.商务舱和经济舱在座位上没有关系。

Si i would suggest you to : 我建议你:

create a method seat.isFree(); 创建方法seat.isFree(); which will return true if the seat is free and false otherwise. 如果座位是免费的,则返回true ,否则返回false

Then iterate each line of seats of your class, and look for the first free Seat of your line. 然后迭代你班级的每一个座位,并寻找你的第一个免费Seat It would look like : 它看起来像:

Seat freeSeat = null;
int length = 0;
for( each line ){
    for( each seat){
        Seat seat = seat.get(indexInLine);
        if(seat.isFree()){
            if(freeSeat == null){
                freeSeat = seat;
                length = 1;
            } else
                length++;
        } else {
            if(freeSeat != null){
                yourList.add(seat, length);
            }
            freeSeat = null;
        }
    }
    if(freeSeat != null){//So if the last seat of the line is also free add it to the free seat list
        yourList.add(seat, length);
    }
}

The purpose of your list is to contain a list of Seat and int objects which would represent the first free Seat and the number of free seats in the line after it ( including the first one ). list的目的是包含一个Seatint对象列表,它们代表第一个免费Seat以及它后面的行中的空闲座位数(包括第一个)。

Then you will get every adjacents free seat in your list. 然后,您将获得列表中每个邻居的免费席位。

You just have to select the first element of the list whose (int value) length is important for all your group or if none, separate the group while you can't find adjacents seats for them. 您只需选择列表的第一个元素,其(int值)长度对于您的所有组都很重要,或者如果没有,则在您找不到它们的邻接位置时将该组分开。

This might help you.这可能对你有帮助。 I have a similar code我有一个类似的代码

Scanner S=new Scanner(System.in);

int count_seat1;

count_seat1=S.nextInt();

int remainder;

remainder=count_seat1%12;

if(remainder==1)
{
    System.out.println( count_seat1+11 +" WS");
}
else if(remainder==2)
{
    System.out.println( count_seat1+9  +" MS");
}
else if(remainder==3)
{
    System.out.println(count_seat1+7 +" AS");
}
else if(remainder==4)
{
    System.out.println(count_seat1+5  +" AS");
}
else if(remainder==5)
{
    System.out.println( count_seat1+3 +" MS");
}
else if(remainder==6)
{
    System.out.println( count_seat1+1 +" WS");
}

else if(remainder==7)
{
    System.out.println(count_seat1-1 +" WS");
}
else if(remainder==8)
{
    System.out.println(count_seat1-3+" MS");
}
else if(remainder==9)
{
    System.out.println(count_seat1-5+" AS");
}
else if(remainder==10)
{
    System.out.println(count_seat1-3+ "AS");
}
else if(remainder==11)
{
    System.out.println(count_seat1-9+ " MS");
}
else if(remainder==0)
{
    System.out.println(count_seat1-11+" WS");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM