4 Commits

Author SHA1 Message Date
mac 34d370ab77 new code 3 years ago
mac e2cba51792 updated 3 years ago
mac 4bfd86ab07 updated 3 years ago
mac be91a2c74c updated 3 years ago
19 changed files with 327 additions and 78 deletions
Split View
  1. BIN
      Favorite_Movies/bin/Database.class
  2. BIN
      Favorite_Movies/bin/LoginFrame$1.class
  3. BIN
      Favorite_Movies/bin/LoginFrame.class
  4. BIN
      Favorite_Movies/bin/Main_HomePage$1.class
  5. BIN
      Favorite_Movies/bin/Main_HomePage$2.class
  6. BIN
      Favorite_Movies/bin/Main_HomePage$3.class
  7. BIN
      Favorite_Movies/bin/Main_HomePage$4.class
  8. BIN
      Favorite_Movies/bin/Main_HomePage.class
  9. BIN
      Favorite_Movies/bin/Star.class
  10. 159
      Favorite_Movies/src/Database.java
  11. 18
      Favorite_Movies/src/Main_HomePage.java
  12. 8
      Favorite_Movies/src/Movie.java
  13. 35
      Favorite_Movies/src/MoviesFrame.java
  14. 12
      Favorite_Movies/src/Rating.java
  15. 23
      Favorite_Movies/src/RatingsFrame.java
  16. 75
      Favorite_Movies/src/ShowMoviesForStarFrame.java
  17. 5
      Favorite_Movies/src/Star.java
  18. 35
      Favorite_Movies/src/StarsFrame.java
  19. 35
      Favorite_Movies/src/TopMovieFrame.java

BIN
Favorite_Movies/bin/Database.class

BIN
Favorite_Movies/bin/LoginFrame$1.class

BIN
Favorite_Movies/bin/LoginFrame.class

BIN
Favorite_Movies/bin/Main_HomePage$1.class

BIN
Favorite_Movies/bin/Main_HomePage$2.class

BIN
Favorite_Movies/bin/Main_HomePage$3.class

BIN
Favorite_Movies/bin/Main_HomePage$4.class

BIN
Favorite_Movies/bin/Main_HomePage.class

BIN
Favorite_Movies/bin/Star.class

159
Favorite_Movies/src/Database.java

