Skip to content

Instantly share code, notes, and snippets.

@Rosa-Fox
Created Sep 18, 2015
Embed
What would you like to do?
Bill Calculator - OOP approach
class Tip
attr_reader :tip
def initialize(bill, tip)
@bill = bill
@tip = tip
end
def get_amount
@bill_amount = @bill.to_i
if @bill_amount == 0
puts "Please specify a valid number for the bill amount:"
end
if @bill_amount < 0
puts "Please specify a positive number"
end
@bill_amount
end
def total_amount
tip = @bill_amount / 100 * @tip
total = @bill_amount + tip
total
end
end
t = Tip.new(100, 50)
puts "The bill is £#{t.get_amount}"
puts "The tip is #{t.tip}%"
puts "The total amount is £#{t.total_amount}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment