tags:

views:

38

answers:

2

I need advice on how to implement. I have two objects ReceiptLine and Discount. Cashier scans item and a receiptline object is added. If the added ReceiptLine object have a Discount Id then, it has to lookup in Discount object and issues discount after validation. I have problem in validation. Here is the situation, cashier scans the item as follows

ReceiptLine Objects

line1 ItemId 1835 qty 2 DiscountId 23 line2 ItemId 1515 qty 2 DiscountId 23 line3 ItemId 1835 qty 2 DiscountId 23

Discount Object Id 23

ItemId 1835 Buy 2 ItemId 1515 Buy 1

Issue Discount $1.00

Situations that Code should Handle

Cashier can scan items in any order Items Quantity can be any amount Discount only allowed if it meets Discount object requirements If the purchase goes twice or any multiple times then the discount also goes that many times

I have the following code, and don't have a clue where to go from there

Public Sub GetDiscount(ByVal newReceiptLine As ReceiptLine)
        Dim discountId As Integer = newReceiptLine.DiscountId
        Dim discountLine As ReceiptLine = Me.Find(Function(l As ReceiptLine) l.DiscountId = discountId)
        If discountLine IsNot Nothing AndAlso discountLine.Discount IsNot Nothing Then
            newReceiptLine.Discount = discountLine.Discount
        Else
            newReceiptLine.Discount = New Discount(discountId)
        End If
        newReceiptLine.Discount.IssueDiscount(Me)
End Sub

Public Sub IssueDiscount(ByVal receiptLines As ReceiptLines)
        Dim discountLines As List(Of ReceiptLine) = receiptLines.FindAll(Function(l As ReceiptLine) l.DiscountId = _id)
        For Each line As ReceiptLine In discountLines
            Dim styleId As Integer = line.StyleId
            Dim item As DiscountItem = _discountItems.Find(Function(i As DiscountItem) i.StyleId = styleId)
           ??????
        Next
End Sub
A: 

I recommend applying all of the discounts in one process. What happens if the customer decides that they don't want the second item after all?

If I was doing this I'd have an ApplyDiscounts method:

  1. Clear all existing discounts. Unflag all items as discounted.
  2. Go through each item in the receipt.
  3. ...Look for a discount for the item in the discounts table.
  4. ...Check the condition for a that discount.
  5. ...If the condition is satisfied in the customer's receipt:
  6. ...... Flag all of the participating items as Discounted (so that you don't count them twice)
  7. ...... Add the discount to the receipt.

Because you clear the discounts at the start of the process, you can run this process each time an item is scanned. This way it stays dynamic, but very easy to manage.

I would also not have a DiscountID in the Items. The items should be independant. The Discounts should have an item id. That way you can add and remove discount scenarios as much as you want without having to touch your items.

Michael Rodrigues
A: 

Thanks!

I am having problem in checking the condition. The discount condition is

ItemId 1835 Buy 2 ItemId 1515 Buy 1 Issue Discount $1.00

As you said, while I am looping the receiptlines I will check the discount object, then i will validate that there are 2 - 1835 and 1 - 1515 and issue discount $1.00.

My code issues discount for all 1835 even if there is 1 - 1515 in receipt.

Muthu Annamalai