-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCartsModel.cs
83 lines (65 loc) · 2.22 KB
/
CartsModel.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StrataTest
{
public class Cart
{
public string Customer;
public string ProductCode;
public int Quantity;
public double UnitPrice;
}
internal class CartModel
{
public bool Add(string sCustomer, string sProductCode, int iQuantity, double dPrice)
{
if(iQuantity <= 0)
return false;
Cart cart =
(from c in Repository.Carts
where c.Customer == sCustomer && c.ProductCode == sProductCode
select c).SingleOrDefault();
if (cart == null)
{
// add
cart = new Cart() { Customer = sCustomer, ProductCode = sProductCode, Quantity = iQuantity, UnitPrice = dPrice };
Repository.Carts.Add(cart);
return true;
}
//update
cart.Quantity += iQuantity;
System.Diagnostics.Debug.Assert(cart.UnitPrice == dPrice);
return true;
}
public bool Delete(string sCustomer)
{
return Repository.Carts.RemoveAll(c => c.Customer == sCustomer) > 0;
}
public bool Delete(string sCustomer, string sProductCode)
{
return Repository.Carts.RemoveAll( c => c.Customer == sCustomer && c.ProductCode == sProductCode ) > 0;
}
public bool SetQuantity(string sCustomer, string sProductCode, int iQuantity)
{
if (iQuantity <= 0)
return false;
Cart cart =
(from c in Repository.Carts
where c.Customer == sCustomer && c.ProductCode == sProductCode
select c).SingleOrDefault();
if (cart == null)
return false;
cart.Quantity = iQuantity;
return true;
}
public IEnumerable<Cart> Get(string sCustomer)
{
return
from c in Repository.Carts
where c.Customer == sCustomer
select c;
}
}
}