-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.sol
69 lines (58 loc) · 1.66 KB
/
complex.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Complex {
enum Gender {
Male,
Female
}
struct People {
string name;
uint8 age;
Gender gender;
}
People[] peoples;
function getAllPeople() external view returns (People[] memory) {
return peoples;
}
function pushPeople(People memory person) external {
peoples.push(person);
}
function pushPeopleArray(People[] memory persons) external {
for (uint i = 0; i < persons.length; i++) {
peoples.push(persons[i]);
}
}
function pushGen(Gender gen) external view returns (string[] memory) {
uint count = 0;
for (uint i = 0; i < peoples.length; i++) {
if (peoples[i].gender == gen) {
count++;
}
}
string[] memory result = new string[](count);
uint index = 0;
for (uint i = 0; i < peoples.length; i++) {
if (peoples[i].gender == gen) {
result[index] = peoples[i].name;
index++;
}
}
return result;
}
function getGender() external pure returns (Gender) {
return Gender.Male;
}
function getPerson() external view returns (People memory) {
if (peoples.length == 0) {
revert("No people available");
}
return peoples[0];
}
function getAllNames() external view returns (string[] memory) {
string[] memory names = new string[](peoples.length);
for (uint i = 0; i < peoples.length; i++) {
names[i] = peoples[i].name;
}
return names;
}
}