HI all, I was wondering if anybody can help me to understand this exercise:
"A small airline has just purchased a computer for its new automated reservation system. Write an application to assign seas on each flight of the airline's only plane
(capacity: 10 seats).
Your application should display the following alternatives: 'Please type 1 for First class and please type 2 for economy'. If the user types 1, your application
should assign a seat in the first class section (seats 1-5), if he types 2 in the economy section ( seats 6-10). Your application should then display
a boarding pass indicating the person's seat number and whether it's in the first-class or economy.
Use a 1 dimensional array of primitive type boolean to represent the seating chart of the plane. Initialize all the elements to false to indicate that all the
seats are empty. As each seat is assigned set the corresponding element of the array to true to indicate that the seat is no longer available. Your application
should never assign a seat that has already been assigned. When the economy section is full ask the person if it is acceptable to be placed in the first class section
and vice versa. If yes make the appropriate sear assignment. If not display the message 'next flight leaves in 3 hrs'"
Couple of things before I show you where I got to:
1)I'd like to clarify that I am not on any course and therefore have no assignments as such to do, I am just programming in java as a hobby.
2)I am reading "Deitel and Deitel how to program in java" and I am at chapter 7 at the moment which is where I got the exercise from
3)I appreciate that there are many many ways to solve the above, but my choices are pretty limited at the moment, so I will have to try to stick to what the author wants as much as I can.
Ok, so I got a little stuck, here is the code, then I will explain what I have done and why I am not sure how to proceed:

Java Code:
//ex 7.19 p 333 deitel and deitel
//SeatsTest.java
import javax.swing.JOptionPane;
public class SeatsTest{
	public static void main( String[] args ){
		Seats seatingPlan = new Seats();
		
		
		//display message
		String action = JOptionPane.showInputDialog( "Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
		//create message			
		String message = String.format( "Thank you. You have chosen %s class.", action );
		//display message
		JOptionPane.showMessageDialog( null, message ); 				
		while(action != 0){
			seatingPlan.assignSeats( action );		
			//ask for input again
			action = JOptionPane.showInputDialog( "Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
			//create message			
			message = String.format( "Thank you. You have chosen %s class.", action );
			//display message
			JOptionPane.showMessageDialog( null, message ); 				
		
			
		}		
	}
}
and here's the other file
Java Code:
//ex 7.19 p 333 deitel and deitel
//Seats.java
import java.util.Random;
public class Seats{
	private int final NUMBER_OF_SEATS = 10;
	private int final PASSENGER_CLASSES = NUMBER_OF_SEATS / 2;//to get first class seats(first 5 seats of the array) and second class (last 5 seats of array)
	private boolean[] seats;
	private int firstSeat;
	private int secondSeat;
	//private boolean seatAvailable;
	Random randomNumbers = new Random();
	
	//constructor
	public Seats(){
		//initializing array
		seats = new boolean[ NUMBER_OF_SEATS ];
		//set values to false - doesn't it do automatically?
		for( int counter = 0; counter < seats.length; counter++ ){
			seats[ counter ] = false;
		}//end of loop		
	}//end of constructor
	
	public void assignSeats( int seatClass ){
		if( seatClass == 1 ){
			firstSeat = generateFirstClassSeats();
			int trueValuesCounter;//will check if all the values of the array are true
			boolean theSeat = seats[ firstSeat - 1 ];
			while(theSeat){
				trueValuesCounter++;
				firstSeat = generateFirstClassSeats();
				theSeat = seats[ firstSeat - 1 ];
				if(trueValuesCounter == PASSENGER_CLASSES)
				break;
			}					
			seats[ firstSeat - 1 ] = true;		
		}
		
		else if( seatClass == 2 ){			
			secondSeat = generateSecondClassSeats();
			int trueValuesCounter;//will check if all the values of the array are true
			boolean theSeat = seats[ secondSeat - 1 ];
			while(theSeat){
				trueValuesCounter++;
				secondSeat = generateSecondClassSeats();
				theSeat = seats[ secondSeat - 1 ];
				if(trueValuesCounter == PASSENGER_CLASSES)
				break;
			}
		seats[ secondSeat - 1 ] = true;	
		}
	}//end of assignSeats
	
	//generate first class seats
	private int generateFirstClassSeats(){
		//generate random number between 1 and 5
		int theSeat = 1 + randomNumbers.nextInt(PASSENGER_CLASSES);
		return theSeat;
	}
	//generate second class seats
	private int generateSecondClassSeats(){
		//generates numbers between 6 and 10
		int theSeat = 6 + randomNumbers.nextint(PASSENGER_CLASSES);
		return theSeat;
	}
	

	
}//end of class
Right. in the first file I use a while look so that I keep the program running till the user enters 0 to quit the application and then call assignSeats( ) that gets things going. That's where I have problems. You see I have to make sure that I don't reassign the same seat again of course and this is what I am attempting to do in here:
Java Code:
public void assignSeats( int seatClass ){
		if( seatClass == 1 ){
			firstSeat = generateFirstClassSeats();
			int trueValuesCounter;//will check if all the values of the array are true
			boolean theSeat = seats[ firstSeat - 1 ];
			while(theSeat){
				trueValuesCounter++;
				firstSeat = generateFirstClassSeats();
				theSeat = seats[ firstSeat - 1 ];
				if(trueValuesCounter == PASSENGER_CLASSES)
				break;
			}					
			seats[ firstSeat - 1 ] = true;		
		}
		
		else if( seatClass == 2 ){			
			secondSeat = generateSecondClassSeats();
			int trueValuesCounter;//will check if all the values of the array are true
			boolean theSeat = seats[ secondSeat - 1 ];
			while(theSeat){
				trueValuesCounter++;
				secondSeat = generateSecondClassSeats();
				theSeat = seats[ secondSeat - 1 ];
				if(trueValuesCounter == PASSENGER_CLASSES)
				break;
			}
		seats[ secondSeat - 1 ] = true;	
		}
	}//end of assignSeats
Because the exercise 'divides' the array of seats in 2 (first class and second class) I then count up to 5 like in
Java Code:
if(trueValuesCounter == PASSENGER_CLASSES)
and in
Java Code:
int theSeat = 1 + randomNumbers.nextInt(PASSENGER_CLASSES);
I just want to draw your attention on this fragment:
Java Code:
while(theSeat){
				trueValuesCounter++;
				firstSeat = generateFirstClassSeats();
				theSeat = seats[ firstSeat - 1 ];
				if(trueValuesCounter == PASSENGER_CLASSES)
				break;
			}					
			seats[ firstSeat - 1 ] = true;
As said above I am trying not to assign the same seat twice. Here if a seat has been assigned already I call again generateFirstClassSeats() and get another value so that when
Java Code:
theSeat
becomes false I get out of the loop. When instead all the seats have been assigned
Java Code:
trueValuesCounter
will have a value of 5 and therefore I will come out of the loop anyway. Problem is that when all the seats are assigned this
Java Code:
seats[ firstSeat - 1 ] = true;
will effectively reassign the seat again, and that shouldn't happen but I can't find a way around this. I spent the whole evening trying to rearrange the code but I can't find a solution to that. What do you guys think?
thanks