Java Problem Excercise: Create a Simple Reservation System for a Concert Hall

The Problem

You need to create a reservation system for a concert hall. You can assume that the rows are all the same length. Let every seat be represented by a pushbutton on the screen. The pushbutton will have the seat and row number displayed on it. If the seat is available, the button will be green. The user reserves the seat by pressing on it. Then it turns red. If the seat is reserved, pressing on it will make it available again.

Analysis

It would be good to separate domain data from presentation and controller logic. So we will create two classes to represent a concert hall (the Hall class) and a seat (the Seat class). A Seat can be booked or not. The Hall class has one-to-many relationship with the Seat class. According to the problem description, all the rows are the same length. We can model this association by using a two dimensional array. For a better flexibility it would be good to encapsulate this requirement so the client code should think that the number of columns is different. Thus if we need a hall with different number of columns per row, the changes will be limited.

We can encapsulate the Hall GUI in a separate panel with corresponding action handler to support user clicking on seats. The GUI uses a GridLayout to layout the buttons representing seats. Each row is padded with empty panels (JPanel objects).

TODO: Class diagram

The Solution

The seat class:

/**
 * Class to represent a bookable seat in a concert hall.
 *
 * @author Ivan Georgiev
 */
public class Seat {
	private boolean booked = false;
 
	public boolean isBooked() {
		return booked;
	}
 
	public void setBooked(boolean isBooked) {
		booked = isBooked;
	}
}

The ConcertHall class:

 
/**
 * A class to represent a concert hall with
 * seats sitauted in rows and columns.
 *
 * @author Ivan Georgiev
 */
public class Hall {
	private Seat theSeats[][];
 
	public Hall(int rows, int cols) {
		theSeats = new Seat[rows][cols];
		for (int row=0; row<rows; row++) {
			for (int col=0; col<cols; col++) {
				theSeats[row][col] = new Seat();
			}
		}
	}
 
	public int numRows() {
		return theSeats.length;
	}
 
	public int numCols(int row) {
		return theSeats[row].length;
	}
 
	public int maxNumCols() {
		int maxNumColsSoFar = 0;
		int numRows = numRows();
		for (int row=0; row<numRows; row++) {
			int numCols = numCols(row);
			if (numCols > maxNumColsSoFar) maxNumColsSoFar = numCols;
		}
		return maxNumColsSoFar;
	}	
	public Seat getSeat(int row, int col) {
		return theSeats[row][col];
	}
}

And the GUI - a simple applet:

/**
 * @(#)Ch13_4_ReservationSystem.java
 *
 * Simple reservation system for a concert hall.
 *
 * @author Ivan Georgiev
 * @version 1.00 2008/9/10
 */
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class Ch13_4_ReservationSystem extends JApplet {
	private Hall theHall;
 
	public void init() {
		createTheHall();
		Container content = getContentPane();
		content.add(new HallUI());
	}
 
	private void createTheHall() {
		int numRows=0;
		int numCols=0;
 
		while (numRows < 1) {
			numRows = inputInt("Enter the number of rows:");
			if (numRows < 1) {
				JOptionPane.showMessageDialog(null, "The number of rows must be greater than 0.");
			}
		}
 
		while (numCols < 1) {
			numCols = inputInt("Enter the number of columns:");
			if (numCols < 1) {
				JOptionPane.showMessageDialog(null, "The number of columns must be greater than 0.");
			}
		}
		theHall = new Hall(numRows, numCols);		
	}
 
	private int inputInt(String prompt) {
		int result;
		while (true) {
			String sUserInput = JOptionPane.showInputDialog(null, prompt);
			try {
				result = Integer.parseInt(sUserInput);
				break;
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(null, sUserInput + " is not a valid integer.");
			}
		}
		return result;
	}
 
	public void paint(Graphics g) {
 
		g.drawString("Welcome to Java!!", 50, 60 );
 
	}
 
	private class HallUI extends JPanel {
		private Color colorBooked = Color.red;
		private Color colorAvailable;
 
		public HallUI() {
			int numRows = theHall.numRows();
			int maxNumCols = theHall.maxNumCols();
			SeatButtonActionListener theButtonActionListener = new SeatButtonActionListener();
			JLabel label;
			setLayout(new GridLayout(numRows, maxNumCols+1));
			for (int row=0; row<numRows; row++) {
				int humanizedRow = row+1;
				label = new JLabel("Row " + humanizedRow);
				add(label);
				int numCols = theHall.numCols(row);
				for (int col=0; col<numCols; col++) {
					int humanizedCol = col+1;
					JButton button = new JButton(humanizedRow + "/" + humanizedCol);
					button.addActionListener(theButtonActionListener);
					add(button);
					colorAvailable = button.getBackground();
				}
				// Fill unused seat positions with blank panels.
				for (int col=numCols; col<maxNumCols; col++) {
					JPanel panel = new JPanel();
					add(panel);
				}
			}
		}
 
		private class SeatButtonActionListener implements ActionListener {
			public void actionPerformed(ActionEvent e) {
				// 1. Get the row/col numbers. Do not forget to convert from human readable by
				// subtracting 1.
				String buttonText = e.getActionCommand();
				int separatorPos = buttonText.indexOf("/");
				// Add error checking here. a) separatorPos; b) NumberFormatException
				// c) row, col is in range.
				int row = Integer.parseInt(buttonText.substring(0,separatorPos)) - 1;
				int col = Integer.parseInt(buttonText.substring(separatorPos+1)) - 1;
 
				// VARIANT: We could also use the following to get the row/col
				// String[] seatInfo = e.getActionCommand().split("/");
				// int row = Integer.parseInt(seatInfo[0]) - 1;
				// int col = Integer.parseInt(seatInfo[1]) - 1;
 
				// 2. Switch the seat state.
				Seat theSeat = theHall.getSeat(row, col);
				theSeat.setBooked(!theSeat.isBooked());
 
				// 3. Add visual indicator
				JButton theButton = (JButton) e.getSource();
				theButton.setBackground( theSeat.isBooked() ? colorBooked : colorAvailable);
			}
		}
	}
}

Example

 
java/excercises/hallreservationsystem.txt · Last modified: 2009/10/31 23:39 (external edit)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki