From b722eec1691d304c577c7bb856fbc3e2789f1208 Mon Sep 17 00:00:00 2001 From: Joe Reich Date: Sun, 13 Dec 2015 12:53:41 -0500 Subject: [PATCH 1/6] solved the initial problems --- GemCity.rb | 56 ------------------------------------------------------ Gemfile | 2 +- 2 files changed, 1 insertion(+), 57 deletions(-) delete mode 100644 GemCity.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' From 397f9c6c6bf90ae017ed4712455bfca55fb41a85 Mon Sep 17 00:00:00 2001 From: Joe Reich Date: Sun, 13 Dec 2015 12:59:03 -0500 Subject: [PATCH 2/6] really finished initial problems --- gem_city_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gem_city_spec.rb b/gem_city_spec.rb index 884b3cb..37f3634 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' 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 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 expect(city.successful_crime_rate).to eq(0) end end From 6d360b80c3f0facc7848a56a19bf953bc7636ba1 Mon Sep 17 00:00:00 2001 From: Joe Reich Date: Sun, 13 Dec 2015 13:27:04 -0500 Subject: [PATCH 3/6] added demographics --- gem_city_spec.rb | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/gem_city_spec.rb b/gem_city_spec.rb index 37f3634..a636262 100644 --- a/gem_city_spec.rb +++ b/gem_city_spec.rb @@ -15,8 +15,8 @@ end it 'officers = thieves' do - city.thieves 1 - city.officers = 1 + city.thieves(1) + city.officers(1) expect(city.successful_crime_rate).to eq(0) end @@ -27,7 +27,7 @@ it 'officers > thieves' do city.thieves 1 - city.officers = 2 + city.officers 2 expect(city.successful_crime_rate).to eq(0) end end @@ -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 5be178c22ae3a7363953ed68f342e91950955b82 Mon Sep 17 00:00:00 2001 From: Joe Reich Date: Sun, 13 Dec 2015 13:27:18 -0500 Subject: [PATCH 4/6] added demographics --- gem_city.rb | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 gem_city.rb diff --git a/gem_city.rb b/gem_city.rb new file mode 100644 index 0000000..90754f7 --- /dev/null +++ b/gem_city.rb @@ -0,0 +1,57 @@ +# 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(officers_number = @people[:officers]) + @people[:officers] = officers_number + end + + def happiness_of_town + # happiness is random... people don't know what they want! + happiness_vals = [] + happiness = 0 + @population.times do + happiness_vals.push(rand((100 - successful_crime_rate)..100)) + 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 || officers > thieves + odds_percent = 0 + else + odds = 1 - officers.to_f / thieves.to_f + odds_percent = odds * 100 + end + odds_percent + end + + def city_demographics + demographics = {} + @people.collect do |type, _number| + demographics[type] = "#{percent_of_pop(type)}%" + end + civilians_pc = 100 - (percent_of_pop(:officers) + percent_of_pop(:thieves)) + demographics[:civilians] = "#{civilians_pc}%" + demographics + end + + def percent_of_pop(type) + (100 * @people[type] / population.to_f).round + end +end From c83c0d3a93ff0b7dbb963aa9423ea83acb07c2bc Mon Sep 17 00:00:00 2001 From: Joe Reich Date: Sun, 13 Dec 2015 13:29:52 -0500 Subject: [PATCH 5/6] added demographics --- gem_city.rb | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/gem_city.rb b/gem_city.rb index 90754f7..58a04d0 100644 --- a/gem_city.rb +++ b/gem_city.rb @@ -2,20 +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(thieves_number = @people[:thieves]) - @people[:thieves] = thieves_number - end - - def officers(officers_number = @people[:officers]) - @people[:officers] = officers_number - end - def happiness_of_town # happiness is random... people don't know what they want! happiness_vals = [] @@ -30,8 +23,6 @@ def happiness_of_town end def successful_crime_rate - thieves = @people[:thieves] - officers = @people[:officers] if thieves <= 0 || officers > thieves odds_percent = 0 else @@ -46,7 +37,7 @@ def city_demographics @people.collect do |type, _number| demographics[type] = "#{percent_of_pop(type)}%" end - civilians_pc = 100 - (percent_of_pop(:officers) + percent_of_pop(:thieves)) + civilians_pc = 100 - (percent_of_pop(officers) + percent_of_pop(thieves)) demographics[:civilians] = "#{civilians_pc}%" demographics end From 7bd5302988854c4643dbd4f3209f49391cf1fd82 Mon Sep 17 00:00:00 2001 From: Joe Reich Date: Sun, 13 Dec 2015 13:53:41 -0500 Subject: [PATCH 6/6] More refactoring --- gem_city.rb | 42 ++++++++++++++++++++---------------------- gem_city_spec.rb | 16 ++++++++-------- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/gem_city.rb b/gem_city.rb index 58a04d0..ea903b5 100644 --- a/gem_city.rb +++ b/gem_city.rb @@ -2,47 +2,45 @@ # 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 + attr_accessor :thieves, :officers, :civilians def initialize @thieves = 5 @officers = 1 @population = 50 end + def civilians + population - (officers + thieves) + end + def happiness_of_town - # happiness is random... people don't know what they want! - happiness_vals = [] - happiness = 0 + overall_happiness = 0 @population.times do - happiness_vals.push(rand((100 - successful_crime_rate)..100)) - end - happiness_vals.each do |value| - happiness += value + overall_happiness += individual_happiness end - happiness / 100 + overall_happiness / 100 + end + + def individual_happiness + # happiness is random... people don't know what they want! + rand((100 - successful_crime_rate)..100) end def successful_crime_rate - if thieves <= 0 || officers > thieves - odds_percent = 0 + if officers > thieves || thieves <= 0 + 0 else - odds = 1 - officers.to_f / thieves.to_f - odds_percent = odds * 100 + (1 - officers / thieves.to_f) * 100 end - odds_percent end def city_demographics - demographics = {} - @people.collect do |type, _number| - demographics[type] = "#{percent_of_pop(type)}%" - end - civilians_pc = 100 - (percent_of_pop(officers) + percent_of_pop(thieves)) - demographics[:civilians] = "#{civilians_pc}%" - demographics + { officers: "#{ percent_of_pop (officers) }%", + thieves: "#{ percent_of_pop (thieves) }%", + civilians: "#{ percent_of_pop (civilians) }%" } end def percent_of_pop(type) - (100 * @people[type] / population.to_f).round + (100 * type / population.to_f).round end end diff --git a/gem_city_spec.rb b/gem_city_spec.rb index a636262..2eb3643 100644 --- a/gem_city_spec.rb +++ b/gem_city_spec.rb @@ -15,19 +15,19 @@ end it 'officers = thieves' do - city.thieves(1) - city.officers(1) + city.thieves = 1 + city.officers = 1 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.officers 2 + city.thieves = 1 + city.officers = 2 expect(city.successful_crime_rate).to eq(0) end 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 @@ -52,8 +52,8 @@ end it 'thieves = 10, officers = 25' do - city.thieves 10 - city.officers 25 + city.thieves = 10 + city.officers = 25 demographics = city.city_demographics expect(demographics[:thieves]).to eq('20%') expect(demographics[:officers]).to eq('50%')