From d50e207da375335a5b2dd16e98b660404b25cd06 Mon Sep 17 00:00:00 2001 From: Samuel Priddy Date: Sun, 13 Dec 2015 14:23:11 -0500 Subject: [PATCH 1/8] Commit initial solutoin --- GemCity.rb | 61 +++++++++++++++++++++--------------------------- gem_city_spec.rb | 4 ++-- 2 files changed, 29 insertions(+), 36 deletions(-) diff --git a/GemCity.rb b/GemCity.rb index 690dc84..1c2dd7c 100644 --- a/GemCity.rb +++ b/GemCity.rb @@ -1,56 +1,49 @@ class GemCity + # This class represents the town of GemCity + # This is a town riddled with crime but we can find out how happy the town is + attr_reader :population -=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 + @people = { thieves: 5, officers: 1 } + @population = 50 + end - def thieves thieves_number=@people[:thieves] - return @people[:thieves] = thieves_number + def thieves(thieves_number = @people[:thieves]) + @people[:thieves] = thieves_number end def officers - return @people[:Officers] + @people[:officers] end - def population; return @population; end - - def setOfficers officers - @people[:Officers] = officers + def officers=(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 + # happiness is random... people don't know what they want! + happiness_vals = [] + happiness = 0 + + @population.times.each do + happiness_vals << rand((100 - successful_crime_rate)..100) end - happinessVals.each do |value| + + happiness_vals.each do |value| happiness += value end - return happiness / 100 + + 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 + officers = @people[:officers] + + if thieves <= 0 || officers > thieves + 0 else - odds = 1 \ - - officers.to_f / thieves.to_f - odds_percent = odds * 100 + 100 * (1 - officers.to_f / thieves.to_f) end - return odds_percent end -end \ No newline at end of file +end diff --git a/gem_city_spec.rb b/gem_city_spec.rb index 884b3cb..7890abb 100644 --- a/gem_city_spec.rb +++ b/gem_city_spec.rb @@ -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 898705650ad91edcd93306ff14de318a95433cfa Mon Sep 17 00:00:00 2001 From: Samuel Priddy Date: Sun, 13 Dec 2015 14:55:02 -0500 Subject: [PATCH 2/8] Get rid of getter and setter functions, everything is broken --- GemCity.rb | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/GemCity.rb b/GemCity.rb index 1c2dd7c..bc89c38 100644 --- a/GemCity.rb +++ b/GemCity.rb @@ -2,24 +2,14 @@ class GemCity # This class represents the town of GemCity # This is a town riddled with crime but we can find out how happy the town is attr_reader :population + attr_accessor :officers, :thieves def initialize - @people = { thieves: 5, officers: 1 } + @thieves = 5 + @officers = 5 @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_vals = [] @@ -29,21 +19,15 @@ def happiness_of_town happiness_vals << rand((100 - successful_crime_rate)..100) end - happiness_vals.each do |value| - happiness += value - end - + happiness_vals.each { |value| happiness += value } happiness / 100 end def successful_crime_rate - thieves = @people[:thieves] - officers = @people[:officers] - - if thieves <= 0 || officers > thieves + if @thieves <= 0 || @officers > @thieves 0 else - 100 * (1 - officers.to_f / thieves.to_f) + 100 * (1 - @officers.to_f / @thieves.to_f) end end end From 2e63b9cad1a64e169ed790a3081c4bb4c3a32ad0 Mon Sep 17 00:00:00 2001 From: Samuel Priddy Date: Sun, 13 Dec 2015 15:14:06 -0500 Subject: [PATCH 3/8] Refactor for conciseness --- GemCity.rb | 18 ++++-------------- gem_city_spec.rb | 8 ++++---- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/GemCity.rb b/GemCity.rb index bc89c38..9b29307 100644 --- a/GemCity.rb +++ b/GemCity.rb @@ -6,28 +6,18 @@ class GemCity def initialize @thieves = 5 - @officers = 5 + @officers = 1 @population = 50 end def happiness_of_town # happiness is random... people don't know what they want! - happiness_vals = [] happiness = 0 - - @population.times.each do - happiness_vals << rand((100 - successful_crime_rate)..100) - end - - happiness_vals.each { |value| happiness += value } + @population.times { happiness += rand((100 - successful_crime_rate)..100)} happiness / 100 end def successful_crime_rate - if @thieves <= 0 || @officers > @thieves - 0 - else - 100 * (1 - @officers.to_f / @thieves.to_f) - end + @thieves <= 0 || @officers > @thieves ? 0 : 100 * (1 - @officers / @thieves.to_f) end -end +end \ No newline at end of file diff --git a/gem_city_spec.rb b/gem_city_spec.rb index 7890abb..a741825 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,7 +38,7 @@ end it 'successful_crime_rate = 0' do - city.thieves 0 + city.thieves = 0 expect(city.happiness_of_town).to eq(50) end end From 0ed3b8423caa1c55ccf87d6f3cf395cafdc6887a Mon Sep 17 00:00:00 2001 From: Samuel Priddy Date: Sun, 13 Dec 2015 15:21:40 -0500 Subject: [PATCH 4/8] Add GemCity#city_demographics method --- GemCity.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/GemCity.rb b/GemCity.rb index 9b29307..e4cabd1 100644 --- a/GemCity.rb +++ b/GemCity.rb @@ -20,4 +20,16 @@ def happiness_of_town def successful_crime_rate @thieves <= 0 || @officers > @thieves ? 0 : 100 * (1 - @officers / @thieves.to_f) end + + def city_demographics + {}.tap do |demo| + demo[:thieves] = demographics_format @thieves + demo[:officers] = demographics_format @officers + demo[:civilians] = demographics_format (@population - @thieves - @officers) + end + end + + def demographics_format people + "#{100 * people / @population}%" + end end \ No newline at end of file From eb0f9c012f42b759e486f839ddfd796a31803165 Mon Sep 17 00:00:00 2001 From: Samuel Priddy Date: Sun, 13 Dec 2015 15:24:30 -0500 Subject: [PATCH 5/8] Uncomment demographics tests, all green --- gem_city_spec.rb | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/gem_city_spec.rb b/gem_city_spec.rb index a741825..c248f55 100644 --- a/gem_city_spec.rb +++ b/gem_city_spec.rb @@ -43,21 +43,21 @@ 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 df3d842c884fa7be82c89dd7783ede4251059f5c Mon Sep 17 00:00:00 2001 From: Samuel Priddy Date: Sun, 13 Dec 2015 15:46:47 -0500 Subject: [PATCH 6/8] Fix as many RuboCop complaints as possible --- GemCity.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/GemCity.rb b/GemCity.rb index e4cabd1..a9a67bc 100644 --- a/GemCity.rb +++ b/GemCity.rb @@ -13,7 +13,7 @@ def initialize def happiness_of_town # happiness is random... people don't know what they want! happiness = 0 - @population.times { happiness += rand((100 - successful_crime_rate)..100)} + @population.times { happiness += rand((100 - successful_crime_rate)..100) } happiness / 100 end @@ -29,7 +29,7 @@ def city_demographics end end - def demographics_format people + def demographics_format(people) "#{100 * people / @population}%" end -end \ No newline at end of file +end From 5da9db896c19b1799273331c71a74765e1229783 Mon Sep 17 00:00:00 2001 From: Samuel Priddy Date: Sun, 13 Dec 2015 15:55:58 -0500 Subject: [PATCH 7/8] Final refactoring --- GemCity.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/GemCity.rb b/GemCity.rb index a9a67bc..2183794 100644 --- a/GemCity.rb +++ b/GemCity.rb @@ -13,20 +13,18 @@ def initialize def happiness_of_town # happiness is random... people don't know what they want! happiness = 0 - @population.times { happiness += rand((100 - successful_crime_rate)..100) } + population.times { happiness += rand((100 - successful_crime_rate)..100) } happiness / 100 end def successful_crime_rate - @thieves <= 0 || @officers > @thieves ? 0 : 100 * (1 - @officers / @thieves.to_f) + thieves <= 0 || officers > @thieves ? 0 : 100 * (1 - officers / thieves.to_f) end def city_demographics - {}.tap do |demo| - demo[:thieves] = demographics_format @thieves - demo[:officers] = demographics_format @officers - demo[:civilians] = demographics_format (@population - @thieves - @officers) - end + { thieves: demographics_format(thieves), + officers: demographics_format(officers), + civilians: demographics_format(population - thieves - officers) } end def demographics_format(people) From 76a22daf6a0ad4561d32952eb7e9729ffec5227f Mon Sep 17 00:00:00 2001 From: Samuel Priddy Date: Sun, 13 Dec 2015 16:01:10 -0500 Subject: [PATCH 8/8] Remove a few more @ signs --- GemCity.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GemCity.rb b/GemCity.rb index 2183794..2844acf 100644 --- a/GemCity.rb +++ b/GemCity.rb @@ -18,7 +18,7 @@ def happiness_of_town end def successful_crime_rate - thieves <= 0 || officers > @thieves ? 0 : 100 * (1 - officers / thieves.to_f) + thieves <= 0 || officers > thieves ? 0 : 100 * (1 - officers / thieves.to_f) end def city_demographics