Creating an Exception Application in Java

Problem:

Reservation

+ reserveSeat(seatNumber : int) : void
+ reserveSeats(seatN umbers : int[]) : void
+ cancelReservation(seatNumber : int) : void
+ cancelReservations(seatNumbers : int[]) : void


MovieReservation
- seats : int[]        
+ MovieReservation(seats : int[])
+ reserveSeat(seatNumber : int) : void
+ reserveSeats(seatN umbers : int[]) : void
 + cancelReservation(seatNumber : int) : void
+ cancelReservations(seatNumbers : int[]) : void
 + toString() : String

TravelTicketReservation

- seats : int[]
+ TravelTicketReservation(seats : int[])
+ reserveSeat(seatN umber : int) : void
+ reserveSeats(seatN umbers : int[]) : void
+ cancelReservation(seatNumber : int) : void
+ cancelReservations(seatNumbers : int[]) : void
 + toString() : String

SeatException

+ SeatException(message : String)



In this lab, you will be creating a Movie Reservation System and a Travel Ticket Reservation that will implement an interface, and a SeatException class.

The Reservation interface has five methods that need to be implemented which are reserveSeat, reserveSeats, cancelReservation, and cancelReservations.

The SeatException class will extend the Java Exception class. Its constructor will take a string as a parameter and call the superclass' constructor passing the string to it as a parameter.

The MovieReservation class and the TravelTicketReservation class will implement the interface's methods. They also have a private variable which is an array of integers representing the seats. The constructor should initialize this variable.

The reserveSeat method will take an integer named seatNumber as a parameter and sets seats[seatNumber] to one indicating that the seat is reserved. If the seat is already reserved, a SeatException should be thrown.

The reserveSeats method will take an integer array named seatNumbers as a parameter and sets all the seat numbers in the array to one indicating that the seat is reserved. If the seat is already reserved, a SeatException should be thrown.

The cancelReservation method will take an integer named seatNumber as a parameter and sets seat[seatNumber] to zero indicating that the seat is empty. If the seat is already empty, a SeatException should be thrown.

The cancelReservations method will take an integer array named seatNumbers as a parameter and sets all the seat numbers in the array to zero indicating that the seat is empty. If the seat is already empty, a SeatException should be thrown.

You should write a tester class that will have an instance of MovieReservation and TravelTicketReservation and then call the previous methods. You should use try and catch blocks to handle the exceptions that might be thrown in these methods.

For each instance, the tester class should read input from the user by using a scanner. For simplicity, make the number of seats ten. First, the user must enter ten numbers which are either zero or one, so use a for loop to read ten numbers and set the value of seats[i] to the read value.

Then, read an int value from the user and call reserveSeat passing the value as a parameter. Then, read four int values from the user and call reserveSeats passing the values as a parameter. Then do the same thing for cancelReservation and cancelReservations.

There are three types of exceptions that can happen when calling these functions. The first one is a SeatException as discussed earlier. The second one is an IndexOutOfBoundsException which will occur if the user enters a number that is less than zero or greater that seats.length. The third one is an InputMismatchException if the user enters a value which is not an integer.


After each call to the functions, you should print the seats array, or print "Seat number does not exist" if an IndexOutOfBoundsException occurs, or "Seat already reserved or empty" if a SeatException occurs, or "Input should be an Integer" if an InputMismatchException occurs.

Solution:

/**------------------Reservation.java--------------*/
public interface Reservation {
 
 public void reserveSeat(int seatNumber) throws SeatException;
 public void reserveSeats(int seatNumbers[]) throws SeatException;
 public void cancelReservation(int seatNumber) throws SeatException;
 public void cancelReservations(int seatNumbers[]) throws SeatException;
 
}

/**------------------MovieReservation.java---------*/
public class MovieReservation implements Reservation {

 private int seats[];
 
 public MovieReservation(int seats[]) {
  this.seats = seats;
 }
 
 @Override
 public void reserveSeat(int seatNumber) throws SeatException {
  if (seats[seatNumber] == 1)
   throw new SeatException("Seat already reserved");
  else
   seats[seatNumber] = 1;
 }

 @Override
 public void reserveSeats(int[] seatNumbers) throws SeatException {
  
  for (int i = 0; i < seatNumbers.length; i++) {
   if (seats[seatNumbers[i]] == 1)
    throw new SeatException("Seat already reserved");
   else
    seats[seatNumbers[i]] = 1;
  }
 }

