-
Notifications
You must be signed in to change notification settings - Fork 30
Dan Meehan solution #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dmee3
wants to merge
1
commit into
paircolumbus:master
Choose a base branch
from
dmee3:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| require_relative 'view' | ||
| require_relative 'model' | ||
|
|
||
| class Controller | ||
| include View | ||
|
|
||
| def initialize | ||
| @beerList = List.new | ||
| end | ||
|
|
||
| def add_beer | ||
| @beerList.add_beer Print::serialize_add_beer | ||
| end | ||
|
|
||
| def edit_beer | ||
| id = Print::get_input("\nEnter the id of the beer you want to EDIT:").to_i | ||
| b = @beerList.find_beer id | ||
| fail "No beer matching id #{id}" unless b | ||
| @beerList.edit_beer(Print::serialize_edit_beer(b.name, b.brewer, b.rating), id) | ||
| end | ||
|
|
||
| def delete_beer | ||
| id = Print::get_input "\nEnter the id of the beer you want to DELETE:" | ||
| @beerList.delete_beer id.to_i | ||
| end | ||
|
|
||
| def sort_list | ||
| cmd = Print::get_input "\nEnter the sort method (N)ame/(B)rewer/(R)ating/(I)d:" | ||
| case cmd.upcase | ||
| when 'N' | ||
| @beerList.sort_by_name! | ||
| when 'B' | ||
| @beerList.sort_by_brewer! | ||
| when 'R' | ||
| @beerList.sort_by_rating! | ||
| else | ||
| @beerList.sort_by_id! | ||
| end | ||
| end | ||
|
|
||
| def run! | ||
| Print::run_spinner | ||
| Print::title_screen | ||
|
|
||
| loop do | ||
| Print::menu | ||
| case Print::get_input.upcase | ||
| when "V" | ||
| Print::print_list(@beerList.beers) | ||
| when "A" | ||
| add_beer | ||
| when "E" | ||
| edit_beer | ||
| when "D" | ||
| delete_beer | ||
| when "S" | ||
| sort_list | ||
| when "Q" | ||
| puts "Cheers!" | ||
| exit | ||
| else | ||
| Print::error_message | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| Controller.new.run! | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| class Beer | ||
| attr_reader :id, :name, :brewer, :rating | ||
|
|
||
| def initialize args | ||
| @id = args[:id] | ||
| @name = args[:name] | ||
| @brewer = args[:brewer] | ||
| @rating = args[:rating] | ||
| end | ||
|
|
||
| def edit input | ||
| @name = input[:name] | ||
| @brewer = input[:brewer] | ||
| @rating = input[:rating] | ||
| end | ||
|
|
||
| def to_s | ||
| "##{@id} #{@name} - #{@brewer} (#{@rating} / 100)" | ||
| end | ||
| end | ||
|
|
||
| class List | ||
| attr_reader :beers | ||
|
|
||
| def initialize | ||
| @id = 0 | ||
| @beers = [] | ||
| populate | ||
| end | ||
|
|
||
| def add_beer input | ||
| @beers << Beer.new(input.merge(generate_id)) | ||
| end | ||
|
|
||
| def delete_beer id | ||
| @beers.delete_if { |b| b.id == id } | ||
| end | ||
|
|
||
| def edit_beer input, id | ||
| b = find_beer id | ||
| fail "No beer matching id #{id}" unless b | ||
| b.edit input | ||
| end | ||
|
|
||
| def find_beer id | ||
| @beers.find { |b| b.id == id } | ||
| end | ||
|
|
||
| def sort_by_brewer! | ||
| @beers.sort_by! { |b| b.brewer } | ||
| end | ||
|
|
||
| def sort_by_id! | ||
| @beers.sort_by! { |b| b.id } | ||
| end | ||
|
|
||
| def sort_by_name! | ||
| @beers.sort_by! { |b| b.name } | ||
| end | ||
|
|
||
| def sort_by_rating! | ||
| @beers.sort_by! { |b| 100 - b.rating } | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def generate_id | ||
| { id: @id += 1 } | ||
| end | ||
|
|
||
| def populate | ||
| list = [ | ||
| { name: "Dortmunder Gold", brewer: "Great Lakes", rating: 95 }, | ||
| { name: "Fat Tire", brewer: "New Belgium", rating: 78 }, | ||
| { name: "Ohio Native Lager", brewer: "Sibling Revelry", rating: 83 }, | ||
| { name: "Inverness", brewer: "Columbus Brewing Company", rating: 72 }, | ||
| { name: "Black & Tan", brewer: "Yuengling", rating: 80 }, | ||
| ] | ||
| list.each { |b| add_beer(b) } | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| module View | ||
|
|
||
| module Print | ||
|
|
||
| class << self | ||
| def run_spinner | ||
| print "Loading (please wait) " | ||
| 5.times { print "."; sleep 0.5; } | ||
| print "\n" | ||
| end | ||
|
|
||
| def error_message | ||
| puts "That's not a command key. Try again!" | ||
| end | ||
|
|
||
| def title_screen | ||
| title = <<TITLE | ||
|
|
||
| ********** | ********** | ||
| * BARTENDER * | ||
| ********** | ********** | ||
|
|
||
| TITLE | ||
| puts title | ||
| end | ||
|
|
||
| def menu | ||
| menu = <<EOS | ||
|
|
||
| ***** Welcome ***** | ||
| - (V)iew your beers | ||
| - (A)dd a beer | ||
| - (E)dit a beer | ||
| - (D)elete a beer | ||
| - (S)ort list | ||
| - (Q)uit program | ||
| ******************* | ||
|
|
||
| EOS | ||
| puts menu | ||
| end | ||
|
|
||
| def print_list(beers) | ||
| beers.each { |b| puts " " + b.to_s } | ||
| end | ||
|
|
||
| def serialize_add_beer | ||
| prompts = [ | ||
| "\nEnter the name:", | ||
| "\nEnter the brewer:", | ||
| "\nEnter your rating (/100):" | ||
| ] | ||
| {}.tap do |obj| | ||
| prompts.each do |t| | ||
| if obj.empty? | ||
| obj[:name] = get_input(t) | ||
| elsif obj.size == 1 | ||
| obj[:brewer] = get_input(t) | ||
| else | ||
| obj[:rating] = get_input(t).to_i | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def serialize_edit_beer name, brewer, rating | ||
| prompts = [ | ||
| "\nEnter new name or blank to leave as #{name}", | ||
| "\nEnter new brewer or blank to leave as #{brewer}", | ||
| "\nEnter new rating or blank to leave as #{rating}"] | ||
| {}.tap do |obj| | ||
| prompts.each do |t| | ||
| if obj.empty? | ||
| obj[:name] = get_input_or_default(name, t) | ||
| elsif obj.size == 1 | ||
| obj[:brewer] = get_input_or_default(brewer, t) | ||
| else | ||
| obj[:rating] = get_input_or_default(rating, t).to_i | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def get_input question=nil | ||
| puts question if question | ||
| print "> " | ||
| gets.chomp | ||
| end | ||
|
|
||
| def get_input_or_default default, question=nil | ||
| puts question if question | ||
| print "> " | ||
| input = gets.chomp | ||
| input == "" ? default : input | ||
| end | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both calls to
find_beerfail when they don't get a result. Shouldfind_beerbe responsible for failing when it doesn't find one?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right,
find_beershould probably handle its own failure so there's a single point of failure instead of having to repeat this logic here and inedit_beeron line 41 of the model.I guess the only reason to return nil and check outside
find_beerwould be if I wanted to do some other error handling instead of failing right away, which I didn't do anyway haha.