diff --git a/001-Labwork/lib/customer.rb b/001-Labwork/lib/customer.rb new file mode 100644 index 0000000..eda5acb --- /dev/null +++ b/001-Labwork/lib/customer.rb @@ -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 \ No newline at end of file diff --git a/001-Labwork/lib/orderprocessor.rb b/001-Labwork/lib/orderprocessor.rb new file mode 100644 index 0000000..4d38d87 --- /dev/null +++ b/001-Labwork/lib/orderprocessor.rb @@ -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