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'"
Ok so there are a few things that I am not sure about. But first let me show you what I came up with so far:
//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 boolean[] seats;
private int firstSeat;
private int secondSeat;
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 ){
for( int counter = 0; counter < seats.length / 2; counter++ ){
//generate random number between 1 and 5
firstSeat = 1 + randomNumbers.nextInt(5);
seats[ firstSeat - 1 ] = true;//firstSeat - 1 to make sure the right element is selected
}
}
else if( seatClass == 2 ){
for( int counter = 0; counter < seats.length / 2; counter++ ){
//generates numbers between 6 and 10
secondSeat = 6 + randomNumbers.nextint(5);
seats[ secondSeat - 1 ] = true;//secondSeat - 1 to make sure the right element is selected
}
}
}//end of class
and this:
//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." );
//create message
String message = String.format( "Thank you. You have chosen %s class.", action );
//display message
JOptionPane.showMessageDialog( null, message );
assignSeats( action );
}
}
Obviously I am not finished yet as you can see.
When assignSeats() is called I use a for loop with a counter smaller than seats.length / 2 because I need to run it only 5 times. Now say the user selects 1 for first class:
assignSeats(), because of the loop, will assign all the first class seats and then when all the seats are filled in, will ask whether the user wants to be seated in the
second class. This happens on the first (and only) call. The thing is it will effectively be only one user taking potentially all the seats in the plane, do you know what I mean? Is that what the exercise is asking?
If so, then I will continue to develop it, but I would have thought that the program should have taken inputs from different users, then store them in memory somehow.
I don't understand if this is what the exercise wants me to do, what do you think? Just one very important thing to notice though: this exercise is on chapter 7 (Arrays)
so I haven't done a great deal so far and part of me thinks that the author wants to keep the exercise as simple as possible and take input from one user rather than
multiple ones, because storing values in memory might be too advanced for students at this point of the book. ANy advice's welcome as usual.
thanks