Recap: Receipts
Lesson 36 of 48 in Coddy's Ruby for Lua Developers course.
Challenge
EasyDefine Receipt(items), storing integer prices, and total returning their sum. DiscountReceipt inherits Receipt, initialized with items and discount, and total returns parent total minus discount, floored at zero. Input is discount then prices.
Return text from solve(input) without printing. The locked driver prints the returned text with one final newline and checks required public APIs independently.
Your code goes in exercise.rb, receipt.rb, discount_receipt.rb. main.rb holds the supplied input/output code and cannot be edited.
Try it yourself
require_relative 'exercise'
require_relative 'receipt'
require_relative 'discount_receipt'
def check(value)
raise 'Public API check failed' unless value
end
check(Receipt.new([3, 7]).total == 10)
check(DiscountReceipt.new([3, 7], 2).total == 8)
check(DiscountReceipt.new([], 4).is_a?(Receipt))
puts solve(STDIN.read)
All lessons in Ruby for Lua Developers
1Start with Ruby
Run Ruby FilesOutput and CommentsNames and ObjectsInteger ArithmeticTruth and Boolean LogicString OperationsSymbols and KeysRecap: Packing Slip2Flow and Methods
Conditional ValuesCase and RangesLoops and Early ExitMethods and ReturnsDefaults and KeywordsRecap: Sensor Run5Objects and State
Modules as NamespacesClasses and InstancesReaders and WritersValidated ConstructionRecap: Wallet8Lending Desk Project
Lending Desk: CatalogLending Desk: Borrow3Collections and Blocks
Arrays and IndexingHashes and Missing KeysYielding to a BlockMap and SelectReduce and PredicatesRecap: Repeated Codes6Composition and Dispatch
Composing ObjectsInheritance and OverridesExtending with SuperModules as MixinsDuck-Typed CollaboratorsRecap: ReceiptsPractice on your own: Online Ruby compiler