From 4044f29b270815fdb53823ed05a0d608910c7eeb Mon Sep 17 00:00:00 2001 From: "G. Wade Johnson" Date: Mon, 5 Oct 2015 13:25:59 -0400 Subject: [PATCH 1/3] Clean up of code to match rubocop rules. Not sure how terse to go with some of the code. I'm still not 100% with accepted Ruby idioms. --- GemCity.rb | 56 ------------------------------------------------ gem_city.rb | 44 +++++++++++++++++++++++++++++++++++++ gem_city_spec.rb | 6 +++--- 3 files changed, 47 insertions(+), 59 deletions(-) delete mode 100644 GemCity.rb create mode 100644 gem_city.rb diff --git a/GemCity.rb b/GemCity.rb deleted file mode 100644 index 690dc84..0000000 --- a/GemCity.rb +++ /dev/null @@ -1,56 +0,0 @@ -class GemCity - -=begin -This class represents the town of GemCity -This is a town riddled with crime but we can find out how happy the town is -=end - def initialize - @people, @population = { - :thieves => 5, - :Officers => 1}, - 50 - end - - def thieves thieves_number=@people[:thieves] - return @people[:thieves] = thieves_number - end - - def officers - return @people[:Officers] - end - - def population; return @population; end - - def setOfficers officers - @people[:Officers] = officers - end - - def happiness_of_town - #happiness is random... people don't know what they want! - happinessVals = Array.new - happiness = 0 - for index in (1..@population) - happinessVals.push(rand((100 - successful_crime_rate) .. 100)) - index += 1 - end - happinessVals.each do |value| - happiness += value - end - return happiness / 100 - end - - def successful_crime_rate - thieves = @people[:thieves] - officers = @people[:Officers] - if thieves <= 0 - odds_percent = 0 - elsif officers > thieves - odds_percent = 0 - else - odds = 1 \ - - officers.to_f / thieves.to_f - odds_percent = odds * 100 - end - return odds_percent - end -end \ No newline at end of file diff --git a/gem_city.rb b/gem_city.rb new file mode 100644 index 0000000..ffa91a3 --- /dev/null +++ b/gem_city.rb @@ -0,0 +1,44 @@ +# This class represents the town of GemCity +# This is a town riddled with crime but we can find out how happy the town is +class GemCity + attr_reader :population + def initialize + @people = { + thieves: 5, + Officers: 1 + } + @population = 50 + end + + def thieves(thieves_number = @people[:thieves]) + @people[:thieves] = thieves_number + end + + def officers + @people[:Officers] + end + + def officers=(officers) + @people[:Officers] = officers + end + + def happiness_of_town + # happiness is random... people don't know what they want! + happiness = 0 + (1..population).each do + happiness += rand((100 - successful_crime_rate)..100) + end + happiness / 100 + end + + def successful_crime_rate + thieves = @people[:thieves] + officers = @people[:Officers] + odds_percent = 0 + if thieves > 0 && officers <= thieves + odds = 1 - officers.to_f / thieves.to_f + odds_percent = odds * 100 + end + odds_percent + end +end diff --git a/gem_city_spec.rb b/gem_city_spec.rb index 884b3cb..3433322 100644 --- a/gem_city_spec.rb +++ b/gem_city_spec.rb @@ -1,5 +1,5 @@ require 'rspec' -require_relative 'GemCity' # This line may need to be changed +require_relative 'gem_city' # This line may need to be changed describe 'Gem City' do let(:city) { GemCity.new } @@ -16,7 +16,7 @@ it 'officers = thieves' do city.thieves 1 - city.setOfficers 1 # This line may need to be changed + city.officers = 1 # This line may need to be changed expect(city.successful_crime_rate).to eq(0) end @@ -27,7 +27,7 @@ it 'officers > thieves' do city.thieves 1 - city.setOfficers 2 # This line may need to be changed + city.officers = 2 # This line may need to be changed expect(city.successful_crime_rate).to eq(0) end end From 216d719ad6093f58c8aeba46a3a458b5fc7b1d8b Mon Sep 17 00:00:00 2001 From: "G. Wade Johnson" Date: Mon, 5 Oct 2015 16:41:39 -0400 Subject: [PATCH 2/3] Do the bonus exercise --- gem_city.rb | 20 +++++++++++++++++++- gem_city_spec.rb | 42 +++++++++++++++++++++--------------------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/gem_city.rb b/gem_city.rb index ffa91a3..12b7888 100644 --- a/gem_city.rb +++ b/gem_city.rb @@ -10,7 +10,11 @@ def initialize @population = 50 end - def thieves(thieves_number = @people[:thieves]) + def thieves + @people[:thieves] + end + + def thieves=(thieves_number) @people[:thieves] = thieves_number end @@ -41,4 +45,18 @@ def successful_crime_rate end odds_percent end + + def city_demographics + civilians = population - (thieves + officers) + { thieves: "#{population_percentage thieves}%", + officers: "#{population_percentage officers}%", + civilians: "#{population_percentage civilians}%" + } + end + + private + + def population_percentage(number) + ((number.to_f / population.to_f) * 100).to_i + end end diff --git a/gem_city_spec.rb b/gem_city_spec.rb index 3433322..72fed07 100644 --- a/gem_city_spec.rb +++ b/gem_city_spec.rb @@ -15,18 +15,18 @@ end it 'officers = thieves' do - city.thieves 1 + city.thieves = 1 city.officers = 1 # This line may need to be changed expect(city.successful_crime_rate).to eq(0) end it 'thieves = 0' do - city.thieves 0 + city.thieves = 0 expect(city.successful_crime_rate).to eq(0) end it 'officers > thieves' do - city.thieves 1 + city.thieves = 1 city.officers = 2 # This line may need to be changed expect(city.successful_crime_rate).to eq(0) end @@ -38,26 +38,26 @@ end it 'successful_crime_rate = 0' do - city.thieves 0 + city.thieves = 0 expect(city.happiness_of_town).to eq(50) end end - # context 'city_demographics' do - # it 'initialize' do - # demographics = city.city_demographics - # expect(demographics[:thieves]).to eq('10%') - # expect(demographics[:officers]).to eq('2%') - # expect(demographics[:civilians]).to eq('88%') - # end - - # it 'thieves = 10, officers = 25' do - # city.thieves = 10 - # city.officers = 25 - # demographics = city.city_demographics - # expect(demographics[:thieves]).to eq('20%') - # expect(demographics[:officers]).to eq('50%') - # expect(demographics[:civilians]).to eq('30%') - # end - # end + context 'city_demographics' do + it 'initialize' do + demographics = city.city_demographics + expect(demographics[:thieves]).to eq('10%') + expect(demographics[:officers]).to eq('2%') + expect(demographics[:civilians]).to eq('88%') + end + + it 'thieves = 10, officers = 25' do + city.thieves = 10 + city.officers = 25 + demographics = city.city_demographics + expect(demographics[:thieves]).to eq('20%') + expect(demographics[:officers]).to eq('50%') + expect(demographics[:civilians]).to eq('30%') + end + end end From 822b9bfab126408f658f97823bc0f02231eb53da Mon Sep 17 00:00:00 2001 From: "G. Wade Johnson" Date: Tue, 6 Oct 2015 14:17:42 -0400 Subject: [PATCH 3/3] More refactoring suggested by MikeGee --- gem_city.rb | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/gem_city.rb b/gem_city.rb index 12b7888..13e8d76 100644 --- a/gem_city.rb +++ b/gem_city.rb @@ -2,30 +2,13 @@ # This is a town riddled with crime but we can find out how happy the town is class GemCity attr_reader :population + attr_accessor :thieves, :officers def initialize - @people = { - thieves: 5, - Officers: 1 - } + @thieves = 5 + @officers = 1 @population = 50 end - def thieves - @people[:thieves] - end - - def thieves=(thieves_number) - @people[:thieves] = thieves_number - end - - def officers - @people[:Officers] - end - - def officers=(officers) - @people[:Officers] = officers - end - def happiness_of_town # happiness is random... people don't know what they want! happiness = 0 @@ -36,8 +19,6 @@ def happiness_of_town end def successful_crime_rate - thieves = @people[:thieves] - officers = @people[:Officers] odds_percent = 0 if thieves > 0 && officers <= thieves odds = 1 - officers.to_f / thieves.to_f