0% found this document useful (0 votes)
21 views31 pages

Model Storerating Ratings Movielist Mov - Path Genre - Path

This UML diagram shows a Recommender class that implements a Genre interface and contains methods for building movie recommendations based on ratings, genres, and moods. It loads rating and genre data from files, calculates item-based recommendations, and returns lists of Movie objects that match a user's preferences.

Uploaded by

Abdullah Naseer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views31 pages

Model Storerating Ratings Movielist Mov - Path Genre - Path

This UML diagram shows a Recommender class that implements a Genre interface and contains methods for building movie recommendations based on ratings, genres, and moods. It loads rating and genre data from files, calculates item-based recommendations, and returns lists of Movie objects that match a user's preferences.

Uploaded by

Abdullah Naseer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

UML:

Recommender:
package org.movie;

import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import
org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.LogLikelihoodSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.similarity.ItemSimilarity;

import java.io.*;
import java.util.*;

public class Recommender implements Genre {


static DataModel model;
static Long[] storeRating = new Long[50];
static String ratings = "data/ratings.csv",
movielist = "data/profiled.csv",
mov_path = "data/moviedetail.csv",
genre_path = "data/genres_raw.csv";

public static void RatingBuild() {


try {
int x = 0;
model = new FileDataModel(new File(ratings));
ItemSimilarity sim = new LogLikelihoodSimilarity(model);
GenericItemBasedRecommender git = new
GenericItemBasedRecommender(model, sim);

for (LongPrimitiveIterator items = model.getItemIDs();


items.hasNext(); ) {
long itemId = items.nextLong();
List<RecommendedItem> recommendations =
git.mostSimilarItems(itemId, 1);

//recommends one item per user


for (RecommendedItem recommendation : recommendations) {
storeRating[x] = recommendation.getItemID();
}
x++;
//10 users
if (x > 20)
break;

}
} catch (IOException e) {
System.out.println("There was an error.");
e.printStackTrace();
} catch (TasteException e) {
System.out.println("There was a Taste Exception.");
e.printStackTrace();
}

public static ArrayList<Movie> displayRatingRec() {


ArrayList<Movie> ratedM = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(mov_path)))
{
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
for (Long rec : storeRating) {
if ((rec != null) && data[0].equals(Long.toString(rec))) {
Movie m = new Movie(Integer.parseInt(data[0]),
data[1], Integer.parseInt(data[2]), Float.parseFloat(data[3]));
ratedM.add(m);
}
// FXMain.movie.setText(data[1]+"\n"+data[3]);}
// System.out.println(data[0] + "\t\t" + data[1] + "\t\t"
+ data[2] + "\t\t" + data[3]);}

}
}
} catch (IOException e) {
e.printStackTrace();
}
return ratedM;
}

public static ArrayList<Movie> GenreBuild() {


ArrayList<Integer> ids = new ArrayList<>(lister());
ArrayList<Movie> finalMovies = new ArrayList<>(getter(ids));
Collections.shuffle(finalMovies);
FXMain.genreHead.setText("Movies for " + User.currentUser.getName());
return finalMovies;
}

static ArrayList<Integer> lister()


{
String preferences = User.getPreferences();

preferences = preferences.replaceAll("\\.",", ");


preferences = preferences.substring(0, preferences.lastIndexOf(","));
ArrayList<Integer> movieIDS = new ArrayList<>();
try {
BufferedReader bf = new BufferedReader(new
FileReader(genre_path));
String line;

String [] pref = preferences.split(",");

while ((line = bf.readLine()) != null)


{
String[] data = line.split(",");
ArrayList<String> genres = new
ArrayList<>(Arrays.asList(data));

int id = Integer.parseInt(genres.get(genres.size()-1));
genres.remove(genres.get(genres.size()-1)); // removed last as
it was id
for(String p: pref)
{
if (genres.contains(p))
{
movieIDS.add(id);
}}}
}catch (IOException e)
{
e.printStackTrace();
}
return movieIDS;
}
public static ArrayList<Movie> getter(ArrayList<Integer> ids)
{
int x = 0;
ArrayList<Movie> finalMovies = new ArrayList<>();
for (Integer id: ids) {
if(x>50)
{ break;}
Movie m = Movie.getMovie(id);
finalMovies.add(m);
x++;
}
return finalMovies;
}
public static ArrayList<Movie> MoodBuild(String genre)
{
ArrayList<Movie> mra = new ArrayList<>();
String moodAssigned = Mood.assignMood(genre);
int x = 0;
FXMain.moodHead.setText(moodAssigned);
ArrayList<Integer> foundId = Search.genresearch(genre);
for(int i: foundId)
{
if(x>19)
{break;}
Movie m = Movie.getMovie(i);
mra.add(m);
x++;
}
return mra;}}

Movie:
package org.movie;

import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import
org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.LogLikelihoodSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.similarity.ItemSimilarity;

import java.io.*;
import java.util.*;

