Skip to content

Commit

Permalink
Revert "Refactor code: move input choice functionality to main.rb"
Browse files Browse the repository at this point in the history
This reverts commit c0be0bd.
  • Loading branch information
aamir-asaram committed Sep 4, 2023
1 parent e7f4bfb commit 6c577d9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 33 deletions.
56 changes: 27 additions & 29 deletions main.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
require_relative 'school_library/app'
require_relative 'school_library/modules/display_menu'
class Main
def main
App.run
end
def main
App.run
end

def handle_choice(choice)
extend DisplayMenu
case choice
when 1
list_books
when 2
list_people
when 3
create_person
when 4
create_book
when 5
create_rental
when 6
print 'Enter person ID: '
person_id = gets.chomp.to_i
list_rentals_for_person(person_id)
else
invalid_choice
end
display_menu
def handle_choice(choice)
extend DisplayMenu
case choice
when 1
list_books
when 2
list_people
when 3
create_person
when 4
create_book
when 5
create_rental
when 6
print 'Enter person ID: '
person_id = gets.chomp.to_i
list_rentals_for_person(person_id)
else
invalid_choice
end
display_menu
end

def invalid_choice
puts 'Invalid choice. Please enter a valid option (1-7).'
end
def invalid_choice
puts 'Invalid choice. Please enter a valid option (1-7).'
end

Main.new.main
main if __FILE__ == $PROGRAM_NAME
9 changes: 6 additions & 3 deletions school_library/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ class App
extend Lists

def self.run
choice = display_menu
choice = handle_choice(choice) while choice != 7
puts 'Thank you for using the School Library App!'
loop do
display_menu
choice = gets.chomp.to_i
handle_choice(choice)
break if choice == 7
end
end
end
31 changes: 30 additions & 1 deletion school_library/modules/display_menu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,35 @@ def display_menu
puts '6. List all rentals for a given person (by ID)'
puts '7. Exit'
print 'Enter your choice: '
gets.chomp.to_i
end

# rubocop:disable Metrics/CyclomaticComplexity
def handle_choice(choice)
case choice
when 1
list_books
when 2
list_people
when 3
create_person
when 4
create_book
when 5
create_rental
when 6
print 'Enter person ID: '
person_id = gets.chomp.to_i
list_rentals_for_person(person_id)
when 7
puts 'Thanks you for using this app!'
else
invalid_choice
end
end

# rubocop:enable Metrics/CyclomaticComplexity

def invalid_choice
puts 'Invalid choice. Please enter a valid option (1-7).'
end
end

0 comments on commit 6c577d9

Please sign in to comment.