-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary_password.rb
More file actions
625 lines (583 loc) · 17 KB
/
library_password.rb
File metadata and controls
625 lines (583 loc) · 17 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
require_relative 'auth_kit'
## TODO
## Set password parameters
## Add username attribute to User and authenticate with username instead of f_name.
## When User is done with an activity, they loop back to the options for their role (rather than looping back to the login)
## Logout option that loops back to authentication method
## Only staff Users have ability to close program.
# CLASSES
class Book
attr_accessor :title, :author_last_name, :author_first_name, :genre, :audience, :binding, :location, :status, :holder
@@catalog = []
@@local_id = 100
def initialize(title, author_firstname, author_lastname, genre, audience, book_binding, location)
@local_id = @@local_id += 1
@title = title
@author_first_name = author_firstname
@author_last_name = author_lastname
@genre = genre
@audience = audience
@binding = book_binding
@location = location
@status = "available"
@holder = "The Library"
@@catalog << self
end
def self.catalog
@@catalog
end
def to_s
puts
"
Local ID: #{@local_id}
Title: #{@title}
Author: #{@author_last_name}, #{@author_first_name}
Genre: #{@genre}
Audience: #{@audience}
Cover: #{@binding}
Located: #{@location}
This book is currently #{@status}
In posession of #{@holder}
"
end
end
# NEW CLASS: User
class User
attr_accessor :f_name, :l_name, :password, :role, :local_id, :checkout_list, :checkout_qty
@@local_id = 100
@@user_list = []
@@staff_list = [] # staff users are stored here for sorting and iterating
@@cardholder_list = [] # cardholder users are stored here for sorting and iterating
@@checkout_qty = 0
def initialize(f_name, l_name, password, role)
@f_name = f_name
@l_name = l_name
@password = password
@role = role
@checkout_list = []
@checkout_qty = @checkout_list.length
@@user_list << self
if role.downcase == "staff"
@local_id = "S—#{@@local_id+= 1}"
@@staff_list << self
@@cardholder_list << self
else
@local_id = "M—#{@@local_id+= 1}"
@@cardholder_list << self
end
end
def self.user_list
@@user_list
end
def self.staff_list
@@staff_list
end
def self.cardholder_list
@@cardholder_list
end
def self.checkout_list
@@checkout_list
end
def to_s
puts
"
Name: #{@l_name}, #{f_name}
Role: #{role}
"
end
end
# Initializing seed users
User.new("Joshua", "Langner", "password123", "staff")
User.new("Ava", "Langner", "password123", "cardholder")
User.new("Krista", "Langner", "password123", "staff")
User.new("Reign", "Langner", "password123", "cardholder")
User.new("Anya", "Langner", "password123", "cardholder")
User.new("John", "Doe", "password123", "cardholder")
User.new("Jane", "Doe", "password123", "cardholder")
# Initializing seed books
Book.new("Six of Crows", "Leigh", "Bardugo", "FNTSY", "YA", "Hardcover", "Office: desk shelf")
Book.new("Crooked Kingdom", "Leigh", "Bardugo", "FNTSY", "YA", "Paperback", "Office: desk shelf")
Book.new("Shadow and Bone", "Leigh", "Bardugo", "FNTSY", "YA", "Paperback", "Office: desk shelf")
Book.new("Ruin and Rising", "Leigh", "Bardugo", "FNTSY", "YA", "Paperback", "Office: desk shelf")
Book.new("Siege and Storm", "Leigh", "Bardugo", "FNTSY", "YA", "Hardcover", "Office: desk shelf")
Book.new("Holes", "Louis", "Sachar", "ADV", "MG", "Paperback", "kid bedroom: chapter book basket")
Book.new("Bride Collector, The", "Ted", "Dekker", "THRL", "A", "Hardcover", "Office: desk dhelf")
Book.new("Recursion", "Blake", "Crouch", "SCI", "A", "Paperback", "Office: desk shelf")
Book.new("Monster's New Undies", "Samantha", "Berger", "CHILD", "CH", "Paperback", "Livingroom: cube shelf")
Book.new("Greatest Tales of Horror", "HP", "Lovecraft", "CLAS", "A", "Harcdover", "Office: desk shelf")
Book.new("Hooked", "Les", "Edgerton", "NF", "A", "Paperback", "Office: Stacked under the sharpie cup")
Book.new("The Foot Book", "Dr.", "Seuss", "CHILD", "TB", "Board Book", "Livingroom: cube shelf")
Book.new("Our Town", "Thornton", "Wilder", "SCR", "A", "Paperback", "Office: desk shelf")
## AUTHENTICATION
# Method: Setting a new password
def password_setter
loop do
puts
print "Password: "
password_input = gets.chomp
puts
print "Verify Password: "
password_verify = gets.chomp
if password_input == password_verify
password = password_input
return password
else
puts
puts "Those passwords did not match"
end
end
end
# Method: New users create a User instance for themselves
def sign_up
puts
print "First Name: "
f_name = gets.chomp
puts
print "Last Name: "
l_name = gets.chomp
password = password_setter
puts
puts "Your user account has been created successfully"
role = "cardholder"
User.new(f_name, l_name, password, role)
end
# Method: Verifying password input vs password attribute
def verify name, password
verification_status = "fail"
User.cardholder_list.each {|user|
if user.f_name.downcase == name.downcase && user.password == password
verification_status = "pass"
end
}
verification_status
end
# Method: Accept input for verification
def authenticator
puts
puts "Please log in"
print "First Name :"
name = gets.chomp
puts
print "Password: "
password = gets.chomp
verification_result = verify name, password
if verification_result != "pass"
puts
puts "That name and password does not exist in our system. Try again."
authenticator
else
puts
puts "Logging in . . ."
end
name
end
# Method: return role attribute
def role_checker name
User.cardholder_list.each {|user|
if user.f_name.downcase == name.downcase
return user.role
end
}
end
# BOOK SEARCH METHODS
# Search Method: Browse All
def browse_all
puts
puts Book.catalog.sort_by {|book| book.title}
end
# Search Method: Search by Title
def search_by_title title
puts
puts "Books matching the title: #{title}"
20.times{print " -"}
puts
Book.catalog.sort_by {|book|
if book.title.downcase == title.downcase
puts "
Title: #{book.title}
Author: #{book.author_last_name}, #{book.author_first_name}"
end
}
puts
puts "End of results"
end
# Search Method: Search by Author
def search_by_author l_name, *f_name
puts
puts "Books matching the author: #{f_name} #{l_name}"
20.times{print " -"}
puts
Book.catalog.sort_by {|book|
if book.author_last_name.downcase == l_name.downcase
puts book.title
end
}
puts
puts "End of results"
end
# Search Method: Search by Genre
def search_by_genre genre
puts
puts "Books matching the genre: #{genre}"
20.times{print " -"}
puts
Book.catalog.sort_by {|book|
if book.genre.downcase == genre.downcase
puts book.title
end
}
puts
puts "End of results"
end
# Search Method: Search by Audience Age
def search_by_audience_age audience
puts
puts "Books matching the audience age: #{audience}"
20.times{print " -"}
puts
Book.catalog.sort_by {|book|
if book.audience.downcase == audience.downcase
puts book.title
end
}
puts
puts "End of results"
end
# Search Method: Search by Genre && Audience Age
def search_by_genre_audience_age genre, audience
puts
puts "Books matching: #{audience} & #{genre}"
20.times{print " -"}
puts
Book.catalog.sort_by {|book|
if book.audience.downcase == audience.downcase && book.genre.downcase == genre.downcase
puts book.title
end
}
puts
puts "End of results"
end
## STAFF ONLY METHODS
# Method: New book instance
def add_book title, author_last_name, author_first_name, genre, audience, book_binding, location
Book.new(title, author_last_name, author_first_name, genre, audience, book_binding, location)
end
# Method: delete book instance
def delete_book title
Book.catalog.sort_by {|book|
if book.title.downcase == title.downcase
Book.catalog.delete(book)
puts
puts "The book has been deletd from the catalog"
end
}
end
# Method: show all card holders
def all_cardholders
User.cardholder_list.sort_by{|user| user.f_name
puts
puts "
Member ID: #{user.local_id}
Name: : #{user.l_name}, #{user.f_name}
Checkout Qty: #{user.checkout_qty}
Titles Checked Out: #{user.checkout_list}
"
}
end
# Method: Show all staff
def all_staff
User.staff_list.sort_by{|user| user.f_name
puts
puts "
Staff ID: #{user.local_id}
Name: : #{user.l_name}, #{user.f_name}"
}
end
# Method: Add new user
# !! This one is not in the control flow yet !!
def staff_add_user
puts
print "First Name: "
f_name = gets.chomp
l_name = gets.chomp
role = gets.chomp
password = gets.chomp
User.new(f_name, l_name, password, role)
end
## CHECK-IN and CHECK-OUT BOOKS
# Method: Check Book Availability
def status_query title
Book.catalog.each { |book|
if book.title.downcase == title.downcase
if book.status.downcase == "available"
return "available"
else
return "checked out"
end
end
}
end
# Method: Checkout (user updated)
#Add book to User checkout_list & Increment checkout_qty
def update_user_data_out title, input_name
User.cardholder_list.each {|user|
if user.f_name.downcase == input_name.downcase
user.checkout_list.append(title.upcase)
user.checkout_qty += 1
end
}
end
# Method: Checkout (book updated)
# status = "checked out" & holder = user f_name
def process_checkout title, name
Book.catalog.each { |book|
if book.title.downcase == title.downcase
book.status = "checked out"
book.holder = name
end
}
update_user_data_out title, name
end
# Method: Checkin (user updated)
# remove book from user checkout_list & Decrement checkout qty
def update_user_data_in user_f_name, title
User.cardholder_list.each {|user|
if user.f_name.downcase == user_f_name.downcase
user.checkout_list.delete(title.upcase)
user.checkout_qty -= 1
end
}
end
# Method: Checkin (book updated)
# status = "available" & holder = "The Library"
def process_checkin title
from_user = ""
Book.catalog.each { |book|
if book.title.downcase == title.downcase
from_user = book.holder
book.status = "available"
book.holder = "The Library"
end
}
update_user_data_in from_user, title
end
# Control flow accessing Search Methods
def search_catalog name, role
search_methods = ["A = Browse All", "B = By Title", "C = By Author", "D = By Genre", "E = By Audience Age", "F = By Age & Genre"]
genres = ["FNTSY = Fantasy", "SCI = Sci-Fi", "THRL = Thriller", "ADV = Adventure", "CHILD = Children's", "NF = Non-Fiction", "SCR = Script", "CLAS = Classic",]
age = ["A = Adult", "YA = Young Adult", "MG = Middle Grade", "CH = Children", "TB = Toddlers & Babies"]
loop do
puts
puts "Please choose one of the following search methods:"
puts search_methods
response = gets.chomp.downcase
if response == "a"
browse_all
elsif response == "b"
puts
puts "Searching by TITLE. . ."
puts "Which title do you want to search?"
title = gets.chomp.downcase
search_by_title title
elsif response == "c"
puts
puts "Searching AUTHOR. . ."
puts "What is the last name of the author you want to search?"
l_name = gets.chomp.downcase
puts
puts "What is the first name of the author you want to search?"
f_name = gets.chomp.downcase
search_by_author l_name, f_name
elsif response == "d"
puts
puts "Searching GENRE. . ."
puts "Which genre would you like to search?"
puts genres
genre_query = gets.chomp.downcase
search_by_genre genre_query
elsif response == "e"
puts
puts "Searching AUDIENCE AGE. . ."
puts "Which age group do you want to searh?"
puts age
age_query = gets.chomp.downcase
search_by_audience_age age_query
elsif response == "f"
puts
puts "Searching AGE & GENRE. . ."
puts "Which age group do you want to searh?"
puts age
age_query = gets.chomp.downcase
puts "Which genre would you like to search?"
puts genres
genre_query = gets.chomp.downcase
search_by_genre_audience_age genre_query, age_query
else
puts
puts "That is not a search method. Try again."
end
puts
puts "Do you want to keep searching? (Y/N)"
continue_response = gets.chomp.downcase
if continue_response != "y"
puts
puts "Exiting search..."
primary_flow
end
end
end
# Control flow accessing the checkout methods
def checkout_flow name
loop do
puts
puts "Which book do you want to check out?"
query = gets.chomp.capitalize
availability = status_query query
puts
puts "#{query} is currently #{availability}"
if availability == "available"
puts "Do you want to check this book out? (Y/N)"
response = gets.chomp.downcase
if response == "y"
new_checkout = process_checkout query, name
puts
puts "Thank you for checking out #{query}"
puts
puts "Would you like to check out another book? (Y/N)"
response = gets.chomp.downcase
if response != "y"
puts "Happy Reading! Hope to see you soon!"
primary_flow
end
else
puts
puts "Okay, we will put that back on the shelf"
puts
puts "Do you want to check out something else? (Y/N)"
response = gets.chomp.downcase
if response != "y"
puts "Thank you. Come again."
primary_flow
end
end
else
puts
puts "Do you want to search for a different book? (Y/N)"
response = gets.chomp.downcase
if response != "y"
primary_flow
end
end
end
end
# Control flow leading to all other methods and flows (Call this method to start the program)
def primary_flow
staff_activity = ["A = Search Library", "B = Check in a Book", "C = Add a Book", "D = Delete a Book", "E = Search Card Holders", "F = Search Staff", "G = Check Out Book", "X = Exit"]
puts
puts "Welcome to the Langner Library."
puts
puts "Are you a returning user? (Y/N)"
response = gets.chomp.downcase
if response != "y"
sign_up
end
name = authenticator
role = role_checker name
loop do
if role == "cardholder"
puts
puts "Welcome #{name.capitalize}!"
puts
puts "Would you like to browse the titles or check something out now?"
puts "Enter B to browse or C to checkout."
flow = gets.chomp.downcase
if flow == "b"
search_catalog name, role
elsif flow == "c"
checkout_flow name
else
"That was not an option. Let's try that again"
end
elsif role == "staff"
puts
puts "Welcome #{name}!"
puts "What would you like to do?"
puts staff_activity
activity_response = gets.chomp.downcase
if activity_response == "a"
puts
puts "Entering Library Search. . ."
search_catalog name, role
elsif activity_response == "b"
puts
puts "Checking in a book. . ."
puts "Which book are you checking in?"
title = gets.chomp.downcase
process_checkin title
elsif activity_response == "c"
puts
puts "Adding a book . . ."
print "Title: "
title = gets.chomp
puts
print "Author L Name: "
author_last_name = gets.chomp
puts
print "Author F Name: "
author_first_name = gets.chomp
puts
print "Genre: "
genre = gets.chomp
puts
print "Age Group: "
audience = gets.chomp
puts
print "Binding: "
book_binding = gets.chomp
puts
print "Location: "
location = gets.chomp
add_book title, author_last_name, author_first_name, genre, audience, book_binding, location
elsif activity_response == "d"
puts
puts "Deleting a book. . ."
puts "Which book would you like to delete?"
print "Title: "
title = gets.chomp.downcase
puts
puts "THIS IS IRREVERSIBLE! DO YOU WANT TO DELETE #{title.upcase}?"
puts "Enter #86! to delete. Enter anything else to cancel."
delete_response = gets.chomp.downcase
if delete_response == "#86!"
delete_book title
end
elsif activity_response == "e"
puts
puts "Searching Cardholders. . ."
all_cardholders
elsif activity_response == "f"
puts
puts "Searching Staff. . ."
all_staff
elsif activity_response == "g"
puts
puts "Switching to Cardholder profile. . ."
checkout_flow
elsif activity_response == "x"
puts
puts "Okay, bye."
primary_flow
else
puts "That is not an option. Try again."
end
puts "Would you like to do something else? (Y/N)"
response = gets.chomp.downcase
if response != "y"
puts
puts "Thank you. See you soon."
primary_flow
end
end
end
end
primary_flow