@ -65,10 +65,22 @@ public class Database {
fw.write("\n");
}
public static void addMovie(Movie m){
public static int addMovie(Movie m){
int lastid = 0;
try {
FileWriter fw = new FileWriter( "c:\\tmp\\src\\files\\data.txt",true);
ArrayList<Movie> list = getMovies();
for (Movie mv: list)
{
if (mv.equals(m))
{
return 0;
}
if (mv.getMovieId() > lastid)
lastid = mv.getMovieId();
}
m.setMovieId(lastid + 1);
FileWriter fw = new FileWriter( "C:\\tmp\\FavoriteMovies\\files\\data.txt",true);
writeMovieToFile(fw, m);
@ -78,6 +90,7 @@ public class Database {
catch (IOException e1) { // TODO Auto-generated catch block
e1.printStackTrace();
}
return lastid+1;
}
@ -93,6 +106,9 @@ public class Database {
Movie m = Movie.parseString(line);
list.add(m);
}
br.close();
fr.close();
}
catch (IOException e1) { // TODO Auto-generated catch block
e1.printStackTrace();
@ -122,7 +138,7 @@ public class Database {
{
ArrayList<Star> list = new ArrayList<Star>();
try{
FileReader fr = new FileReader( "c:\\tmp\\src\\files\\star_data.txt");
FileReader fr = new FileReader( "C:\\tmp\\FavoriteMovies\\files\\star_data.txt");
BufferedReader br = new BufferedReader(fr);
String line = null;
while ((line=br.readLine())!=null)
@ -130,6 +146,8 @@ public class Database {
Star s = Star.parseString(line);
list.add(s);
}
br.close();
fr.close();
}
catch (IOException e1) { // TODO Auto-generated catch block
e1.printStackTrace();
@ -137,14 +155,93 @@ public class Database {
return list;
}
public static void addStar(Star s)
public static ArrayList<Rating> getRatings()
{
ArrayList<Rating> list = new ArrayList<Rating>();
try{
FileReader fr = new FileReader( "C:\\tmp\\FavoriteMovies\\files\\ratings_data.txt");
BufferedReader br = new BufferedReader(fr);
String line = null;
while ((line=br.readLine())!=null)
{
Rating s = Rating.parseString(line);
list.add(s);
}
br.close();
fr.close();
}
catch (IOException e1) { // TODO Auto-generated catch block
e1.printStackTrace();
}
return list;
}
public static boolean addStar(Star s)
{
boolean movieFound = false;
try
{
ArrayList<Movie> list = getMovies();
for (Movie m: list)
{
if (m.getMovieId() == s.getMovieId())
{
movieFound = true;
}
}
if (movieFound)
{
FileWriter fw = new FileWriter( "C:\\tmp\\FavoriteMovies\\files\\star_data.txt",true);
fw.write(String.valueOf(s.getMovieId()));
fw.append(" ").write(s.getMovieTitle());
fw.append(" ").write(String.valueOf(s.getYear()));
fw.append(" ").write(s.getNameSurname());
fw.append(" ").write("\n");
fw.close();
}
}
catch (IOException e1) { // TODO Auto-generated catch block
e1.printStackTrace();
movieFound = false;
}
return movieFound;
}
public static void addRating(Rating r)
public static boolean addRating(Rating r)
{
boolean movieFound = false;
try
{
ArrayList<Movie> list = getMovies();
for (Movie m: list)
{
if (m.getMovieId() == r.getMovieId())
{
movieFound = true;
}
}
if (movieFound)
{
//(movieId: int, NameSurname: string, relation: string, rating: int)
FileWriter fw = new FileWriter( "C:\\tmp\\FavoriteMovies\\files\\ratings_data.txt",true);
fw.write(String.valueOf(r.getMovieId()));
fw.append(" ").write(r.getNameSurname());
fw.append(" ").write(r.getRelation());
fw.append(" ").write(String.valueOf(r.getRating()));
fw.append(" ").write("\n");
fw.close();
UpdateRating(r.getMovieId(), r.getRating());
}
}
catch (IOException e1) { // TODO Auto-generated catch block
e1.printStackTrace();
movieFound = false;
}
return movieFound;
}
public static int findMovieId(String title)
@ -186,10 +283,12 @@ public class Database {
m.setAvgRating(0);
// TODO 8: search all movies by genre, and find the one with the top rating and return it
ArrayList<Movie> list = getMovies();
for (Movie movie : list)
{
if (movie.getGenre().equals(genre))
{
System.out.println("rating is " + movie.getAvgRating());
if (movie.getAvgRating() > m.getAvgRating())
m = movie;
}
@ -200,14 +299,58 @@ public class Database {
public static ArrayList<Movie> findMoviesForStar(String starName)
{
ArrayList<Movie> list = new ArrayList<Movie>();
// TODO 9: search all movies in which the star has acted and return a list
ArrayList<Integer> movieIdList = new ArrayList<Integer>();
ArrayList<Star> starList = getStars();
for (Star s: starList)
{
if (s.getNameSurname().equals(starName))
movieIdList.add(s.getMovieId());
}
ArrayList<Movie> fullList = getMovies();
for (Movie movie : fullList)
ArrayList<Movie> list = new ArrayList<Movie>();
for(Movie m: fullList)
{
if (movieIdList.contains(m.getMovieId()))
{
System.out.println("Adding " + m.getTitle() + " to list");
list.add(m);
}
}
return list;
}
public static void UpdateRating(int movieId, int newRating)
{
// get all ratings
ArrayList<Rating> ratings = getRatings();
// calculate avg rating
int totalRating = 0;
int numRatings = 0;
for (Rating r: ratings)
{
if (r.getMovieId() == movieId)
{
totalRating += r.getRating();
numRatings++;
}
}
// find movie by id
ArrayList<Movie> list = getMovies();
// update movie
for (Movie m: list)
{
if (m.getMovieId() == movieId)
{
m.setAvgRating(totalRating/numRatings);
break;
}
}
// save movies to file
saveMoviesList(list);
}
}

18
Favorite_Movies/src/Main_HomePage.java

@ -216,9 +216,7 @@ public class Main_HomePage extends JFrame {
}
}
}
});
});
btnShowHighRatedMovie.setBounds(10, 240, 220, 23);
panel.add(btnShowHighRatedMovie);
@ -229,6 +227,20 @@ public class Main_HomePage extends JFrame {
// form should have a text field to take the name of the star
// upon clicking the button, it should call Database.findMoviesForStar(String starName)
// and display a list of movie titles below
<<<<<<< Updated upstream
=======
>>>>>>> Stashed changes
ShowMoviesForStarFrame sm=new ShowMoviesForStarFrame();
sm.setVisible(true);
if(sm.isVisible()) {
//use to display only one screen
Main_HomePage.this.dispose();
}
<<<<<<< Updated upstream
=======
>>>>>>> Stashed changes
}
});

