Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions 001-Labwork/lib/customer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class Customer
attr_reader :first_name, :last_name, :available_credit, :date_of_birth
attr_reader :purchases

def initialize first_name, last_name, date_of_birth, available_credit
@first_name = first_name
@last_name = last_name
@available_credit = available_credit
@date_of_birth = date_of_birth
@purchases = []
end

def full_name
"#{first_name} #{last_name}"
end

def age
Time.now.year - @date_of_birth.year
end

def use_credit(amount)
if @available_credit >= amount
@available_credit-=amount
true
else
false
end
end

def attempt_credit_purchase (amount, description)
if use_credit amount

# TODO: Ooops I forgot to store the item description in an array for the customer!
# You'll need to initialize the purchases array in the initialize method or
# check it here, how will I do this once Customer is a Class?
@purchases.push description

"APPROVED: $#{'%.2f' % amount} #{description}"
else
"DECLINED: $#{'%.2f' % amount} #{description} Purchase! Only $#{'%.2f' % available_credit} credit available"
end
end

def purchase_history
return @purchases.join "\\n"
end
end
29 changes: 29 additions & 0 deletions 001-Labwork/lib/orderprocessor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module OrderProcessor

def self.processOrder (customer, items)
receipt = []
receipt.push '=' * 80
receipt.push "\nOrder Receipt"
receipt.push '-------------'
receipt.push "Customer name: #{customer.full_name}"
receipt.push "Customer age: #{customer.age}"
receipt.push "Customer's opening credit limit: $#{'%.2f' % customer.available_credit}"


receipt.push "\nItems Requested"
receipt.push '---------------'
items.each do |item|
receipt.push customer.attempt_credit_purchase(item[:price], item[:description])
end

receipt.push "\nCustomer's closing credit limit is $#{'%.2f' % customer.available_credit}"
receipt.push "\nPurchase History"
receipt.push '----------------'
receipt.push customer.purchase_history
receipt.push ""
receipt.push '=' * 80

receipt.join("\n")
end

end