1
1
import models.BookModel
2
2
import models.ReaderModel
3
- import okhttp3.OkHttpClient
4
- import okhttp3.Request
3
+ import okhttp3.*
5
4
import requests.BookRequest
6
5
import requests.ReaderRequest
7
- import java.io.IOException
8
6
9
- val client = OkHttpClient ();
7
+ val client = OkHttpClient ()
10
8
11
- fun main (args : Array < String > ) {
9
+ fun main () {
12
10
13
- newBook()
11
+ if (checkServer())
12
+ println (" Connected to server" )
13
+ else {
14
+ println (" Server is not running" )
15
+ return
16
+ }
14
17
15
- newReader()
18
+ while (true ) {
19
+ println (" 1. Add book" )
20
+ println (" 2. Add reader" )
21
+ println (" 3. Exit" )
22
+ println ()
23
+ print (" Enter your choice: " )
16
24
25
+ when (readln().toIntOrNull()) {
26
+ 1 -> {
27
+ val book = newBook()
28
+
29
+ val request = BookRequest ()
30
+ request.post(book, object : Callback {
31
+ override fun onFailure (call : Call , e : java.io.IOException ) {
32
+ println (" Failed to execute request" )
33
+ }
34
+
35
+ override fun onResponse (call : Call , response : Response ) {
36
+ if (response.code == 200 )
37
+ println (" Book added successfully" )
38
+ else
39
+ println (" Failed to add book (Status code: ${response.code} )" )
40
+ }
41
+ })
42
+ }
43
+ 2 -> {
44
+ val reader = newReader()
45
+
46
+ val request = ReaderRequest ()
47
+ request.post(reader, object : Callback {
48
+ override fun onFailure (call : Call , e : java.io.IOException ) {
49
+ println (" Failed to execute request" )
50
+ }
51
+
52
+ override fun onResponse (call : Call , response : Response ) {
53
+ println (" Reader added successfully" )
54
+ }
55
+ })
56
+ }
57
+ 3 -> {
58
+ println (" Exiting..." )
59
+ break
60
+ }
61
+ else -> {
62
+ println (" Invalid choice" )
63
+ }
64
+ }
65
+ }
17
66
}
18
67
19
- fun newBook () : BookModel {
20
- val title: String
21
- var author: String
22
- var year: Int
23
- var pages: Int
24
- var language: String
25
- var publisher: String
26
- var isbn: String
27
- var price: Double
28
- var format: String
29
- var genre: String
30
- var description: String
31
- var cover: String
32
- var url: String
33
- var id: Int
68
+ fun checkServer (): Boolean {
69
+ val request = Request .Builder ()
70
+ .url(Environment .url)
71
+ .build()
34
72
35
- println (" Enter the title of the book" )
36
- title = readLine()!!
37
- println (" Enter the author of the book" )
38
- author = readLine()!!
39
- println (" Enter the year of the book" )
40
- year = readLine()!! .toInt()
41
- println (" Enter the number of pages of the book" )
42
- pages = readLine()!! .toInt()
43
- println (" Enter the language of the book" )
44
- language = readLine()!!
45
- println (" Enter the publisher of the book" )
46
- publisher = readLine()!!
47
- println (" Enter the ISBN of the book" )
48
- isbn = readLine()!!
49
- println (" Enter the price of the book" )
50
- price = readLine()!! .toDouble()
51
- println (" Enter the format of the book" )
52
- format = readLine()!!
53
- println (" Enter the genre of the book" )
54
- genre = readLine()!!
55
- println (" Enter the description of the book" )
56
- description = readLine()!!
57
- println (" Enter the cover of the book" )
58
- cover = readLine()!!
59
- println (" Enter the url of the book" )
60
- url = readLine()!!
61
- println (" Enter the id of the book" )
62
- id = readLine()!! .toInt()
73
+ return try {
74
+ client.newCall(request).execute()
75
+ true
76
+ } catch (e: Exception ) {
77
+ false
78
+ }
79
+ }
80
+
81
+ fun newBook (): BookModel {
82
+ println (" Enter the title of the book:" )
83
+ val title: String = readln()
84
+ println (" Enter the author of the book:" )
85
+ val author: String = readln()
86
+ println (" Enter the language of the book:" )
87
+ val language: String = readln()
88
+ println (" Enter the genre of the book:" )
89
+ val genre: String = readln()
63
90
64
- val book = BookModel (title, author, year, pages, language, publisher, isbn, price, format, genre, description, cover, url, id)
65
- return book
91
+ return BookModel (title, author, language, genre)
66
92
}
67
93
68
94
fun newReader (): ReaderModel {
69
- var id: Int
70
- var name: String
71
- var email: String
72
- var password: String
73
- var role: String
74
- var token: String
75
- var books: List <BookModel >
76
- var booksBorrowed: List <BookModel >
77
- var booksReserved: List <BookModel >
78
- var booksReturned: List <BookModel >
79
- var booksLost: List <BookModel >
80
- var booksDamaged: List <BookModel >
81
- var booksBorrowedHistory: List <BookModel >
82
- var booksReservedHistory: List <BookModel >
83
- var booksReturnedHistory: List <BookModel >
84
- var booksLostHistory: List <BookModel >
85
- var booksDamagedHistory: List <BookModel >
86
- var booksBorrowedHistoryCount: Int
87
- var booksReservedHistoryCount: Int
88
- var booksReturnedHistoryCount: Int
89
- var booksLostHistoryCount: Int
90
- var booksDamagedHistoryCount: Int
91
- var booksBorrowedCount: Int
92
- var booksReservedCount: Int
93
- var booksReturnedCount: Int
94
- var booksLostCount: Int
95
- var booksDamagedCount: Int
96
- var booksCount: Int
97
- var booksBorrowedHistoryPage: Int
98
- var booksReservedHistoryPage: Int
99
- var booksReturnedHistoryPage: Int
100
- var booksLostHistoryPage: Int
101
- var booksDamagedHistoryPage: Int
102
- var booksBorrowedPage: Int
103
- var booksReservedPage: Int
104
- var booksReturnedPage: Int
105
- var booksLostPage: Int
106
- var booksDamagedPage: Int
107
- var booksPage: Int
108
- var booksBorrowedHistoryPageCount: Int
109
- var booksReservedHistoryPageCount: Int
110
- var booksReturnedHistoryPageCount: Int
111
- var booksLostHistoryPageCount: Int
112
-
113
- println (" Enter the id of the reader" )
114
- id = readLine()!! .toInt()
115
- println (" Enter the name of the reader" )
116
- name = readLine()!!
117
- println (" Enter the email of the reader" )
118
- email = readLine()!!
119
- println (" Enter the password of the reader" )
120
- password = readLine()!!
121
- println (" Enter the role of the reader" )
122
- role = readLine()!!
123
- println (" Enter the token of the reader" )
124
- token = readLine()!!
125
- println (" Enter the books of the reader" )
126
- books = readLine()!! .split(" ," ).map { it.trim() }.map { BookModel (it, " " , 0 , 0 , " " , " " , " " , 0.0 , " " , " " , " " , " " , " " , 0 ) }
127
- println (" Enter the booksBorrowed of the reader" )
128
- booksBorrowed = readLine()!! .split(" ," ).map { it.trim() }.map { BookModel (it, " " , 0 , 0 , " " , " " , " " , 0.0 , " " , " " , " " , " " , " " , 0 ) }
129
- println (" Enter the booksReserved of the reader" )
130
- booksReserved = readLine()!! .split(" ," ).map { it.trim() }.map { BookModel (it, " " , 0 , 0 , " " , " " , " " , 0.0 , " " , " " , " " , " " , " " , 0 ) }
131
- println (" Enter the booksReturned of the reader" )
132
- booksReturned = readLine()!! .split(" ," ).map { it.trim() }.map { BookModel (it, " " , 0 , 0 , " " , " " , " " , 0.0 , " " , " " , " " , " " , " " , 0 ) }
133
- println (" Enter the booksLost of the reader" )
134
- booksLost = readLine()!! .split(" ," ).map { it.trim() }.map { BookModel (it, " " , 0 , 0 , " " , " " , " " , 0.0 , " " , " " , " " , " " , " " , 0 ) }
135
- println (" Enter the booksDamaged of the reader" )
136
- booksDamaged = readLine()!! .split(" ," ).map { it.trim() }.map { BookModel (it, " " , 0 , 0 , " " , " " , " " , 0.0 , " " , " " , " " , " " , " " , 0 ) }
137
- println (" Enter the booksBorrowedHistory of the reader" )
138
- booksBorrowedHistory = readLine()!! .split(" ," ).map { it.trim() }.map { BookModel (it, " " , 0 , 0 , " " , " " , " " , 0.0 , " " , " " , " " , " " , " " , 0 ) }
139
- println (" Enter the booksReservedHistory of the reader" )
140
- booksReservedHistory = readLine()!! .split(" ," ).map { it.trim() }.map { BookModel (it, " " , 0 , 0 , " " , " " , " " , 0.0 , " " , " " , " " , " " , " " , 0 ) }
141
- println (" Enter the booksReturnedHistory of the reader" )
142
- booksReturnedHistory = readLine()!! .split(" ," ).map { it.trim() }.map { BookModel (it, " " , 0 , 0 , " " , " " , " " , 0.0 , " " , " " , " " , " " , " " , 0 ) }
143
- println (" Enter the booksLostHistory of the reader" )
144
- booksLostHistory = readLine()!! .split(" ," ).map { it.trim() }.map { BookModel (it, " " , 0 , 0 , " " , " " , " " , 0.0 , " " , " " , " " , " " , " " , 0 ) }
145
- println (" Enter the booksDamagedHistory of the reader" )
146
- booksDamagedHistory = readLine()!! .split(" ," ).map { it.trim() }.map { BookModel (it, " " , 0 , 0 , " " , " " , " " , 0.0 , " " , " " , " " , " " , " " , 0 ) }
147
- println (" Enter the booksBorrowedHistoryCount of the reader" )
148
- booksBorrowedHistoryCount = readLine()!! .toInt()
149
- println (" Enter the booksReservedHistoryCount of the reader" )
150
- booksReservedHistoryCount = readLine()!! .toInt()
151
- println (" Enter the booksReturnedHistoryCount of the reader" )
152
- booksReturnedHistoryCount = readLine()!! .toInt()
153
- println (" Enter the booksLostHistoryCount of the reader" )
154
- booksLostHistoryCount = readLine()!! .toInt()
155
- println (" Enter the booksDamagedHistoryCount of the reader" )
156
- booksDamagedHistoryCount = readLine()!! .toInt()
157
- println (" Enter the booksBorrowedCount of the reader" )
158
- booksBorrowedCount = readLine()!! .toInt()
159
- println (" Enter the booksReservedCount of the reader" )
160
- booksReservedCount = readLine()!! .toInt()
161
- println (" Enter the booksReturnedCount of the reader" )
162
- booksReturnedCount = readLine()!! .toInt()
163
- println (" Enter the booksLostCount of the reader" )
164
- booksLostCount = readLine()!! .toInt()
165
- println (" Enter the booksDamagedCount of the reader" )
166
- booksDamagedCount = readLine()!! .toInt()
167
- println (" Enter the booksCount of the reader" )
168
- booksCount = readLine()!! .toInt()
169
- println (" Enter the booksBorrowedHistoryPage of the reader" )
170
- booksBorrowedHistoryPage = readLine()!! .toInt()
171
- println (" Enter the booksReservedHistoryPage of the reader" )
172
- booksReservedHistoryPage = readLine()!! .toInt()
173
- println (" Enter the booksReturnedHistoryPage of the reader" )
174
- booksReturnedHistoryPage = readLine()!! .toInt()
175
- println (" Enter the booksLostHistoryPage of the reader" )
176
- booksLostHistoryPage = readLine()!! .toInt()
177
- println (" Enter the booksDamagedHistoryPage of the reader" )
178
- booksDamagedHistoryPage = readLine()!! .toInt()
179
- println (" Enter the booksBorrowedPage of the reader" )
180
- booksBorrowedPage = readLine()!! .toInt()
181
- println (" Enter the booksReservedPage of the reader" )
182
- booksReservedPage = readLine()!! .toInt()
183
- println (" Enter the booksReturnedPage of the reader" )
184
- booksReturnedPage = readLine()!! .toInt()
185
- println (" Enter the booksLostPage of the reader" )
186
- booksLostPage = readLine()!! .toInt()
187
- println (" Enter the booksDamagedPage of the reader" )
188
- booksDamagedPage = readLine()!! .toInt()
189
- println (" Enter the booksPage of the reader" )
190
- booksPage = readLine()!! .toInt()
191
- println (" Enter the booksBorrowedHistoryPageCount of the reader" )
192
- booksBorrowedHistoryPageCount = readLine()!! .toInt()
193
- println (" Enter the booksReservedHistoryPageCount of the reader" )
194
- booksReservedHistoryPageCount = readLine()!! .toInt()
195
- println (" Enter the booksReturnedHistoryPageCount of the reader" )
196
- booksReturnedHistoryPageCount = readLine()!! .toInt()
197
- println (" Enter the booksLostHistoryPageCount of the reader" )
198
- booksLostHistoryPageCount = readLine()!! .toInt()
95
+ println (" Enter the name of the reader:" )
96
+ val name: String = readln()
199
97
200
- val reader = ReaderModel (
201
- id,
202
- name,
203
- email,
204
- password,
205
- role,
206
- token,
207
- books,
208
- booksBorrowed,
209
- booksReserved,
210
- booksReturned,
211
- booksLost,
212
- booksDamaged,
213
- booksBorrowedHistory,
214
- booksReservedHistory,
215
- booksReturnedHistory,
216
- booksLostHistory,
217
- booksDamagedHistory,
218
- booksBorrowedHistoryCount,
219
- booksReservedHistoryCount,
220
- booksReturnedHistoryCount,
221
- booksLostHistoryCount,
222
- booksDamagedHistoryCount,
223
- booksBorrowedCount,
224
- booksReservedCount,
225
- booksReturnedCount,
226
- booksLostCount,
227
- booksDamagedCount,
228
- booksCount,
229
- booksBorrowedHistoryPage,
230
- booksReservedHistoryPage,
231
- booksReturnedHistoryPage,
232
- booksLostHistoryPage,
233
- booksDamagedHistoryPage,
234
- booksBorrowedPage,
235
- booksReservedPage,
236
- booksReturnedPage,
237
- booksLostPage,
238
- booksDamagedPage,
239
- booksPage,
240
- booksBorrowedHistoryPageCount,
241
- booksReservedHistoryPageCount,
242
- booksReturnedHistoryPageCount,
243
- booksLostHistoryPageCount
244
- )
245
- return reader
98
+ return ReaderModel (name)
246
99
}
0 commit comments