8
Favorite_Movies/src/Movie.java

@ -91,6 +91,14 @@ public class Movie {
this.numWatched = numWatched;
}
public boolean equals(Movie m)
{
if (m.getTitle().equals(this.getTitle())
&& m.getGenre().equals(this.getGenre()))
return true;
return false;
}
public static Movie parseString(String s)
{
String[] parts = s.split(" ");

35
Favorite_Movies/src/MoviesFrame.java

@ -52,12 +52,12 @@ public class MoviesFrame extends JFrame{
lblNewLabel.setBounds(273,11, 160, 29); lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 24));
lblNewLabel.setForeground(new Color(255, 255, 255)); panel.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Movie Id:");
/*JLabel lblNewLabel_1 = new JLabel("Movie Id:");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblNewLabel_1.setForeground(Color.WHITE);
lblNewLabel_1.setBounds(31, 74, 109, 14);
panel.add(lblNewLabel_1);
*/
JLabel lblNewLabel_1_1 = new JLabel("Title:");
lblNewLabel_1_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblNewLabel_1_1.setForeground(Color.WHITE);
@ -112,9 +112,10 @@ public class MoviesFrame extends JFrame{
lblNewLabel_1_3_6.setBounds(31, 479, 127,23);
panel.add(lblNewLabel_1_3_6);
/*
tf_mid = new JTextField(); tf_mid.setBounds(231, 66, 222, 29);
panel.add(tf_mid);
*/
tf_title = new JTextField(); tf_title.setBounds(231, 110, 222, 29);
panel.add(tf_title);
@ -160,13 +161,13 @@ public class MoviesFrame extends JFrame{
JButton btnM_ok = new JButton("OK");
btnM_ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(tf_mid.getText().isEmpty() || tf_title.getText().isEmpty() ||tf_year.getText().isEmpty() || tf_genre.getText().isEmpty()||tf_num_watch.getText().isEmpty() || tf_avgrate.getText().isEmpty() ||tf_pname.getText().isEmpty() || tf_sname.getText().isEmpty() ||tf_shortDesc.getText().isEmpty() || tf_length.getText().isEmpty()) {
if(tf_title.getText().isEmpty() ||tf_year.getText().isEmpty() || tf_genre.getText().isEmpty()||tf_num_watch.getText().isEmpty() || tf_avgrate.getText().isEmpty() ||tf_pname.getText().isEmpty() || tf_sname.getText().isEmpty() ||tf_shortDesc.getText().isEmpty() || tf_length.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "Please Enter All the Fields !!");
}
else{
Movie mv = new Movie();
mv.setMovieId(Integer.parseInt(tf_mid.getText()));
//mv.setMovieId(Integer.parseInt(tf_mid.getText()));
mv.setTitle(tf_title.getText());
mv.setLength(Integer.parseInt(tf_length.getText()));
mv.setYear(Integer.parseInt(tf_year.getText()));
@ -176,16 +177,22 @@ public class MoviesFrame extends JFrame{
mv.setShortDescription(tf_shortDesc.getText());
mv.setAvgRating(Double.parseDouble(tf_avgrate.getText()));
mv.setNumWatched(Integer.parseInt(tf_num_watch.getText()));
Database.addMovie(mv);
JOptionPane.showMessageDialog(null, "Movie added Successfully.....");
tf_mid.setText("");
tf_title.setText("");
tf_year.setText(""); tf_length.setText(""); tf_genre.setText("");
tf_sname.setText(""); tf_pname.setText(""); tf_shortDesc.setText("");
tf_avgrate.setText(""); tf_num_watch.setText("");
int movieId = Database.addMovie(mv);
if (movieId > 0)
{
JOptionPane.showMessageDialog(null, "Movie added Successfully.....new id: " + String.valueOf(movieId));
tf_title.setText("");
tf_year.setText(""); tf_length.setText(""); tf_genre.setText("");
tf_sname.setText(""); tf_pname.setText(""); tf_shortDesc.setText("");
tf_avgrate.setText(""); tf_num_watch.setText("");
}
else
{
JOptionPane.showMessageDialog(null, "Movie already exists!!");
}
}
}

12
Favorite_Movies/src/Rating.java

@ -39,4 +39,16 @@ public class Rating {
this.nameSurname = nameSurname;
}
public static Rating parseString(String s)
{
//(movieId: int, NameSurname: string, relation: string, rating: int)
Rating r = new Rating();
String[] parts = s.split(" ");
r.setMovieId(Integer.parseInt(parts[0]));
r.setNameSurname(parts[1]);
r.setRelation(parts[2]);
r.setRating(Integer.parseInt(parts[3]));
return r;
}
}

23
Favorite_Movies/src/RatingsFrame.java

@ -96,28 +96,19 @@ public class RatingsFrame extends JFrame {
rt.setRelation(tf_relationR.getText());
rt.setNameSurname(tf_snameR.getText());
rt.setRating(Integer.parseInt(tf_ratingsR.getText()));
try {
FileWriter fw = new FileWriter( "C:\\Users\\skc\\eclipse-workspace\\first\\Favorite_Movies\\src\\files\\ratings_data.txt",true);
tf_midR.write(fw);
tf_snameR.write(fw.append(" "));
tf_relationR.write(fw.append(" "));
tf_ratingsR.write(fw.append(" "));
fw.write("\n");
fw.close();
if (Database.addRating(rt))
{
JOptionPane.showMessageDialog(null, "Ratings added Successfully.....");
tf_midR.setText("");
tf_relationR.setText("");
tf_ratingsR.setText("");
tf_snameR.setText("");
} catch (IOException e1) { // TODO Auto-generated catch block
e1.printStackTrace(); }
}
else
{
JOptionPane.showMessageDialog(null, "Movie not found, Ratings not added!!");
}
}
}
});

