From c6b870fe4c4ddb8208ea4aa65ee56e43a5be7347 Mon Sep 17 00:00:00 2001 From: janmilosh Date: Mon, 23 May 2016 08:01:43 -0400 Subject: [PATCH 1/5] All but two offenses fixed --- GemCity.rb | 56 ------------------------------------------------ Gemfile | 2 +- gem_city.rb | 50 ++++++++++++++++++++++++++++++++++++++++++ gem_city_spec.rb | 6 +++--- 4 files changed, 54 insertions(+), 60 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/Gemfile b/Gemfile index 94b6f8c..6fd63b3 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ source 'https://rubygems.org' gem 'rubocop', require: false -gem 'rspec' \ No newline at end of file +gem 'rspec' diff --git a/gem_city.rb b/gem_city.rb new file mode 100644 index 0000000..9d19b2d --- /dev/null +++ b/gem_city.rb @@ -0,0 +1,50 @@ +# 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 assign_officers(officers) + @people[:Officers] = officers + end + + def happiness_of_town + # happiness is random... people don't know what they want! + happiness_vals = [] + happiness = 0 + for index in (1..@population) + happiness_vals.push(rand((100 - successful_crime_rate)..100)) + index += 1 + end + happiness_vals.each do |value| + happiness += value + end + 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 + odds_percent + end +end diff --git a/gem_city_spec.rb b/gem_city_spec.rb index 884b3cb..44ac6ef 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.assign_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.assign_officers 2 # This line may need to be changed expect(city.successful_crime_rate).to eq(0) end end From ae030d26cad5ec271da1acb76c8ae743038c4311 Mon Sep 17 00:00:00 2001 From: janmilosh Date: Mon, 23 May 2016 18:40:21 -0400 Subject: [PATCH 2/5] more refactoring --- gem_city.rb | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/gem_city.rb b/gem_city.rb index 9d19b2d..fd58b1b 100644 --- a/gem_city.rb +++ b/gem_city.rb @@ -24,9 +24,8 @@ def happiness_of_town # happiness is random... people don't know what they want! happiness_vals = [] happiness = 0 - for index in (1..@population) + (1..@population).each do happiness_vals.push(rand((100 - successful_crime_rate)..100)) - index += 1 end happiness_vals.each do |value| happiness += value @@ -37,14 +36,8 @@ def happiness_of_town 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 - odds_percent + return 0 if thieves <= 0 || officers > thieves + odds = 1 - officers.to_f / thieves.to_f + odds * 100 end end From 65953476ce7e43ea4cad25131fed2f0fa3d4ada7 Mon Sep 17 00:00:00 2001 From: janmilosh Date: Mon, 23 May 2016 20:35:50 -0400 Subject: [PATCH 3/5] Adds demographics methods --- Gemfile | 1 + Gemfile.lock | 11 +++++++++++ gem_city.rb | 25 ++++++++++++++++++++++--- gem_city_spec.rb | 47 ++++++++++++++++++++++++----------------------- 4 files changed, 58 insertions(+), 26 deletions(-) diff --git a/Gemfile b/Gemfile index 6fd63b3..cbe227d 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,4 @@ source 'https://rubygems.org' gem 'rubocop', require: false gem 'rspec' +gem 'pry' diff --git a/Gemfile.lock b/Gemfile.lock index 771b310..a631a45 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -4,10 +4,16 @@ GEM ast (2.0.0) astrolabe (1.3.1) parser (~> 2.2) + coderay (1.1.1) diff-lcs (1.2.5) + method_source (0.8.2) parser (2.2.2.6) ast (>= 1.1, < 3.0) powerpack (0.1.1) + pry (0.10.3) + coderay (~> 1.1.0) + method_source (~> 0.8.1) + slop (~> 3.4) rainbow (2.0.0) rspec (3.3.0) rspec-core (~> 3.3.0) @@ -29,10 +35,15 @@ GEM rainbow (>= 1.99.1, < 3.0) ruby-progressbar (~> 1.4) ruby-progressbar (1.7.5) + slop (3.6.0) PLATFORMS ruby DEPENDENCIES + pry rspec rubocop + +BUNDLED WITH + 1.11.2 diff --git a/gem_city.rb b/gem_city.rb index fd58b1b..9a85dee 100644 --- a/gem_city.rb +++ b/gem_city.rb @@ -8,7 +8,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 @@ -16,8 +20,12 @@ def officers @people[:Officers] end - def assign_officers(officers) - @people[:Officers] = officers + def officers=(officers_number) + @people[:Officers] = officers_number + end + + def civilians + @population - officers - thieves end def happiness_of_town @@ -40,4 +48,15 @@ def successful_crime_rate odds = 1 - officers.to_f / thieves.to_f odds * 100 end + + def city_demographics + { thieves: percentage(@people[:thieves]), + officers: percentage(@people[:Officers]), + civilians: percentage(civilians) + } + end + + def percentage(number_in_group) + "#{(100.0 * number_in_group / @population).round}%" + end end diff --git a/gem_city_spec.rb b/gem_city_spec.rb index 44ac6ef..17c3b8c 100644 --- a/gem_city_spec.rb +++ b/gem_city_spec.rb @@ -1,4 +1,5 @@ require 'rspec' +require 'pry' require_relative 'gem_city' # This line may need to be changed describe 'Gem City' do @@ -15,19 +16,19 @@ end it 'officers = thieves' do - city.thieves 1 - city.assign_officers 1 # This line may need to be changed + 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.assign_officers 2 # This line may need to be changed + city.thieves = 1 + city.officers = 2 # This line may need to be changed expect(city.successful_crime_rate).to eq(0) end end @@ -38,26 +39,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 1c786f0cd2db4a14105962363b6160022418bded Mon Sep 17 00:00:00 2001 From: janmilosh Date: Tue, 24 May 2016 18:16:39 -0400 Subject: [PATCH 4/5] simplify accessors --- gem_city.rb | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/gem_city.rb b/gem_city.rb index 9a85dee..e6cef3d 100644 --- a/gem_city.rb +++ b/gem_city.rb @@ -2,30 +2,16 @@ # 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_number) - @people[:Officers] = officers_number - end - def civilians - @population - officers - thieves + @population - @officers - @thieves end def happiness_of_town @@ -42,16 +28,14 @@ def happiness_of_town end def successful_crime_rate - thieves = @people[:thieves] - officers = @people[:Officers] - return 0 if thieves <= 0 || officers > thieves - odds = 1 - officers.to_f / thieves.to_f + return 0 if @thieves <= 0 || @officers > @thieves + odds = 1 - @officers.to_f / @thieves.to_f odds * 100 end def city_demographics - { thieves: percentage(@people[:thieves]), - officers: percentage(@people[:Officers]), + { thieves: percentage(@thieves), + officers: percentage(@officers), civilians: percentage(civilians) } end From 70c3300aad3337fef7f7f8da61f2be413f78cab8 Mon Sep 17 00:00:00 2001 From: janmilosh Date: Tue, 24 May 2016 18:21:32 -0400 Subject: [PATCH 5/5] Make method private --- gem_city.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gem_city.rb b/gem_city.rb index e6cef3d..fd76b9e 100644 --- a/gem_city.rb +++ b/gem_city.rb @@ -40,6 +40,8 @@ def city_demographics } end + private + def percentage(number_in_group) "#{(100.0 * number_in_group / @population).round}%" end