HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Airline Reservations

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
airlinereservationsstackoverflow

Problem

I am looking for general feedback on this small, simple program I wrote. It works perfectly, but I am striving for readable, clean code.

I also have specific questions:

  • Is the switch statement the most efficient/logical way to write this class?



  • Is my main method too large? Should I break it into smaller methods? How would you suggest splitting it up?



```
/**
* This AirlineReservation class assigns seats on an airplane and prints boarding passes.
* (1) A boolean array is used to store a seating chart and which seats are available
* (2) The user is asked if they want a first class reservation, second class reservation, or end
* (3) A switch statement case is selected based on the user's input
* (4) The requested class of flight is checked for available seats
* (a) seats 1-5 are first class
* (b) seats 6-10 are economy
* (5) if available, seat is booked and boarding pass is printed
* (6) if not available, user is notified and presented with the original 3 options.
*
* NOTE: I have commented out a method I was using to check my array for accuracy.
*/

import java.util.Scanner;
import java.util.Arrays;

public class AirlineReservations
{

public static void main(String[] args)
{
int totalSeats = 10;
int firstClassSeats = 5;
int economySeats = 5;
int classChoice = 0;
boolean continueInput = true;

Scanner input = new Scanner(System.in);

boolean[] seatingChart = new boolean[totalSeats];
Arrays.fill(seatingChart, false);

while (continueInput == true)
{
System.out.print("Please type 1 for First Class, 2 for Economy Class, or 3 to exit System: ");
classChoice = input.nextInt();
switch (classChoice)
{
case 1://first class requested
if (firstClassSeats > 0)//seat available
{
seatingChart[firstClassSeats-1]=true;
firstClassSeats--;
printBoardingPass((firstClassSeats+1), classC

Solution

Is my main method too large? Should I break it into smaller methods? How would you suggest splitting it up?

Your main method isn't too large; it is doing too many different things.

It is more typical in a java program to use the main method to set up an instance of a class that is going to do the work, and then hand off control of the program to that object.

In this case, main is initializing your seat map, running your input/output loop, providing your business behavior.


Is the 'switch' statement the most efficient/logical way to write this class

Probably not -- there are two problems here. First is that you are switching on an int, where the int is code for something else. There's no particular reason that "1" should be first class and "2" should be economy -- that's just an arbitrary encoding required by this assignment. A better choice would be to use an enum to represent the different seating classes.

public enum SeatingClass {
    FIRST_CLASS, ECONOMY
}


The second problem here is that you are using the switch to drive changes in behavior. That usually means that there is a class hiding in the code, waiting for you to notice it. In this case, there are two: first is the fact that what you are really doing is converting the inputs to Commands.

Also, notice that case 1 and case 2 are doing exactly the same behavior, but with different data. That indicates that there should be a class that knows how to provide that behavior, and two different instances of it with different data values. So here, you should have some sort of SeatSectionAssigner; each has a number of seats and an offset into the seat map that determines which seats get assigned.


Any improvement/feedback is greatly appreciated, just keep in mind I am a little more than a month into learning java

Look for ways to decompose programs into classes that do one thing.


I found the exact same assignment (Airline Reservations System) but it was really difficult for me to follow the program. Maybe that comes with programming experience? Reading other's code with ease, that is. But there isn't a "main" method there.

Yes, that code doesn't do anything on its own. The implied assumption is that somewhere else is some code that looks like

Airline airline = new Airline();
airline.start();

Code Snippets

public enum SeatingClass {
    FIRST_CLASS, ECONOMY
}
Airline airline = new Airline();
airline.start();

Context

StackExchange Code Review Q#107973, answer score: 3

Revisions (0)

No revisions yet.