- Build a class with instance methods.
- Call instance methods inside of other instance methods.
- Use instance methods to track information pertinent to an instance of a class.
We're going to create an Object Oriented Cash Register that can:
- Add items of varying quantities and prices
- Calculate discounts
- Keep track of what's been added to it
- Void the last transaction
This is a test-driven lab! You will need to read the spec file and the test output very carefully to solve this one.
Hint #1: Keep in mind that to call an instance method inside another instance method, we use the self
keyword to refer to the instance on which we are operating. For example:
class Person
attr_accessor :age
def initialize(age)
@age = 0
end
def birthday
self.age += 1
end
end
Follow along with the specs in spec/cash_register_spec.rb
. They have been set to --fail-fast
, so by completing one test at a time, you'll end up with a relatively complex class at the end.
Take it one step at a time!
Hint #2: The void_last_transaction
method will remove the last transaction from the total. You'll need to make an additional attribute accessor and keep track of that last transaction amount somehow. In what method of the class are you working with an individual item?
View Object Oriented Cash Register on Learn.co and start learning to code for free.