public class Recommender implements Genre {


static DataModel model;
static Long[] storeRating = new Long[50];
static String ratings = "data/ratings.csv",
movielist = "data/profiled.csv",
mov_path = "data/moviedetail.csv",
genre_path = "data/genres_raw.csv";

public static void RatingBuild() {


try {
int x = 0;
model = new FileDataModel(new File(ratings));
ItemSimilarity sim = new LogLikelihoodSimilarity(model);
GenericItemBasedRecommender git = new
GenericItemBasedRecommender(model, sim);
for (LongPrimitiveIterator items = model.getItemIDs();
items.hasNext(); ) {
long itemId = items.nextLong();
List<RecommendedItem> recommendations =
git.mostSimilarItems(itemId, 1);

//recommends one item per user


for (RecommendedItem recommendation : recommendations) {
storeRating[x] = recommendation.getItemID();
}
x++;
//10 users
if (x > 20)
break;

}
} catch (IOException e) {
System.out.println("There was an error.");
e.printStackTrace();
} catch (TasteException e) {
System.out.println("There was a Taste Exception.");
e.printStackTrace();
}

public static ArrayList<Movie> displayRatingRec() {


ArrayList<Movie> ratedM = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(mov_path)))
{
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
for (Long rec : storeRating) {
if ((rec != null) && data[0].equals(Long.toString(rec))) {
Movie m = new Movie(Integer.parseInt(data[0]),
data[1], Integer.parseInt(data[2]), Float.parseFloat(data[3]));
ratedM.add(m);
}
// FXMain.movie.setText(data[1]+"\n"+data[3]);}
// System.out.println(data[0] + "\t\t" + data[1] + "\t\t"
+ data[2] + "\t\t" + data[3]);}

}
}
} catch (IOException e) {
e.printStackTrace();
}
return ratedM;
}

public static ArrayList<Movie> GenreBuild() {


ArrayList<Integer> ids = new ArrayList<>(lister());
ArrayList<Movie> finalMovies = new ArrayList<>(getter(ids));

Collections.shuffle(finalMovies);
FXMain.genreHead.setText("Movies for " + User.currentUser.getName());
return finalMovies;
}

static ArrayList<Integer> lister()


{
String preferences = User.getPreferences();

preferences = preferences.replaceAll("\\.",", ");


preferences = preferences.substring(0, preferences.lastIndexOf(","));
ArrayList<Integer> movieIDS = new ArrayList<>();
try {
BufferedReader bf = new BufferedReader(new
FileReader(genre_path));
String line;

String [] pref = preferences.split(",");

while ((line = bf.readLine()) != null)


{
String[] data = line.split(",");
ArrayList<String> genres = new
ArrayList<>(Arrays.asList(data));

int id = Integer.parseInt(genres.get(genres.size()-1));
genres.remove(genres.get(genres.size()-1)); // removed last as
it was id
for(String p: pref)
{
if (genres.contains(p))
{
movieIDS.add(id);
}}}
}catch (IOException e)
{
e.printStackTrace();
}
return movieIDS;
}
public static ArrayList<Movie> getter(ArrayList<Integer> ids)
{
int x = 0;
ArrayList<Movie> finalMovies = new ArrayList<>();
for (Integer id: ids) {
if(x>50)
{ break;}
Movie m = Movie.getMovie(id);
finalMovies.add(m);
x++;
}
return finalMovies;
}
public static ArrayList<Movie> MoodBuild(String genre)
{
ArrayList<Movie> mra = new ArrayList<>();
String moodAssigned = Mood.assignMood(genre);
int x = 0;
FXMain.moodHead.setText(moodAssigned);
ArrayList<Integer> foundId = Search.genresearch(genre);
for(int i: foundId)
{
if(x>19)
{break;}
Movie m = Movie.getMovie(i);
mra.add(m);
x++;
}
return mra;}}

