BookStoreGem 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d2c888aa048beddf4cab5a8dc7bd30d7021f15ce
4
+ data.tar.gz: a3feba0c0213fe9e71c7990cb6827c3ccb712e62
5
+ SHA512:
6
+ metadata.gz: 6c3c96a30c8aa4925fafa1505f8d37383db62733441dfe3998f17c7abf3ae33e2d789178ee918dac42a8b7f167971af30a27e5251ec5a07bc2a3c7ba01910b74
7
+ data.tar.gz: 04dbbe4c53db1e433bafee7ccf425f1936db48b0900ee47b3ff2c01a397f1f28fb347264a2ee970961a7bc8390486bf7ecbfd7ef4752a919c83de2503a5e6a7f
data/lib/book.rb ADDED
@@ -0,0 +1,10 @@
1
+ # Book.rb
2
+ class Book
3
+ attr_reader :title
4
+ attr_reader :rating
5
+
6
+ def initialize(title, rating)
7
+ @title = title
8
+ @rating = rating
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ Book 1|4
2
+ Book 2|1
3
+ Book 3|5
4
+ Book 4|3
5
+ Book 5|4
data/lib/bookstore.rb ADDED
@@ -0,0 +1,69 @@
1
+ require 'singleton'
2
+ require_relative 'book'
3
+
4
+ # BookStore.rb
5
+ class BookStore
6
+ include Singleton
7
+
8
+ def initialize
9
+ @books = []
10
+
11
+ IO.foreach('book_store.txt') do |line|
12
+ name = line.split('|').first
13
+ rating = line.split('|').last
14
+
15
+ book = Book.new(name.intern, rating.to_i)
16
+
17
+ @books << book
18
+ end
19
+ end
20
+
21
+ def add_book(name, rating)
22
+ book = Book.new(name.intern, rating.to_i)
23
+
24
+ @books << book
25
+ update_store
26
+ end
27
+
28
+ def delete_book(name)
29
+ @books.select! { |book| book.title != name.intern }
30
+
31
+ update_store
32
+ end
33
+
34
+ def search_book(name)
35
+ result = []
36
+ @books.each do |book|
37
+ next unless book.title.downcase.to_s.include? name.downcase
38
+ result << Book.new(book.title, book.rating)
39
+ end
40
+
41
+ result
42
+ end
43
+
44
+ def display_books
45
+ puts 'Books available in store'
46
+ @books.each do |book|
47
+ puts "Book Name: #{book.title}"
48
+ puts "Book Rating: #{book.rating}"
49
+ puts ''
50
+ end
51
+ end
52
+
53
+ def book_exists(title)
54
+ @books.each do |book|
55
+ return true if book.title == title
56
+ end
57
+ false
58
+ end
59
+
60
+ private
61
+
62
+ def update_store
63
+ book_store = File.open('book_store.txt', 'w')
64
+ @books.each do |book|
65
+ book_store.syswrite("#{book.title}|#{book.rating}\n")
66
+ end
67
+ book_store.close
68
+ end
69
+ end
@@ -0,0 +1,57 @@
1
+ require_relative 'bookstore'
2
+
3
+ # BookStore Execution
4
+ class BookStoreRun
5
+ def self.start
6
+ book_store = BookStore.instance
7
+
8
+ puts 'Welcome to the book store'
9
+ puts 'What would you like to do'
10
+ puts '1. Add a book'
11
+ puts '2. Search for a book'
12
+ puts '3. Delete a book'
13
+ puts '4. View all the books'
14
+ puts 'Enter your choice (1-4)'
15
+
16
+ choice = gets.chomp
17
+ case choice
18
+ when '1'
19
+ puts 'Enter title of book'
20
+ title = gets.chomp
21
+ if book_store.book_exists(title.intern)
22
+ puts 'Sorry, the book already exists in our store'
23
+ else
24
+ puts 'Enter book rating'
25
+ rating = gets.chomp
26
+ book_store.add_book(title, rating)
27
+ puts 'Book added to store'
28
+ end
29
+ when '2'
30
+ puts 'What would you like to search for'
31
+ search = gets.chomp
32
+ books = book_store.search_book(search)
33
+
34
+ if !books.empty?
35
+ books.each do |book|
36
+ puts "Book Name: #{book.title}"
37
+ puts "Book Rating: #{book.rating}"
38
+ end
39
+ else
40
+ puts 'No book found'
41
+ end
42
+ when '3'
43
+ puts 'Enter title of book'
44
+ title = gets.chomp
45
+ if book_store.book_exists(title.intern)
46
+ book_store.delete_book(title)
47
+ puts 'Book removed from store'
48
+ else
49
+ puts 'Sorry, that book does not exist in our store'
50
+ end
51
+ when '4'
52
+ book_store.display_books
53
+ else
54
+ puts 'Invalid input entered'
55
+ end
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: BookStoreGem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Bilal Naeem
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: "A simple book store gem. \nInstructions:\nrequire 'bookstore_run'\nBookStoreRun.start"
14
+ email: bilal.naeem@to
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/book.rb
20
+ - lib/book_store.txt
21
+ - lib/bookstore.rb
22
+ - lib/bookstore_run.rb
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
47
+ test_files: []