ok no worries. So, I compiled and run everything. The good news is that I managed to remove all the compiling errors and the program even runs! The bad news is that it doest't run the way it should, I must have made some logic error somewhere, and I think I know where, in the switch satement:
switch( classRequested ){
case 1:
//ask user to change class
action = JOptionPane.showInputDialog( "Sorry, the first class is full. Do you want to have a seat in second class? Type y for yes and n for no." );
userChoice = String.format( "\nYou have selected %s", action );
userInput = userChoice.charAt(0);//convert user input to char
JOptionPane.showMessageDialog( null, userChoice );
if( userInput == 'y' ){
seatingPlan.assignSeat( SECOND_CLASS_SELECTION );
}
else{
//System.out.println( " The next plane leaves in 3 hrs " );
break;
}
//break;
case 2:
//ask user to change class
action = JOptionPane.showInputDialog( "Sorry, the second class is full. Do you want to have a seat in first class? Type y for yes and n for no." );
userChoice = String.format( "\nYou have selected %s ", action );
userInput = userChoice.charAt(0);//convert user input to char
JOptionPane.showMessageDialog( null, userChoice );
if( userInput == 'y' ){
seatingPlan.assignSeat( FIRST_CLASS_SELECTION );
}
else{
//System.out.println( " The next plane leaves in 3 hrs " );
break;
}
Right, userChoice
in userChoice = String.format( "\nYou have selected %s", action );
is declared as a string and userInput = userChoice.charAt(0);
(userInput is a char) simply convert a string to a char. Maybe I am not doing it correctly but what happens if you run the program is that if the first (or second for that matter) class is full and the user answers 'y' to "Do you want to have a seat in second/first class? Type y for yes and n for no" this if( userInput == 'y' )
- for whatever reason - is always false resulting in the else to be executed. I found out this because of this line JOptionPane.showMessageDialog( null, userChoice );
that correctly returns 'y' or 'n'. But, the moment I change that to this JOptionPane.showMessageDialog( null, userInput );
as it should be, it returns nothing so if( userInput == 'y' )
will never be true and everything falls apart...why is that?
I include the files below (I don't understand why on this forum I can't attach any file in a reply...!):
//ex 7.19 p 333 deitel and deitel
//Seats.java
public class Seats{
private final int NUMBER_OF_SEATS = 10;
private boolean[] seats;
//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 int assignSeat( int type){
int seatPlace = -1; //default seatPlace value
int firstI = 0; // firstIndex
int lastI = 4;
if ( type == 2 ){
firstI += 5;//second index
lastI += 5;
}
for ( int i = firstI; i <= lastI; i++ ){
if ( !seats[i] ){ // the element = false? this seat hasn't been assigned yet, assign it
seats[i] = true;
if ( type == 1 ){
System.out.printf( "You will seat in firs class on seat number %d\n" , ( i + 1 ) );
}
else if ( type == 2 ){
System.out.printf( "You will seat in second class on seat number %d\n" , ( i + 1 ) );
}
return seatPlace = i; // flag sucessfully seat assignment
}
}
// if the method reaches this point, it means no seats were still available,
return seatPlace;
}//end of assignSeats
}//end of class
and the other one:
//ex 7.19 p 333 deitel and deitel
//SeatsTest.java
import javax.swing.JOptionPane;
public class SeatsTest{
//constants
static final int FIRST_CLASS_SELECTION = 1;//first class
static final int SECOND_CLASS_SELECTION = 2;//second class
public static void main( String[] args ){
Seats seatingPlan = new Seats();
//int firstCounter = 0;//keep the seat count
//int secondCounter = 0;
//display message
String action = JOptionPane.showInputDialog( "Please type 1 for First Class and 2 for Economy Class, 0 to quit" );
//flag determines whether seats have been assigned or not
int seatAssigned = 0;
//create message
String message = String.format( "Thank you. You have chosen %s class.", action );
//display message
JOptionPane.showMessageDialog( null, message );
//convert string to int
int classRequested = Integer.parseInt( action );
while( classRequested != 0){
//call function with class chose by user
seatAssigned = seatingPlan.assignSeat( classRequested );
/* if( seatAssigned != -1 ){
switch( classRequested ){
case 1:
firstCounter++;
break;
case 2:
secondCounter++;
break;
}
} */
if( seatAssigned == -1 ){
//contains y (assign seat in the other class )or n (dont' assign seat in the other class)
String userChoice;
//to convert user input from string to char
char userInput;
//switch statement to assign seats of the other class if one is full
switch( classRequested ){
case 1:
//ask user to change class
action = JOptionPane.showInputDialog( "Sorry, the first class is full. Do you want to have a seat in second class? Type y for yes and n for no." );
userChoice = String.format( "\nYou have selected %s", action );
userInput = userChoice.charAt(0);//convert user input to char
JOptionPane.showMessageDialog( null, userChoice );
if( userInput == 'y' ){
seatingPlan.assignSeat( SECOND_CLASS_SELECTION );
}
else{
//System.out.println( " The next plane leaves in 3 hrs " );
break;
}
//break;
case 2:
//ask user to change class
action = JOptionPane.showInputDialog( "Sorry, the second class is full. Do you want to have a seat in first class? Type y for yes and n for no." );
userChoice = String.format( "\nYou have selected %s ", action );
userInput = userChoice.charAt(0);//convert user input to char
JOptionPane.showMessageDialog( null, userChoice );
if( userInput == 'y' ){
seatingPlan.assignSeat( FIRST_CLASS_SELECTION );
}
else{
//System.out.println( " The next plane leaves in 3 hrs " );
break;
}
//break;
}//end of switch
break;
/*if( ( firstCounter == 5 ) && ( secondCounter == 5) ){//the flight is full
System.out.println( "\nSorry the flight is full, the next one leaves in 3hrs\n" );
break;//leaves the while loop
}
*/
}//end of if( seatAssigned == -1 )
//ask for input again
action = JOptionPane.showInputDialog( "More seats? 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 );
classRequested = Integer.parseInt( action );
}//end of while
System.out.println( " The next plane leaves in 3 hrs " );
}//end of main
}//end of class