-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMovieDatabase.java
137 lines (132 loc) · 3.13 KB
/
MovieDatabase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Written by Noah Shaw
*/
import java.io.*;
import java.util.*;
public class MovieDatabase {
//Generic linked list of Movies
private GenLinkedList<Movie> movies;
//Delimiter
public static final String DELIM = "\t";
public static final int FIELD_AMT = 5;
//Constructor
public MovieDatabase()
{
movies = new GenLinkedList<Movie>();
}
//Methods
public void addMovie(Movie aMovie) //Adds a movie to the list
{
movies.insert(aMovie);
}
public void removeMovie(String aName) //Removes a movie from a list
{
movies.resetCurrent();
while(movies.hasMore() && !(movies.getCurrent().getName().equalsIgnoreCase(aName)))
{
movies.goToNext();
}
if(movies.getCurrent().getName().equalsIgnoreCase(aName))
{
movies.deleteCurrent();
}
}
public void search(String aBase, String aCriteria) //Searches for a movie based on a search base and search criteria
{
if(aBase.equalsIgnoreCase("title")) //Title
{
movies.resetCurrent();
while(movies.hasMore())
{
if(movies.getCurrent().getName().equalsIgnoreCase(aCriteria))
{
System.out.println(movies.getCurrent());
}
movies.goToNext();
}
}
else if(aBase.equalsIgnoreCase("director")) //Director
{
movies.resetCurrent();
while(movies.hasMore())
{
if(movies.getCurrent().getDirector().equalsIgnoreCase(aCriteria))
{
System.out.println(movies.getCurrent());
}
movies.goToNext();
}
}
}
public void search(String aBase, int aCriteria) //Overloaded method for search
{
if(aBase.equalsIgnoreCase("year")) //Year
{
movies.resetCurrent();
while(movies.hasMore())
{
if(movies.getCurrent().getYear() == aCriteria)
{
System.out.println(movies.getCurrent());
}
movies.goToNext();
}
}
else if(aBase.equalsIgnoreCase("rating")) //Rating
{
movies.resetCurrent();
while(movies.hasMore())
{
if(movies.getCurrent().getRating() == aCriteria)
{
System.out.println(movies.getCurrent());
}
movies.goToNext();
}
}
}
public void print() //Prints the list of movies
{
movies.print();
}
public void writeToFile(String aFileName) //Writes to a file
{
try
{
PrintWriter fileWriter = new PrintWriter(new FileOutputStream(aFileName + ".txt"));
movies.resetCurrent();
while(movies.hasMore())
{
fileWriter.println(movies.getCurrent());
movies.goToNext();
}
fileWriter.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
public void readFromFile(String fileName) //Reads from a file
{
try
{
Scanner fileScanner = new Scanner(new File(fileName + ".txt"));
while(fileScanner.hasNextLine())
{
String fileLine = fileScanner.nextLine();
String[] splitLine = fileLine.split(DELIM);
if(splitLine.length != FIELD_AMT)
{
continue;
}
this.addMovie(new Movie(splitLine[0], Integer.parseInt(splitLine[1]), Integer.parseInt(splitLine[2]), splitLine[3], Integer.parseInt(splitLine[4])));
}
fileScanner.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}