Friday, June 3, 2016

Design a Cinema Management System.

Solution:

As we know any cinema associated with a number of theaters and each theater contains a set of rows with a number of seats and also each seats will have specification.

Implementation:

import java.io.Serializable;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 *
 */

/**
 * @author Abhinaw.Tripathi
 *
 */
class SeatSepecification implements Serializable,Cloneable
{
  private HashMap<String,Object> features;
  public SeatSepecification()
  {
features =new HashMap<>();
  }

  public void addFeature(String featureLabel,Object featureValue) throws  InvalidParameterException
  {
 if(featureValue == null)
 throw new InvalidParameterException("fetaure value cannot be null");
 else
  features.put(featureLabel, featureValue);
  }

  public Object checkFeature(String featureLabel)
  {
 return features.get(featureLabel);
  }

  public boolean match(SeatSepecification givenSpec)
  {
String currFeature;
if(givenSpec == null)
return false;
else
{
Iterator it=givenSpec.features.keySet().iterator();
while(it.hasNext())
{
currFeature=(String)it.next();
if(!features.get(currFeature).equals(givenSpec.features.get(currFeature)))
return false;
}
return true;
}
  }

  public  SeatSepecification clone()
  {
  try
  {
  SeatSepecification c=(SeatSepecification)super.clone();
  c.features=new HashMap<>();
  Iterator it=features.keySet().iterator();
  while(it.hasNext())
  {
  String currFeatureLabel=(String)it.next();
  Object currentFeatureValue=features.get(currFeatureLabel);
  c.features.put(currFeatureLabel, currentFeatureValue);
  }
  return c;
  }
 
  catch(CloneNotSupportedException e)
  {
  return null;
  }
  }

}

class Seat implements Serializable,Cloneable
{
  private SeatSepecification spec;
  private String id;

  public Seat(String id) {
this.id=id;
spec=new SeatSepecification();
}

  public SeatSepecification getSpec() {
return spec;
}
public void setSpec(SeatSepecification spec) {
this.spec = spec;
}
public String getId() {
return id;
}

public void setSeatSpecification(String featureLabel,Object featureValue) throws InvalidParameterException
{
spec.addFeature(featureLabel, featureValue);
}

public boolean match(SeatSepecification givenSpec)
{
return spec.match(givenSpec);
}

public boolean isReserved()
{
return true;
}

public Seat clone()
{
Seat cloneSeat=new Seat(id);
return cloneSeat;
}

}

class RowOfSeats implements Serializable,Cloneable
{
 private HashMap<String,Seat> seats;
 private String rowId;
 private int numOfSeats;
 ArrayList<String> keys;

 public RowOfSeats(String rowId,int numOfSeats) throws InvalidParameterException
 {
seats=new HashMap<>();
int i=0;
Seat s;

while(i<numOfSeats)
{
s=new Seat(Integer.toString(i));
seats.put(s.getId(), s);
i++;
}

keys =new ArrayList<>();
this.rowId =rowId;
this.numOfSeats=numOfSeats;
 }

 public void setSeatFeature(String seatID,String featureLabel,Object fetureValue) throws InvalidParameterException
 {
if(seats.containsKey(seatID))
{
Seat s=seats.get(seatID);
s.setSeatSpecification(featureLabel, fetureValue);
}
else
{
throw new InvalidParameterException("no seat " + seatID);
}
 }

 public String getRowId()
 {
return rowId;
 }

 public int getNumOfSeats()
 {
return seats.size();
 }

 public void addSeat()
 {
Seat s;
numOfSeats +=1;
s=new Seat(Integer.toString(numOfSeats));
seats.put(s.getId(), s);
keys.add(Integer.toString(numOfSeats));
 }

 public void removeSeat(String seatID) throws InvalidParameterException
 {
if(seats.containsKey(seatID))
{
seats.remove(seatID);
}
else
{
throw new InvalidParameterException("The specific seat not found!!!!");
}
 }

 public RowOfSeats clone()
 {
RowOfSeats newRowOfSeats=(RowOfSeats) super.clone();
Iterator it=seats.keySet().iterator();
for(int i=0;i<=keys.size();i++)
{
String seatID=keys.get(i);
while(it.hasNext())
{
newRowOfSeats.put(seatID,seats.get(seatID).clone());
}
}
return newRowOfSeats;
 }

}

public class CinemaManagementSystemTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

}


}

Need to add few more classes such as

1)SeatingChart Class
2)Theater Class
3)Movie Class
4)CineGoer Class=basically models a cinema goer.
5)Show Class
6)ReservationSpecification Class
7)Reservation Class
8)CinemaManagmentSystem

These are all classes which will be implemented using Serialization and Clonable .





No comments: