-
Notifications
You must be signed in to change notification settings - Fork 62
/
library.rb
152 lines (128 loc) · 3.11 KB
/
library.rb
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
#The initialize method requests title, author, and desciption of the book
#then stores that info
#
#
class Book
def initialize
puts "What is the title of the book?"
@title = gets.chomp
#@title_sym = @title.downcase.tr(" ", "_").to_sym
puts "Who is the author of the book?"
@author = gets.chomp
#@author_sym = @author.downcase.tr(" ", "_").to_sym
puts "Please enter a short description of the book:"
@description = gets.chomp
@status = :available
end
#This method returns the saved information on a book.
#It does not take an argument.
#
#
def info
puts "__________________________"
puts " "
puts "Title: #{@title}"
puts "Author: #{@author}"
puts "Description: #{@description}"
puts "Status: #{@status}"
puts "__________________________"
end
#return the value of the book instance variable
def printbook
@title
end
#return the value of the author instance variable
def printauthor
@author
end
#return the value of the status instance variable
def printstatus
@status
end
#Checkout method
#
#
#
def check_out
@status = :checked_out
end
def return
@status = :available
end
def lost
@status = :lost
end
end
class Library
def initialize
#@books {title, author}
@books = Hash.new
@book_stats = Hash.new
#@checked_out {title, status}
@checked_out = Hash.new
end
# this method will maintain a list of books in the library
# after the add command is called and a new book is created
# get data and add to library
def add(title, author)
@books[title] = author
end
def add_status(title, status)
@book_stats[title] = status
puts "#{@book_stats}"
end
def checked_out(title, status)
@checked_out[title] = status
end
def availability(title)
@book_stats.each do |key, value|
if @book_stats.key?(key) && value == :available
puts "This book is available."
else
puts "Sorry this book is #{value}."
end
end
end
def books
puts "__________________________"
puts "The MakerSquare Library:"
@books.each do |key, value|
puts "Title: #{key}, Author: #{value}"
end
puts "__________________________"
end
end
#create the first makersquare library
makersquare = Library.new()
command = "active"
until command == "q"
puts "Welcome to the Makersquare Library."
puts "What would you like to do?"
puts "Commands:"
puts "add -Add books to the library"
puts "list - list all books in the library"
puts "co - check out a book"
puts "q -quit"
command = gets.chomp.downcase
case command
when "add"
currentbook = Book.new()
currentbook.info
title = currentbook.printbook
author = currentbook.printauthor
status = currentbook.printstatus
makersquare.add(title, author)
makersquare.add_status(title, status)
makersquare.books
when "co"
puts "What book would you like to check out?"
title = gets.chomp.downcase
makersquare.availability(title)
when "list"
makersquare.books
when "q"
break
else
puts "Response not understood."
end
end