Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 0 additions & 56 deletions GemCity.rb

This file was deleted.

2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'https://rubygems.org'

gem 'rubocop', require: false
gem 'rspec'
gem 'rspec'
50 changes: 50 additions & 0 deletions gem_city.rb
Original file line number Diff line number Diff line change
@@ -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 }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be simpler if these weren't stuck together in @people. Try defining them as @thieves and @officers.

@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 = (1..@population).collect do
rand((100 - successful_crime_rate)..100)
end
happiness = happiness_vals.reduce(0, :+)
happiness / 100
end

def successful_crime_rate
thieves = @people[:thieves]
officers = @people[:officers]
if thieves <= 0 || officers > thieves
0
else
(1 - officers.to_f / thieves.to_f) * 100
end
end

def percentage_of_population(group)
format('%.0f%', (group.to_f / @population.to_f) * 100.0)
end

def city_demographics
civilians = @population - @people[:thieves] - @people[:officers]

{ thieves: percentage_of_population(@people[:thieves]),
officers: percentage_of_population(@people[:officers]),
civilians: percentage_of_population(civilians) }
end
end
40 changes: 20 additions & 20 deletions gem_city_spec.rb
Original file line number Diff line number Diff line change
@@ -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 }
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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