lr10 -> task_2

This commit is contained in:
2026-04-05 21:23:04 +05:00
parent 500c41df80
commit 598306fb7a
5 changed files with 415 additions and 0 deletions

143
lr10/task_2/e2.java Normal file
View File

@@ -0,0 +1,143 @@
package lr10.task_2;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.Scanner;
public class e2 {
private static final String FILE_NAME = "lr10/task_2/movies.json";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in, "UTF-8");
while (true) {
System.out.println("\n--- JSON Менеджер Фильмов ---");
System.out.println("1. Показать все фильмы");
System.out.println("2. Поиск по режиссёру");
System.out.println("3. Добавить фильм");
System.out.println("4. Удалить фильм по названию");
System.out.println("0. Выход");
System.out.print("Выберите действие: ");
String choice = scanner.nextLine();
try {
switch (choice) {
case "1":
displayAll();
break;
case "2":
System.out.print("Введите имя режиссёра: ");
searchByAuthor(scanner.nextLine());
break;
case "3":
System.out.print("Название: ");
String title = scanner.nextLine();
System.out.print("Режиссёр: ");
String author = scanner.nextLine();
System.out.print("Год: ");
int year = Integer.parseInt(scanner.nextLine());
addMovie(title, author, year);
break;
case "4":
System.out.print("Введите название для удаления: ");
deleteMovie(scanner.nextLine());
break;
case "0":
return;
}
} catch (Exception e) {
System.out.println("Ошибка: " + e.getMessage());
}
}
}
private static JSONObject loadData() throws Exception {
JSONParser parser = new JSONParser();
return (JSONObject) parser.parse(
new InputStreamReader(new FileInputStream(FILE_NAME), "UTF-8"));
}
private static void saveData(JSONObject data) throws Exception {
try (OutputStreamWriter file = new OutputStreamWriter(
new FileOutputStream(FILE_NAME), "UTF-8")) {
file.write(data.toJSONString());
}
}
// 1. Показать все
private static void displayAll() throws Exception {
JSONObject data = loadData();
JSONArray movies = (JSONArray) data.get("movies");
for (Object obj : movies) {
JSONObject movie = (JSONObject) obj;
System.out.println("\nНазвание: " + movie.get("title"));
System.out.println("Режиссёр: " + movie.get("author"));
System.out.println("Год: " + movie.get("year"));
}
}
// 2. Поиск по автору — используем stream() как в задании
@SuppressWarnings("unchecked")
private static void searchByAuthor(String author) throws Exception {
JSONObject data = loadData();
JSONArray movies = (JSONArray) data.get("movies");
System.out.println("Результаты поиска:");
((java.util.List<JSONObject>) (java.util.List<?>) movies).stream()
.filter(m -> author.equalsIgnoreCase((String) m.get("author")))
.forEach(m -> {
System.out.println("\nНазвание: " + m.get("title"));
System.out.println("Режиссёр: " + m.get("author"));
System.out.println("Год: " + m.get("year"));
});
}
// 3. Добавление
@SuppressWarnings("unchecked")
private static void addMovie(String title, String author, int year) throws Exception {
JSONObject data = loadData();
JSONArray movies = (JSONArray) data.get("movies");
JSONObject newMovie = new JSONObject();
newMovie.put("title", title);
newMovie.put("author", author);
newMovie.put("year", year);
movies.add(newMovie);
saveData(data);
System.out.println("Фильм добавлен!");
}
// 4. Удаление через Iterator
private static void deleteMovie(String title) throws Exception {
JSONObject data = loadData();
JSONArray movies = (JSONArray) data.get("movies");
Iterator iterator = movies.iterator();
boolean found = false;
while (iterator.hasNext()) {
JSONObject movie = (JSONObject) iterator.next();
if (title.equalsIgnoreCase((String) movie.get("title"))) {
iterator.remove();
found = true;
}
}
if (found) {
saveData(data);
System.out.println("Фильм удалён!");
} else {
System.out.println("Фильм не найден.");
}
}
}