Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

followed srp principle. #4

Open
wants to merge 1 commit into
base: poodr02
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
26 changes: 4 additions & 22 deletions bike.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
# Bike
require_relative 'bike_cargo'

class Bike

STANDARD_WEIGHT = 200 # lbs
MAX_CARGO_ITEMS = 10

attr_accessor :id, :color, :price, :weight, :rented, :cargo_contents
attr_accessor :id, :color, :price, :weight, :rented, :cargo_contents, :bike_cargo

def initialize(id, color, price, weight = STANDARD_WEIGHT, rented = false)
@id = id
@color = color
@price = price
@weight = weight
@rented = rented
@cargo_contents = []
@bike_cargo = BikeCargo.new
end

def rent!
self.rented = true
end

def add_cargo(item)
self.cargo_contents << item
end

def remove_cargo(item)
self.cargo_contents.remove(item)
end

def pannier_capacity
MAX_CARGO_ITEMS
end

def pannier_remaining_capacity
MAX_CARGO_ITEMS - self.cargo_contents.size
end

end
end
27 changes: 27 additions & 0 deletions bike_cargo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class BikeCargo
MAX_CARGO_ITEMS = 10

attr_accessor :cargo_contents

def initialize
@cargo_contents = []
end

def add_cargo(item)
if pannier_remaining_capacity > 0
@cargo_contents << item
end
end

def remove_cargo(item)
@cargo_contents.remove(item)
end

def pannier_capacity
MAX_CARGO_ITEMS
end

def pannier_remaining_capacity
MAX_CARGO_ITEMS - @cargo_contents.size
end
end
11 changes: 6 additions & 5 deletions main.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
!# /usr/bin/env ruby
# /usr/bin/env ruby

require_relative 'bike'
require_relative 'bike_cargo'

bike = Bike.new(1, :pink, 99.99)

bike.add_cargo(:apples)
bike.add_cargo(:water)
bike.add_cargo(:repair_kit)
bike.bike_cargo.add_cargo(:apples)
bike.bike_cargo.add_cargo(:water)
bike.bike_cargo.add_cargo(:repair_kit)

puts "Space for #{bike.pannier_remaining_capacity} items left."
puts "Space for #{bike.bike_cargo.pannier_remaining_capacity} items left."

bike.rent!