bookstore-1 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/bookstore-1.rb +194 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2bdb41361682395709a07233b886dbea963eba4e
|
4
|
+
data.tar.gz: 6ef10a009a190768ef34b993cb179bf1c06d12f2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bbf6fee91699fe7a8b4827b982fa74215083f7353269c4a50e8585a814590869ec6ad9130f22443ca360fcc4a3a21049aa0d9512e4b8ac5f8a82c5e272d0032c
|
7
|
+
data.tar.gz: a78420617fed135b972fc28c5a60b84abf06b7f49e63c909a9407965054001c8716f892f521c53f909e6aefb253f167648e1dce6d392912fe02e77fc06f2152b
|
data/lib/bookstore-1.rb
ADDED
@@ -0,0 +1,194 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
# Globals
|
4
|
+
|
5
|
+
$BOOKS_DB_NAME = "books.txt"
|
6
|
+
|
7
|
+
# Global hash for books with existing entries
|
8
|
+
|
9
|
+
$books = {}
|
10
|
+
$books["The Metamorphosis"] = 8
|
11
|
+
$books["Three Cups of Tea"] = 7
|
12
|
+
$books["The Invisible Man"] = 7
|
13
|
+
|
14
|
+
# Global string literals for menu choices
|
15
|
+
|
16
|
+
$ADD = "add"
|
17
|
+
$DELETE = "delete"
|
18
|
+
$SEARCH = "search"
|
19
|
+
$DISPLAY = "display"
|
20
|
+
$SAVE = "save"
|
21
|
+
$QUIT = "quit"
|
22
|
+
|
23
|
+
#############################################
|
24
|
+
|
25
|
+
def showMenuAndPrompt
|
26
|
+
puts "=================================="
|
27
|
+
puts " Bookstore Inventory System "
|
28
|
+
puts "=================================="
|
29
|
+
puts " (#{$ADD}) : Add a book"
|
30
|
+
puts " (#{$DELETE}) : Delete a book"
|
31
|
+
puts " (#{$SEARCH}) : Search a book"
|
32
|
+
puts "(#{$DISPLAY}) : Display all books"
|
33
|
+
puts " (#{$SAVE}) : Save books information"
|
34
|
+
puts " (#{$QUIT}) : Quit the application"
|
35
|
+
puts "=================================="
|
36
|
+
print "Enter your choice: "
|
37
|
+
|
38
|
+
choice = gets.chomp
|
39
|
+
choice
|
40
|
+
end
|
41
|
+
|
42
|
+
def start
|
43
|
+
puts "Starting..."
|
44
|
+
|
45
|
+
loadBooks
|
46
|
+
|
47
|
+
loop do
|
48
|
+
choice = showMenuAndPrompt
|
49
|
+
puts
|
50
|
+
|
51
|
+
case choice
|
52
|
+
when $ADD then addBook
|
53
|
+
when $DELETE then deleteBook
|
54
|
+
when $SEARCH then searchBooks
|
55
|
+
when $DISPLAY then displayBooks
|
56
|
+
when $SAVE then saveBooks
|
57
|
+
when $QUIT then quit
|
58
|
+
else invalidChoice(choice)
|
59
|
+
end
|
60
|
+
|
61
|
+
break if choice == $QUIT
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Inventory manipulation methods
|
66
|
+
|
67
|
+
def addBook
|
68
|
+
puts ">> Adding a new book..."
|
69
|
+
|
70
|
+
print "Enter book name : "
|
71
|
+
name = gets.chomp
|
72
|
+
|
73
|
+
print "Enter book rating: "
|
74
|
+
rating = gets.chomp
|
75
|
+
|
76
|
+
if $books.include?(name)
|
77
|
+
puts "Already exsits: { '#{name}', #{rating} }"
|
78
|
+
else
|
79
|
+
$books[ name.to_s ] = rating.to_i
|
80
|
+
puts "Added: { '#{name}', #{rating} }"
|
81
|
+
end
|
82
|
+
|
83
|
+
puts
|
84
|
+
end
|
85
|
+
|
86
|
+
def deleteBook
|
87
|
+
puts ">> Deleting an existing book..."
|
88
|
+
|
89
|
+
if $books.empty?
|
90
|
+
puts ">> Inventory is empty! Nothing to search!"
|
91
|
+
else
|
92
|
+
print "Enter book name: "
|
93
|
+
name = gets.chomp
|
94
|
+
|
95
|
+
if !$books.include?(name)
|
96
|
+
puts "'#{name}' does not exist!"
|
97
|
+
else
|
98
|
+
$books.delete(name)
|
99
|
+
puts "Deleted: { '#{name}' }"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
puts
|
104
|
+
end
|
105
|
+
|
106
|
+
def searchBooks
|
107
|
+
puts ">> Searching existing books..."
|
108
|
+
|
109
|
+
if $books.empty?
|
110
|
+
puts ">> Inventory is empty! Nothing to search!"
|
111
|
+
else
|
112
|
+
print "Enter substring to search: "
|
113
|
+
substring = gets.chomp
|
114
|
+
|
115
|
+
$books.each { |name, rating|
|
116
|
+
if name.to_s.include? substring
|
117
|
+
puts "Found: { '#{name}', #{rating} }"
|
118
|
+
end
|
119
|
+
}
|
120
|
+
|
121
|
+
puts
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def displayBooks
|
126
|
+
puts ">> Displaying all the books..."
|
127
|
+
|
128
|
+
if $books.empty?
|
129
|
+
puts ">> Inventory is empty! Nothing to display!"
|
130
|
+
else
|
131
|
+
puts "TOTAL BOOKS: #{$books.length}"
|
132
|
+
$books.each { |name, rating| puts "{ '#{name}': #{rating} }" }
|
133
|
+
puts
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def loadBooks
|
138
|
+
puts ">> Loading existing books from #{$BOOKS_DB_NAME}..."
|
139
|
+
|
140
|
+
if !File.exist?($BOOKS_DB_NAME)
|
141
|
+
puts "Books database `#{$BOOKS_DB_NAME}` not found! Nothing to load!"
|
142
|
+
else
|
143
|
+
puts "Books database '#{$BOOKS_DB_NAME}' found! Loading..."
|
144
|
+
|
145
|
+
$books.clear
|
146
|
+
|
147
|
+
file = File.open($BOOKS_DB_NAME, "r")
|
148
|
+
file.each { |line|
|
149
|
+
book_entry = line.split(",")
|
150
|
+
|
151
|
+
name = book_entry[0]
|
152
|
+
rating = book_entry[1].to_i
|
153
|
+
|
154
|
+
puts "Loaded: { '#{name}', #{rating} }"
|
155
|
+
|
156
|
+
$books[ name ] = rating
|
157
|
+
}
|
158
|
+
file.close
|
159
|
+
end
|
160
|
+
|
161
|
+
puts
|
162
|
+
end
|
163
|
+
|
164
|
+
def saveBooks
|
165
|
+
puts ">> Saving current inventory..."
|
166
|
+
|
167
|
+
if File.exist?($BOOKS_DB_NAME)
|
168
|
+
File.truncate($BOOKS_DB_NAME, 0)
|
169
|
+
end
|
170
|
+
|
171
|
+
file = File.open($BOOKS_DB_NAME, "w")
|
172
|
+
|
173
|
+
$books.each { |name, rating|
|
174
|
+
puts "Saved: { '#{name}', #{rating} }"
|
175
|
+
|
176
|
+
book_entry = name + "," + rating.to_s
|
177
|
+
file.write( book_entry + "\n" )
|
178
|
+
}
|
179
|
+
file.close
|
180
|
+
|
181
|
+
puts
|
182
|
+
end
|
183
|
+
|
184
|
+
def quit
|
185
|
+
puts ">> Quitting... Bye... :)"
|
186
|
+
end
|
187
|
+
|
188
|
+
def invalidChoice(choice)
|
189
|
+
puts ">> ERORR! Invalid choice! [#{choice}]"
|
190
|
+
end
|
191
|
+
|
192
|
+
# Starting program here
|
193
|
+
|
194
|
+
start
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bookstore-1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Azeem sajid
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2010-04-28 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple file-based bookstore inventory system
|
14
|
+
email: azeem.sajid@confiz.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/bookstore-1.rb
|
20
|
+
homepage: http://rubygems.org/gems/bookstore-1
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.5.1
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Bookstore Inventory System
|
44
|
+
test_files: []
|