patternjavaMinor
Airline Reservations System
Viewed 0 times
airlinesystemreservations
Problem
Would it be possible to get some feedback on some code I've just written? I've been working my way through Deitels' Java How to Program for the past few months and thought I'd better ask for some help before my bad coding habits get too ingrained.
The current problem I've just been working on is this:
(Airline Reservations System) A small airline has just purchased a computer for its new automated reservations system. You have been
asked to develop the new system. You are to write an application to
assign seats on each flight of the airline’s only plane (capacity: 10
seats).
Your application should display the following alternatives:
If the user types 1, your application should assign a seat in the
first-class section (seats 1–5). If the user types 2, your application
should assign a seat in the economy section (seats 6–10). Your
application should then display a boarding pass indicating the
person's seat number and whether it is in the first-class or economy
section of the plane.
Use a one-dimensional array of primitive type
the seating chart of the plane. Initialize all the elements of the
array to false to indicate that all the seats are empty. As each seat
is assigned, set the corresponding elements 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, your application should
ask the person if it is acceptable to be placed in the first-class
section (and vice versa). If yes, make the appropriate seat
assignment. If no, display the message "Next flight leaves in 3
hours."
Below is what I've come up with. Please feel free to tear it apart.
```
import java.util.Scanner;
public class Airline
{
boolean[] seating = new boolean[11]; /* create 10 seat numbers (array[0] will n
The current problem I've just been working on is this:
(Airline Reservations System) A small airline has just purchased a computer for its new automated reservations system. You have been
asked to develop the new system. You are to write an application to
assign seats 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"
- "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 the user types 2, your application
should assign a seat in the economy section (seats 6–10). Your
application should then display a boarding pass indicating the
person's seat number and whether it is in the first-class or economy
section of the plane.
Use a one-dimensional array of primitive type
boolean to representthe seating chart of the plane. Initialize all the elements of the
array to false to indicate that all the seats are empty. As each seat
is assigned, set the corresponding elements 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, your application should
ask the person if it is acceptable to be placed in the first-class
section (and vice versa). If yes, make the appropriate seat
assignment. If no, display the message "Next flight leaves in 3
hours."
Below is what I've come up with. Please feel free to tear it apart.
```
import java.util.Scanner;
public class Airline
{
boolean[] seating = new boolean[11]; /* create 10 seat numbers (array[0] will n
Solution
I have several suggestions:
-
For readability I would suggest to introduce
If you have to make several decisions depending on the seats type
-
You are doing twice the same thing: Iterating through a partial array.
It would be more streamlined if you write a method for both cases, like:
This makes the following possible:
So your code becomes much smaller. And the number of if-else-whatelse decreases a lot.
-
For readability I would suggest to introduce
enums likepublic enum SeatType {
FIRSTCLASS, ECONOMY }If you have to make several decisions depending on the seats type
SeatType.FIRSTCLASS reads better, than perhaps `seatnumber-
You are doing twice the same thing: Iterating through a partial array.
It would be more streamlined if you write a method for both cases, like:
private Integer tryReservation(SeatType s) {
Integer reservation=null;
int start=(s==SeatType.FIRSTCLASS)?firstClassStart:economyStart;
int stop=start+capacity;
for(int number=start;number<stop;number+=1){
if(seats[number]==free){
seats[number]=!free;
reservation=number;
break;
}
}
return reservation;
}This makes the following possible:
private Integer makeReservation(SeatType s) {
SeatType alternative=getAlternative(s);
Integer reservedSeat=tryReservation(s);
if(reservedSeat==null && !isClassFull(alternative)){
if(alternativeWanted){
reservedSeat=tryReservation(alternative);
}
}
return reservedSeat;
}So your code becomes much smaller. And the number of if-else-whatelse decreases a lot.
Code Snippets
public enum SeatType {
FIRSTCLASS, ECONOMY }private Integer tryReservation(SeatType s) {
Integer reservation=null;
int start=(s==SeatType.FIRSTCLASS)?firstClassStart:economyStart;
int stop=start+capacity;
for(int number=start;number<stop;number+=1){
if(seats[number]==free){
seats[number]=!free;
reservation=number;
break;
}
}
return reservation;
}private Integer makeReservation(SeatType s) {
SeatType alternative=getAlternative(s);
Integer reservedSeat=tryReservation(s);
if(reservedSeat==null && !isClassFull(alternative)){
if(alternativeWanted){
reservedSeat=tryReservation(alternative);
}
}
return reservedSeat;
}Context
StackExchange Code Review Q#25978, answer score: 5
Revisions (0)
No revisions yet.