|
1 | | - // A simple Person contract |
| 1 | +// A simple Person contract |
2 | 2 | // |
3 | 3 | // reference: https://developers.flow.com/cadence/language/contracts |
4 | 4 | pub contract Person { |
5 | 5 | // declaration of a public variable |
6 | 6 | pub var name: String |
7 | 7 | // declaration of a private variable, reference: https://developers.flow.com/cadence/language/access-control#access-control |
8 | | - access(self) var mood: Mood |
9 | | - |
10 | | - // Enums type declaration, reference: https://developers.flow.com/cadence/language/enumerations#enum-declaration |
11 | | - pub enum Mood: UInt8 { |
12 | | - pub case happy |
13 | | - pub case angry |
14 | | - pub case melanholic |
15 | | - } |
| 8 | + access(self) var happy: Bool |
16 | 9 |
|
17 | 10 | // initialization method for our contracts, this gets run on deployment |
18 | 11 | init() { |
19 | 12 | self.name = "Alice" |
20 | | - self.mood = Mood.happy |
| 13 | + self.happy = true |
21 | 14 | } |
22 | 15 |
|
23 | 16 | pub fun hello(): String { |
24 | 17 | log("saying hello") // when we log we can see the log in the emulator output, helpful for debugging |
25 | 18 | |
26 | | - switch self.mood { |
27 | | - case Mood.happy: |
28 | | - return "😍 Hello!" |
29 | | - case Mood.angry: |
30 | | - return "😤 Go away!" |
31 | | - case Mood.melanholic: |
32 | | - return "😐 Hi I guess?" |
33 | | - default: |
34 | | - return "💩 Not sure!" |
35 | | - } |
| 19 | + return self.happy == true ? "😍 Hello!" : "😤 Go away!" |
36 | 20 | } |
37 | 21 |
|
38 | 22 | pub fun WhoAmI(): String { |
39 | 23 | return "I am ".concat(self.name) |
40 | 24 | } |
41 | 25 |
|
42 | | - // set a new name |
43 | | - pub fun setName(name: String) { |
44 | | - self.name = name |
| 26 | + pub fun changeMyMood() { |
| 27 | + self.happy = !self.happy |
45 | 28 | } |
46 | 29 |
|
| 30 | + // create a new friendship resource |
| 31 | + pub fun makeFriends(): @Friendship { |
| 32 | + return <-create Friendship() |
| 33 | + } |
| 34 | + |
47 | 35 | // Friendship resource are types of values that can only exist in one place |
48 | 36 | // |
49 | 37 | // read more about this unique and powerful Cadence feature here https://developers.flow.com/cadence/language/resources |
50 | 38 | pub resource Friendship { |
51 | | - |
| 39 | + pub var strength: UInt64 |
| 40 | + |
| 41 | + init() { |
| 42 | + self.strength = unsafeRandom() |
| 43 | + } |
52 | 44 | } |
53 | | - |
54 | | - } |
| 45 | +} |
55 | 46 |
|
56 | 47 |
|
| 48 | + |
0 commit comments