 @Override
 public void cancelReservation(int seatNumber) throws SeatException {
  if (seats[seatNumber] == 0)
   throw new SeatException("Seat already empty");
  else
   seats[seatNumber] = 0;
 }

 @Override
 public void cancelReservations(int[] seatNumbers) throws SeatException {

  for (int i = 0; i < seatNumbers.length; i++) {
   if (seats[seatNumbers[i]] == 0)
    throw new SeatException("Seat already empty");
   else
    seats[seatNumbers[i]] = 0;
  }
 }

 public String toString() {
  return "";
 }
}

/**-------------TravelTicketReservation.java-------*/
public class TravelTicketReservation implements Reservation {

private int seats[];
 
 public TravelTicketReservation(int seats[]) {
  this.seats = seats;
 }
 
 @Override
 public void reserveSeat(int seatNumber) throws SeatException {
  if (seats[seatNumber] == 1)
   throw new SeatException("Seat already reserved");
  else
   seats[seatNumber] = 1;
 }

 @Override
 public void reserveSeats(int[] seatNumbers) throws SeatException {
  
  for (int i = 0; i < seatNumbers.length; i++) {
   if (seats[seatNumbers[i]] == 1)
    throw new SeatException("Seat already reserved");
   else
    seats[seatNumbers[i]] = 1;
  }
 }

 @Override
 public void cancelReservation(int seatNumber) throws SeatException {
  if (seats[seatNumber] == 0)
   throw new SeatException("Seat already empty");
  else
   seats[seatNumber] = 0;
 }

 @Override
 public void cancelReservations(int[] seatNumbers) throws SeatException {

  for (int i = 0; i < seatNumbers.length; i++) {
   if (seats[seatNumbers[i]] == 0)
    throw new SeatException("Seat already empty");
   else
    seats[seatNumbers[i]] = 0;
  }
 }

 public String toString() {
  return "";
 }
}

/**------------------SeatException.java------------*/
public class SeatException extends Exception {
 //simple
 public SeatException(String message) {
  super(message);
 }
 
}

/**------------------Tester.java-------------------*/
import java.util.InputMismatchException;
import java.util.Scanner;

public class Tester {

 public static void main(String[] args) {
  
  Scanner scan = new Scanner(System.in);
  
  int seats[] = new int[10];
  
  System.out.print("Please initialize seats: ");
  
  for (int i = 0; i < seats.length; i++)
   seats[i] = scan.nextInt();
  
  MovieReservation movieReservation = new MovieReservation(seats);
  
  try {
   System.out.print("Enter a seat number to reserve: ");
   int seatNumber = scan.nextInt();
   
   movieReservation.reserveSeat(seatNumber);
   System.out.println("Seat reserved successfully");
   
   /**-------------------------------------------*/
   
   System.out.println("Enter four seats to reserve: ");
   int seatNumbers[] = new int[4];
   
   for (int i = 0; i < seatNumbers.length; i++)
    seatNumbers[i] = scan.nextInt();
   
   movieReservation.reserveSeats(seatNumbers);
   System.out.println("Seats reserved successfully");
   
   /**-------------------------------------------*/
   
   System.out.print("Enter a seat number to cancel: ");
   seatNumber = scan.nextInt();
   
   movieReservation.cancelReservation(seatNumber);
   System.out.println("Seat cancelled successfully");
   
   /**-------------------------------------------*/
   
   System.out.println("Enter four seats to cancel: ");
   seatNumbers = new int[4];
   
   for (int i = 0; i < seatNumbers.length; i++)
    seatNumbers[i] = scan.nextInt();
   
   movieReservation.cancelReservations(seatNumbers);
   System.out.println("Seats cancelled successfully");
   
  } catch (SeatException e) {
   System.out.println("Error: could not reserve or cancel seat");
  } catch (ArrayIndexOutOfBoundsException e) {
   System.out.println("Error: entered number must be greater than -1 and less than 10");
  } catch (InputMismatchException e) {
   System.out.println("Error: entered value must be a number");
  }
  
  scan.close();
 }

}


1 comment :

  1. after liposuction Korea's #1 Liposculpture Clinic. Lydian plastic surgery is the home of VIP patients. Celebrities, Influencers and Diplomats all know and trust Doctor An and Lydian plastic surgery clinic to provide detailed results.

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact