Skip to content

Commit b2daf15

Browse files
committed
Add lab3
1 parent 6aacd90 commit b2daf15

File tree

6 files changed

+399
-3
lines changed

6 files changed

+399
-3
lines changed

src/main/java/dp/module2/lab1/store/Film.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import org.w3c.dom.Document;
44
import org.w3c.dom.Element;
55

6+
import java.io.DataInputStream;
7+
import java.io.IOException;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
611
public class Film {
712
private int id;
813
private String name;
@@ -24,19 +29,34 @@ public Film(int id, String name, Float duration, Genre genre) {
2429
this.genre = genre;
2530
}
2631

32+
//TO DO: replace by creating with genre name
2733
public Film(String name, Float duration, Genre genre) {
34+
this.id = -1;
2835
this.name = name;
2936
this.duration = duration;
3037
this.genre = genre;
3138
}
3239

33-
public Film (Element filmElement, Element genreElement) {
40+
public Film(Element filmElement, Element genreElement) {
3441
this.id = Integer.parseInt(filmElement.getAttribute("id"));
3542
this.name = filmElement.getAttribute("name");
3643
this.duration = Float.parseFloat(filmElement.getAttribute("duration"));
3744
this.genre = new Genre(genreElement);
3845
}
3946

47+
public static int listSize() {
48+
return Genre.listSize() + 3;
49+
}
50+
51+
public static Film parseFilm(DataInputStream in) throws IOException {
52+
List<String> list = new ArrayList<>();
53+
54+
for (int i = 0, n = Film.listSize(); i < n; i++)
55+
list.add(in.readUTF());
56+
57+
return new Film(list);
58+
}
59+
4060
public Genre getGenre() {
4161
return genre;
4262
}
@@ -45,6 +65,13 @@ public int getId() {
4565
return id;
4666
}
4767

68+
public List<String> toList() {
69+
List<String> filmList = new ArrayList<>(List.of(Integer.toString(id), name, Float.toString(duration)));
70+
filmList.addAll(genre.toList());
71+
72+
return filmList;
73+
}
74+
4875
@Override
4976
public String toString() {
5077
return "Film{" +

src/main/java/dp/module2/lab1/store/Genre.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
import org.w3c.dom.Document;
44
import org.w3c.dom.Element;
55

6+
import java.io.DataInputStream;
7+
import java.io.IOException;
8+
import java.util.ArrayList;
9+
import java.util.List;
610
import java.util.Objects;
711

812
public class Genre {
913
private int id;
1014
private String name;
1115

1216
public Genre(String name) {
17+
this.id = -1;
1318
this.name = name;
1419
}
1520

@@ -23,6 +28,28 @@ public Genre(Element genreElement) {
2328
this.name = genreElement.getAttribute("name");
2429
}
2530

31+
public Genre(List<String> arguments) {
32+
this.id = Integer.parseInt(arguments.get(0));
33+
this.name = arguments.get(1);
34+
}
35+
36+
public static int listSize() {
37+
return 2;
38+
}
39+
40+
public static Genre parseGenre(DataInputStream in) throws IOException {
41+
List<String> list = new ArrayList<>();
42+
43+
for (int i = 0, n = Genre.listSize(); i < n; i++)
44+
list.add(in.readUTF());
45+
46+
return new Genre(list);
47+
}
48+
49+
public List<String> toList() {
50+
return new ArrayList<>(List.of(Integer.toString(id), name));
51+
}
52+
2653
@Override
2754
public String toString() {
2855
return "Genre{" +
@@ -60,5 +87,4 @@ public Element getElement(Document document) {
6087

6188
return element;
6289
}
63-
6490
}

src/main/java/dp/module2/lab2/VideoStoreDAO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public boolean updateFilm(Film oldFilm, Film newFilm) throws Exception {
175175

176176
statement.setString(1, newFilm.getName());
177177
statement.setFloat(2, newFilm.getDuration());
178-
statement.setInt(3, newFilm.getGenre().getId());
178+
statement.setInt(3, getGenreId(newFilm.getGenre().getName()));
179179
statement.setString(4, oldFilm.getName());
180180

181181
statement.executeUpdate();
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
package dp.module2.lab3.client;
2+
3+
import dp.module2.lab1.store.Film;
4+
import dp.module2.lab1.store.Genre;
5+
import dp.module2.lab3.common.QueryType;
6+
7+
import java.io.DataInputStream;
8+
import java.io.DataOutputStream;
9+
import java.io.IOException;
10+
import java.net.Socket;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import java.util.Scanner;
14+
15+
public class Client {
16+
private Socket socket;
17+
private DataOutputStream out;
18+
private DataInputStream in;
19+
20+
public Client(String ip, int port) throws IOException {
21+
socket = null;
22+
23+
while (true) {
24+
try {
25+
System.out.println("Connecting to server...");
26+
socket = new Socket(ip, port);
27+
System.out.println("Connected");
28+
break;
29+
} catch (IOException e) {
30+
int timeout = 5000;
31+
System.out.printf("Failed. Waiting %d ms...%n", timeout);
32+
try {
33+
Thread.sleep(timeout);
34+
} catch (InterruptedException ex) {
35+
ex.printStackTrace();
36+
Thread.currentThread().interrupt();
37+
}
38+
}
39+
}
40+
41+
in = new DataInputStream(socket.getInputStream());
42+
out = new DataOutputStream(socket.getOutputStream());
43+
}
44+
45+
public boolean addGenre(Genre genre) throws IOException {
46+
sendQuery(QueryType.ADD_GENRE, List.of(genre.getName()));
47+
return in.readInt() == 0;
48+
}
49+
50+
public boolean deleteGenre(Genre genre) throws IOException {
51+
sendQuery(QueryType.DELETE_GENRE, List.of(genre.getName()));
52+
return in.readInt() == 0;
53+
}
54+
55+
public boolean addFilm(Film film) throws IOException {
56+
sendQuery(QueryType.ADD_FILM, film.toList());
57+
return in.readInt() == 0;
58+
}
59+
60+
public boolean deleteFilm(Film film) throws IOException {
61+
sendQuery(QueryType.ADD_FILM, film.toList());
62+
return in.readInt() == 0;
63+
}
64+
65+
public boolean updateFilm(Film oldFilm, Film newFilm) throws IOException {
66+
List<String> arguments = oldFilm.toList();
67+
arguments.addAll(newFilm.toList());
68+
sendQuery(QueryType.UPDATE_FILM, arguments);
69+
70+
return in.readInt() == 0;
71+
}
72+
73+
public int countFilmsByGenre(Genre genre) throws IOException {
74+
sendQuery(QueryType.COUNT_FILMS_BY_GENRE, List.of(genre.getName()));
75+
int count = in.readInt();
76+
int status = in.readInt();
77+
78+
return count;
79+
}
80+
81+
public Film getFilmByName(Film film) throws IOException {
82+
sendQuery(QueryType.GET_FILM_BY_NAME, film.toList());
83+
Film resultFilm = Film.parseFilm(in);
84+
int status = in.readInt();
85+
return resultFilm;
86+
}
87+
88+
public List<Film> getFilmsByGenre(Genre genre) throws IOException {
89+
sendQuery(QueryType.GET_FILMS_BY_GENRE, genre.toList());
90+
int count = in.readInt();
91+
92+
List<Film> films = new ArrayList<>();
93+
94+
for (int i = 0; i < count; i++) {
95+
films.add(Film.parseFilm(in));
96+
}
97+
98+
int status = in.readInt();
99+
100+
return films;
101+
}
102+
103+
public List<Genre> getGenres() throws IOException {
104+
sendQuery(QueryType.GET_GENRES, List.of());
105+
int count = in.readInt();
106+
107+
List<Genre> genres = new ArrayList<>();
108+
109+
for (int i = 0; i < count; i++) {
110+
genres.add(Genre.parseGenre(in));
111+
}
112+
113+
int status = in.readInt();
114+
115+
return genres;
116+
}
117+
118+
public void disconnect() throws IOException {
119+
System.out.println("Disconnected from server");
120+
socket.close();
121+
}
122+
123+
private boolean sendQuery(QueryType type, List<String> arguments) throws IOException {
124+
out.writeUTF(type.name());
125+
126+
for (String argument : arguments) {
127+
out.writeUTF(argument);
128+
}
129+
130+
return true;
131+
}
132+
133+
public static void main(String[] args) throws IOException {
134+
Client client = null;
135+
try {
136+
client = new Client("localhost", 2710);
137+
} catch (IOException e) {
138+
e.printStackTrace();
139+
}
140+
141+
Scanner sc = new Scanner(System.in);
142+
String genreName;
143+
String filmName;
144+
float duration;
145+
146+
147+
while (true) {
148+
// System.out.print("Enter a genre: ");
149+
// genreName = sc.nextLine();
150+
//
151+
// if (Objects.equals(genreName, "stop")) {
152+
// break;
153+
// }
154+
//
155+
// client.addGenre(new Genre(genreName));
156+
157+
for(Genre genre : client.getGenres()) {
158+
System.out.println(genre);
159+
}
160+
161+
System.out.print("Enter a film name: ");
162+
filmName = sc.nextLine();
163+
//
164+
if (filmName == "stop")
165+
break;
166+
//
167+
// System.out.print("Enter a film duration: ");
168+
// duration = Float.parseFloat(sc.nextLine());
169+
// System.out.print("Enter a film genre: ");
170+
// genreName = sc.nextLine();
171+
//
172+
// client.addFilm(new Film(filmName, duration, genreName));
173+
174+
175+
176+
}
177+
178+
try {
179+
client.disconnect();
180+
} catch (IOException e) {
181+
e.printStackTrace();
182+
}
183+
}
184+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package dp.module2.lab3.common;
2+
3+
public enum QueryType {
4+
ADD_GENRE, DELETE_GENRE, ADD_FILM, DELETE_FILM, UPDATE_FILM, COUNT_FILMS_BY_GENRE, GET_FILM_BY_NAME,
5+
GET_FILMS_BY_GENRE, GET_GENRES;
6+
}

0 commit comments

Comments
 (0)