75
Favorite_Movies/src/ShowMoviesForStarFrame.java

@ -0,0 +1,75 @@
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JList;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
public class ShowMoviesForStarFrame extends JFrame {
private JPanel contentPane;
private JTextField tf_starName;
private JList lMovies;
/**
* Create the frame.
*/
public ShowMoviesForStarFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 378, 375);
contentPane = new JPanel();
contentPane.setBackground(new Color(0, 0, 51));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Enter Star Name:");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblNewLabel.setForeground(new Color(255, 255, 255));
lblNewLabel.setBounds(66, 91, 162, 24);
contentPane.add(lblNewLabel);
tf_starName = new JTextField();
tf_starName.setBounds(66, 129, 210, 24);
contentPane.add(tf_starName);
tf_starName.setColumns(10);
lMovies = new JList();
lMovies.setFont(new Font("Tahoma", Font.PLAIN, 14));
lMovies.setForeground(new Color(255, 255, 255));
lMovies.setBounds(66, 160, 210, 100);
contentPane.add(lMovies);
JButton btnOkSM = new JButton("ok");
btnOkSM.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ArrayList<Movie> movies = Database.findMoviesForStar(tf_starName.getText());
if (movies.size()>0)
{
JOptionPane.showMessageDialog(null, "Movies Found for Star!");
for(Movie m: movies)
{
lMovies.add(new JLabel(m.getTitle()));
}
}
else {
JOptionPane.showMessageDialog(null, "Title not found !!");
}
}
});
btnOkSM.setBounds(68, 300, 89, 23);
contentPane.add(btnOkSM);
}
}

