Created
September 18, 2015 21:47
-
-
Save Rosa-Fox/32a5cfbd824e6d46406c to your computer and use it in GitHub Desktop.
Bill Calculator - OOP approach
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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