Skip to content

Commit

Permalink
methods-adding-listing-movies
Browse files Browse the repository at this point in the history
  • Loading branch information
Obimbo07 committed Dec 20, 2023
1 parent 73dcf8b commit c453652
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 21 deletions.
59 changes: 59 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require './classes/movie'
require './classes/source'

class App
def initialize
@movies = []
@sources = []
end

def list_all_movies
if @movies.empty?
puts 'No movies were found'
else
@movies.each do |movie|
puts "ID: #{movie}"
end
end
end

def list_all_sources
if @sources.empty?
puts 'No sources were found'
else
@sources.each do |source|
puts "ID: #{source.id}, Source: #{source.name}"
end
end
end

def add_movie
puts 'Enter Title: '
title = gets.chomp.to_s
puts 'Enter genre: '
genre = gets.chomp.to_s
puts 'Enter Author: '
author = gets.chomp.to_s
puts 'Enter source: '

source_name = gets.chomp.to_s
source = @sources.find { |s| s.name == source_name }

unless source
source = Source.new(source_name)
@sources << source
end

puts 'Enter publish_date: '
publish_date = gets.chomp.to_s
print 'Is it silent? (true/false): '
silent = gets.chomp.downcase == 'true'

movie = Movie.new(title, genre, author, source, publish_date, silent)
source.add_item(movie)

@movies << movie

puts 'Movie added successfully!'
end
end
8 changes: 5 additions & 3 deletions classes/movie.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require './classes/item'

class Movie < item
class Movie < Item
attr_accessor :silent

def initialize(genre, author, source, publish_date, silent)
super(genre, author, source, publish_date)
def initialize(title, genre, author, source, publish_date, silent, id = nil)
super(genre, author, source, nil, publish_date)
@id = id || Random.rand(1..1000)
@title = title
@silent = silent
end

Expand Down
4 changes: 2 additions & 2 deletions classes/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ class Source < Item
attr_reader :id, :items

def initialize(name, id = nil)
super
super(source, nil, nil, nil, nil)
@id = id || Random.rand(1..1000)
@name = name
@items = []
end

def add_item(item)
@items << item
item.label = self
item.source = self.name
end
end
39 changes: 23 additions & 16 deletions main.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
puts 'Welcome to your Catalog of things'
require_relative 'app'

class Menu
def initialize(app)
@app = app
end

def display_menu
puts '===== Library Menu ====='
puts '1. List all books'
Expand All @@ -23,38 +27,41 @@ def display_menu
def run_options(choice)
case choice
when 1
list_all_books
@app.list_all_books
when 2
list_all_music_albums
@app.list_all_music_albums
when 3
list_all_movies
@app.list_all_movies
when 4
list_all_games
@app.list_all_games
when 5
list_all_genres
@app.list_all_genres
when 6
list_all_labels
@app.list_all_labels
when 7
list_all_authors
@app.list_all_authors
when 8
list_all_sources
@app.list_all_sources
when 9
add_book
@app.add_book
when 10
add_music_album
@app.add_music_album
when 11
add_movie
@app.add_movie
when 12
add_game
@app.add_game
else
puts 'Invalid option.Please try again.'
puts 'Invalid option. Please try again.'
end
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/CyclomaticComplexity
end

menu = Menu.new
app_instance = App.new
menu = Menu.new(app_instance)

puts 'Welcome to your Catalog of things'

loop do
menu.display_menu
Expand All @@ -70,4 +77,4 @@ def run_options(choice)
else
puts 'Invalid choice. Please input a valid choice'
end
end
end

0 comments on commit c453652

Please sign in to comment.