Refactoring
This commit is contained in:
37
app/models/battery.rb
Normal file
37
app/models/battery.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
class Battery
|
||||
|
||||
attr_accessor :battery_kwh, :battery_max_kwh
|
||||
|
||||
def initialize(battery_capacity=10.0)
|
||||
@battery_kwh = 0.0
|
||||
@battery_max_kwh = battery_capacity
|
||||
end
|
||||
|
||||
def charge(kwh)
|
||||
return 0.0 if kwh.nil?
|
||||
if battery_kwh + kwh <= battery_max_kwh
|
||||
@battery_kwh += kwh.to_f
|
||||
#p "Battery is now at %s kwh" % battery_kwh
|
||||
return kwh
|
||||
else
|
||||
old_kwh = battery_kwh
|
||||
battery_kwh = battery_max_kwh
|
||||
#p "Battery is now at %s kwh" % battery_kwh
|
||||
return (battery_max_kwh-old_kwh)
|
||||
end
|
||||
end
|
||||
|
||||
def discharge(kwh)
|
||||
return 0.0 if kwh.nil?
|
||||
if battery_kwh > kwh.to_f
|
||||
@battery_kwh -= kwh.to_f
|
||||
#p "Battery is now at %s kwh" % battery_kwh
|
||||
return kwh
|
||||
else
|
||||
old_kwh = battery_kwh
|
||||
battery_kwh = 0.0
|
||||
#p "Battery is now at %s kwh" % battery_kwh
|
||||
return old_kwh
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user