Skip to content

Instantly share code, notes, and snippets.

@Rosa-Fox
Created September 18, 2015 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rosa-Fox/32a5cfbd824e6d46406c to your computer and use it in GitHub Desktop.
Save Rosa-Fox/32a5cfbd824e6d46406c to your computer and use it in GitHub Desktop.
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