From b975cea987ee3f43af9e7e11bdabfc8c4cfaf6a4 Mon Sep 17 00:00:00 2001 From: Dominic Michalec Date: Sun, 13 Mar 2016 21:53:05 -0400 Subject: [PATCH] finish GemCity --- GemCity.rb | 56 --------------------------------------- Gemfile | 2 +- Gemfile.lock | 18 ++++++------- gem_city.rb | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ gem_city_spec.rb | 51 ++++++++++++++++++------------------ 5 files changed, 104 insertions(+), 91 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/Gemfile.lock b/Gemfile.lock index 771b310..5a3838f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,12 +1,10 @@ GEM remote: https://rubygems.org/ specs: - ast (2.0.0) - astrolabe (1.3.1) - parser (~> 2.2) + ast (2.2.0) diff-lcs (1.2.5) - parser (2.2.2.6) - ast (>= 1.1, < 3.0) + parser (2.3.0.1) + ast (~> 2.2) powerpack (0.1.1) rainbow (2.0.0) rspec (3.3.0) @@ -22,12 +20,11 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.3.0) rspec-support (3.3.0) - rubocop (0.32.1) - astrolabe (~> 1.3) - parser (>= 2.2.2.5, < 3.0) + rubocop (0.36.0) + parser (>= 2.3.0.0, < 3.0) powerpack (~> 0.1) rainbow (>= 1.99.1, < 3.0) - ruby-progressbar (~> 1.4) + ruby-progressbar (~> 1.7) ruby-progressbar (1.7.5) PLATFORMS @@ -36,3 +33,6 @@ PLATFORMS DEPENDENCIES rspec rubocop + +BUNDLED WITH + 1.10.6 diff --git a/gem_city.rb b/gem_city.rb new file mode 100644 index 0000000..c4d7935 --- /dev/null +++ b/gem_city.rb @@ -0,0 +1,68 @@ +class TypeError < StandardError; end + +# start GemCity class +class GemCity + attr_reader :thieves, :officers, :population, :civilians + # This class represents the town of GemCity + # This is a town riddled with crime but we can find out how happy the town is + + def initialize + @thieves = 5 + @officers = 1 + @population = 50 + @civilians = @population - @thieves - @officers + end + + def thieves=(number) + fail TypeError, 'Input must be an integer' unless number.is_a? Integer + @thieves = number + end + + def officers=(number) + fail TypeError, 'Input must be an integer' unless number.is_a? Integer + @officers = number + end + + def civilians=(number) + fail TypeError, 'Input must be an integer' unless number.is_a? Integer + @civilians = number + end + + def population=(number) + fail TypeError, 'Input must be an integer' unless number.is_a? Integer + @population = number + end + + def demographics + @demographics = { 'thieves' => rounded_percentage(thieves), + 'officers' => rounded_percentage(officers), + 'civilians' => rounded_percentage(civilians) } + end + + def happiness_of_town + # happiness is random... people don't know what they want! + happiness_values = [] + happiness = 0 + counter = 0 + while counter < population + happiness_values.push(rand((100 - successful_crime_rate)..100)) + counter += 1 + end + happiness_values.each { |value| happiness += value } + happiness / 100 + end + + def successful_crime_rate + thieves <= 0 || officers > thieves ? 0 : odds_percent + end + + private + + def odds_percent + (1 - officers.to_f / thieves.to_f) * 100 + end + + def rounded_percentage(person_type) + ((person_type.to_f / population.to_f) * 100).to_i.to_s + '%' + end +end diff --git a/gem_city_spec.rb b/gem_city_spec.rb index 884b3cb..e4ac547 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 } @@ -15,49 +15,50 @@ end it 'officers = thieves' do - city.thieves 1 - city.setOfficers 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.setOfficers 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 context 'happiness' do it 'on initialize' do - expect((10..50).include? city.happiness_of_town).to eq(true) + expect((10..50).cover?(city.happiness_of_town)).to eq(true) 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.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 + city.civilians = 15 + demographics = city.demographics + expect(demographics['thieves']).to eq('20%') + expect(demographics['officers']).to eq('50%') + expect(demographics['civilians']).to eq('30%') + end + end end