Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
mvahes
/
Lennubroneerimis_tarkvara
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
ecae42cc
authored
Mar 18, 2023
by
karade
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parent
088af928
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
224 additions
and
0 deletions
broneering.c
broneering.c
0 → 100644
View file @
ecae42cc
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
#include <time.h>
#include "broneering.h"
int
main
(
void
)
{
srand
(
time
(
NULL
));
int
i
=
0
;
users
data
[
SEAT_MAX
];
int
cont
=
0
;
puts
(
"Choose destination
\n
"
);
MYSQL
*
con
=
connectToMySQLServer
();
int
flightId
=
ShowDestinations
(
con
);
data
[
i
].
flight_id
=
flightId
;
while
(
cont
==
0
)
{
printf
(
"
\n
Choose seat"
);
//kontrolli ja plaani kuvamise funktsioon lisada
char
seat
[
SEAT_MAX
];
scanf
(
"%s"
,
seat
);
strcpy
(
data
[
i
].
seat
,
seat
);
printf
(
"
\n
Enter Your document number:"
);
char
docNum
[
DOC_MAX
];
scanf
(
"%s"
,
docNum
);
strcpy
(
data
[
i
].
documentNum
,
docNum
);
printf
(
"
\n
Enter first name:"
);
//lisada juurde et kui on tühik siis
char
fName
[
NAME_MAX
];
//muudetakse see alakriipsuks.
scanf
(
"%s"
,
fName
);
strcpy
(
data
[
i
].
fName
,
fName
);
printf
(
"
\n
Enter last name:"
);
//sama kommentaar mis eelmisele
char
lName
[
NAME_MAX
];
scanf
(
"%s"
,
lName
);
strcpy
(
data
[
i
].
lName
,
lName
);
printf
(
"
\n
Enter date of birth (Format: 'XX.Month.Year'):"
);
char
dateOB
[
DOB_MAX
];
scanf
(
"%s"
,
dateOB
);
strcpy
(
data
[
i
].
dateOfBirth
,
dateOB
);
printf
(
"
\n
Enter email:"
);
char
meil
[
STR_MAX
];
scanf
(
"%s"
,
meil
);
strcpy
(
data
[
i
].
email
,
meil
);
printf
(
"
\n
Enter residency:"
);
char
residency
[
STR_MAX
];
scanf
(
"%s"
,
residency
);
strcpy
(
data
[
i
].
residency
,
residency
);
data
[
i
].
checkedIn
=
false
;
printf
(
"
\n
What luggage do you want to bring with you:"
);
int
luggage
;
scanf
(
"%d"
,
&
luggage
);
data
[
i
].
luggageClass
=
luggage
;
insertPersonIntoTable
(
con
,
data
[
i
]);
i
++
;
puts
(
"Add a passenger? 1-yes 0-no"
);
scanf
(
"%d"
,
&
cont
);
}
}
void
insertPersonIntoTable
(
MYSQL
*
con
,
users
data
)
{
char
query
[
1024
];
//salvestan mysql päringu stringi
sprintf
(
query
,
"INSERT INTO users (documentNum, firstName, "
"lastName, dateOfBirth, email, residency, checkedIn, seat,"
"luggageClass, flight_id) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', %d, %d);"
,
data
.
documentNum
,
data
.
fName
,
data
.
lName
,
data
.
dateOfBirth
,
data
.
email
,
data
.
residency
,
data
.
checkedIn
,
data
.
seat
,
data
.
luggageClass
,
data
.
flight_id
);
printf
(
"DEBUG:%s
\n\n
"
,
query
);
if
(
mysql_query
(
con
,
query
))
{
//päring serverile
printf
(
"Error inserting user into database: %s
\n
"
,
mysql_error
(
con
));
exit
(
EXIT_FAILURE
);
}
char
bookingCode
[
9
];
const
char
letters
[]
=
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
;
int
i
;
for
(
i
=
0
;
i
<
8
;
i
++
)
//broneeringunumbri genereerimine
{
bookingCode
[
i
]
=
letters
[
GetRand
(
0
,
35
)];
}
bookingCode
[
8
]
=
'\0'
;
puts
(
"Your booking number: "
);
printf
(
"
\033
[0;32m"
);
printf
(
"%s"
,
bookingCode
);
printf
(
"
\033
[0m"
);
puts
(
"Write this down to change your booking before the flight"
);
//päring et saada kätte viimane sisestatud väljale loodud ID
if
(
mysql_query
(
con
,
"SELECT last_insert_id();"
))
{
finish_with_error
(
con
);
}
MYSQL_RES
*
result
=
mysql_store_result
(
con
);
MYSQL_ROW
row
;
row
=
mysql_fetch_row
(
result
);
int
res
=
atoi
(
row
[
0
]);
sprintf
(
query
,
"INSERT INTO bookings (bookingNumber, flight_id, user_id)"
"VALUES ('%s', %d, %d);"
,
bookingCode
,
data
.
flight_id
,
res
);
if
(
mysql_query
(
con
,
query
))
{
printf
(
"Error creating bookings for the users: %s
\n
"
,
mysql_error
(
con
));
exit
(
EXIT_FAILURE
);
}
}
int
ShowDestinations
(
MYSQL
*
con
)
{
if
(
mysql_query
(
con
,
"SELECT id,destination,time FROM Flights;"
))
{
finish_with_error
(
con
);
}
MYSQL_RES
*
result
=
mysql_store_result
(
con
);
int
num_fields
=
mysql_num_fields
(
result
);
MYSQL_ROW
row
;
int
count
=
0
;
while
((
row
=
mysql_fetch_row
(
result
)))
{
for
(
int
i
=
0
;
i
<
num_fields
;
i
++
)
{
printf
(
"%s "
,
row
[
i
]
?
row
[
i
]
:
"NULL"
);
}
printf
(
"
\n
"
);
count
++
;
}
printf
(
"
\n
To select enter (1-%d):"
,
count
);
int
selection
;
do
{
scanf
(
"%d"
,
&
selection
);
if
(
selection
>
count
||
selection
<
1
)
{
puts
(
"Invalid selection: Retry"
);
}
}
while
(
selection
>
count
||
selection
<
1
);
char
query
[
512
];
sprintf
(
query
,
"SELECT destination FROM Flights WHERE Flights.id = %d;"
,
selection
);
if
(
mysql_query
(
con
,
query
))
{
finish_with_error
(
con
);
}
result
=
mysql_store_result
(
con
);
printf
(
"
\033
[0;32m"
);
printf
(
"
\n
Selection:"
);
PrintRes
(
result
);
printf
(
"
\033
[0m"
);
mysql_free_result
(
result
);
return
selection
;
}
MYSQL
*
connectToMySQLServer
()
{
MYSQL
*
con
=
mysql_init
(
NULL
);
if
(
con
==
NULL
)
{
fprintf
(
stderr
,
"mysql_init() failed
\n
"
);
exit
(
1
);
}
if
(
mysql_real_connect
(
con
,
"127.0.0.1"
,
"root"
,
"Tarkvaraprojekt2023"
,
"Lennubroneering"
,
0
,
NULL
,
0
)
==
NULL
)
{
finish_with_error
(
con
);
}
return
con
;
}
void
finish_with_error
(
MYSQL
*
con
)
{
fprintf
(
stderr
,
"%s
\n
"
,
mysql_error
(
con
));
mysql_close
(
con
);
exit
(
EXIT_FAILURE
);
}
void
PrintRes
(
MYSQL_RES
*
result
)
{
int
num_fields
=
mysql_num_fields
(
result
);
MYSQL_ROW
row
;
while
((
row
=
mysql_fetch_row
(
result
)))
{
for
(
int
i
=
0
;
i
<
num_fields
;
i
++
)
{
printf
(
"%s "
,
row
[
i
]
?
row
[
i
]
:
"NULL"
);
}
printf
(
"
\n
"
);
}
}
int
GetRand
(
int
min
,
int
max
)
{
return
(
rand
()
%
(
max
-
min
+
1
))
+
min
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment