Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java and JavaFX Suppose there are a set of hotels, each hotel has a value, price, distance. You are required to create an object for

Java and JavaFX

Suppose there are a set of hotels, each hotel has a value, price, distance. You are required to create an object for each hotel, and sort them. You need to have a graphic interface for sorting.

I have already started it. My logic was working fine for price, but when I added the rateSort class it broke.

Hotel.java Class

import java.util.ArrayList; public class Hotel { int price; int rating; int distance; String name; public Hotel() { } public Hotel(String name, int price, int rating, int distance) { this.price = price; this.rating = rating; this.distance = distance; this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public int getRating() { return rating; } public void setRating(int rating) { this.rating = rating; } public int getDistance() { return distance; } public void setDistance(int distance) { this.distance = distance; } public String printHotel() { return (name + " Price: $" + getPrice() + " Distance: " + getDistance() + " miles" + " Rating: " + getRating() + " stars" + " "); } }

HotelGUI.java Class (main)

import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.stage.Stage; import java.util.ArrayList; public class HotelGUI extends Application { @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hotel Finder"); BorderPane stBPane = new BorderPane(); Hotel DaysInn = new Hotel("Days Inn",50, 2, 10); Hotel Ramadan = new Hotel("Ramadan", 60, 3, 1); Hotel Hilton = new Hotel("Hilton", 150, 4, 5); Hotel test = new Hotel("dummy", 0,0,0); ArrayList hotels = new ArrayList<> (); hotels.add(DaysInn); hotels.add(Ramadan); hotels.add(Hilton); hotels.add(test); HBox buttons = new HBox(); Button price = new Button("Price"); Button rate = new Button("Rating"); Button distance = new Button("Distance"); buttons.getChildren().addAll(price, rate, distance); stBPane.setTop(buttons); stBPane.setAlignment(buttons, Pos.TOP_CENTER); buttons.setAlignment(Pos.TOP_CENTER); Label hotel1Label = new Label(); Label hotel2Label = new Label(); Label hotel3Label = new Label(); Label hotel4Label = new Label(); Label[] priceLabelsArr = {hotel1Label, hotel2Label, hotel3Label, hotel4Label}; VBox priceLabels = new VBox(); ArrayList priceHotels; priceHotels = priceSort(hotels); for(int i=1; i rateHotels; rateHotels = rateSort(hotels); for(int i=1; i { stBPane.setCenter(rateLabels); }); price.setOnAction(e -> { stBPane.setCenter(priceLabels); }); //stBPane.setAlignment(buttons, Pos.CENTER); priceLabels.setAlignment(Pos.CENTER); rateLabels.setAlignment(Pos.CENTER); buttons.setSpacing(10); stBPane.setPadding(new Insets(50, 50, 50, 50)); Scene stScene = new Scene(stBPane, 1080, 720, Color.GREEN); primaryStage.setScene(stScene); primaryStage.show(); } public ArrayList priceSort(ArrayList hotels) { ArrayList sorted = new ArrayList<>(); ArrayList list1 = new ArrayList<>(); ArrayList list2 = new ArrayList<>(); if (hotels.size() % 2 == 0) { int i = hotels.size() / 2; for (int j = 0; j <= i - 1; j++) { list1.add(hotels.get(j)); } for (int k = i; k <= i * 2; k++) { list2.add(hotels.get(k-1)); } } else if (hotels.size() % 2 == 1) { int i = hotels.size() / 2; for (int j = 0; j <= i - 1; j++) { list1.add(hotels.get(j)); } if(i>1) { for (int k = i; k <= (i * 2) + 1; k++) { list2.add(hotels.get(k)); } } else { for (int k = i; k <= i + 1; k++) { list2.add(hotels.get(k)); } } } while (list1.size() > 0 || list2.size() > 0) { int i; int j; if (list1.size() > 0) { i = list1.get(0).getPrice(); } else { i = 1000; } if (list2.size() > 0) { j = list2.get(0).getPrice(); } else { j = 1000; } //list1 less than list2 if (i < j) { sorted.add(list1.get(0)); list1.remove(0); } //equal else if (i == j) { sorted.add(list1.get(0)); list1.remove(0); list2.remove(0); } //list1 greater than list2 else if (j < i) { sorted.add(list2.get(0)); list2.remove(0); } else if(list1.size()==0) { sorted.addAll(list2); } else if(list2.size()==0) { sorted.addAll(list1); } } for(int l=0; lout.println(sorted.get(l).printHotel()); } return sorted; } public ArrayList rateSort(ArrayList hotels) { ArrayList sorted = new ArrayList<>(); ArrayList list1 = new ArrayList<>(); ArrayList list2 = new ArrayList<>(); if (hotels.size() % 2 == 0) { int i = hotels.size() / 2; for (int j = 0; j <= i - 1; j++) { list1.add(hotels.get(j)); } for (int k = i; k <= (i * 2)-1; k++) { list2.add(hotels.get(k)); } } else if (hotels.size() % 2 == 1) { int i = hotels.size() / 2; for (int j = 0; j <= i - 1; j++) { list1.add(hotels.get(j)); } if(i>1) { for (int k = i; k <= (i * 2) + 1; k++) { list2.add(hotels.get(k)); } } else { for (int k = i; k <= i + 1; k++) { list2.add(hotels.get(k)); } } } while (list1.size() > 0 || list2.size() > 0) { int i; int j; if (list1.size() > 0) { i = list1.get(0).getRating(); } else { i = 0; } if (list2.size() > 0) { j = list2.get(0).getRating(); } else { j = 0; } //list1 less than list2 if (i > j) { sorted.add(list1.get(0)); list1.remove(0); } //equal else if (i == j) { sorted.add(list1.get(0)); list1.remove(0); list2.remove(0); } //list1 greater than list2 else if (j > i) { sorted.add(list2.get(0)); list2.remove(0); } else if(list1.size()==0) { sorted.addAll(list2); } else if(list2.size()==0) { sorted.addAll(list1); } } for(int l=0; lout.println(sorted.get(l).printHotel()); } return sorted; } public static void main (String[]args){ // write your code here launch(args); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases

Authors: Ian Robinson, Jim Webber, Emil Eifrem

1st Edition

1449356265, 978-1449356262

More Books

Students also viewed these Databases questions