Java Code For Airline Industry

Java Program Applicable for Airline Industry

In this assignment the students are required to write lines of code in Java programming language.  This  program finds its application in airlines industry. The question is asking students to write a java program which would be used by airline company while performing a check during passenger check in. The passengers check in with baggage of various dimensions and weight. The required program would accept  baggage dimensions, viz, length, breadth, and height. It would also accept the weight of baggage as input. The computer would process  input information and give the results whether the given baggage would be allowed to check in or not. The constraints are defined in the program. Maximum value of length, breadth, height is given. Maximum value of sum of these three dimensions is also defined and maximum permissible weight of baggage is also stated.  Finally the program would return the charge applicable on checking in with baggage if entry is allowed.  Multiple entry in computer is not allowed. However if students write a loop to accept multiple entries , it would be accepted by the examiner.

SOLUTION : –

public class Baggage {

public static final double COST_PER_POUND = 1.50;

public static final double MINIMUM_CHARGE = 5;

public static final int MINIMUM_DIMENSION = 1;

public static final int MAXIMUM_DIMENSION = 32;

public static final int MAXIMUM_TOTAL_DIMENSIONS = 60;

public static final int MINIMUM_WEIGHT = 1;

public static final int MAXIMUM_WEIGHT = 50;

private int length;

private int width;

private int height;

private int weight;

// Create a new baggage

public Baggage(int length, int width, int height, int weight) {

// Do validation

if (length < MINIMUM_DIMENSION || length > MAXIMUM_DIMENSION) {

throw new IllegalArgumentException(“Invalid length dimension.”);

}

if (width < MINIMUM_DIMENSION || width > MAXIMUM_DIMENSION) {

throw new IllegalArgumentException(“Invalid width dimension.”);

}

if (height < MINIMUM_DIMENSION || height > MAXIMUM_DIMENSION) {

throw new IllegalArgumentException(“Invalid height dimension.”);

}

if (length + width + height > MAXIMUM_TOTAL_DIMENSIONS) {

throw new IllegalArgumentException(“Invalid overall dimension.”);

}

if (weight < MINIMUM_WEIGHT || weight > MAXIMUM_WEIGHT) {

throw new IllegalArgumentException(“Invalid weight.”);

}

// If validation passed, assign the properties

this.length = length;

this.width = width;

this.height = height;

this.weight = weight;

}

// Compute the cost

public double getCost() {

double cost = weight * COST_PER_POUND;

if (cost > MINIMUM_CHARGE) {

return cost;

}

return MINIMUM_CHARGE;

}

// Return the sum of all dimensions

public int getTotalDimensions() {

return length + width + height;

}

// Return the length of baggage

public int getLength() {

return length;

}

// Return the width of baggage

public int getWidth() {

return width;

}

// Return the height of baggage

public int getHeight() {

return height;

}

// Return the weight of baggage

public int getWeight() {

return weight;

}

}

import java.util.Scanner;

public class Main {

private static Scanner keyboard = new Scanner(System.in);

// A utility function that forces the user to enter an integer value

private static int readInt(String prompt) {

while (true) {

try {

System.out.print(prompt);

return Integer.parseInt(keyboard.nextLine());

} catch (Exception e) {

System.out.println(“Error: Please enter an integer value.”);

}

}

}

// Entry point of the program

public static void main(String[] args) {

while (true) {

try {

System.out.println(“Please make sure that the total baggage dimensions is within ” + Baggage.MAXIMUM_TOTAL_DIMENSIONS);

int length = readInt(“Enter Baggage Length between ” + Baggage.MINIMUM_DIMENSION + “-” + Baggage.MAXIMUM_DIMENSION + “: “);

int width = readInt(“Enter Baggage Width between ” + Baggage.MINIMUM_DIMENSION + “-” + Baggage.MAXIMUM_DIMENSION + “: “);

int height = readInt(“Enter Baggage Height between ” + Baggage.MINIMUM_DIMENSION + “-” + Baggage.MAXIMUM_DIMENSION + “: “);

int weight = readInt(“Enter Baggage Weight between ” + Baggage.MINIMUM_WEIGHT + “-” + Baggage.MAXIMUM_WEIGHT + “: “);

Baggage baggage = new Baggage(length, width, height, weight);

System.out.println(“Baggage accepted, the total cost is: $” + String.format(“%.2f”, baggage.getCost()));

} catch (Exception e) {

System.out.println(“Baggage Rejected: ” + e.getMessage());

}

System.out.print(“Would you like to try again? (Y/N): “);

if (keyboard.nextLine().equalsIgnoreCase(“n”)) {

break;

}

}

}

}