public class Concert implements java.io.Serializable{
private static final int FREE = ConcertSeat.FREE;
private static final int RESERVED = ConcertSeat.RESERVED;
private static final int ALLOCATED = ConcertSeat.ALLOCATED;
private ConcertSeat[] seatArray;
/** This method is Concert constructor
* @param Block[] block array - block array that has been initialised
* @return No return value for constructor
* @exception without throws exceptions
*/
public Concert(Block[] blockArray){
int seatsInBlock, totalSeats = 0, i, rowNo = 0, blockIndex;
int rowIndex, seatIndex, seatArrayIndex = 0;
for(i = 0; i < blockArray.length; i++){
seatsInBlock = (blockArray[i].getRows()) * (blockArray[i].getAcross());
totalSeats += seatsInBlock;
}
seatArray = new ConcertSeat[totalSeats];
for(blockIndex = 0; blockIndex < blockArray.length; blockIndex++)
for(rowIndex = 0; rowIndex < blockArray[blockIndex].getRows(); rowIndex ++){
rowNo++;
for(seatIndex = 1; seatIndex <= blockArray[blockIndex].getAcross(); seatIndex++){
seatArray[seatArrayIndex] = new ConcertSeat(rowNo, seatIndex-1);
seatArrayIndex++;
}
}
}
/** This method is use to get the seat
* @param String seatString - the seat number that serve as search criteria
* @return ConcertSeat - ConcertSeat object handler whose seat number match with the parameter
* @exception without throws exceptions
*/
public ConcertSeat getSeat(String seatString){
int i = 0;
ConcertSeat foundSeat = null;
for(i = 0; i < seatArray.length; i++){
if(seatString.compareTo(seatArray[i].seatNumber()) == 0){
foundSeat = seatArray[i];
break;
}
}
return foundSeat;
}
/** This method is use to get the Seats with status free
* @param No parameter
* @return ConcertSeat - array of seats with the status free
* @exception without throws exceptions
*/
public ConcertSeat[] getFreeSeats(){
return (getSeatsOfStatus(FREE));
}
/** This method is use to get the Seats with status reserved
* @param No parameter
* @return ConcertSeat - array of seats with the status reserved
* @exception without throws exceptions
*/
public ConcertSeat[] getReservedSeats(){
return (sort(getSeatsOfStatus(RESERVED)));
}
/** This method is use to get the Seats with status Allocated
* @param No parameter
* @return ConcertSeat - array of seats with the status Allocated
* @exception without throws exceptions
*/
public ConcertSeat[] getAllocatedSeats(){
return (sort(getSeatsOfStatus(ALLOCATED)));
}
/** This method is use to get the seats based on the particular status
* @param int status - the status of seat which serve as search criteria
* @return ConcertSeat[] - array of seats which has been retrieve based on the search criteria
* @exception without throws exceptions
*/
public ConcertSeat[] getSeatsOfStatus(int status){
int i, count = 0;
for(i = 0; i < seatArray.length; i++)
if(seatArray[i].getStatus() == status)
count++;
if(count == 0)
return (null);
else{
ConcertSeat[] foundSeats = new ConcertSeat[count];
int foundIndex = 0;
for(i = 0; i < seatArray.length; i++)
if(seatArray[i].getStatus() == status){
foundSeats[foundIndex] = seatArray[i];
foundIndex++;
}
return foundSeats;
}
}
/** This method is use to sort the seats based on the operator name
* @param ConsertSeat[] seatArray - array of seats that going to be sort
* @return ConcertSeat[] - array of seats which has been sort.
* @exception without throws exceptions
*/
public ConcertSeat[] sort(ConcertSeat[] seatArray){
if(seatArray == null)
return null;
else{
int i, j;
for(i = 0; i < seatArray.length; i ++){
ConcertSeat temp = new ConcertSeat(0, 0);
for(j = 0; j < i; j ++)
if(seatArray[j].getOperator().compareTo(seatArray[j + 1].getOperator()) > 0){
temp = seatArray[j];
seatArray[j] = seatArray[j + 1];
seatArray[j + 1] = temp;
}
}
return seatArray;
}
}
}