User:
package org.movie;
import java.io.*;
import java.util.ArrayList;
class User implements Comparable<User> {
static ArrayList<String> usernames = new ArrayList<>();
static User currentUser;
static String user_path = "data/userdata.csv";
private final String username;
private final String password;
static String gender;

public void setPreferences(String preferences) {


this.preferences = preferences;
}
private String preferences;
private int age;
static boolean isUser;
public User(String username, String password, String gender, int age) {
this.username = username;
this.password = password;
User.gender = gender;
this.age = age;
}
@Override
public int compareTo(User o) {
if (this.getName().equals(o.getName()))
{
return 0;
}
return -1;
}

//Accessors and mutators


public String getName() {
return username;
}
public String getPassword() {
return password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
User.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static String getPreferences() { return currentUser.preferences;}
public static String loadPreferences(User user)
{try {
BufferedReader r = new BufferedReader(new
FileReader(("data/userdata.csv")));
String line;
boolean found = false;
while ((line = r.readLine()) != null) {
String[] data = line.split(",");

if (data[0].equals(user.getName()) && data[4] != null) {


return data[4];
}}
if(!found)
{
System.out.println("not found");
}
r.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
}

static boolean reg(){


String username, password;
gender = "Not entered";
int age;
isUser = false;
username = FXMain.username.getText();
password = FXMain.password.getText();

try {
BufferedReader r = new BufferedReader(new
FileReader((user_path)));
do{
String line;
while ((line = r.readLine()) != null)
{
String [] data = line.split(",");
usernames.add(data[0]);
}

if(usernames.contains(username))
{isUser = true;
return true;}
else {isUser = false;}
}while(isUser);

age = Integer.parseInt(FXMain.age.getText());
if(!isUser)
{ usernames.add(username);
currentUser = new User(username,password,gender,age);}

}catch (IOException e)
{
e.printStackTrace();}
return false;
}
static boolean login(){
String username, password, line;
boolean isUser = true, logged =false;
try{

while (true){
BufferedReader f = new BufferedReader(new FileReader(user_path));

username = FXMain.username.getText();
password = FXMain.password.getText();

while ((line = f.readLine()) != null) {


String[] data = line.split(",");
if (data[0].equals(username) && data[1].equals(password)) {
currentUser = new
User(data[0],data[1],data[2],Integer.parseInt(data[3]));
currentUser.setPreferences(loadPreferences(currentUser));
FXMain.loginstat.setText("Login successful.");
return true;
}
}}
} catch (IOException e) {
e.printStackTrace();
}return false;
}

@Override
public String toString() {
return username + ',' + password + ',' + gender + ',' + age + ',' +
preferences;
}
}

enum UserRating {
ONE("1 star"),
TWO("2 stars"),
THREE("3 stars"),
FOUR("4 stars"),
FIVE("5 stars"),
SIX("6 stars"),
SEVEN("7 stars"),
EIGHT("8 stars"),
NINE("9 stars"),
TEN("10 stars");

private final String description;

UserRating(String description) {
this.description = description;
}

Genre:
package org.movie;
public interface Genre {
static String assignGenre(String genre) {
String [] data = genre.split(" ");

String x = "";
for (String a:data ) {

switch (a) {
case "10749" -> a = "Romantic";
case "28" -> a = "Action";
case "35" -> a = "Comedy";
case "27" -> a = "Horror";
case "12" -> a = "Adventure";
case "14" -> a = "Fantasy";
case "16" -> a = "Animation";
case "36" -> a = "History";
case "37" -> a = "Western";
case "80" -> a = "Crime";
case "878" -> a = "Science Fiction";
case "9648" -> a = "Mystery";
case "10402" -> a = "Music";
case "10751" -> a = "Family";
case "10752" -> a = "War";
case "10769" -> a = "Foreign";
case "10770" -> a = "TV Movie";
default -> a = "";
}
x += " "+ a;
}
return x;
}
static String assignID(String genre) {
String [] data = genre.split("\\.");
String x = "";
for (String a:data ) {
switch (a) {
case "Romantic"-> a = "10749";
case "Action"-> a = "28";
case "Comedy"-> a = "35" ;
case "Horror"-> a = "27" ;
case "Adventure"-> a = "12" ;
case "Fantasy"-> a = "14" ;
case "Animation"-> a = "16" ;
case "History"-> a = "36" ;
case "Western"-> a = "37" ;
case "Crime"-> a = "80" ;
case "Science Fiction" -> a = "878" ;
case "Mystery"-> a = "9648" ;
case "Music"-> a = "10402" ;
case "Family"-> a = "10751" ;
case "War"-> a = "10752" ;
case "Foreign"-> a = "10769" ;
case "TV Movie"-> a = "10770" ;
default -> a = "";
}
x += a+ ".";
}
return x;
}
}

Mood:
package org.movie;
public class Mood {
public static String assignMood(String genre) {
return switch (genre.toLowerCase()) {
case "10749", "35", "10402", "10751", "16" -> "Happy, Lively";
case "28", "12", "14", "80", "9648" -> "Exciting, Thrilling";
case "36", "99", "878" -> "Curious, Informative";
case "10752", "18" -> "Sad";
default -> "";
};
}
}

Search:
package org.movie;
import java.io.*;
import java.util.ArrayList;
class Search {
static String genre_path = "data/genres_raw.csv";

public static ArrayList<Movie> results(Movie key) {


ArrayList<Movie> foundMovies = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new
FileReader(Recommender.movielist));
String line;

while ((line = reader.readLine()) != null) {


String[] data = line.split(",");
Movie movie = new Movie(Integer.parseInt(data[0]),
data[1],data[2],data[3],
Integer.parseInt(data[4]),data[5],Float.parseFloat(data[6]));

if (movie.getTitle().contains((key).getTitle())) {
foundMovies.add(movie);
}
}
} catch (IOException | ArrayIndexOutOfBoundsException e) {
System.out.println("End of results.");
return foundMovies;
}
return null;
}

public static ArrayList<Integer> genresearch(String key) {


ArrayList<Integer> mid = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new
FileReader(genre_path));
String line;
while ((line = reader.readLine()) != null) {
String[] data = line.split(",");
if (data[0].equals(key)) {
mid.add(Integer.parseInt(data[data.length - 1]));
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return mid;
}
}

FXMain:
package org.movie;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FXMain extends Application implements Genre {


private static final int Max = 25;
private ListView<String> movieList;
private Stage stage;

static MenuItem m1 = new MenuItem("Browse");


static Label loginstat = new Label(),
userexists = new Label(),
moodHead = new Label(),
searchedKey = new Label(),
user = new Label(),
pw = new Label(),
gender1 = new Label(),
agelabel = new Label(),
genreHead = new Label();

static Pane currentPane = new Pane(), full;


static TextField username,age, key = new TextField();
static RadioButton m = new RadioButton("Male"), f = new
RadioButton("Female") ;
static PasswordField password;
static GridPane prefs = new GridPane();
static ArrayList<Movie> searchResults = new ArrayList<>();
static Button login,signup, back = new Button(),
saveButton = new Button("Save"),
searcher = new Button("Search"),
submit = new Button("Submit"),
register = new Button("Register"),
tile = new Button();
public static void main(String[] args) {
launch();
}

@Override
public void start(Stage primestage) throws Exception {
Recommender.RatingBuild();

stage = primestage;
currentPane = welcomeScreen();
Scene s = new Scene(currentPane);
stage.setScene(s);

back.setText("Back");
back.setAlignment(Pos.TOP_LEFT);
back.getStyleClass().add("back");
back.setOnAction(e-> {
currentPane = home();
stage.getScene().setRoot(currentPane);
});

stage.setTitle("MRS-45K");

stage.getIcons().add(new Image("img.png"));
stage.show();
}
Pane welcomeScreen()
{
//WELCOME SCREEN
//Login Button
login = new Button("Login");
login.getStyleClass().add("logsign-button");

login.setOnAction(e-> {
currentPane = login();
stage.getScene().setRoot(currentPane);
});

//Signup Button
signup= new Button("Sign up");
signup.getStyleClass().add("logsign-button");

signup.setOnAction(e-> {
currentPane = reg();
stage.getScene().setRoot(currentPane);
});

//Greeting
Label greet = new Label("It's nice to have you here.");
greet.getStyleClass().add("greet");

Label welcome = new Label("Welcome!");


welcome.getStyleClass().add("welc");

//Title
Label title = new Label("MRS-45K");
title.getStyleClass().add("title");

//Headers
VBox top = new VBox(title,welcome,greet);
top.setAlignment(Pos.TOP_CENTER);
top.getStylesheets().add("style.css");

//Buttons on first page


HBox logsign = new HBox();
logsign.getChildren().addAll(login,signup);
logsign.getStyleClass().add("logsign");

//border check
//top.setBorder(new Border(new
BorderStroke(Color.BLACK,BorderStrokeStyle.DOTTED,CornerRadii.EMPTY,new
BorderWidths(2))));

//Entire first page


VBox welcSc = new VBox();
welcSc.getChildren().addAll(top,logsign);
welcSc.getStyleClass().add("welcome-screen");
welcSc.getStylesheets().add("style.css");

return welcSc; }
Pane login()
{
username = new TextField();
username.getStyleClass().add("login-fields");

password = new PasswordField();


password.getStyleClass().add("login-fields");

Label title = new Label("Login");


title.getStyleClass().add("login-head");

Label logintxt = new Label("Please enter your details below.");


logintxt.getStyleClass().add("login-txt");

Label user = new Label("Username ");


user.getStyleClass().add("login-labels");
Label pw = new Label("Password ");
pw.getStyleClass().add("login-labels");

GridPane gp = new GridPane();


gp.add(user,0,0);
gp.add(username,1,0);
gp.add(pw,0,1);
gp.add(password,1,1);

gp.getStyleClass().add("gpfields");

loginstat.getStyleClass().add("login-stat");

submit.getStyleClass().add("submit-button");
submit.setOnAction(e->{
boolean b = User.login();
if(b){
currentPane = home();
stage.getScene().setRoot(currentPane);}
if (!b)
{
loginstat.setText("Invalid credentials.");
}
});

full = new VBox(title,logintxt,gp,loginstat,submit);


full.getStyleClass().add("welcome-screen");
full.getStylesheets().add("style.css");
return full;
}
Pane reg(){
Label title = new Label("Register");
title.getStyleClass().add("login-head");

Label logintxt = new Label("Please enter your details below.");


logintxt.getStyleClass().add("login-txt");

user = new Label("Username ");


user.getStyleClass().add("login-labels");
pw = new Label("Password ");
pw.getStyleClass().add("login-labels");
gender1 = new Label("Gender ");
gender1.getStyleClass().add("login-labels");
agelabel = new Label("Age ");
agelabel.getStyleClass().add("login-labels");

username = new TextField();


username.getStyleClass().add("login-fields");
password = new PasswordField();
password.getStyleClass().add("login-fields");
age = new TextField();
age.getStyleClass().add("age-box");

age.textProperty().addListener((observable, current, newval) -> {


if (!newval.matches("\\d*")) { // allow digits
age.setText(newval.replaceAll("[^\\d]", "")); // remove non-
digit characters
}
}); //need to review

m.getStyleClass().add("gender-button");
f.getStyleClass().add("gender-button");
m.setOnAction(e->{
if(m.isSelected())
{
f.setSelected(false);
}

});
f.setOnAction(e->{
if(f.isSelected())
{
m.setSelected(false);
}

});

HBox gend = new HBox(m,f);


gend.getStyleClass().add("gender-box");

GridPane gp = new GridPane();


gp.add(user,0,0);
gp.add(username,1,0);
gp.add(pw,0,1);
gp.add(password,1,1);
gp.add(agelabel,0,2);
gp.add(age,1,2);
gp.add(gender1,0,3);
gp.add(gend,1,3);

gp.getStyleClass().add("gpfields");

register.getStyleClass().add("submit-button");

register.setOnAction(e->{
boolean b = User.reg();
userexists.getStyleClass().add("login-stat");
if(b) {

userexists.setText("Username already exists. Try another.");


}
if(!b){
User.currentUser = new
User(username.getText(),password.getText(),User.gender,Integer.parseInt(age.ge
tText()));
currentPane = getPreferences();
stage.getScene().setRoot(currentPane);}
});

userexists.getStyleClass().add("userexist-stat");

full = new VBox(title,logintxt,gp,userexists,register);


full.getStyleClass().add("welcome-screen");
full.getStylesheets().add("style.css");
return full;
}

Pane home(){

//Search Bar
HBox search = new HBox(key,searcher);

search.setTranslateX(190);
key.getStyleClass().add("search-bar");
searcher.getStyleClass().add("search");
searcher.setOnAction(e -> {
String toSearch = key.getText();
Movie m = new Movie(0,toSearch,0, 0.0F);
searchResults = Search.results(m);
currentPane = searched();
stage.getScene().setRoot(currentPane);
});

//Menu
MenuButton menu = new MenuButton("Menu");
menu.getItems().add(m1);
m1.getStyleClass().add("menu-item");
menu.getStyleClass().add("menu");
m1.setOnAction( e-> {
currentPane = browse();
stage.getScene().setRoot(currentPane);
});

//Rating Build
Label rated = new Label("Well rated movies you might like");
rated.getStyleClass().add("mood-head");
HBox hb = new HBox();
ArrayList<Movie> mra = Recommender.displayRatingRec();
tile(hb, mra);
hb.setSpacing(5);
hb.getStyleClass().add("movie-scroll");

ScrollPane recMovie = new ScrollPane(hb);


recMovie.setPrefWidth(870);
recMovie.getStyleClass().add("movie-scroll");
recMovie.setHbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);

//Mood Build
ArrayList<Movie> moodB = Recommender.MoodBuild("16");
HBox mood = new HBox();
tile(mood, moodB);
moodHead.getStyleClass().add("mood-head");
mood.setSpacing(5);
mood.getStyleClass().add("movie-scroll");

ScrollPane moodMovie = new ScrollPane(mood);


moodMovie.setPrefWidth(870);
moodMovie.getStyleClass().add("movie-scroll");
moodMovie.setHbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);

