-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDZ_9
More file actions
105 lines (85 loc) · 2.89 KB
/
DZ_9
File metadata and controls
105 lines (85 loc) · 2.89 KB
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
import "hardhat/console.sol";
pragma solidity >0.8.2;
library StringComparer{
function compare(string memory str1, string memory str2) public pure returns (bool) {
return keccak256(abi.encodePacked(str1)) == keccak256(abi.encodePacked(str2));
}
}
abstract contract Animal{
string name;
constructor(string memory _name){
name = _name;
}
function eat(string calldata _food) virtual public returns(string memory){
return "Animals eats";
}
function sleep() public view returns(string memory){
return "Z-z-z-z-z-z-z-z";
}
function speak() virtual public view returns(string memory){
return "Animals speak";
}
}
abstract contract Herbivore is Animal{
string canEat = "plant";
function eat(string calldata _food) override virtual view public returns (string memory){
require(StringComparer.compare(_food, canEat), "Herbivore cannot eat this!");
return "Nom-nom";
}
}
abstract contract Carnivore is Animal{
string eatsMeat = "meat";
function eat(string calldata _food) override virtual view public returns (string memory){
require(StringComparer.compare(_food, eatsMeat), "Carnivore cannot eat this!");
return "Nom-nom";
}
}
abstract contract Omnivore is Animal{
string eatsPlant = "plant";
string eatsMeat = "meat";
string cantEatChocolate = "chocolate";
function eat(string calldata _food) override virtual view public returns (string memory){
require(StringComparer.compare(_food, eatsPlant) || StringComparer.compare(_food, eatsMeat) || StringComparer.compare(_food, cantEatChocolate), "Omnivore cannot eat this!");
return "Nom-nom";
}
}
contract Horse is Herbivore{
constructor(string memory _name) Animal(_name){
}
function speak() override view public returns (string memory){
return "I-go-go";
}
}
contract Cow is Herbivore{
constructor(string memory _name) Animal(_name){
}
function speak() override view public returns (string memory){
return "Moooooooo";
}
}
contract Dog is Carnivore{
string eatsPlant = "plant";
constructor(string memory _name) Animal(_name){
}
function eat(string calldata _food) override view public returns (string memory){
require(!StringComparer.compare(_food, "chokolate"), "Dogs cannot eat chocolate!");
if(StringComparer.compare(_food, eatsMeat) || StringComparer.compare(_food,eatsPlant)){
return "Nom-nom";
}
revert("Dog cannot eat this");
}
function speak() override view public returns (string memory){
return "Gav-gav-gav";
}
}
contract Wolf is Carnivore{
constructor(string memory _name) Animal(_name){
}
function speak() override view public returns (string memory){
return "A-wooooooo";
}
}
contract Pig is Omnivore{
constructor(string memory _name) Animal(_name){
}
}