Hi everyone .. so i'm creating this project for college it's a system for airplane reservation
i'm using multi dimensional array of objects i have two classes ticket and passenger ..
this is the main

/**
 * Name = Rami sohail Mohammed Jamous
 * ID = 1324057
 * Java 203 Project 2
 * Section : AA
 * project KAUairBookingSystem
 * 02/11/2012
 */

package kauairbookingsystem;
import java.util.Scanner;

public class KAUairBookingSystem {


    public static void bookfirstclass(Ticket[][] tc,Scanner input,double ff)
    {
      System.out.println("How many tickets would you like in First Class:");
         int seat = input.nextInt();
         System.out.println("You must now enter the passenger information for each ticket/seat");
         int psngrnumber = 1;


         for(int i = 0;i<2;i++)
         {

             for(int j = 0;j<7;j++)
             {
          if (tc[i][j] == null) 
                     {
                         System.out.println("\nPersonal Data for Passenger #" + psngrnumber++ );
                         System.out.print("Enter the First Name : ") ; 
                         String fname = input.next();
                         System.out.print("Enter the last Name : ");
                         String lname = input.next();
                         System.out.print("Enter the ID number : ");
                         int id = input.nextInt();

                         Passenger psn = new Passenger();
                         psn.setfname(fname);
                         psn.setlname(lname);
                         psn.setid(id);
                         tc[i][j] = new Ticket();
                         seat--;
                         System.out.print(fname + " " + lname + " is Confirmed with Ticket #Kau" +  tc[i][j].getTicketNumber());
                         System.out.print("");
                           if(seat == 0)
                           {
                               i = 2;
                               j = 7;
                           }

                     }  

          else
          {
              System.out.println("No Seats Available " + seat + " Seats Couldn't be reserved.");            
          }

             }
         }
    }
    public static void ShowMenu()
    {
        System.out.println("Welcome To The KAU Air --- Seat Reservation System");
        System.out.println("-------------------------------------------\n" +
                           "1. Book First Class Seats\n" +
                           "2. Book Buisness Class Seats\n" +
                           "3. Book Economy Class Seats\n" +
                           "4. View Air Plane Seating assignment\n" +
                           "5. View Passenger list\n" +
                           "6. System Status\n" +
                           "7. Know Fair\n" +
                           "8. System Reset\n" +
                           "9. Exit From The Booking System\n\n" +
                           "Enter Your Choice :" );
    }

    public static void displayseats(Ticket[][] tc)
    {
        System.out.println("\nView Air Plane Seating Assignment");
        System.out.println("-------------------------------------------");
        for(int i=0;i<tc.length;i++)
        {
            for(int j=0;j<tc[i].length;j++)
            {
                if(tc[i][j] == null)
                {
                   System.out.print(0 + "    "); 
                }
                else
                {
                   System.out.print(1 + "    "); 
                }

            }

            System.out.println();
        }
        System.out.println("-------------------------------------------\n");
    }

    public static void main(String[] args)
    {
     Ticket [][] tc = new Ticket[12][7];

     while(true)
     {
     ShowMenu();   
     Scanner input = new Scanner(System.in); 
     int choice = input.nextInt();

       if(choice == 1)
        {
        bookfirstclass(tc,input,100);
        displayseats(tc);    
        }

       if(choice == 2)
        {

        }
       if(choice == 3)
        {

        }

       if(choice == 4)
        {
            displayseats(tc);
        }

       if(choice == 5)
        {

        }

       if(choice == 6)
        {

        }

       if(choice == 7)
        {

        }

       if(choice == 8)
        {

        }

       if(choice == 9)
      {

      }
    }
}}

my problem is with the bookfirstclass method
i want the if condition to check how many seats available and the entered number of seats if the emty seats are not enough it prints an error message but now it just keep on reserving seats whether it's full or not ..

Edited 4 Years Ago by rami sohaill

Nothing in your description or your code has any information about Seats (although the array of Tickets looks like it may correspond to seat positions? Tickets certainly do not come in 12x7 grids!).
Maybe you need a Seat class that has, at the very least, an available/ticketed flag. Each flight will then have arrays of seats for each class. That way you can see how many / which seats are available, and associate a Ticket with a Seat and vice-versa.

i can't do that this is what the project tells me to do in the bookfirstclass method

public static void bookFirstClass(Ticket[][] ticket, Scanner input, double ff) We send to this method three parameters: (1) a reference to the 2-d array of tickets, (2) a reference to the Scanner object, and (3) the cost of the first class fair (ff). You must first ask the customer how many seats they want, and then you will use the Scanner variable, input, to scan/save their input. The customer can requested more than one seat, and you must first make sure the seats are available. If the seats are available, you will create a new Ticket object for each seat requested. Once you create a new Ticket object, you must then scan the first name, last name, and ID of the passenger and save them into the Passenger object. Finally, you will print a confirmation message. This message should print the first name, last name, ID, seat number, and ticket number for the passenger. (see sample output file). If the seats are not available, you should print an appropriate message (see sample output file).

this is how the output should be

Welcome To KAU Air --- Seat Reservation System

  1. Book First Class Seats
  2. Book Business Class Seats
  3. Book Economy Class Seats
  4. View Air Plane Seating Assignment
  5. View Passenger List
  6. System Status
  7. Know Fair
  8. System Reset
  9. Exit From The Booking System

Enter Your Choice: 1

How many tickets would you like in First Class: 15
Sorry, we do not have enough seats in First Class to process your request.

What a rubbish design! But I understand that yo have no choice.
Your Ticket[][] array will initially contain all null values. When you create a Ticket you store it in the appropriate element of that array. At any time you can count the number of elements that are still null, and that's the number of available seats.

i understand what you said but what is the code to count how many null left and compare it to seat and if it's not enough print error message and if it is reserve the seats ..

Loop through all the array elements and if they are null increment a counter. I'm sure you know how to compare two ints and print a message.

This article has been dead for over six months. Start a new discussion instead.