BookStore 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/book.rb +10 -0
- data/lib/book_store.rb +67 -0
- data/lib/book_store_run.rb +46 -0
- data/lib/books.txt +1 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 86e77009d05fe6d327265efd5808666226cbb20d
|
4
|
+
data.tar.gz: abbe07a92ec614a6922bc6b450236e21fd5143ec
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b627380e353848bc6683ecf3236a3c9a7047a8fe0ef48b8653d54b13acf5eeeb9a634385d1670a19245972c0d9ead978b3b174868ee639e7eadf5de72c3fac9b
|
7
|
+
data.tar.gz: c2075a839dd62d5832caaad74cccd1c8b0286a810cef8429cb2c2d3128ff8fde6d747beed7318827e3c63ae630261b7e1edfe319fbfb3544645c6c127c655955
|
data/lib/book.rb
ADDED
data/lib/book_store.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require_relative 'book' # Requiring the class we have just created for use
|
3
|
+
# BookStore Class
|
4
|
+
class BookStore
|
5
|
+
include Singleton
|
6
|
+
# Loading the Data into the books from file
|
7
|
+
def initialize
|
8
|
+
@books = []
|
9
|
+
File.open(File.dirname(__FILE__) + '/books.txt').each do |line|
|
10
|
+
book_name = line.split('|').first
|
11
|
+
book_rating = line.split('|').last
|
12
|
+
book = Book.new(book_name.intern, book_rating)
|
13
|
+
@books << book
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Add Book to the file and also to the books
|
18
|
+
def book_add(name, rating)
|
19
|
+
book = Book.new(name.intern, rating)
|
20
|
+
@books << book
|
21
|
+
File.open(File.dirname(__FILE__) + '/books.txt', 'a') do |line|
|
22
|
+
line.puts book.name.to_s + '|' + book.rating + "\r"
|
23
|
+
end
|
24
|
+
puts 'Book added successfully!'
|
25
|
+
end
|
26
|
+
|
27
|
+
# Delete Book to the file and also to the books
|
28
|
+
def book_delete(name)
|
29
|
+
file_lines = ''
|
30
|
+
IO.readlines(File.dirname(__FILE__) + '/books.txt').each do |line|
|
31
|
+
file_lines += line unless line.split('|').first == name
|
32
|
+
end
|
33
|
+
File.open(File.dirname(__FILE__) + '/books.txt', 'w') do |file|
|
34
|
+
file.puts file_lines
|
35
|
+
end
|
36
|
+
initialize
|
37
|
+
puts 'Book deleted successfully!'
|
38
|
+
end
|
39
|
+
|
40
|
+
# Displays the content
|
41
|
+
def book_display
|
42
|
+
@books.each do |book|
|
43
|
+
puts "Book: #{book.name.to_s}, Rating: #{book.rating}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Search for the content in the list of the books
|
48
|
+
def book_search(search)
|
49
|
+
flag = false
|
50
|
+
@books.each do |book|
|
51
|
+
book2 = book.name.to_s.downcase
|
52
|
+
if book2.include? search
|
53
|
+
puts "Book: #{book.name.to_s}, Rating: #{book.rating}"
|
54
|
+
flag = true
|
55
|
+
end
|
56
|
+
end
|
57
|
+
puts flag ? '' : 'No Book found matching your criteria'
|
58
|
+
end
|
59
|
+
|
60
|
+
# Method to check if book exists
|
61
|
+
def book_exists(name)
|
62
|
+
@books.each do |book|
|
63
|
+
return true if book.name == name
|
64
|
+
end
|
65
|
+
false
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require_relative 'book_store'
|
2
|
+
|
3
|
+
# BookStoreRun Class
|
4
|
+
class BookStoreRun
|
5
|
+
def self.start
|
6
|
+
book_store = BookStore.instance
|
7
|
+
# puts commands to display instructions to the user
|
8
|
+
puts 'Welcome to the Ruby Bookstore.'
|
9
|
+
puts 'Please choose a follow operation from below to perform.'
|
10
|
+
puts 'Type --1-- to add a new book'
|
11
|
+
puts 'Type --2-- to find a book of your own choice'
|
12
|
+
puts 'Type --3-- to have a list of all available books'
|
13
|
+
puts 'Type --4-- to delete a book'
|
14
|
+
# getting user's input
|
15
|
+
choice = gets.chomp.downcase
|
16
|
+
# performing what user wants
|
17
|
+
case choice
|
18
|
+
when '1'
|
19
|
+
puts 'Enter the name of the book you want to add?'
|
20
|
+
name = gets.chomp
|
21
|
+
if book_store.book_exists(name.intern)
|
22
|
+
puts 'Book already exists!'
|
23
|
+
else
|
24
|
+
puts 'Enter the rating of the book?'
|
25
|
+
rating = gets.chomp
|
26
|
+
book_store.book_add(name, rating)
|
27
|
+
puts 'Book added to the store!'
|
28
|
+
end
|
29
|
+
when '2'
|
30
|
+
puts 'Enter what you want to search?'
|
31
|
+
search = gets.chomp
|
32
|
+
book_store.book_search(search)
|
33
|
+
when '3' then book_store.book_display
|
34
|
+
when '4'
|
35
|
+
puts 'Enter the name of the book you want to Delete?'
|
36
|
+
name = gets.chomp
|
37
|
+
if book_store.book_exists(name.intern)
|
38
|
+
book_store.book_delete(name)
|
39
|
+
else
|
40
|
+
puts 'No Book Found'
|
41
|
+
end
|
42
|
+
else
|
43
|
+
puts 'Didn\'t get what you are trying to ask!'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/books.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: BookStore
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Muhammad Asad
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-07-20 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A learning book store gem
|
14
|
+
email: muhammad.asad@confiz.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/book.rb
|
20
|
+
- lib/book_store.rb
|
21
|
+
- lib/book_store_run.rb
|
22
|
+
- lib/books.txt
|
23
|
+
homepage:
|
24
|
+
licenses:
|
25
|
+
- MIT
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 2.5.1
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: Book Store Gem
|
47
|
+
test_files: []
|