//GenreBuild
ArrayList<Movie> genreB = Recommender.GenreBuild();
HBox genre = new HBox();
for (int i = 0; i < 30; i++) {
tile = new Button();
Movie cM = genreB.get(i);
tile.setWrapText(true);
tile.setText(cM.getTitle()+"\n"+cM.getVoteAvg());
tile.getStyleClass().add("movie");// Set button size
genre.getChildren().add(tile);
}
for(Node b : genre.getChildren())
{
((Button) b).setOnAction( e -> {
currentPane = movie((Button) b);
stage.getScene().setRoot(currentPane);
});
}
genreHead.getStyleClass().add("mood-head");

genre.setSpacing(5);
genre.getStyleClass().add("movie-scroll");

ScrollPane genreMovie = new ScrollPane(genre);


genreMovie.setPrefWidth(870);
genreMovie.getStyleClass().add("movie-scroll");
genreMovie.setHbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);

VBox vb = new
VBox(rated,recMovie,genreHead,genreMovie,moodHead,moodMovie);

BorderPane full = new BorderPane();

HBox top = new HBox(menu,search);


top.setSpacing(50);
top.setAlignment(Pos.TOP_LEFT);
// top.setSpacing(100);
full.setTop(top);
full.setBottom(vb);
// full.getChildren().addAll(search,menu,vb);
full.getStyleClass().add("home-screen");
//full.getStyleClass().add("home-screen");
full.getStylesheets().add("style.css");

