×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Create a Program to Create Holiday Destination Booking System in Java Assignment Solution

July 08, 2024
Dr. Marlowe Emberly
Dr. Marlowe
🇬🇧 United Kingdom
Java
Dr. Marlowe Emberly is a trailblazer in the field of computer science, having earned her PhD from the University of Cambridge, UK, renowned for its excellence in research and academia. With eight years of experience in the industry, Marlowe has completed over 800 Java Homework assignments with unparalleled dedication and expertise.
Key Topics
  • Instructions
  • Requirements and Specifications
  • Scenario: Holiday Destinations
  • Problem Specification
Tip of the day
Familiarize yourself with OCaml's pattern matching; it simplifies handling recursive data structures like lists and trees, making your code concise and easier to debug.
News
In 2024, Girls Who Code introduced a Data Science + AI track in their free summer programs for high school students, fostering skills in cybersecurity and creative coding​

Instructions

Objective

Write a program to create holiday destination booking system in java language.

Requirements and Specifications

Holiday Destinations

Java Version

Your code will be tested with Java 11 (what you've most likely been using already if you've followed the instructions on this module). You are strongly recommended to use Java 11 to develop your submission. If you use a later version of Java, you risk using a feature that is not present in Java 11, which means your code may not compile and will not work when tested for marking. If this happens your code will be classed as not compiling and not working. At the very least it is your responsibility to check this if you use a version newer than 11.

Objectives

The objectives of this piece of work are for you to demonstrate that you can:

  • Define classes, fields, constructors and methods appropriately in Java.
  • Use appropriate types, including collections
  • Implement basic algorithms using collections
  • Write a main method including console/terminal input and output
  • Document your solution with appropriate comments

You should note that at the time of release of this work, you are still learning some of the concepts and materials relevant to this piece of work. For example, classes, constructors and objects. You may need to research relevant Java Class libraries in order to get help with certain features.

Scenario: Holiday Destinations

Countries around the world have a host of holiday destinations available to the general public. With the eventual loosening of travel restrictions, these holiday destinations are now starting to open back up and accept tourists. A new travel agent has just opened and they want to plot each holiday destination in order to help them build a portfolio of available locations. To help them do this, you are asked to write a java program to track details of destinations and allow people to compare destinations to one another.

Problem Specification

  1. Define a Java class Destination with appropriate fields, methods, and constructors to store and retrieve information about the name, capacity and latitude and longitude (in full).
  2. Define a Java class Country with appropriate fields, methods, and constructors to store and retrieve the name of the country, the language of the country, and flag which indicates whether or not the country is open to tourists, or not. The Country class should also list the number of Destinations it has that are in it. Note: different countries will not hold the same Destinations and there is no need to create a master list of all Destinations.

In the Country class, include methods that return:

  • The Destination with the highest capacity.
  • The average capacity of the destinations recorded by a Country.
  • A list of all Destinations recorded by the Country with a capacity larger than a given number.

Define a Java class Continent, which holds information about all Countries. Include methods to return:

  • The Country which has the recorded highest average Destination capacity.
  • The Destination with the highest capacity.
  • A list of all Countries that have been recorded, that have a capacity larger than a given number.

Finally, you need to implement a class Booking, with a main method which does the following:

  • Presents the user with a menu (printed to the console) of features:
  • Add a new Country to a Continent, given the name, language spoken, and entry flag (see above).
  • Add new details of a Destination (name, capacity, latitude, and longitude) to the record for a given Country.
  • Provide statistics: the Country that has recorded the highest average capacity of Destinations; the highest capacity Destination recorded in the system, and all Destinations recorded with a capacity larger than a given number.
  • Exit (close program)
  • Takes user input from the console/terminal to choose one of the menu features.
  • Allows the user to input, via the console/terminal, the details of Destinations and Countries that need to be recorded.
  • After executing one of the features, returns the user to the menu to choose another option.

Documentation

In this piece of this work, you should properly document all classes with comments. Java comments must include an adequate amount of information and detail.

Source Code

BOOKING

