JavaFX (Java) MVC (Model portion) Currently making an application that will proj
ID: 3915477 • Letter: J
Question
JavaFX (Java) MVC (Model portion)
Currently making an application that will project a Hogwarts (harry potter) type theme. In order to process this, I will need the applicaiton, controller, model, and javaFXML files all while utilizing pictures. Need 3 classes to be made School.java, House.java, Student.java
This is the model portion that I'm stuck on and the instructions are as follows.
The Model
The model of this application will be School.java, which will utilize House.java and Student.java.
This app will have a single School object, which will contain four House objects. Following the MVC design pattern, none of the model classes may directly update the view - this should always be handled by a constructor.
You will need to decide what other methods are needed in these classes. As a general rule, always include getters and setters for all class variables.
School objects must each have a name and four House objects.
House objects must each have a name, a color, the name of a professor who is the Head of the house, and a collection of Student objects.
Houses also have a crest/logo associated with them, examples have been provided with this lab.
For convenience, the heads of each house are currently assigned (but should be able to change in the future - read and save these from a file!):
Gryffindor: Minerva McGonagall
Ravenclaw: Filius Flitwick
Hufflepuff: Pomona Sprout
Slytherin: Severus Snape
Houses should sort their students by year first, then by last name, then first name. That is, if Ginny Weasley (year 1), Fred Weasley (year 4), and Hermione Granger (year 2) are in Gryffindor, then they should be listed in order with Ginny first, then Hermione, then Fred.
Houses should be populated using the data from the text files given in Lab 1. Copy these into your project to ensure they are submitted in your zip file.
There is one file per house needed in this software. Each file is a csv (comma-separated values) containing one row per student in that house. The student name is first, followed by their year.
For example:
Harry Potter,2
Katie Bell,3
Student objects must each have a name (first & last) and a year. For example, the following could be used to instantiate a new Student:
Student student1 = new Student("Harry Potter", 2);
Valid years for students are 1-7.
Datafiles are in the following format: Gryffindor.csv, Hufflepuff.csv, Ravenclaw.csv, Slytherin.csv
Explanation / Answer
hi
I am not able to upload the multiple files, therefore uploading all the data into 1 file. You can break the single file into multiple where you find the package keyword to imprt the package.
I have created the funtion to read from file and used the sample input to create the House object. there we can also use the input read from csv by splitting on comma, also i have created the sort function to sort the students based on year, lastname, firstname.
Please provide your feedback by clicking on the Thumps up/down icon.
Thanks
package javahellocollections;
import JavaMVCControllers.*;
/**
*
* @author Radhika
*/
public class JavaHelloCollections {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
HelloWorldController controller = new HelloWorldController();
// Start the application
controller.Sorting();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package JavaMVCControllers;
import JavaMVCViews.*;
import JavaMVCModels.*;
import java.awt.List;
import java.util.*;
public class HelloWorldController {
public void Sorting()
{
School schoolObject= new School();
schoolObject.Name="Hogward";
schoolObject.Houses= new ArrayList<House>();
ArrayList<Student> stdarray= new ArrayList<Student>();
stdarray.add(new Student("harry", "potter", 2));
stdarray.add(new Student("hermoine", "grenjor", 2));
stdarray.add(new Student("ginne", "weasly", 1));
stdarray.add(new Student("fred", "weasly", 4));
schoolObject.Houses.add(new House("grif", "blue","sanpe", stdarray));
System.out.println("Unsorted");
for (int i=0; i<schoolObject.Houses.size(); i++)
{ House houseObj=schoolObject.Houses.get(i);
System.out.println(houseObj.Name);
System.out.println(houseObj.Color);
System.out.println(houseObj.HeadProfessor);
for(int j=0;j<houseObj.Students.size();j++)
{
Student stdobj=houseObj.Students.get(j);
System.out.println(stdobj.getYear()+ ":"+stdobj.getLastName()+":"+stdobj.getFirstName());
}
}
System.out.println("Sorted");
for (int i=0; i<schoolObject.Houses.size(); i++)
{ House houseObj=schoolObject.Houses.get(i);
System.out.println(houseObj.Name);
System.out.println(houseObj.Color);
System.out.println(houseObj.HeadProfessor);
houseObj.Students=houseObj.SortStudent(houseObj.Students);
for(int j=0;j<houseObj.Students.size();j++)
{
Student stdobj=houseObj.Students.get(j);
System.out.println(stdobj.getYear()+ ":"+stdobj.getLastName()+":"+stdobj.getFirstName());
}
}
}
public String ReadData() {
try {
CustomFileReader model = new CustomFileReader();
return model.getData();
} catch (Exception er) {
return "There was an error.";
}
}
}
package JavaMVCModels;
import java.util.*;
public class School {
public String Name;
public ArrayList<House> Houses;
}
package JavaMVCModels;
import java.awt.Image;
import java.util.*;
import java.util.Comparator;
public class House {
public String Name;
public String Color;
public String HeadProfessor;
public Image Logo;
public ArrayList<Student> Students;
public House(String name, String color, String headProfessor, ArrayList<Student> stdList)
{
this.HeadProfessor = headProfessor;
this.Name = name;
this.Color = color;
this.Students= stdList;
}
public ArrayList<Student> SortStudent(ArrayList<Student> allStudents ) {
if(allStudents!=null) {
Collections.sort(allStudents, new CustomComparator());
}
return allStudents;
}
public class CustomComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
int c;
c = o1.getYear()-o2.getYear();
if (c == 0)
c = o1.getLastName().compareTo(o2.getLastName());
if (c == 0)
c = o1.getFirstName().compareTo(o2.getFirstName());
return c;
}
}
}
package JavaMVCModels;
import java.util.*;
public class School {
public String Name;
public ArrayList<House> Houses;
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package JavaMVCModels;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
public class CustomFileReader {
public String getData() throws FileNotFoundException, IOException {
if(!(new File("E:\file.txt").isFile())) {
// Create -- Make sure file exists -- the file before continuing
Files.createFile(Paths.get("E:\file.txt"));
}
String data;
// We will be using a try-with-resource block
try (BufferedReader reader = new BufferedReader(
new FileReader("E:\file.txt"))) {
// Access the data from the file
// Create a new StringBuilder
StringBuilder string = new StringBuilder();
// Read line-by-line
String line = reader.readLine();
string.append("<html>");
// While there comes a new line, execute this
while(line != null) {
// Add these lines to the String builder
string.append(line);
string.append("<br />");
// Read the next line
line = reader.readLine();
}
string.append("</html>");
data = string.toString();
} catch (Exception er) {
// Since there was an error, you probably want to notify the user
// For that error. So return the error.
data = er.getMessage();
}
// Return the string read from the file
return data;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.