return full;}

private void tile(HBox hb, ArrayList<Movie> mra) {


for (int i = 0; i < mra.size(); i++) {
tile = new Button();
Movie cM = mra.get(i);
tile.setWrapText(true);
tile.setText(cM.getTitle()+"\n"+cM.getVoteAvg());
tile.getStyleClass().add("movie");// Set button size
hb.getChildren().add(tile);
tile.setOnAction(e-> System.out.println("button working"));
}
for(Node b : hb.getChildren())
{
((Button) b).setOnAction( e -> {
currentPane = movie((Button) b);
stage.getScene().setRoot(currentPane);
});
}
}

Pane searched()
{
searchedKey.getStyleClass().add("mood-head");
searchedKey.setText("Search results for \""+key.getText()+"\"");

VBox results = new VBox();


for (int i = 0; i < searchResults.size(); i++) {
tile = new Button();
Movie cM = searchResults.get(i);
tile.setWrapText(true);
tile.setText(cM.getTitle()+"\n"+cM.getVoteAvg());
tile.getStyleClass().add("searched-movies");
results.getChildren().add(tile);
}
for(Node b : results.getChildren())
{
((Button) b).setOnAction( e -> {
currentPane = movie((Button) b);
stage.getScene().setRoot(currentPane);
});
}
results.setSpacing(5);
results.getStyleClass().add("result-scroll");

ScrollPane resultingMovies = new ScrollPane(results);


resultingMovies.getStyleClass().add("result-scroll");
resultingMovies.setFitToWidth(true);
resultingMovies.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);

full = new VBox(back,searchedKey,resultingMovies);


full.getStyleClass().add("result-screen");

full.getStylesheets().add("style.css");

return full;
}

