Browse Source

fixed todo1

master
mac 3 years ago
parent
commit
83e9aeb476
12 changed files with 170 additions and 26 deletions
  1. BIN
      Favorite_Movies/bin/Database.class
  2. BIN
      Favorite_Movies/bin/Main_HomePage$1.class
  3. BIN
      Favorite_Movies/bin/Main_HomePage$2.class
  4. BIN
      Favorite_Movies/bin/Main_HomePage$3.class
  5. BIN
      Favorite_Movies/bin/Main_HomePage$4.class
  6. BIN
      Favorite_Movies/bin/Main_HomePage.class
  7. BIN
      Favorite_Movies/bin/Star.class
  8. 148
      Favorite_Movies/src/Database.java
  9. 18
      Favorite_Movies/src/Movie.java
  10. 16
      Favorite_Movies/src/ShowMovieFrame.java
  11. 10
      Favorite_Movies/src/Star.java
  12. 4
      ReadMe.txt

BIN
Favorite_Movies/bin/Database.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

148
Favorite_Movies/src/Database.java

@ -1,10 +1,12 @@
import java.io.IOException;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.io.BufferedReader;
public class Database {
@ -37,31 +39,38 @@ public class Database {
return found;
}
public static void addMovie(Movie m){
try {
private static void writeMovieToFile(FileWriter fw, Movie m) throws IOException
{
FileWriter fw = new FileWriter( "C:\\Users\\skc\\eclipse-workspace\\first\\Favorite_Movies\\src\\files\\data.txt",true);
fw.write(m.getMovieId());
fw.write(m.getMovieId());
fw.append(" ").write(m.getTitle());
fw.append(" ").write(m.getTitle());
fw.append(" ").write(m.getYear());
fw.append(" ").write(m.getLength());
fw.append(" ").write(m.getGenre());
fw.append(" ").write(m.getStudioName());
fw.append(" ").write(m.getProducerName());
fw.append(" ").write(m.getShortDescription());
fw.append(" ").write(String.valueOf(m.getAvgRating()));
fw.append(" ").write(m.getNumWatched());
fw.append(" ").write(m.getYear());
fw.append(" ").write(m.getLength());
fw.append(" ").write(m.getGenre());
fw.append(" ").write(m.getStudioName());
fw.append(" ").write(m.getProducerName());
fw.append(" ").write(m.getShortDescription());
fw.append(" ").write(String.valueOf(m.getAvgRating()));
fw.append(" ").write(m.getNumWatched());
fw.write("\n");
}
public static void addMovie(Movie m){
try {
FileWriter fw = new FileWriter( "c:\\tmp\\src\\files\\data.txt",true);
fw.write("\n");
writeMovieToFile(fw, m);
fw.close();
@ -72,6 +81,62 @@ public class Database {
}
public static ArrayList<Movie> getMovies()
{
ArrayList<Movie> list = new ArrayList<Movie>();
try{
FileReader fr = new FileReader( "C:\\tmp\\FavoriteMovies\\files\\data.txt");
BufferedReader br = new BufferedReader(fr);
String line = null;
while ((line=br.readLine())!=null)
{
Movie m = Movie.parseString(line);
list.add(m);
}
}
catch (IOException e1) { // TODO Auto-generated catch block
e1.printStackTrace();
}
return list;
}
public static void saveMoviesList(ArrayList<Movie> list)
{
try {
FileWriter fw = new FileWriter( "C:\\tmp\\FavoriteMovies\\files\\data.txt");
for (Movie m: list)
{
writeMovieToFile(fw, m);
}
fw.close();
}
catch (IOException e1) { // TODO Auto-generated catch block
e1.printStackTrace();
}
}
public static ArrayList<Star> getStars()
{
ArrayList<Star> list = new ArrayList<Star>();
try{
FileReader fr = new FileReader( "c:\\tmp\\src\\files\\star_data.txt");
BufferedReader br = new BufferedReader(fr);
String line = null;
while ((line=br.readLine())!=null)
{
Star s = Star.parseString(line);
list.add(s);
}
}
catch (IOException e1) { // TODO Auto-generated catch block
e1.printStackTrace();
}
return list;
}
public static void addStar(Star s)
{
@ -85,18 +150,50 @@ public class Database {
public static int findMovieId(String title)
{
// TODO 6: search all titles and find matching movie id , return 0 if not found
return 1;
System.out.println("Finding for title: " + title);
ArrayList<Movie> list = getMovies();
for (Movie movie : list)
{
System.out.println("title: " + movie.getTitle());
if (movie.getTitle().equals(title))
return movie.getMovieId();
}
return 0;
}
public static void incNumWatched(int movieId)
public static boolean incNumWatched(int movieId)
{
// TODO 7: find movie by id and increment numWatched by 1
boolean found = false;
ArrayList<Movie> list = getMovies();
for (Movie movie : list)
{
if (movie.getMovieId() == movieId)
{
movie.setNumWatched(movie.getNumWatched()+1);
found = true;
}
}
if (found)
saveMoviesList(list);
return found;
}
public static Movie findTopMovieByGenre(String genre)
{
Movie m = new Movie();
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))
{
if (movie.getAvgRating() > m.getAvgRating())
m = movie;
}
}
return m;
}
@ -105,6 +202,11 @@ public class Database {
ArrayList<Movie> list = new ArrayList<Movie>();
// TODO 9: search all movies in which the star has acted and return a list
ArrayList<Movie> fullList = getMovies();
for (Movie movie : fullList)
{
}
return list;
}

18
Favorite_Movies/src/Movie.java

@ -90,4 +90,22 @@ public class Movie {
public void setNumWatched(int numWatched) {
this.numWatched = numWatched;
}
public static Movie parseString(String s)
{
String[] parts = s.split(" ");
Movie m = new Movie();
m.setMovieId(Integer.parseInt(parts[0]));
m.setTitle(parts[1]);
// TODO 10: populate rest of the fields
return m;
}
public String toString()
{
//movieId: int, title: string, year: int, length: int, genre: string, studioName: string, producerName:
//string, shortDescription: String, avgRating: double, numWatched: int
return this.movieId + " " + this.title + " " + this.year + " " + this.length + " " + this.genre + " " + this.studioName
+ " " + this.producerName + " " + this.shortDescription + " " + this.avgRating + " " + this.numWatched;
}
}

16
Favorite_Movies/src/ShowMovieFrame.java

@ -18,7 +18,8 @@ public class ShowMovieFrame extends JFrame {
private JPanel contentPane;
private JTextField tf_mt_sm;
Database db=new Database();
private JLabel lb_mid;
//Database db=new Database(); Database methods are static
/**
* Launch the application.
@ -58,15 +59,24 @@ public class ShowMovieFrame extends JFrame {
tf_mt_sm.setBounds(66, 129, 210, 24);
contentPane.add(tf_mt_sm);
tf_mt_sm.setColumns(10);
lb_mid = new JLabel();
lb_mid.setFont(new Font("Tahoma", Font.PLAIN, 14));
lb_mid.setForeground(new Color(255, 255, 255));
lb_mid.setBounds(66, 150, 210, 24);
contentPane.add(lb_mid);
JButton btnOkSM = new JButton("ok");
btnOkSM.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int found = db.findMovieId(tf_mt_sm.getText());
if(found==1) {
int movieId = Database.findMovieId(tf_mt_sm.getText());
if(movieId > 0) {
JOptionPane.showMessageDialog(null, "Title Found!");
lb_mid.setText(String.valueOf(movieId));
}
else {
lb_mid.setText("");
JOptionPane.showMessageDialog(null, "Title not found !!");
}
}

10
Favorite_Movies/src/Star.java

@ -38,4 +38,14 @@ public class Star{
this.nameSurname = nameSurname;
}
public static Star parseString(String s)
{
Star st = new Star();
String[] parts = s.split(" ");
st.setMovieId(Integer.parseInt(parts[0]));
return st;
}
}

4
ReadMe.txt

@ -1,3 +1,7 @@
Mac:
1. updated Database
2. updated ShowMovieFrame to display movieid if found
PMR:
1.completed todo2

Loading…
Cancel
Save