forked from janhebnes/putn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShoppingService.cs
68 lines (59 loc) · 2.31 KB
/
ShoppingService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
namespace Putn
{
public class ShoppingService : IShoppingService
{
private readonly ILoggingService loggingService;
private readonly IItemRepository itemRepo;
private readonly IPaymentService paymentService;
private readonly IMemberRepository memberRepo;
public ShoppingService(ILoggingService loggingService,
IItemRepository itemRepo,
IPaymentService paymentService,
IMemberRepository memberRepo)
{
this.loggingService = loggingService;
this.itemRepo = itemRepo;
this.paymentService = paymentService;
this.memberRepo = memberRepo;
}
public void Checkout(IEnumerable<int> itemIDs, int memberID, string promoCode, DateTime when)
{
var member = this.memberRepo.FindByID(memberID);
if (member == null)
throw new MemberNotFoundException(memberID);
var items = this.itemRepo.FindByIDs(itemIDs) ?? new Item[]{};
// decide birthday discount
var birthdayDiscountPercentage = 0;
var isBirthday = member.Birthday.Month == when.Month && member.Birthday.Date == when.Date;
if (isBirthday)
{
birthdayDiscountPercentage = 50;
}
// decide promo discount
var promoDiscountPercentage = 0;
if (promoCode == "AM" && when.Hour < 12)
{
promoDiscountPercentage = 8;
}
else if (promoCode == "PM" && when.Hour >= 12)
{
promoDiscountPercentage = 6;
}
// decide applicable discount and create purchases
var discountToApply = Math.Max(birthdayDiscountPercentage, promoDiscountPercentage);
var totalPayable = items.Sum(item => {
if (item.IsDiscountable)
return item.Price * (100 - discountToApply) / 100;
else
return item.Price;
});
// log and persist
this.loggingService.Log(LogLevel.Info, $"We got member {member.Name} hooked!");
this.paymentService.Charge(memberID, totalPayable);
}
}
}