Pane browse() {
ComboBox<String> genreComboBox = new ComboBox<>();
genreComboBox.setPromptText("Select Genre");
genreComboBox.getStyleClass().add("combo-box");
genreComboBox.getItems().addAll(
"Action", "Adventure", "Animation", "Comedy", "Crime",
"Family", "Fantasy",
"Foreign", "History", "Horror", "Music", "Mystery",
"Romantic", "Science Fiction",
"TV Movie", "War", "Western");

ComboBox<String> alphabetComboBox = new ComboBox<>();


alphabetComboBox.setPromptText("Select Alphabet");
alphabetComboBox.getStyleClass().add("combo-box");
alphabetComboBox.getItems().addAll(
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z");

Button browseByGenreButton = new Button("Browse by Genre");


browseByGenreButton.getStyleClass().add("defaultzaid");
browseByGenreButton.setOnAction(e -> {
String selectedGenre = genreComboBox.getValue();
if (selectedGenre != null) {
List<String> movies = getMoviesByGenre(selectedGenre);
movieList.getStyleClass().add("list-cell:selected");
showMovies(movies);
} else {
showAlert("Please select a genre.");
}
});
movieList = new ListView<>();
movieList.setPrefHeight(400);
movieList.getStyleClass().add("list-cell");

Button browseByAlphabetButton = new Button("Browse by Alphabet");


browseByAlphabetButton.getStyleClass().add("defaultzaid");
browseByAlphabetButton.setOnAction(e -> {
String selectedAlphabet = alphabetComboBox.getValue();
if (selectedAlphabet != null) {
List<String> movies = getMoviesByAlphabet(selectedAlphabet);
movieList.getStyleClass().add("list-cell:selected");
showMovies(movies);
} else {
showAlert("Please select an alphabet.");
}
});
VBox browse = new VBox(10);
browse.setPadding(new Insets(10));
browse.getChildren().addAll(back,genreComboBox, browseByGenreButton,
alphabetComboBox, browseByAlphabetButton, movieList);
browse.getStyleClass().add("browse");

browse.getStylesheets().add("style.css");
return browse;
}
Pane getPreferences(){
Label thanks = new Label("Thank you for registering with us. \n Please
select your preferences:");
thanks.getStyleClass().add("pref-head");

String [] genres = {"Romantic","Action","Comedy",


"Horror","Adventure","Science Fiction","Fantasy","Animation",
"History","Western",
"Crime","Mystery","Family", "Foreign", "TV
Movie","Music","War"};
int row =0,column =0;
for (int i = 0; i < genres.length; i++) {
CheckBox tile = new CheckBox();
tile.setText(genres[i]);
tile.setWrapText(true);
tile.getStyleClass().add("genre");
prefs.add(tile,column,row);
column++;
if(column >5)
{
row++;
column = 0;
}

}
prefs.getStyleClass().add("genrebox");
saveButton.getStyleClass().add("save-button");

saveButton.setOnAction(e -> {
String preferences ="";
for (Node node : prefs.getChildren()) {
if (node instanceof CheckBox && (((CheckBox)
node).isSelected())) {
preferences += ((CheckBox) node).getText()+".";
}
}
User.currentUser.setPreferences(Genre.assignID(preferences));
if(FXMain.m.isSelected())
{
User.currentUser.setGender("Male");
}
if(FXMain.f.isSelected())
{ User.currentUser.setGender("Female");
}
savePreferenceToFile(User.currentUser, preferences); // Save
preference to file
currentPane = home();
stage.getScene().setRoot(currentPane);
});

full = new VBox(thanks,prefs,saveButton);

full.getStyleClass().add("pref-screen");
full.getStylesheets().add("style.css");
return full;
}

Pane movie(Button b)
{
String title = b.getText().substring(0,b.getText().lastIndexOf("\n"));

Label name = new Label("Details of " + title);


name.getStyleClass().add("m-name");

//label working

Label released = new Label();


released.getStyleClass().add("m-name");

Label runtime = new Label();


runtime.getStyleClass().add("m-name");

Label rating = new Label();


rating.getStyleClass().add("m-name");

Label genres = new Label();


genres.getStyleClass().add("m-name");

Label plot = new Label("Synopsis: ");


plot.getStyleClass().add("m-name");

Label syn = new Label();


syn.getStyleClass().add("m-plot");
syn.setWrapText(true);
//set label style

try {
String line;
BufferedReader br = new BufferedReader(new
FileReader(Recommender.movielist));
while ((line = br.readLine()) != null)
{
String data [] = line.split(",");
if(title.contains(data[1]))
{
syn.setText(data[2].replaceAll("~",","));
released.setText("Release date: " +data[3]);
runtime.setText("Runtime in minutes: "+data[4]);
genres.setText("Genres: " +Genre.assignGenre(data[5]));
rating.setText("IMDb rating: " + data[6]);
}
}
}catch (IOException e)
{e.printStackTrace();}
catch (ArrayIndexOutOfBoundsException e)
{

}
VBox vb = new VBox(name, released,runtime,rating,genres,plot,syn);
vb.setTranslateY(20);
vb.setTranslateX(10);
vb.setSpacing(10);
vb.setPadding(new Insets(10));
BorderPane bp = new BorderPane();
bp.setTop(back);
bp.setCenter(vb);

bp.getStyleClass().add("profile");
bp.getStylesheets().add("style.css");
return bp;
}

private List<String> getMoviesByGenre(String genre) {


List<String> movies = new ArrayList<>();

try (BufferedReader reader = new BufferedReader(new


FileReader("data/profiled.csv"))) {
String line;
while ((line = reader.readLine()) != null) {
String[] data = line.split(",");
if (data.length >= 6) {
String movieTitle = data[1];
String genreIds = data[5].trim();
String[] genreIdArray = genreIds.split(" ");

for (String genreId : genreIdArray) {


String genreName = Genre.assignGenre(genreId);
if (genreName != null && genreName.contains(genre)) {
movies.add(movieTitle);
break;
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}

Collections.sort(movies);
return movies.subList(0, Max);
}

private List<String> getMoviesByAlphabet(String alphabet) {


List<String> movies = new ArrayList<>();

try {
BufferedReader reader = new BufferedReader(new
FileReader("data/profiled.csv"));
String line;
while ((line = reader.readLine()) != null) {
String[] atad = line.split(",");
if (atad.length >= 6) {
String mTitle = atad[1];
if (mTitle.startsWith(alphabet)) {
movies.add(mTitle);
}
}

}
}catch (IOException e) {
e.printStackTrace();
}

Collections.sort(movies);
return movies.subList(0, Math.min(movies.size(), Max));
}

private void showMovies(List<String> movies) {


ObservableList<String> movieItems =
FXCollections.observableArrayList(movies);
movieList.setItems(movieItems);
}
private static void savePreferenceToFile(User user, String preferences) {
try {
BufferedWriter writer = new BufferedWriter(new
FileWriter("data/userdata.csv",true));
user.setPreferences(Genre.assignID(preferences));
writer.write("\n"+ user);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void showAlert(String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Error");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
}

FlowCheck:
package org.movie;

import java.util.Scanner;

public class FlowCheck {


public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Recommender.RatingBuild();
while (true) {
System.out.println("1. reg 2. login 3.exit");
switch (scan.nextInt()) {
case 1 -> User.reg();
case 2 -> {
User.login();
Recommender.GenreBuild();
Recommender.displayRatingRec();
}
case 3 -> {
System.out.println("Exiting.");
System.exit(0);
}
}
}
}
}

Style Sheet:
.logsign-button {
-fx-background-color: #891000;
-fx-text-fill: #ebe1df;
-fx-font-size: 35px;
-fx-padding: 10px 20px;
-fx-font-family: Bahnschrift;
-fx-pref-height: 65;
-fx-pref-width: 190;
}
.back {
-fx-background-color: #891000;
-fx-text-fill: #ebe1df;
-fx-pref-height: 40;
-fx-font-size: 20;
-fx-padding: 10px 20px;
-fx-font-family: Bahnschrift;
}
.title {
-fx-text-fill: #fffefd;
-fx-font-family: Bahnschrift;
-fx-font-size: 58;
-fx-padding: 15;
}
.welc {
-fx-text-fill: #fffefd;
-fx-padding: 10;
-fx-font-family: Bahnschrift;
-fx-font-size: 38;
-fx-translate-y: 30;
}
.greet {
-fx-text-fill: #fffefd;
-fx-font-family: Bahnschrift;
-fx-font-size: 35;
-fx-translate-y: 40;
}

.logsign {
-fx-spacing: 150;
-fx-alignment: center;
-fx-translate-y: 240;
-fx-padding: 20;
}

.welcome-screen {
-fx-alignment: top-center;
-fx-pref-width: 900;
-fx-pref-height: 680;
-fx-background-color: #af2311;
}
.home-screen {
-fx-alignment: bottom-left;
-fx-pref-width: 900;
-fx-pref-height: 680;
-fx-background-color: #af2311;
}
.result-screen {
-fx-alignment: center-left;
-fx-pref-width: 900;
-fx-pref-height: 680;
-fx-background-color: #af2311;
}
.profile {
-fx-pref-width: 900;
-fx-pref-height: 680;
-fx-background-color: #af2311;
}
.pref-screen {
-fx-alignment: center;
-fx-pref-width: 900;
-fx-pref-height: 680;
-fx-background-color: #af2311;
}
.login-head {
-fx-text-fill: #fffefd;
-fx-font-family: Bahnschrift;
-fx-font-size: 47;
-fx-translate-y: 40;
}
.pref-head {
-fx-text-fill: #fffefd;
-fx-font-family: Bahnschrift;
-fx-font-size: 37;
-fx-translate-x: 30;
-fx-translate-y: 30;
-fx-alignment: top-left;
}

.search {
-fx-border-color: #630b00;
-fx-background-color: #891000;
-fx-text-fill: #ebe1df;
-fx-font-size: 15px;
-fx-font-family: Bahnschrift;
-fx-pref-height: 45;
-fx-pref-width: 80;
}
.search-bar {
-fx-border-color: #630b00;
-fx-background-color: #891000;
-fx-text-fill: #ebe1df;
-fx-font-size: 15px;
-fx-font-family: Bahnschrift;
-fx-pref-height: 45;
-fx-pref-width: 180;
}

.login-txt {
-fx-text-fill: #fffefd;
-fx-font-family: Bahnschrift;
-fx-font-size: 32;
-fx-translate-y: 80;
}
.login-fields {
-fx-background-color: #630b00;
-fx-text-fill:#fffefd;
-fx-font-size: 17;
-fx-font-family: Bahnschrift;
-fx-pref-height: 50;
-fx-pref-width: 350;
}
.login-labels {
-fx-text-fill: #fffefd;
-fx-font-size: 28;
}
.login-stat {
-fx-text-fill: #fffefd;
-fx-font-size: 28;
-fx-alignment: center;
-fx-translate-y: 40;
}
.userexist-stat {
-fx-text-fill: #fffefd;
-fx-font-size: 24;
-fx-alignment: bottom-center;
-fx-translate-y: 90;
}
.gpfields {
-fx-hgap: 40;
-fx-vgap: 20;
-fx-translate-y: 90;
-fx-alignment: center;
-fx-pref-width: 300;
-fx-pref-height: 300;
}
.genrebox {
-fx-hgap: 2;
-fx-vgap: 10;
-fx-translate-x: -30;
-fx-alignment: center;
-fx-pref-width: 300;
-fx-pref-height: 300;
}
.submit-button {
-fx-background-color: #630b00;
-fx-text-fill: #ebe1df;
-fx-font-size: 33px;
-fx-translate-y: 110;
-fx-font-family: Bahnschrift;
-fx-pref-height: 65;
-fx-pref-width: 220;
-fx-alignment: center;
}
.save-button {
-fx-background-color: #630b00;
-fx-text-fill: #ebe1df;
-fx-font-size: 33px;
-fx-translate-y: 60;
-fx-font-family: Bahnschrift;
-fx-pref-height: 65;
-fx-pref-width: 180;
-fx-alignment: center;
}
.gender-button {
-fx-text-fill: #fffefd;
-fx-font-size: 20;
-fx-font-family: Bahnschrift;
-fx-translate-x: 30;
-fx-pref-height: 60;
-fx-pref-width: 100;
}
.gender-box {
-fx-spacing:100;
-fx-alignment: center-left;
}
.age-box {
-fx-background-color: #630b00;
-fx-text-fill:#fffefd;
-fx-font-size: 17;
-fx-font-family: Bahnschrift;
-fx-pref-height: 50;
-fx-pref-width: 350;
}
.movie {
-fx-background-color: #891000;
-fx-text-fill: #ebe1df;
-fx-text-alignment: center;
-fx-font-size: 15px;
-fx-padding: 10px;
-fx-font-family: Bahnschrift;
-fx-pref-height: 110;
-fx-pref-width: 100;
}
.searched-movies {
-fx-background-color: #891000;
-fx-text-fill: #ebe1df;
-fx-text-alignment: center;
-fx-font-size: 15px;
-fx-padding: 10px;
-fx-font-family: Bahnschrift;
-fx-pref-height: 80;
-fx-pref-width: 300;
}

.genre {
-fx-background-color: #891000;
-fx-text-fill: #ebe1df;
-fx-text-alignment: center;
-fx-padding: 10px;
-fx-font-family: Bahnschrift;
}
.mood-head {
-fx-padding: 10;
-fx-text-fill: #fffefd;
-fx-font-size: 28;
-fx-alignment: center-left;
}

.movie-scroll {
-fx-alignment: bottom-left;
-fx-background-color: #5b0b00;
-fx-padding: 3;
}
.result-scroll {
-fx-alignment: bottom-left;
-fx-background-color: #5b0b00;
-fx-padding: 3;
-fx-pref-width: 300;
}
.menu .label {
-fx-text-fill:#fffefd;
-fx-background-color: #630b00;
-fx-font-size: 15;
-fx-font-family: Bahnschrift;}
.menu {
-fx-background-color: #630b00;
}
.menu-item .label{
-fx-text-fill:#fffefd;
-fx-background-color: #630b00;
-fx-alignment: center;
-fx-font-size: 13;
-fx-font-family: Bahnschrift;
}

.menu-item {
-fx-background-color: #630b00;
-fx-pref-width: 90;
-fx-alignment: center;
}
.movie-result {
-fx-background-color: #891000;
-fx-text-fill: #ebe1df;
-fx-text-alignment: center;
-fx-font-size: 15px;
-fx-padding: 10px;
-fx-font-family: Bahnschrift;
-fx-pref-height: 110;
-fx-pref-width: 400;
}.browse {
-fx-pref-height: 700;
-fx-pref-width: 1000;
-fx-background-color: #AF2311;
}

.combo-box {
-fx-background-color: #891000;
-fx-text-fill: #FFFEFD;
-fx-prompt-text-fill: #FFFEFD;
}

.defaultzaid {
-fx-background-color: #FF0700;
-fx-text-fill: #FFFEFD;
-fx-font-size: 19;
}

.list-cell {
-fx-pref-width: 1000;
-fx-background-color: #891000;
-fx-text-fill: #FFFEFD;
}

.list-cell:selected {
-fx-background-color: #891000;
-fx-text-fill: #FFFEFD;
}
.m-name {
-fx-padding: 10;
-fx-text-fill: #fffefd;
-fx-font-size: 25;
-fx-alignment: center-left;
-fx-background-color: #861405;
}
.m-plot {
-fx-padding: 10;
-fx-text-fill: #fffefd;
-fx-font-size: 19;
-fx-alignment: center-left;
-fx-background-color: #861405;
}

You might also like