adjoining seats returner

Many theater only provide single seat reservation and adjoing seat reservation,

single seat reservation is easy to implement, and adjoing seat reservation can

implements base on single reservation.

 

In this essay, I will introduce a original method to get a group of adjoining seats

. Users need to input where the seat is start and where the seat is end.

 

Code is as following:

 

import java.util.*;
/**
 * This is a class represents a group of seats, each seat
 * have row index and seat index.
 */

public class SeatManager
{
    private String[][] seats;
    private int rowNumber;
    private int seatNumber;
    
    /**
     * Constractor in this class is to initialize seats
     * @param row row number in this seating area
     * @param seat seat number in this seating area
     */
    public SeatManager(int row, int seat)
    {
        rowNumber =row;
        seatNumber =seat;
        seats= new String[rowNumber][seatNumber];
        for(int i=0;i<row;i++)
        {
            for(int j =0;j<seat;j++)
            {
                seats[i][j] = "Row"+(i+1)+"Seat"+(j+1);// Every seat is represented by a String.
            }
        }
    }
    
    /**
     * Get adjoining seats from this seats area
     * @param rowStart the row index of the start seat
     * @param seatStart the seat index of the start seat
     * @param rowEnd the row index of the end seat
     * @param seatEnd the seat index of the end seat
     * @return ArrayList<String> return the object grouping of String which is demanded.
     */
    public ArrayList<String> getAdjoinSeats(int rowStart,int seatStart, int rowEnd, int seatEnd)
    {
        ArrayList<String> seat = new ArrayList<String>();
        
        if(rowStart == rowEnd)
        {
            for(int i =seatStart; i<= seatEnd; i++)
            {
                seat.add(seats[rowStart-1][i-1]);
            }
        }
        
        else
        {
            for(int i=seatStart; i<= seatNumber;i++)
            {
                seat.add(seats[rowStart-1][i-1]);
            }
        
            for(int i =rowStart+1;i<rowEnd ;i++)
            {
                for(int j= 0; j<seatNumber;j++)
                {
                    seat.add(seats[i-1][j]);
                }
            }
        
            for(int i =0;i<seatEnd;i++)
            {
                seat.add(seats[rowEnd-1][i]);
            }
        }
        
        return seat;
        
    }
}

 

 

Then, I will interpret the method getAdjoinSeats() by giving pseudo-code:

 

if the start seat and end seat are in the same row

then add seats from seats[rowStart][seatStart] to seats[rowStart][seatEnd] into a arraylist

 

if the start seat and end seat are in different row

then add these seats in 3 steps:

 

1.add the started row seat into arraylist(like the above red code).

2.add the completely middle row into arraylist , need a double for loop.

3.add the end row seat into arraylist.

 

finally, return this arraylist to upper user.

 

------------------------------------------------------------------------------------------------------------

In this method, I mainly use the for loop. Here, recall the detail of for.

 

for (初始化 ; 条件判断 ;  操 作 )    {  //code here  }

         其执行顺序 是:1. 初始化

                                2.条件判断。若正确,执行大括号中的代码,如果错误,结束循环。

                                3.操作。对索引值进行操作,以免是循环达到想要的次数。

                                4.再次条件判断(返回第二个步骤)

                                .......  直到条件判断不再满足,结束循环。

 

 

你可能感兴趣的:(J#)