-
Notifications
You must be signed in to change notification settings - Fork 6
/
PetSanctuary.sol
130 lines (102 loc) · 3.9 KB
/
PetSanctuary.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
pragma solidity >=0.4.22 <0.6.2;
contract PetSanctuary {
address owner;
uint durationTime;
mapping (string => bool) allowedPets;
mapping (string => uint) pets;
string[] private allPets;
string[] private allCustomers;
struct Customer {
string gender;
uint age;
string pet;
uint index;
uint time;
}
mapping (string => Customer) private Customers;
constructor() public {
owner = msg.sender;
allowedPets["fish"] = true;
allowedPets["cat"] = true;
allowedPets["dog"] = true;
allowedPets["parrot"] = true;
allowedPets["rabbit"] = true;
pets["fish"] = 0;
pets["cat"] = 0;
pets["dog"] = 0;
pets["parrot"] = 0;
pets["rabbit"] = 0;
}
function isCustomer(string memory name)
internal
returns(bool)
{
if(allCustomers.length == 0) return false;
return keccak256(bytes(name)) == keccak256(bytes(allCustomers[Customers[name].index]));
}
modifier onlyOwner() {
require(owner == msg.sender, "Only Shop Owner is authorized");
_;
}
modifier onlyAllowed(string memory animal) {
require(allowedPets[animal], "Animal provided is not allowed");
_;
}
modifier inTime(string memory customer) {
durationTime = Customers[customer].time + 5 minutes;
require(now <= durationTime, "Out of Time: Deadline to return your pet has been reached");
_;
}
function addPets(string memory animal, uint amount) public onlyOwner onlyAllowed(animal) {
pets[animal] += amount;
}
function buyPet(string memory customer,
uint cAge,
string memory cGender,
string memory animal)
public onlyAllowed(animal)
returns(bool successfulTransaction) {
require(pets[animal] > 0, "Animal is no longer in stock");
if (isCustomer(customer)) {
require(keccak256(bytes(Customers[customer].pet)) == keccak256(bytes("RETURNED")),
"Only One pet for a Lifetime");
} else if (keccak256(bytes(cGender)) == keccak256(bytes("male"))) {
require(keccak256(bytes(animal)) == keccak256(bytes("dog"))
|| keccak256(bytes(animal)) == keccak256(bytes("fish")),
"You, Sir, are only allowed a pet Dog or Fish");
Customers[customer].index = allCustomers.push(customer)-1;
} else if (keccak256(bytes(cGender)) == keccak256(bytes("female")) && cAge < 40) {
require(keccak256(bytes(animal)) != keccak256(bytes("cat")), "You, Madam, are not allowed a Cat yet");
Customers[customer].index = allCustomers.push(customer)-1;
}
Customers[customer].gender = cGender;
Customers[customer].age = cAge;
Customers[customer].pet = animal;
Customers[customer].time = block.timestamp;
pets[animal]--;
return true;
}
function returnPet(string memory customer, string memory pet) inTime(customer) public onlyAllowed(pet)
returns(bool successfulReturn) {
require(isCustomer(customer), "Must be a previous customer");
require(keccak256(bytes(Customers[customer].pet)) == keccak256(bytes(pet)), "Returning Pet does not match animal purchased");
Customers[customer].pet = "RETURNED";
pets[pet]++;
return true;
}
function getReceipt(string memory customer) public view returns(uint cAge,
string memory cGender,
string memory pet,
uint timeOfPurchase) {
return (Customers[customer].age,
Customers[customer].gender,
Customers[customer].pet,
Customers[customer].time);
}
function getPetCount(string memory animal) public view returns(uint amount) {
return(pets[animal]);
}
function getInventory() public view returns(uint amount) {
return(pets["fish"] + pets["cat"] + pets["dog"] + pets["parrot"] + pets["rabbit"]);
}
}