Go Back   CodeCall Programming Forum > Software Development > Java Help
Register Blogs Search Today's Posts Mark Forums Read

Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-05-2009, 03:55 PM
Newbie
 
Join Date: Jun 2009
Posts: 9
mahoni is an unknown quantity at this point
Seat problem in an Airplane

The problem is described as follows:
Quote:
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();
    
		}
	}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-05-2009, 05:16 PM
Sinipull's Avatar
Programmer
 
Join Date: Jun 2009
Location: Estonia
Age: 21
Posts: 151
Sinipull will become famous soon enough
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Win7] Problem Steps Recorder Termana Tutorials 1 04-14-2009 10:24 AM
JNLP saveFileDialog problem d98tp Java Help 0 03-04-2009 06:10 AM
Peculiar UI Problem Needs Tackling adriyel C# Programming 2 04-06-2008 08:46 AM
Problem read pwd protected Access2K dbase - CR9 & VB6 mrbar Visual Basic Programming 2 03-10-2008 05:50 AM
How to tackle a programming problem? TcM General Programming 10 01-07-2008 12:29 PM


All times are GMT -5. The time now is 09:26 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0