-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cairo
169 lines (142 loc) · 4.49 KB
/
main.cairo
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// find out how to choose between <org and attendee>
// figure out how access would be granted
use option::OptionTrait;
use array::ArrayTrait;
use debug::PrintTrait;
// create a structure for an event
#[derive(Copy,Drop)]
struct Event{
name:felt252,
description:felt252,
date:felt252,
time:felt252,
location:felt252,
ticket_price:u16,
available_tickets:u16,
}
#[derive(Copy,Drop)]
struct User{
username:felt252,
address:felt252,
usertype:UserType,
}
// structure to store array of events
#[derive(Drop)]
struct EventsStore{
events:Array<Event>,
users:Array<User>
}
#[derive(Copy,Drop)]
enum UserType{
Organization:(),
Attendee:(),
}
trait EventTrait{
fn purchase_ticket(ref self:Event,price:u16);
fn display_info(ref self: Event);
}
trait EventsStoreTrait{
fn add_to_store(ref self:EventsStore,event:Event);
fn add_user(ref self:EventsStore,user:User);
fn search_user(self:@EventsStore,username:felt252);
fn search_events(self:@EventsStore,name:felt252);
fn display_events(self:@EventsStore);
}
impl EventImpl of EventTrait {
fn purchase_ticket(ref self:Event,price:u16){
if self.available_tickets>0{
'sold'.print();
'access granted'.print();
let mut remaining_tickets=self.available_tickets-1;
remaining_tickets.print()
}
'sold out'.print()
}
fn display_info(ref self: Event){
self.name.print();
self.ticket_price.print();
}
}
impl EventsStoreImpl of EventsStoreTrait{
fn add_to_store(ref self:EventsStore,event:Event){
self.events.append(event);
}
// add the user to the database after creation
fn add_user(ref self:EventsStore,user:User){
let mut added_user=self.users.append(user);
}
fn search_user(self:@EventsStore,username:felt252){
let mut i =0;
loop {
let mut found_user:User=*self.users[i];
if found_user.username==username{
'user found'.print();
username.print();
found_user.address.print();
found_user.usertype.print();
break;
}
i=i+1;
}
}
fn search_events(self:@EventsStore,name:felt252){
let mut i = 0;
loop{
let found_event:Event=*self.events[i];
if found_event.name==name{
'event found'.print();
found_event.name.print();
break;
}
i=i+1;
}
}
fn display_events(self:@EventsStore){
let len = self.events.len();
let mut i = 0;
loop {
if (i >= len) {
break;
}
let mut event: Event = *self.events[i];
event.display_info(); // event.ticket_price.print();
i += 1;
}
}
}
impl ProcessUserTypeImpl of PrintTrait<UserType>{
fn print(self:UserType){
match self{
UserType::Organization(()) => {
'registered as organization'.print();
},
UserType::Attendee(()) => {
'registered as attendee'.print();
},
}
}
}
fn main(){
// create an instance
let mut new_event = Event{name:'Starknet hackathon',description:'best event ever ',
date:'23/10/2023', time:'4pm',location:'Nairobi,Kenya',ticket_price:2,available_tickets:0,};
let mut new_event2 = Event{name:'Cairo hackathon',description:'best event ever ',
date:'23/10/2023', time:'4pm',location:'Nairobi,Kenya',ticket_price:9,available_tickets:0,};
new_event.description.print();
new_event.purchase_ticket(2);
// add the event created to the array of events
let mut added_new_data=EventsStore{
events:ArrayTrait::new(),
users:ArrayTrait::new(),
};
added_new_data.add_to_store(new_event);
added_new_data.add_to_store(new_event2);
// added_new_data.events.len().print();
added_new_data.search_events('Starknet hackathon');
// create an instance of the user
let mut new_user = User{username:'Cecilia',address:'0x12345',usertype:UserType::Attendee(())};
added_new_data.add_user(new_user);
added_new_data.search_user('Cecilia');
added_new_data.display_events();
added_new_data.display_events();
}