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.再次条件判断(返回第二个步骤)
....... 直到条件判断不再满足,结束循环。
分享到:
相关推荐
Conjugate adjoining problem in braid groups and new design of braid-based signatures
adjoining structure 毗邻构筑物 adjudication fee 评估契据费用 adjusted replacement cost 经调整重置成本 administrator of estate 遗产管理人 admixture 外加剂;搀和剂 advance drainage diversion ...
In this code each SPE will synthesizes two overlap blocks to make the the area of the boundary to adjoining blocks which the other SPE synthesizes more smooth
The AravallieDelhi and Satpura Mobile Belts (ADMB and SMB) and the Eastern Ghat Mobile Belt (EGMB) inIndia form major Proterozoic mobile belts with adjoining cratons and contemporary basins....
A couple of simple-structure phase modulators were used ... The laser produces pulse as short as 5.7 ps whose repetition rate tuning can cover the spacing of the adjoining order mode-locking frequencies.
CL工具箱 总览 用Java实现的“解析为推演”系统,可以对所有内容和一些相关工具进行解析,以达到教育目的。 该实现基于Laura Kallmeyer的幻灯片。 目前,它包含: 解析为推导: CYK为CFG Earley for CFG ...