-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticket reservation.cpp
182 lines (151 loc) · 3.06 KB
/
ticket reservation.cpp
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
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
#include <assert.h>
using namespace std ;
class Ticket
{
public:
string name;
int number;
};
class Ticket_Manager
{
public:
bool cancel( int Number )
{
for(auto it = TICKET.begin() ; it != TICKET.end() ; it++)
{
if(Number == it->number){
TICKET.erase(it);
return true ;
}
}
return false ;
}
bool cancel( string Name )
{
for(auto it = TICKET.begin() ; it != TICKET.end() ; it++)
{
if(Name == it->name){
TICKET.erase(it);
return true ;
}
}
return false ;
}
bool check( string Name )
{
for(int i=0; i<TICKET.size(); i++)
{
if(Name == TICKET[i].name){
return true ;
}
}
return false ;
}
bool check( int Number )
{
for(int i=0; i<TICKET.size(); i++)
{
if(Number == TICKET[i].number){
return true ;
}
}
return false ;
}
bool reserve( int Number , string Name )
{
Ticket T;
T.name = Name;
T.number = Number;
if(check(Number)){
return false ;
}
if(check(Name)){
cancel(Name);
}
TICKET.push_back(T);
return true ;
}
void show_ticket()
{
for(auto it = TICKET.begin() ; it != TICKET.end() ; it++)
{
cout<< it->number <<" ";
cout << it->name<< endl;
}
}
private:
vector<Ticket> TICKET;
};
bool if_string(string str)
{
assert(str.size() > 0);
return isalpha(str[0]);
}
int main()
{
int n;
string str1 , str2 ,dastoor ;
Ticket_Manager rojin;
cin >> n ;
for(int i=0 ; i<n ; i++)
{
string s;
int num;
cin>> dastoor;
if( dastoor == "reserve" ){
cin>> str1 >> str2;
if(if_string(str1)){
s = str1;
num = stoi(str2);
}else{
s = str2;
num = stoi(str1);
}
if(rojin.reserve(num , s)){
cout<< "done"<<endl;
}else{
cout<<"seat not available"<<endl;
}
}
else if(dastoor == "check"){
cin>> str1 ;
if(if_string(str1)){
if(rojin.check(str1)){
cout<< "yes"<<endl;
}else{
cout<<"no"<<endl;
}
}else{
num = stoi(str1);
if(rojin.check(num)){
cout<< "yes"<<endl;
}else{
cout<<"no"<<endl;
}
}
}else if(dastoor == "cancel"){
cin>> str1;
if(if_string(str1)){
if(rojin.cancel(str1)){
cout<<"done"<<endl;
}else{
cout<<"ticket not found"<<endl;
}
}else{
num = stoi(str1);
if(rojin.cancel(num)){
cout<<"done"<<endl;
}else{
cout<<"ticket not found"<<endl;
}
}}else{
throw runtime_error("dastoor eshtebahee!!");
}
}
rojin.show_ticket();
return 0;
}