-
Notifications
You must be signed in to change notification settings - Fork 0
/
managetwoway.c
212 lines (182 loc) · 7.15 KB
/
managetwoway.c
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdio.h>
#include <string.h>
#define MAX_CUSTOMERS 100 // Define the maximum number of customers to be found
struct Customer {
int age;
char idpassport[10];
char fname[50];
char lname[50];
char phone[15];
char destination[30];
char time[10];
int day;
int month;
char returntime[10];
int returnday;
int returnmonth;
};
// Function to initialize the customer structure with default values
void initializeCustomer(struct Customer* customer) {
customer->age = 0;
strcpy(customer->idpassport, "");
strcpy(customer->fname, "");
strcpy(customer->lname, "");
strcpy(customer->phone, "");
strcpy(customer->destination, "");
strcpy(customer->time, "");
customer->day = 0;
customer->month = 0;
strcpy(customer->returntime, "");
customer->returnday = 0;
customer->returnmonth = 0;
}
// Function to search for customers by first name and passport number
int findCustomersByFirstNameAndPassport(const char* csvFileName, const char* firstName, const char* passportNumber, struct Customer* matchingCustomers) {
int numMatches = 0;
FILE* file = fopen(csvFileName, "r");
if (file == NULL) {
printf("Error opening file.\n");
return numMatches;
}
char buffer[200];
while (fgets(buffer, sizeof(buffer), file)) {
struct Customer currentCustomer;
initializeCustomer(¤tCustomer);
sscanf(buffer, "%d,%9[^,],%49[^,],%49[^,],%14[^,],%29[^,],%9[^,],%d,%d,%9[^,],%d,%d",
¤tCustomer.age,
currentCustomer.idpassport,
currentCustomer.fname,
currentCustomer.lname,
currentCustomer.phone,
currentCustomer.destination,
currentCustomer.time,
¤tCustomer.day,
¤tCustomer.month,
currentCustomer.returntime,
¤tCustomer.returnday,
¤tCustomer.returnmonth
);
if (strcmp(currentCustomer.fname, firstName) == 0 && strcmp(currentCustomer.idpassport, passportNumber) == 0) {
matchingCustomers[numMatches++] = currentCustomer;
}
}
fclose(file);
return numMatches;
}
int counttotalTwoWays(const char* csvFileName) {
int totalTwoWays = 0;
FILE* file = fopen(csvFileName, "r");
if (file == NULL) {
printf("Error opening file.\n");
return totalTwoWays;
}
char buffer[200];
while (fgets(buffer, sizeof(buffer), file)) {
totalTwoWays++;
}//count the number of lines in the file
fclose(file);
return totalTwoWays - 1;//subtract the header line
}
void displayBanner()
{
printf("\n\n\t\tPepeaKenya Management system.\n");
}
int main()
{
char csvFileName[] = "twowaybooking.csv";
char searchFirstName[50];
char searchPassport[10];
printf("\n\t----------PEPEAKENYA FLIGHT MANAGEMENT---------\n\n");
printf("\tEnter FIRST NAME : ");
scanf("%s", searchFirstName);
printf("\tEnter ID/Passport Number : ");
scanf("%s", searchPassport);
struct Customer matchingCustomers[MAX_CUSTOMERS];
for (int i = 0; i < MAX_CUSTOMERS; i++) {
initializeCustomer(&matchingCustomers[i]);
}
int numMatches = findCustomersByFirstNameAndPassport(csvFileName, searchFirstName, searchPassport, matchingCustomers);
if (numMatches == 0) {
printf("\n\tTicket NOT FOUND\tfirst name : %s | passport number: %s\n", searchFirstName, searchPassport);
} else {
printf("\n\t\t------------Tickets Found(%d)------------\n", numMatches);
for (int i = 0; i < numMatches; i++) {
struct Customer foundCustomer = matchingCustomers[i];
printf("\t\tNAME: %s %s\t\tAGE: %d\n", foundCustomer.fname, foundCustomer.lname, foundCustomer.age);
printf("\t\tID/PASSPORT NO: %s\n", foundCustomer.idpassport);
printf("\t\tDEPARTURE DATE: %d/%d/2023\n", foundCustomer.day, foundCustomer.month);
printf("\t\tRETURN DATE: %d/%d/2023\n", foundCustomer.returnday, foundCustomer.returnmonth);
printf("\t\t-----------------------------------------\n");
}
int choice;
printf("\n\t\t-------Actions Available-------\n");
printf("\t\t1. Delete Two-Way Ticket\n");
printf("\t\t1. Exit\n");
printf("\n\t\tSelect an action(1 or 2):\t");
scanf("%d", &choice);
switch (choice) {
case 1:
FILE* tempFile = fopen("temp.csv", "w");
if (tempFile == NULL) {
printf("Error creating temporary file.\n");
return 1;
}
FILE* file = fopen(csvFileName, "r");
if (file == NULL) {
printf("Error opening file.\n");
fclose(tempFile);
return 1;
}
char buffer[200]; //create a buffer to hold the contents of the file
while (fgets(buffer, sizeof(buffer), file)) {
struct Customer currentCustomer;
initializeCustomer(¤tCustomer);
sscanf(buffer, "%d,%9[^,],%49[^,],%49[^,],%14[^,],%29[^,],%9[^,],%d,%d,%9[^,],%d,%d",
¤tCustomer.age,
currentCustomer.idpassport,
currentCustomer.fname,
currentCustomer.lname,
currentCustomer.phone,
currentCustomer.destination,
currentCustomer.time,
¤tCustomer.day,
¤tCustomer.month,
currentCustomer.returntime,
¤tCustomer.returnday,
¤tCustomer.returnmonth
);
if (strcmp(currentCustomer.fname, searchFirstName) != 0 || strcmp(currentCustomer.idpassport, searchPassport) != 0) {
fprintf(tempFile, "%d,%s,%s,%s,%s,%s,%s,%d,%d,%s,%d,%d\n",
currentCustomer.age,
currentCustomer.idpassport,
currentCustomer.fname,
currentCustomer.lname,
currentCustomer.phone,
currentCustomer.destination,
currentCustomer.time,
currentCustomer.day,
currentCustomer.month,
currentCustomer.returntime,
currentCustomer.returnday,
currentCustomer.returnmonth
);
}
}
fclose(file);
fclose(tempFile);
// Remove the original file and rename the temporary file to the original file name
remove(csvFileName);
rename("temp.csv", csvFileName);
printf("\n\tTicket DELETED\tfirst name :%s | passport number: %s\n", searchFirstName, searchPassport);
printf("\n\tNumber of Two-Way Tickets Remaining: %d\n", counttotalTwoWays(csvFileName));
displayBanner();
break;
case 2:
displayBanner();
break;
default:
printf("Invalid choice. No action taken.\n");
}
}
return 0;
}