5
Favorite_Movies/src/Star.java

@ -43,7 +43,10 @@ public class Star{
Star st = new Star();
String[] parts = s.split(" ");
st.setMovieId(Integer.parseInt(parts[0]));
//movieId: int, movieTitle: string, movieYear: int, NameSurname: string
st.setMovieTitle(parts[1]);
st.setYear(Integer.parseInt(parts[2]));
st.setNameSurname(parts[3]);
return st;
}

35
Favorite_Movies/src/StarsFrame.java

@ -90,28 +90,33 @@ public class StarsFrame extends JFrame {
s.setMovieTitle(tf_mtitleS.getText());
s.setNameSurname(tf_snameS.getText());
s.setYear(Integer.parseInt(tf_yearS.getText()));
try {
FileWriter fw = new FileWriter( "C:\\Users\\skc\\eclipse-workspace\\first\\Favorite_Movies\\src\\files\\star_data.txt",true);
tf_midS.write(fw);
tf_mtitleS.write(fw.append(" "));
tf_snameS.write(fw.append(" "));
tf_yearS.write(fw.append(" "));
fw.write("\n");
fw.close();
<<<<<<< Updated upstream
=======
>>>>>>> Stashed changes
if (Database.addStar(s))
{
JOptionPane.showMessageDialog(null, "Star added Successfully.....");
tf_midS.setText("");
tf_mtitleS.setText("");
tf_yearS.setText("");
tf_snameS.setText("");
<<<<<<< Updated upstream
tf_snameS.setText("");
}
else{
} catch (IOException e1) { // TODO Auto-generated catch block
e1.printStackTrace(); }
JOptionPane.showMessageDialog(null, "Movie not found, star not addded.....");
}
=======
tf_snameS.setText("");
}
else {
JOptionPane.showMessageDialog(null, "Movie not found, Star not added!!");
}
>>>>>>> Stashed changes
}
}
});btnStar.setBounds(195, 320, 89, 23);

35
Favorite_Movies/src/TopMovieFrame.java

@ -18,22 +18,7 @@ public class TopMovieFrame extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TopMovieFrame frame = new TopMovieFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
@ -52,21 +37,29 @@ public class TopMovieFrame extends JFrame {
lblSelectGenre.setBounds(116, 45, 162, 24);
contentPane.add(lblSelectGenre);
JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"", "Comic", "Horror", "Comedy", "Drama", "Action", "Thriller"}));
JComboBox<String> comboBox = new JComboBox<String>();
comboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"", "Comic", "Horror", "Comedy", "Drama", "Action", "Thriller"}));
comboBox.setToolTipText("");
comboBox.setBounds(107, 100, 171, 22);
contentPane.add(comboBox);
JLabel lblDisplayTopMovie = new JLabel();
lblDisplayTopMovie.setForeground(Color.WHITE);
lblDisplayTopMovie.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblDisplayTopMovie.setBounds(116, 145, 162, 24);
contentPane.add(lblDisplayTopMovie);
JButton btnOkTmg = new JButton("ok");
btnOkTmg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String genre=comboBox.getToolTipText().toString();
Database.findTopMovieByGenre(genre);
String genre=comboBox.getItemAt(comboBox.getSelectedIndex());
System.out.println("selected genre is " + genre);
Movie m = Database.findTopMovieByGenre(genre);
lblDisplayTopMovie.setText(m.getTitle());
}
});
btnOkTmg.setBounds(118, 154, 89, 23);
btnOkTmg.setBounds(118, 180, 89, 23);
contentPane.add(btnOkTmg);

Loading…
Cancel
Save