Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A simple boarding pass....

 
David J. Gonzo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again..
I am attempting to design a simple console program that does the following.

A small airline has just purchased a computer for its new automated reservations system. The president has asked you to program the new system in Java. You are to write a program to assign seats on each flight of the airline�s only plane (capacity: 10 seats). Your program should display the following menu
of alternatives:

Please type 1 for �smoking�
Please type 2 for �nonsmoking�

If the person types 1, then your program should assign a seat in the smoking section (seats 1-5). If the person types 2, then your program should assign a seat in the nonsmoking section (seats 6-10). Your program should then print a boarding pass indication the person�s seat number and whether it is in smoking or nonsmoking section of the plane. Use a single subscript array to represent the seating char of the plane. Initialize all the elements of the array to 0 to indicate all seats are empty. As each seat is assigned, set the corresponding elements of the array to 1 to indicate that the seat is no longer available.Your program should, of course, never assign a seat that has already been assigned. When the smoking section is full, your program should ask the person if
it is acceptable to be placed in the nonsmoking section (and vice versa). If yes, then make the appropriate seat assignment. If no, then print the message �Next flight leaves in 3 hours�


This is what I have so far. It compiles but hangs. I could use some advice as to wether it is possible to get it to work this way or should I go in a completely different direction.


 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David J. Gonzo:
... It compiles but hangs...

Actually, the first problem is that it doesn't hang. After it assigns the first seat then asks whether the user wants to book another, it immediately ends without waiting for a response. I suggest you look at some alternatives to using System.in.read(), which reads one byte at a time.

Are you allowed to use new Java 1.5 features?

On a more general level, I suggest that you consider this as an object (with its own fields and methods) rather than one big process stuffed into main. Break it down by writing and testing one method at a time.
[ February 16, 2005: Message edited by: marc weber ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the following method or something similar for reading input from the console:

String getUserInput(){
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader stream = new BufferedReader( input );
String answer = new String();

try{
answer = stream.readLine();
}
catch (java.io.IOException e){
return answer;
}
return answer;
}

Merrill
 
David J. Gonzo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You so much...

I will use the advice. This should keep me busy for a while...
 
David J. Gonzo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O.K

I think I am on my way. Could you take a look at the following and tell me what you think? I am getting a ';' error, and cannot seem to find it...

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The compiler is telling you that on line 14 of your code, it's expecting a semicolon between "getUserInput" and the parentheses. In other words, it's expecting a field declaration here rather than a method. Why? Because you're already in a method (main), and you can't define methods within methods.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main problem with this program is that you are trying to put methods inside other methods.

The Smoking(), NonSmoiking() and getUserInput() must all be placed after the StartOut() method, not inside it as is currently the case.

Also, get in the habit of following java naming conventions: The class name should start with an Upper-Case letter (BoardingPassStep) and all method names and attribute names should start with lower case letters: (smoking(), nonSmoking(), ect).

You're going to have to un-tangle this and get the methods separated out before you can go any further with this.

Here's a basic outline to get you started:

import java.io.*;
import java.util.*;
import java.lang.*;
public class BoardingPassStep {
BufferedReader stream = new BufferedReader(new InputStreamReader(System.in));
String[] booking = new String[10];
public static void main(String[] arguments) {
BoardingPassStep pass = new BoardingPassStep();
pass.StartOut();
}
private void StartOut() {
String answer;
boolean done = false;
while (!done) {
System.out.println("Please type 1 for 'smoking' section or 2 for 'non-smoking' section. Press 'enter' when done.");
answer = getUserInput();
if (Integer.parseInt(answer)==1) {
smoking();
} else {
nonSmoking();
}
}
}
private void smoking() {
}
private void nonSmoking() {
}

private String getUserInput() {
String answer = new String();
try {
answer = stream.readLine();
} catch (java.io.IOException e) {
return answer;
}
return answer;
}
}

Merrill

[ February 16, 2005: Message edited by: Merrill Higginson ]
[ February 16, 2005: Message edited by: Merrill Higginson ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the start of one possible approach to breaking the process down and writing methods to perform specific tasks. Note how the handleCustomer() method calls the other methods as needed. I think you'll see how much easier this would be to develop (and maintain) than one huge process in a single method.

[ February 17, 2005: Message edited by: marc weber ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic