-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataBaseLab1.cpp
286 lines (264 loc) · 5.31 KB
/
DataBaseLab1.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <iostream>
#include "SongFile.h"
SongFile sFl = SongFile();
AlbumFile aFl = AlbumFile();
IndexFile iFl = IndexFile();
DeletedAlbums dAl = DeletedAlbums();
DeletedSongs dSongs = DeletedSongs();
bool intValidate()
{
if (cin.fail())
{
cout << "Input is incorrect" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return 0;
}
else return 1;
}
bool inputInt(int& x)
{
cin >> x;
return intValidate();
}
Album inputAlbum()
{
Album album;
cout << "Input name of album: ";
cin >> album.name;
cout << "Input release date of album: ";
cin >> album.date;
cout << "Input recording studio of album: ";
cin >> album.recordStudio;
return album;
}
Song inputSong(string& error)
{
Song song;
cout << "Input name of song: ";
cin >> song.name;
cout << "Input duration of song: ";
if (!inputInt(song.duration)) {
error = "Input is incorrect";
return Song();
}
cout << "Input genre of song: ";
cin >> song.genre;
return song;
}
void addAlbum()
{
string error;
Album album = inputAlbum();
album.firstSongAdress = -1;
album.isDeleted = 0;
int key = aFl.addAlbum(album, error);
if (error == "") {
cout << "Album successfully added with key = " << key << endl;
}
else {
cout << error << endl;
}
}
void addSong()
{
string error;
int albumId;
cout << "Input key of album: ";
if (!inputInt(albumId))return;
Song song = inputSong(error);
if (error != "") {
cout << error << endl;
return;
}
song.albumId = albumId;
song.isDeleted = 0;
song.nextAdress = -1;
int songKey = sFl.addSong(albumId, song, error);
if (error == "") {
cout << "Song successfully added with key = " << songKey << endl;
}
else {
cout << error << endl;
}
}
void updateAlbum()
{
string error;
int albumId;
cout << "Input key of album: ";
if (!inputInt(albumId))return;
Album album = inputAlbum();
Album oldAlbum = aFl.getAlbum(albumId, error);
if (error != "") {
cout << error << endl;
return;
}
oldAlbum.name = album.name;
oldAlbum.date = album.date;
oldAlbum.recordStudio = album.recordStudio;
aFl.updateAlbum(albumId, oldAlbum, error);
if (error == "")
{
cout << "Album successfully updated" << endl;
}
else {
cout << error << endl;
}
}
void updateSong()
{
string error;
int albumId, songId;
cout << "Input key of album: ";
if (!inputInt(albumId))return;
cout << "Input key of song: ";
if (!inputInt(songId))return;
Song song = inputSong(error);
if (error != "") {
cout << error << endl;
return;
}
Song oldSong = sFl.getSong(albumId, songId, error);
if (error != "") {
cout << error << endl;
return;
}
oldSong.genre = song.genre;
oldSong.name = song.name;
oldSong.duration = song.duration;
sFl.updateSong(albumId, songId, oldSong, error);
if (error == "") {
cout << "Song successfully updated" << endl;
}
else {
cout << error << endl;
}
}
void deleteAlbum()
{
string error;
int albumId;
cout << "Input key of album: ";
if (!inputInt(albumId))return;
sFl.deleteAlbum(albumId, error);
if (error == "") {
cout << "Album successfully deleted" << endl;
}
else {
cout << error << endl;
}
}
void deleteSong()
{
string error;
int albumId, songId;
cout << "Input key of album: ";
if (!inputInt(albumId))return;
cout << "Input key of song: ";
if (!inputInt(songId))return;
sFl.deleteSong(albumId, songId, error);
if (error == "") {
cout << "Song successfully deleted" << endl;
}
else {
cout << error << endl;
}
}
void getAlbum()
{
string error;
int albumId;
cout << "Input key of album: ";
if (!inputInt(albumId))return;
Album album = aFl.getAlbum(albumId, error);
if (error == "") {
cout << "Name of album: " << album.name << endl;
cout << "Release date of album: " << album.date << endl;
cout << "Recording studio of album: " << album.recordStudio << endl;
}
else {
cout << error << endl;
}
}
void getSong()
{
string error;
int albumId, songId;
cout << "Input key of album: ";
if (!inputInt(albumId))return;
cout << "Input key of song: ";
if (!inputInt(songId))return;
Song song = sFl.getSong(albumId, songId, error);
if (error == "") {
cout << "Name of song: " << song.name << endl;
cout << "Duration of song: " << song.duration << endl;
cout << "Genre of song: " << song.genre << endl;
}
else {
cout << error << endl;
}
}
void writeDatabase()
{
string error;
sFl.writeAlbumAndSongsInfo(error);
if (error != "") {
cout << error << endl;
}
}
int main()
{
iFl.createFile();
aFl.createFile();
sFl.createFile();
dAl.createFile();
dSongs.createFile();
while (true) {
cout << "Choose option:" << endl;
cout << "1. Add album " << endl;
cout << "2. Delete album" << endl;
cout << "3. Modify album" << endl;
cout << "4. Get album" << endl;
cout << "5. Add song " << endl;
cout << "6. Delete song" << endl;
cout << "7. Modify song" << endl;
cout << "8. Get song" << endl;
cout << "9. Get all database" << endl;
int x;
cin >> x;
if (!intValidate())continue;
if (x < 1 || x>9) {
cout << "Option is incorrect" << endl;
}
else {
if (x == 1) {
addAlbum();
}
else if (x == 2) {
deleteAlbum();
}
else if (x == 3) {
updateAlbum();
}
else if (x == 4) {
getAlbum();
}
else if (x == 5) {
addSong();
}
else if (x == 6) {
deleteSong();
}
else if (x == 7) {
updateSong();
}
else if (x == 8) {
getSong();
}
else {
writeDatabase();
}
}
}
}