Commit 60ef1396 by krmaet

Delete broneering.c

parent 7dccaf1e
Showing with 0 additions and 342 deletions
#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)
{
int seatmap[ROWS][COLS]= {0};
srand(time(NULL));
int i = 0;
users data[SEAT_MAX];
int cont = 0;
MYSQL* con = connectToMySQLServer();
puts("Choose destination\n");
int flightId = ShowDestinations(con);
data[i].flight_id = flightId;
FillSeatmap(seatmap, ROWS, COLS, con, flightId);
PrintSeats(seatmap, ROWS, COLS);
while(cont == 0)
{
printf("\nChoose seat"); //kontroll lisamata
char seat[SEAT_MAX];
scanf("%s", seat);
strcpy(data[i].seat, seat);
printf("\nEnter Your document number:");
char docNum[DOC_MAX];
scanf("%s", docNum);
strcpy(data[i].documentNum, docNum);
printf("\nEnter 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("\nEnter last name:"); //sama kommentaar mis eelmisele
char lName[NAME_MAX];
scanf("%s", lName);
strcpy(data[i].lName, lName);
printf("\nEnter date of birth (Format: 'XX.Month.Year'):");
char dateOB[DOB_MAX];
scanf("%s", dateOB);
strcpy(data[i].dateOfBirth, dateOB);
printf("\nEnter email:");
char meil[STR_MAX];
scanf("%s", meil);
strcpy(data[i].email, meil);
printf("\nEnter residency:");
char residency[STR_MAX];
scanf("%s", residency);
strcpy(data[i].residency, residency);
data[i].checkedIn = false;
printf("\nWhat luggage do you want to bring with you");
puts("\n0-Small bag + Carry-On(default included in fare),"
"1-Check-In 20kg suitcase, 2-Oversized(bikes, 20+kg)");
puts("Enter value:");
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 FillSeatmap(int seats[ROWS][COLS], int rows, int cols, MYSQL *con, int flightId)
{
char query[512];
sprintf(query, "SELECT seat FROM users WHERE flight_id = %d;", flightId);
if (mysql_query(con, query)) { //päring serverile
printf("Error getting seat data: %s\n", mysql_error(con));
exit(EXIT_FAILURE);
}
MYSQL_RES *result = mysql_store_result(con);
int num_fields = mysql_num_fields(result);
char temp[4];
char tempRow[3];
int tempRowInt;
char colNum;
int col;
MYSQL_ROW row;
while ((row = mysql_fetch_row(result)))
{
for(int i = 0; i < num_fields; i++)
{
//printf("%s ", row[i] ? row[i] : "NULL");
strcpy(temp,row[i]);
strncpy(tempRow, temp, 2);
tempRow[2] = '\n';
tempRowInt = atoi(tempRow);
colNum = temp[2];
if (colNum == 'A')
{
col = 0;
}
else if (colNum == 'B')
{
col = 1;
}
else if (colNum == 'C')
{
col = 2;
}
else if (colNum == 'D')
{
col = 3;
}
seats[tempRowInt][col] = 1;
}
//printf("\n");
}
}
void PrintSeats(int data[ROWS][COLS], int rows, int cols)
{
int i, j;
char colId;
printf("\nSeats available :\n");
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
if (j == 0)
{
colId = 'A';
}
else if (j == 1)
{
colId= 'B';
}
else if (j == 2)
{
colId = 'C';
}
else if (j == 3)
{
colId = 'D';
}
if(data[i][j] == 1)
{
printf("\033[0;31m");
printf(" X ");
printf("\033[0m");
}
else if(data[i][j] == 0)
{
printf("\033[0;32m");
if(i < 10)
printf("0%d%c", i, colId);
else
printf("%d%c", i, colId);
printf("\033[0m");
}
if(j == 1)
printf("\t");
else
printf(" ");
}
printf("\n");
}
}
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("\nTo 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("\nSelection:");
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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment