-
Notifications
You must be signed in to change notification settings - Fork 0
/
Joke
144 lines (124 loc) · 4.96 KB
/
Joke
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
138
139
140
141
142
143
144
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Joke implements Serializable {
private static final long serialVersionUID = 1L;
private String content;
private int rating;
public Joke(String content) {
this.content = content;
this.rating = 0;
}
public String getContent() {
return content;
}
public int getRating() {
return rating;
}
public void rateJoke(int rating) {
if (rating >= 1 && rating <= 5) {
this.rating = rating;
System.out.println("Шутка оценена на " + rating + " баллов!");
} else {
System.out.println("Оценка должна быть от 1 до 5.");
}
}
@Override
public String toString() {
return "\"" + content + "\" (оценка: " + rating + ")";
}
}
class JokeList implements Serializable {
private static final long serialVersionUID = 1L;
private List<Joke> jokes;
public JokeList() {
jokes = new ArrayList<>();
}
public void addJoke(Joke joke) {
jokes.add(joke);
System.out.println("Шутка добавлена: " + joke.getContent());
}
public void displayJokes() {
if (jokes.isEmpty()) {
System.out.println("Список шуток пуст.");
} else {
System.out.println("Список шуток:");
for (Joke joke : jokes) {
System.out.println("- " + joke);
}
}
}
public void rateJoke(int index, int rating) {
if (index >= 0 && index < jokes.size()) {
Joke joke = jokes.get(index);
joke.rateJoke(rating);
} else {
System.out.println("Неверный индекс шутки.");
}
}
public void saveJokesToFile(String filename) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
oos.writeObject(this);
System.out.println("Список шуток сохранен в файл: " + filename);
} catch (IOException e) {
e.printStackTrace();
}
}
public static JokeList loadJokesFromFile(String filename) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
return (JokeList) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return new JokeList(); // если произошла ошибка, вернем пустой список шуток
}
}
}
public class JokeManager {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
JokeList jokeList = new JokeList();
// Загрузка списка шуток из файла (если файл существует)
jokeList = JokeList.loadJokesFromFile("jokes.dat");
while (true) {
System.out.println("\nВыберите действие:");
System.out.println("1. Добавить шутку");
System.out.println("2. Показать шутки");
System.out.println("3. Оценить шутку");
System.out.println("4. Сохранить список шуток в файл");
System.out.println("5. Загрузить список шуток из файла");
System.out.println("6. Выйти");
int choice = scanner.nextInt();
scanner.nextLine(); // очистим буфер после ввода числа
switch (choice) {
case 1:
System.out.println("Введите текст шутки:");
String content = scanner.nextLine();
Joke newJoke = new Joke(content);
jokeList.addJoke(newJoke);
break;
case 2:
jokeList.displayJokes();
break;
case 3:
System.out.println("Введите индекс шутки для оценки:");
int index = scanner.nextInt();
System.out.println("Введите оценку шутки (от 1 до 5):");
int rating = scanner.nextInt();
jokeList.rateJoke(index, rating);
break;
case 4:
jokeList.saveJokesToFile("jokes.dat");
break;
case 5:
jokeList = JokeList.loadJokesFromFile("jokes.dat");
break;
case 6:
System.out.println("Программа завершена.");
System.exit(0);
default:
System.out.println("Неверный выбор. Пожалуйста, выберите снова.");
}
}
}
}