Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JavaFX Model View Controller: Find a way to access building data I put a static ArrayList to store the buildings, a method to get all

JavaFX Model View Controller:

Find a way to access building data I put a static ArrayList to store the buildings, a method to get all buildings (load), and a method to return a single building based on the indices of that building in my Building class:

// this is static member of the building class

public static ArrayList buildings = new ArrayList<>();

// this is just the signature, take code from your previous project to read the file and load an ArrayList of Buildings... public static ArrayList load() throws FileNotFoundException {

... put your code here to read file...

}

//signature of method to return one building, this should not be called until load is called..

public static Building getBuilding(int indicies)

{

...return the building at the certain index passed in

}

Though I used my Building class, you can create a singleton to hold data as learned in Beginning Java

Write a UI (JavaFX with material learned in Chapters 14-16) to create a UI similar to the pictures below:

Where the information on the right shows up for the building chosen on the left. Use a ListView control, and do not have multiple selection set as an option (just one at a time).

Spruce up your design.

TIPS:

1. Inside the listener for the ListView, I used this line to find the index:

int index = buildingListView.getSelectionModel().getSelectedIndex();

2. I then called my method to get the building object to populate my UI

example:

Building b = Building.getBuiding(index);

Then, the name is b.getName(); the picture is b.getImageName(); etc.

3. Building text:

Jamestown Davis_Hall 35.998172 -79.918971 dh_ext_side.png DH

Jamestown Applied_Technologies 35.999081 -79.917802 at_ext_back.png AT

Jamestown James_Williams_Hall 35.99766 -79.917963 jwh_ext_court_door.png JWH

Jamestown Emergency_Responder_Training 36.001754 -79.912974 etrc1_ext.png ERTC

Jamestown Public_Safety 36.000662 -79.915887 ps_ext_lot.png PS

Jamestown Cline_Observatory 35.999235 -79.915739 co_ext_front.png CO

Jamestown Auto_Body 35.999063 -79.914132 autobody_ext_bays.png AB

Jamestown Science_Hall 35.998584 -79.915347 sh_ext_front.png SH

Pictures of Buildings:

James William Hall:

Autobody:

Davis Hall:

CRITERIA POINTS

Create appropriate back - end of application (this is the Building application

including Building, Coordinate, InvalidLatitude, InvalidLongitude, etc)this

is basically the last version of the app, with the main program removed an another way to read the file, store a list of buildings and also to return a single building. This can be

accomplished in the Building class as described above (and in video) or in a

singleton class designed just to hold data (Model)

Create appropriate UI Use JavaFX without FXML to show a ListView of building names

and then show the detail of each building as they are chosen one at a time. Your design

can be as desired so long as it contains attributes and images from a building object -

you can use one view like shown OR create another JavaFx Window or AlertBox as long as it is professional

All code must compile and work and have the architecture described.

Back-end of application:

public class Main {

public static void main(String args[]) throws FileNotFoundException, InvalidLatitudeException, InvalidLongitudeException {

File fileInput = new File("src/BuildingData.txt");

Scanner sc = new Scanner(fileInput);

// Array list to hold buildings

ArrayList list = new ArrayList<>();

// while loop to read data into variables

while (sc.hasNextLine()) {

String campus = sc.next();

String name = sc.next();

double latitude = Double.parseDouble(sc.next());

double longitude = Double.parseDouble(sc.next());

String imageName = sc.next();

String buildingCode = sc.next();

// Instantiate building object using constructor

try {

Building buildingObject = new Building(campus, name, latitude, longitude, imageName, buildingCode);

if (latitude >= 34 && latitude <=36) {

if (longitude >= -80 && longitude <= 78)

// Adding Building to Array list

list.add(buildingObject);

for (int i = 0; i < 1; i++) {

System.out.println(buildingObject.toString());

}

}

} catch (InvalidLatitudeException e) {

System.out.println(e.getMessage());

throw e;

}catch (InvalidLongitudeException e) {

System.out.println(e.getMessage());

throw e;

}

}

}

}

public class Building extends Coordinate {

// constructor

public Building(String campus, String name, double latitude, double longitude, String imageName, String buildingCode) throws InvalidLatitudeException , InvalidLongitudeException {

super(latitude,longitude);

try {

setLatitude(latitude);

setLongitude(longitude);

} catch (InvalidLatitudeException e) {

throw e;

} catch (InvalidLongitudeException e) {

throw e;

}

this.campus=campus;

this.name=name;

this.imageName=imageName;

this.buildingCode=buildingCode;

}

// attributes

private String campus;

private String name;

private String imageName;

private String buildingCode;

// getter-setter

public String getCampus() {

return campus;

}

public void setCampus(String campus) {

this.campus = campus;

}

public String getImageName() {

return imageName;

}

public void setImageName(String imageName) {

this.imageName = imageName;

}

public String getBuildingCode() {

return buildingCode;

}

public void setBuildingCode(String buildingCode) {

this.buildingCode = buildingCode;

}

// toString

@Override

public String toString() {

return "Building{" +

"campus='" + campus + '\'' +

", name='" + name + '\'' +

super.toString() +

", imageName='" + imageName + '\'' +

", buildingCode='" + buildingCode + '\'' +

'}';

}

}

public class Coordinate {

// attributes

private double latitude;

private double longitude;

// constructor

public Coordinate(double latitude, double longitude) throws InvalidLatitudeException, InvalidLongitudeException {

try {

setLatitude(latitude);

} catch (InvalidLatitudeException e) {

throw e;

}

try {

setLongitude(longitude);

} catch (InvalidLongitudeException e) {

throw e;

}

}

public Coordinate() {

}

// getter-setter

public double getLatitude() {

return latitude;

}

public void setLatitude(double latitude) throws InvalidLatitudeException{

if (latitude > 36 && latitude < 34) {

throw new InvalidLatitudeException();

}

this.latitude = latitude;

}

public double getLongitude() {

return longitude;

}

public void setLongitude(double longitude) throws InvalidLongitudeException {

if (longitude > -78 && longitude < -80) {

throw new InvalidLongitudeException();

}

this.longitude = longitude;

}

// toString

@Override

public String toString() {

return "Coordinate{" +

"latitude=" + latitude +

", longitude=" + longitude +

'}';

}

}

@Override

public String getMessage() {

return "Invalid Latitude";

}

@Override

public String getMessage() {

return "Invalid Longitude";

}

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

More Books

Students also viewed these Databases questions