+ Reply to Thread
Results 1 to 2 of 2

Thread: Seat problem in an Airplane

  1. #1
    Newbie mahoni is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    9

    Seat problem in an Airplane

    The problem is described as follows:
    Write a program to assign passengers seats in an airplane. Assume a small airplane with seat numberings as follows:
    1 A B C D
    2 A B C D
    3 A B C D
    4 A B C D
    5 A B C D
    6 A B C D
    7 A B C D

    The program should display the seat pattern, with an 'X' marking the seats already assigned. For example, after seats 1A, 2B, and 4C are taken, the display should look like:
    1 X B C D
    2 A X C D
    3 A B C D
    4 A B X D
    5 A B C D
    6 A B C D
    7 A B C D

    After displaying the seats available, the program should prompt for the seat desired, the user can type in a seat, and then the display of available seats should be updated. This continues until all seats are filled or until the user signals that the program should end. If the user types in a seat that is already assigned, the program should say that that seat is occupied and ask for another seat.
    And for this problem I decided to have a Seat class to provide a 2D Seat array in driver and make use of it. Actually there is no big problem, I nearly implemented all the requirements but there is only one problem within giving a message to the user that "it is taken" when a previously taken seat is chosen. I do not understand what the problem is with this code, can anybody help me? Seat and Airplane(driver) classes that I wrote is below:

    Code:
    public class Seat {
    	
    	private boolean visible;
    	private String name;
    
        public Seat(String name) {
        	visible=true;
        	this.name=name;
        }
        
        public void setVisible(boolean b){
        	visible=b;
        }
        public String getName(){
        	return name;
        }
        public boolean getVisible(){
        	return visible;
        }
        
        public String toString(){
        	if(visible)
        		return name;
        	else
        		name="X";
        		return name;	
        		
        }
        
        
    }
    Code:
    import java.util.*;
    
    public class Airplane {
    	public static void main (String[] args) {
    
    		Scanner scan=new Scanner(System.in);
    		Seat[][]array=new Seat[7][4];
    		Seat desiredSeat;
    		int count=0;
    		int row;
    		String name;
    				
    		for (int i = 0; i<7; i++){
    			array[i][0]=new Seat("A");
    			array[i][1]=new Seat("B");
    			array[i][2]=new Seat("C");
    			array[i][3]=new Seat("D"); 
    	  	}
    		do{
    			do{
    				printArray(array);
    				System.out.println("Which place do you want to take: ");
    				row=scan.nextInt();
    				name=scan.next();
    			}while(row>7);
    			
    			for(int i=0;i<4;i++){
    				if(array[row-1][i].getName().equalsIgnoreCase(name)){
    					if(array[row-1][i].getVisible()==false){
    						System.out.println("The seat is already taken, please select another one.");
    						
    					}	
    					else{
    						array[row-1][i].setVisible(false);
    						count++;
    					}	
    				}
    			}
    		}while(count!=28);
    		printArray(array);
    		System.out.println("All seats have been sold!!!");	
    	}
    	
    	
    	public static void printArray(Seat[][] a){
    		for(int i=0;i<7;i++){
    	  		System.out.print(i+1+" ");
    	  		for(int j=0;j<4;j++){
    	  			System.out.print(a[i][j]+" ");
    	  		}
    	  		System.out.println();
        
    		}
    	}
    }

  2. #2
    Programmer Sinipull will become famous soon enough Sinipull's Avatar
    Join Date
    Jun 2009
    Location
    Estonia
    Age
    21
    Posts
    168

    Re: Seat problem in an Airplane

    Code:
     public String toString(){
        	if(visible)
        		return name;
        	else
        		name="X";
        		return name;	
        		
     }
    i see a big problem here. Why would you change the seat name to X when it is taken? I mean you don't just display it as X, you literally change the seat's name to X and original name is lost.

    Code:
     public String toString(){
        	if(visible)
        		return name;
        	else
        		return "X";	
        		
     }
    This should fix it.

    "The seat is already taken, please select another one." works..

    you should also catch the exceptions, it's really annoying if program crashes on false inputs.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

     

Similar Threads

  1. [Win7] Problem Steps Recorder
    By Termana in forum Tutorials
    Replies: 1
    Last Post: 04-14-2009, 09:24 AM
  2. JNLP saveFileDialog problem
    By d98tp in forum Java Help
    Replies: 0
    Last Post: 03-04-2009, 05:10 AM
  3. Peculiar UI Problem Needs Tackling
    By adriyel in forum C# Programming
    Replies: 2
    Last Post: 04-06-2008, 07:46 AM
  4. Problem read pwd protected Access2K dbase - CR9 & VB6
    By mrbar in forum Visual Basic Programming
    Replies: 2
    Last Post: 03-10-2008, 04:50 AM
  5. How to tackle a programming problem?
    By TcM in forum General Programming
    Replies: 10
    Last Post: 01-07-2008, 11:29 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts