Top Level Namespace
Instance Method Summary collapse
-
#addBook ⇒ Object
Inventory manipulation methods.
- #deleteBook ⇒ Object
- #displayBooks ⇒ Object
- #invalidChoice(choice) ⇒ Object
- #loadBooks ⇒ Object
- #quit ⇒ Object
- #saveBooks ⇒ Object
- #searchBooks ⇒ Object
- #showMenuAndPrompt ⇒ Object
- #start ⇒ Object
Instance Method Details
#addBook ⇒ Object
Inventory manipulation methods
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/bookstoretest.rb', line 67 def addBook puts ">> Adding a new book..." print "Enter book name : " name = gets.chomp print "Enter book rating: " = gets.chomp if $books.include?(name) puts "Already exsits: { '#{name}', #{rating} }" else $books[ name.to_s ] = .to_i puts "Added: { '#{name}', #{rating} }" end puts end |
#deleteBook ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/bookstoretest.rb', line 86 def deleteBook puts ">> Deleting an existing book..." if $books.empty? puts ">> Inventory is empty! Nothing to search!" else print "Enter book name: " name = gets.chomp if !$books.include?(name) puts "'#{name}' does not exist!" else $books.delete(name) puts "Deleted: { '#{name}' }" end end puts end |
#displayBooks ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/bookstoretest.rb', line 125 def displayBooks puts ">> Displaying all the books..." if $books.empty? puts ">> Inventory is empty! Nothing to display!" else puts "TOTAL BOOKS: #{$books.length}" $books.each { |name, | puts "{ '#{name}': #{rating} }" } puts end end |
#invalidChoice(choice) ⇒ Object
188 189 190 |
# File 'lib/bookstoretest.rb', line 188 def invalidChoice(choice) puts ">> ERORR! Invalid choice! [#{choice}]" end |
#loadBooks ⇒ Object
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 |
# File 'lib/bookstoretest.rb', line 137 def loadBooks puts ">> Loading existing books from #{$BOOKS_DB_NAME}..." if !File.exist?($BOOKS_DB_NAME) puts "Books database `#{$BOOKS_DB_NAME}` not found! Nothing to load!" else puts "Books database '#{$BOOKS_DB_NAME}' found! Loading..." $books.clear file = File.open($BOOKS_DB_NAME, "r") file.each { |line| book_entry = line.split(",") name = book_entry[0] = book_entry[1].to_i puts "Loaded: { '#{name}', #{rating} }" $books[ name ] = } file.close end puts end |
#quit ⇒ Object
184 185 186 |
# File 'lib/bookstoretest.rb', line 184 def quit puts ">> Quitting... Bye... :)" end |
#saveBooks ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/bookstoretest.rb', line 164 def saveBooks puts ">> Saving current inventory..." if File.exist?($BOOKS_DB_NAME) File.truncate($BOOKS_DB_NAME, 0) end file = File.open($BOOKS_DB_NAME, "w") $books.each { |name, | puts "Saved: { '#{name}', #{rating} }" book_entry = name + "," + .to_s file.write( book_entry + "\n" ) } file.close puts end |
#searchBooks ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/bookstoretest.rb', line 106 def searchBooks puts ">> Searching existing books..." if $books.empty? puts ">> Inventory is empty! Nothing to search!" else print "Enter substring to search: " substring = gets.chomp $books.each { |name, | if name.to_s.include? substring puts "Found: { '#{name}', #{rating} }" end } puts end end |
#showMenuAndPrompt ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bookstoretest.rb', line 25 def showMenuAndPrompt puts "==================================" puts " Bookstore Inventory System " puts "==================================" puts " (#{$ADD}) : Add a book" puts " (#{$DELETE}) : Delete a book" puts " (#{$SEARCH}) : Search a book" puts "(#{$DISPLAY}) : Display all books" puts " (#{$SAVE}) : Save books information" puts " (#{$QUIT}) : Quit the application" puts "==================================" print "Enter your choice: " choice = gets.chomp choice end |
#start ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/bookstoretest.rb', line 42 def start puts "Starting..." loadBooks loop do choice = showMenuAndPrompt puts case choice when $ADD then addBook when $DELETE then deleteBook when $SEARCH then searchBooks when $DISPLAY then displayBooks when $SAVE then saveBooks when $QUIT then quit else invalidChoice(choice) end break if choice == $QUIT end end |