import java.util.List; import java.util.Scanner; public class Booking { // Function to display menu public static void displayMenu() { System.out.println("1) Add new Country"); System.out.println("2) Add Destination"); System.out.println("3) Print Statistics"); System.out.println("4) Exit"); } public static int getPositiveInteger(String message, Scanner sc) { // This funtion prompts the user until s/he enters a positive integer while(true) { try{ System.out.print(message); int number = Integer.valueOf(sc.nextLine()); if(number > 0) { return number; } else System.out.println("Please enter a positive number."); } catch(Exception ex) { // User entered an invalid number System.out.println("Please enter a valid positive number."); } } } public static int getPositiveInteger(String message, Scanner sc, int lb, int ub) { // This funtion prompts the user until s/he enters a positive integer n such that lb<= n <= ub while(true) { try{ System.out.print(message); int number = Integer.valueOf(sc.nextLine()); if(number >= lb && number <= ub) { return number; } else System.out.println("Please enter a number between " + lb + " and " + ub + "."); } catch(Exception ex) { // User entered an invalid number System.out.println("Please enter a valid positive number."); } } } public static float getFloat(String message, Scanner sc){ // This function prompts the user until s/he entersa valid float while(true) { try{ System.out.print(message); float number = Float.valueOf(sc.nextLine()); return number; } catch(Exception ex) { // User entered an invalid number System.out.println("Please enter a valid number."); } } } public static boolean getYesNo(String message, Scanner sc) { // This function prompts the user until s/he enters 'yes' or 'no' // Then, if user entered 'yes', function returns true. If user enters 'no', the function returns false while(true) { System.out.print(message); String input = sc.nextLine(); if(input.toLowerCase().compareTo("yes") == 0) return true; else if(input.toLowerCase().compareTo("no") == 0) return false; else { System.out.println("Invalid input."); } } } public static void main(String[] args) { // Create scanner Scanner sc = new Scanner(System.in); // Create Continent Continent continent = new Continent(); // Begin with program int menu_option; String name, language; boolean is_open; int capacity; float latitude, longitude; while(true) { displayMenu(); // Get option menu_option = getPositiveInteger("Enter option: ", sc, 1, 4); if(menu_option == 1)// add new country { // Ask name System.out.print("Enter Country name: "); name = sc.nextLine(); // Ask language System.out.print("Enter Country language: "); language = sc.nextLine(); //Ask if the country is open for tourists or not is_open = getYesNo("Is the Country open for tourists? Enter 'yes' or 'no': ", sc); // Create country Country country = new Country(name, language, is_open); // Add to continent continent.addCountry(country); System.out.println("Country added correctly!"); } else if(menu_option == 2) // add destination { // First, ask for country name System.out.print("Enter Country name to which you want to add the destination: "); name = sc.nextLine(); Country country = continent.getCountryByName(name); if(country != null) { // Ask for Destination name System.out.print("Enter Destination name: "); name = sc.nextLine(); // Ask for capacity capacity = getPositiveInteger("Enter Destination capacity: ", sc); // Ask for latitude latitude = getFloat("Enter latitude: ", sc); // Ask for longitude longitude = getFloat("Enter longitude: ", sc); // Add Destination Destination destination = new Destination(name, capacity, latitude, longitude); // Add to country country.addDestination(destination); } else System.out.println("There is no Country with that name."); } else if(menu_option == 3) { // Get country with highest average capacity Country country_highest_cap = continent.getCountryWithHighestAvgCapaity(); // Highest capacity destination Destination destination_highest_cap = continent.getDestinationWithHighestCapacity(); // Ask user for minimum capacity capacity = getPositiveInteger("Enter minimum capacity: ", sc); System.out.println(""); // Get destinations with capacity higher than the given capacity List destinations_highest_cap = continent.getDestinationsWithCapacityLargerThan(capacity); // Now print System.out.println("The Country with the Highest Capacity is:"); System.out.println(country_highest_cap.toString()); if(destination_highest_cap != null) System.out.println("The Destination with the Highest Capacity is: " + destination_highest_cap.toString()); if(destinations_highest_cap.size() > 0) { System.out.println("The Destinations with a capacity larger than " + capacity + " are:"); for(Destination dest: destinations_highest_cap) System.out.println(dest.toString()); } else { System.out.println("There are no destinations with a capacity larger than " + capacity + "."); } } else if(menu_option == 4) { System.out.println("Good Bye!"); break; } System.out.println(""); } sc.close(); } }

CONTINENT

import java.util.ArrayList; import java.util.List; public class Continent { private List countries; // Default constructor public Continent() { this.countries = new ArrayList(); } // getters public List getCountries() {return this.countries;} // methods public void addCountry(Country country) {this.countries.add(country);} public Country getCountryWithHighestAvgCapaity() { if(getCountries().size() == 0) return null; Country country = getCountries().get(0); for(int i = 1; i < getCountries().size(); i++) { if(getCountries().get(i).getAverageCapacity() > country.getAverageCapacity()) { country = getCountries().get(i); } } return country; } // Method to get Destination with highest capacity public Destination getDestinationWithHighestCapacity() { Destination dest = null; for(Country c: getCountries()) { for(Destination d: c.getDestinations()) { if(dest == null) { dest = d; } else { if(d.getCapacity() > dest.getCapacity()) { dest = d; } } } } return dest; } // Get countries with a capacity larger than the given one public List getCountriesWithCapacityLargerThan(int capacity) { List result = new ArrayList(); for(Country country: this.getCountries()) { if(country.getTotalCapacity() > capacity) { result.add(country); } } return result; } // Get destinations with a capacity larger than the given one public List getDestinationsWithCapacityLargerThan(int capacity) { List result = new ArrayList(); for(Country c: getCountries()) { for(Destination d: c.getDestinations()) { if(d.getCapacity() > capacity) { result.add(d); } } } return result; } // Get country by name public Country getCountryByName(String name) { Country country = null; for(Country c: this.getCountries()) { if(c.getName().toLowerCase().compareTo(name.toLowerCase()) == 0) { country = c; break; } } return country; } }

COUNTRY

import java.util.ArrayList; import java.util.List; public class Country { private String name; private String language; private boolean is_open; private int n_destinations; private List destinations; // DefaultConstructor public Country() { this.name = ""; this.language = ""; this.is_open = true; this.n_destinations = 0; this.destinations = new ArrayList(); } // Overloaded constructor public Country(String name, String language, boolean is_open) { this.name = name; this.language = language; this.is_open = is_open; this.destinations = new ArrayList(); this.n_destinations = 0; } // Getters and setters public String getName() {return this.name;} public void setName(String name) {this.name = name;} public String getLanguage() {return this.language;} public void setLanguage(String language) {this.language = language;} public boolean isOpenForTourists() {return this.is_open;} public void setOpenForTourists(boolean open) {this.is_open = open;} public int getDestinationsCount() {return this.n_destinations;} public List getDestinations() {return this.destinations;} // methods public void addDestination(Destination dest) { this.destinations.add(dest); this.n_destinations++; } public Destination getDestinationWithHigherCapacity() { if(getDestinationsCount() == 0) return null; Destination dest = getDestinations().get(0); for(int i = 1;i < getDestinationsCount(); i++) { if(getDestinations().get(i).getCapacity() > dest.getCapacity()) { dest = getDestinations().get(i); } } return dest; } public float getAverageCapacity() { float avg_capacity = 0.0f; for(Destination dest: getDestinations()){ avg_capacity += dest.getCapacity(); } return avg_capacity/getDestinationsCount(); } // Get total capacvity for country public float getTotalCapacity() { float capacity = 0.0f; for(Destination dest: this.getDestinations()) { capacity += dest.getCapacity(); } return capacity; } public List getDestinationsWithCapacityLargerThan(int capacity) { List result = new ArrayList(); for(Destination dest: getDestinations()) { if(dest.getCapacity() > capacity){ result.add(dest); } } return result; } // Add toString @Override public String toString() { String open = "is"; if(!isOpenForTourists()) open = "is not "; String ret = "Name: " + getName() + "(" + getLanguage() + ") - This country " + open + " open for tourists, and contains " + getDestinationsCount() + " destinations:\n"; for(Destination dest: getDestinations()) { ret += "\t" + dest.toString() + "\n"; } return ret; } }

DESTINATION

public class Destination { private String name; private int capacity; private float latitude; private float longitude; // Default constructor public Destination() { this.name = ""; this.capacity = 0; this.latitude = 0; this.longitude = 0; } // Overloaded constructor public Destination(String name, int capacity, float latitude, float longitude) { this.name = name; this.capacity = capacity; this.latitude = latitude; this.longitude = longitude; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public int getCapacity() { return this.capacity; } public void setCapacity(int capacity) { this.capacity = capacity; } public float getLatitude() { return this.latitude; } public void setLatitude(float latitude) { this.latitude = latitude; } public float getLongitude() { return this.longitude; } public void setLongitude(float longitude) { this.longitude = longitude; } // Add toString @Override public String toString() { return String.format("%s with a capacity of %d, located at (%.4f, %.4f)", getName(), getCapacity(), getLatitude(), getLongitude()); } }

Related Samples

Unlock your potential with our free Java assignment samples. These expertly crafted examples simplify complex concepts and provide detailed solutions, helping you excel in your studies and master Java programming.