-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.FancyShirts.sol
75 lines (64 loc) · 1.56 KB
/
5.FancyShirts.sol
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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;
contract FancyShirts {
enum Size {
Small,
Medium,
Large
}
enum Color {
Red,
Green,
Blue
}
struct Shirt {
Size size;
Color color;
}
mapping(address => Shirt[]) shirts;
modifier correctAmount(Size size, Color color) {
require(
getShirtPrice(size, color) == msg.value,
"Incorrect amount sent"
);
_;
}
function getShirtPrice(Size size, Color color)
public
pure
returns (uint256)
{
uint256 price;
if (size == Size.Small) {
price += 10;
} else if (size == Size.Medium) {
price += 15;
} else {
price += 20;
}
if (color != Color.Red) {
price += 5;
}
return price;
}
function buyShirt(Size size, Color color)
public
payable
correctAmount(size, color)
{
Shirt memory shirt = Shirt(size, color);
shirts[msg.sender].push(shirt);
}
function getShirts(Size size, Color color) public view returns (uint256) {
uint256 count;
for (uint256 idx; idx < shirts[msg.sender].length; idx++) {
if (
shirts[msg.sender][idx].size == size &&
shirts[msg.sender][idx].color == color
) {
count++;
}
}
return count;
}
}