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.再次条件判断(返回第二个步骤)

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值