Instructions
Requirements and Specifications
- 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
Scenario: Holiday Destinations
Problem Specification
- 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).
- 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:
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());
}
}