Commit 5df15104 by krmaet

Update readdata.c, final

parent d0154dd1
Showing with 111 additions and 104 deletions
...@@ -9,51 +9,6 @@ ...@@ -9,51 +9,6 @@
#include "readdata.h" #include "readdata.h"
#include "main.h" #include "main.h"
int CheckUser(MYSQL *con, char *username, char *password)
{
char query[512];
int position;
sprintf(query, "SELECT password, position FROM profiles WHERE username = '%s';", username);
if (mysql_query(con, query)) { //päring serverile
printf("Error getting data: %s\n", mysql_error(con));
exit(EXIT_FAILURE);
}
MYSQL_RES *result = mysql_store_result(con);
mysql_num_fields(result);
char tempPassword[128] = {" "};
char tempPosition[128] = {" "};
MYSQL_ROW row;
while ((row = mysql_fetch_row(result)))
{
strcpy(tempPassword,row[0]);
strcpy(tempPosition,row[1]);
}
position = atoi(tempPosition);
if (!strcmp(tempPassword, password))
{
if (position == ADMIN)
{
return ADMIN;
}
else if (position == ASSISTANT)
{
return ASSISTANT;
}
else if (position == CLIENT)
{
return CLIENT;
}
}
return -1;
}
void NewTicket(int *row, int *col, char *seat, int flightID, MYSQL *con, int count) void NewTicket(int *row, int *col, char *seat, int flightID, MYSQL *con, int count)
{ {
srand(time(NULL)); srand(time(NULL));
...@@ -67,7 +22,7 @@ void NewTicket(int *row, int *col, char *seat, int flightID, MYSQL *con, int cou ...@@ -67,7 +22,7 @@ void NewTicket(int *row, int *col, char *seat, int flightID, MYSQL *con, int cou
WINDOW *ticket_win; WINDOW *ticket_win;
ticket_win = create_newwin(28, 100, 1, *col / 2 - 100/2); ticket_win = create_newwin(WIN_LENGTH, WIN_WIDTH, 1, *col / 2 - WIN_WIDTH/2);
wrefresh(ticket_win); wrefresh(ticket_win);
int flightId = flightID; int flightId = flightID;
...@@ -75,48 +30,59 @@ void NewTicket(int *row, int *col, char *seat, int flightID, MYSQL *con, int cou ...@@ -75,48 +30,59 @@ void NewTicket(int *row, int *col, char *seat, int flightID, MYSQL *con, int cou
strcpy(data[count].seat, seat); strcpy(data[count].seat, seat);
echo(); echo();
//Ask for document number
mvwprintw(ticket_win, 1, 1,"Enter Your document number:"); mvwprintw(ticket_win, 1, 1,"Enter Your document number:");
char docNum[DOC_MAX]; char docNum[DOC_MAX];
wscanw(ticket_win, "%s", docNum); wscanw(ticket_win, "%s", docNum);
strcpy(data[count].documentNum, docNum); strcpy(data[count].documentNum, docNum);
mvwprintw(ticket_win, 3, 1,"Enter first name:"); //lisada juurde et kui on tühik siis //Ask for first name
char fName[NAME_MAX]; //muudetakse see alakriipsuks. mvwprintw(ticket_win, 3, 1,"Enter first name:");
char fName[NAME_MAX];
wscanw(ticket_win, "%[^\n]%*c", fName); //Reads until newline wscanw(ticket_win, "%[^\n]%*c", fName); //Reads until newline
strcpy(data[count].fName, fName); strcpy(data[count].fName, fName);
mvwprintw(ticket_win, 5, 1,"Enter last name:"); //sama kommentaar mis eelmisele //Ask for last name
mvwprintw(ticket_win, 5, 1,"Enter last name:");
char lName[NAME_MAX]; char lName[NAME_MAX];
wscanw(ticket_win, "%[^\n]%*c", lName); //Reads until newline wscanw(ticket_win, "%[^\n]%*c", lName); //Reads until newline
strcpy(data[count].lName, lName); strcpy(data[count].lName, lName);
mvwprintw(ticket_win, 7, 1,"Enter date of birth (Format: 'DD.Month.Year'):"); //Ask for date of birth
mvwprintw(ticket_win, 7, 1,"Enter date of birth(DD.Month.Year):");
char dateOB[DOB_MAX]; char dateOB[DOB_MAX];
wscanw(ticket_win, "%s", dateOB); wscanw(ticket_win, "%s", dateOB);
strcpy(data[count].dateOfBirth, dateOB); strcpy(data[count].dateOfBirth, dateOB);
//Ask for email
mvwprintw(ticket_win, 9, 1,"Enter email:"); mvwprintw(ticket_win, 9, 1,"Enter email:");
char meil[STR_MAX]; char meil[STR_MAX];
wscanw(ticket_win, "%s", meil); wscanw(ticket_win, "%s", meil);
strcpy(data[count].email, meil); strcpy(data[count].email, meil);
//Ask for residency
mvwprintw(ticket_win, 11, 1,"Enter residency:"); mvwprintw(ticket_win, 11, 1,"Enter residency:");
char residency[STR_MAX]; char residency[STR_MAX];
wscanw(ticket_win, "%s", residency); wscanw(ticket_win, "%s", residency);
strcpy(data[count].residency, residency); strcpy(data[count].residency, residency);
//Declare checkedIn to false
data[count].checkedIn = false; data[count].checkedIn = false;
noecho(); noecho();
mvwprintw(ticket_win, 13, 1, "Choose luggage type"); mvwprintw(ticket_win, 13, 1, "Choose luggage type");
mvwprintw(ticket_win, 14, 1, "-------------------"); mvwprintw(ticket_win, 14, 1, "-------------------");
char *options[3] = {"Small bag + Carry-On(default included in fare)", char *options[LUGGAGE_COUNT] = {"Small bag + Carry-On(default included in fare)",
"Check-In 20kg suitcase", "Oversized(bikes, 20+kg)"}; "Check-In 20kg suitcase", "Oversized(bikes, 20+kg)"};
//Create list with options selectable with arrow key
while (1) while (1)
{ {
for (i = 0; i < 3; i++) //Print list
for (i = 0; i < LUGGAGE_COUNT; i++)
{ {
//If current option is selected, highlight it
if (i == highlight) if (i == highlight)
{ {
wattron(ticket_win, A_REVERSE); wattron(ticket_win, A_REVERSE);
...@@ -128,21 +94,21 @@ void NewTicket(int *row, int *col, char *seat, int flightID, MYSQL *con, int cou ...@@ -128,21 +94,21 @@ void NewTicket(int *row, int *col, char *seat, int flightID, MYSQL *con, int cou
mvwprintw(ticket_win, i + 15, 1, "%s", options[i]); mvwprintw(ticket_win, i + 15, 1, "%s", options[i]);
} }
} }
//Ask for choice in arrow keys
choice = wgetch(ticket_win); choice = wgetch(ticket_win);
switch(choice){ switch(choice){
case UP_KEY: case UP_KEY:
highlight--; highlight--;
if (highlight == -1) if (highlight == -1) //List is at the top
{ {
highlight = 0; highlight = 0;
} }
break; break;
case DOWN_KEY: case DOWN_KEY:
highlight++; highlight++;
if (highlight == 3) if (highlight == LUGGAGE_COUNT) //List is at the bottom
{ {
highlight = 3 - 1; highlight = LUGGAGE_COUNT - 1;
} }
break; break;
default: default:
...@@ -150,12 +116,12 @@ void NewTicket(int *row, int *col, char *seat, int flightID, MYSQL *con, int cou ...@@ -150,12 +116,12 @@ void NewTicket(int *row, int *col, char *seat, int flightID, MYSQL *con, int cou
} }
if (choice == ENTER_KEY) if (choice == ENTER_KEY)
{ {
break; break;
} }
} }
data[count].luggageClass = highlight; data[count].luggageClass = highlight;
wrefresh(ticket_win);
delwin(ticket_win); delwin(ticket_win);
insertPersonIntoTable(con, data[count], row, col); insertPersonIntoTable(con, data[count], row, col);
...@@ -185,7 +151,6 @@ void FillSeatmap(int seats[ROWS][COLUMNS], int rows, int cols, MYSQL *con, int f ...@@ -185,7 +151,6 @@ void FillSeatmap(int seats[ROWS][COLUMNS], int rows, int cols, MYSQL *con, int f
{ {
for(int i = 0; i < num_fields; i++) for(int i = 0; i < num_fields; i++)
{ {
//printf("%s ", row[i] ? row[i] : "NULL");
strcpy(temp,row[i]); strcpy(temp,row[i]);
strncpy(tempRow, temp, 2); strncpy(tempRow, temp, 2);
tempRow[2] = '\n'; tempRow[2] = '\n';
...@@ -210,34 +175,36 @@ void FillSeatmap(int seats[ROWS][COLUMNS], int rows, int cols, MYSQL *con, int f ...@@ -210,34 +175,36 @@ void FillSeatmap(int seats[ROWS][COLUMNS], int rows, int cols, MYSQL *con, int f
seats[tempRowInt][col] = 1; seats[tempRowInt][col] = 1;
} }
//printf("\n");
} }
} }
void PrintSeats(int data[ROWS][COLUMNS], int rows, int cols, int *row, int *col, char *seat) void PrintSeats(int data[ROWS][COLUMNS], int rows, int cols, int *row, int *col, char *seat)
{ {
int i, j; int i, j;
int count = 0;
char colId; char colId;
char temp[STR_MAX]; char temp[STR_MAX];
char seats[SEATS][STR_MAX]; //Seat array to check if seat is available and exists
WINDOW *menu_win; WINDOW *menu_win;
menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2); menu_win = create_newwin(WIN_LENGTH, WIN_WIDTH, 1, *col / 2 - WIN_WIDTH/2);
wrefresh(menu_win); wrefresh(menu_win);
mvwprintw(menu_win, 1, 1, "Choose your seat"); mvwprintw(menu_win, 1, 1, "Choose your seat");
mvwprintw(menu_win, 2, 1, "----------------"); mvwprintw(menu_win, 2, 1, "----------------");
wmove(menu_win, 3, 1); wmove(menu_win, 3, 1);
//Go through all rows
for(i = 0; i < rows; i++) for(i = 0; i < rows; i++)
{ {
//Go through all columns
for(j = 0; j < cols; j++) for(j = 0; j < cols; j++)
{ {
//Change colId accordingly to the seat column
if (j == 0) if (j == 0)
{ {
colId = 'A'; colId = 'A';
} }
else if (j == 1) else if (j == 1)
{ {
colId= 'B'; colId = 'B';
} }
else if (j == 2) else if (j == 2)
{ {
...@@ -248,40 +215,68 @@ void PrintSeats(int data[ROWS][COLUMNS], int rows, int cols, int *row, int *col, ...@@ -248,40 +215,68 @@ void PrintSeats(int data[ROWS][COLUMNS], int rows, int cols, int *row, int *col,
colId = 'D'; colId = 'D';
} }
if(data[i][j] == 1) if(data[i][j] == 1) //Seat is taken
{ {
//~ attron(COLOR_RED);
wprintw(menu_win, " X "); wprintw(menu_win, " X ");
//~ attroff(COLOR_RED); //Print no seat into seat array
sprintf(seats[count], "00A");
} }
else if(data[i][j] == 0) else if(data[i][j] == 0) //Seat is free
{ {
//~ attron(COLOR_GREEN);
if(i < 10) if(i < 10)
{
//Print seat into window
wprintw(menu_win, "0%d%c", i, colId); wprintw(menu_win, "0%d%c", i, colId);
//Print seat into seat array
sprintf(seats[count], "0%d%c", i, colId);
}
else else
{
//Print seat into window
wprintw(menu_win, "%d%c", i, colId); wprintw(menu_win, "%d%c", i, colId);
//~ attroff(COLOR_GREEN); //Print seat into seat array
sprintf(seats[count], "%d%c", i, colId);
}
} }
if(j == 1) if(j == 1)
wprintw(menu_win, " "); wprintw(menu_win, " ");
else else
wprintw(menu_win, " "); wprintw(menu_win, " ");
wrefresh(menu_win); wrefresh(menu_win);
count++;
} }
wmove(menu_win, i + 3, 1); wmove(menu_win, i + 3, 1);
} }
mvwprintw(menu_win, 23, 1, "Please type your seat and press enter: "); mvwprintw(menu_win, 23, 1, "Please type your seat");
echo();
mvwprintw(menu_win, 24, 1,">");
noecho();
//Check if seat is available and exists
int check = 1;
while (check == 1)
{
echo(); echo();
wscanw(menu_win, "%s", temp); mvwscanw(menu_win, 24, 2,"%s", temp);
noecho(); noecho();
//Check if seat is in the created string array
for (i = 0; i < SEATS; i++)
{
//If seat is in the string array, make check = 0, to exit loop
if (strcmp(temp, seats[i]) == 0)
{
check = 0;
}
else
{
mvwprintw(menu_win, 23, 1, "Seat not available, try again:");
mvwprintw(menu_win, 24, 2," ");
}
}
}
strcpy(seat, temp); strcpy(seat, temp);
//~ mvwprintw(menu_win, 25, 1, "%s", seat);
//~ for (i = 0; i < 5; i++)
//~ {
//~ seat[i] = temp[i];
//~ }
wrefresh(menu_win); wrefresh(menu_win);
delwin(menu_win); delwin(menu_win);
} }
...@@ -289,10 +284,10 @@ void PrintSeats(int data[ROWS][COLUMNS], int rows, int cols, int *row, int *col, ...@@ -289,10 +284,10 @@ void PrintSeats(int data[ROWS][COLUMNS], int rows, int cols, int *row, int *col,
void insertPersonIntoTable(MYSQL* con, users data, int *rows, int *cols) void insertPersonIntoTable(MYSQL* con, users data, int *rows, int *cols)
{ {
WINDOW *ticket_win; WINDOW *ticket_win;
ticket_win = create_newwin(28, 100, 1, *cols / 2 - 100/2); ticket_win = create_newwin(WIN_LENGTH, WIN_WIDTH, 1, *cols / 2 - WIN_WIDTH/2);
wrefresh(ticket_win); wrefresh(ticket_win);
char query[1024]; //salvestan mysql päringu stringi char query[1024]; //String to save query into
sprintf(query, "INSERT INTO users (documentNum, firstName, " sprintf(query, "INSERT INTO users (documentNum, firstName, "
"lastName, dateOfBirth, email, residency, checkedIn, seat," "lastName, dateOfBirth, email, residency, checkedIn, seat,"
...@@ -303,7 +298,7 @@ void insertPersonIntoTable(MYSQL* con, users data, int *rows, int *cols) ...@@ -303,7 +298,7 @@ void insertPersonIntoTable(MYSQL* con, users data, int *rows, int *cols)
//~ printf("DEBUG:%s\n\n", query); //~ printf("DEBUG:%s\n\n", query);
if (mysql_query(con, query)) { //päring serverile if (mysql_query(con, query)) { //Query to database
clear(); clear();
printf("Error inserting user into database: %s\n", mysql_error(con)); printf("Error inserting user into database: %s\n", mysql_error(con));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
...@@ -312,7 +307,7 @@ void insertPersonIntoTable(MYSQL* con, users data, int *rows, int *cols) ...@@ -312,7 +307,7 @@ void insertPersonIntoTable(MYSQL* con, users data, int *rows, int *cols)
char bookingCode[9]; char bookingCode[9];
const char letters[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const char letters[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i; int i;
for (i = 0; i < 8; i++) //broneeringunumbri genereerimine for (i = 0; i < 8; i++) //Generate booking number
{ {
bookingCode[i] = letters[GetRand(0, 35)]; bookingCode[i] = letters[GetRand(0, 35)];
} }
...@@ -320,8 +315,7 @@ void insertPersonIntoTable(MYSQL* con, users data, int *rows, int *cols) ...@@ -320,8 +315,7 @@ void insertPersonIntoTable(MYSQL* con, users data, int *rows, int *cols)
mvwprintw(ticket_win, 3, 1, "Your booking number: %s", bookingCode); mvwprintw(ticket_win, 3, 1, "Your booking number: %s", bookingCode);
mvwprintw(ticket_win, 4, 1, "Write this down to change your booking before the flight"); //Query to get last id created
//päring et saada kätte viimane sisestatud väljale loodud ID
if (mysql_query(con, "SELECT last_insert_id();")) if (mysql_query(con, "SELECT last_insert_id();"))
{ {
finish_with_error(con); finish_with_error(con);
...@@ -354,6 +348,7 @@ void insertPersonIntoTable(MYSQL* con, users data, int *rows, int *cols) ...@@ -354,6 +348,7 @@ void insertPersonIntoTable(MYSQL* con, users data, int *rows, int *cols)
int num_fields = mysql_num_fields(result2); int num_fields = mysql_num_fields(result2);
MYSQL_ROW row2; MYSQL_ROW row2;
//Create file named after the booking code
char fileName[STR_MAX]; char fileName[STR_MAX];
sprintf(fileName, "Booking_%s.txt", bookingCode); sprintf(fileName, "Booking_%s.txt", bookingCode);
FILE *fp = fopen(fileName, "w"); FILE *fp = fopen(fileName, "w");
...@@ -365,6 +360,7 @@ void insertPersonIntoTable(MYSQL* con, users data, int *rows, int *cols) ...@@ -365,6 +360,7 @@ void insertPersonIntoTable(MYSQL* con, users data, int *rows, int *cols)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
//Print info into file
fprintf(fp, "BOOKING %s\n", bookingCode); fprintf(fp, "BOOKING %s\n", bookingCode);
fprintf(fp, "\nName: %s %s", data.fName, data.lName); fprintf(fp, "\nName: %s %s", data.fName, data.lName);
fprintf(fp, "\nDocument number: %s", data.documentNum); fprintf(fp, "\nDocument number: %s", data.documentNum);
...@@ -376,7 +372,7 @@ void insertPersonIntoTable(MYSQL* con, users data, int *rows, int *cols) ...@@ -376,7 +372,7 @@ void insertPersonIntoTable(MYSQL* con, users data, int *rows, int *cols)
{ {
for(int i = 0; i < num_fields; i++) for(int i = 0; i < num_fields; i++)
{ {
fprintf(fp, "%s ", row[i] ? row[i] : "NULL"); fprintf(fp, "%s ", row2[i] ? row2[i] : "NULL");
} }
} }
...@@ -397,28 +393,33 @@ void insertPersonIntoTable(MYSQL* con, users data, int *rows, int *cols) ...@@ -397,28 +393,33 @@ void insertPersonIntoTable(MYSQL* con, users data, int *rows, int *cols)
} }
fclose(fp); fclose(fp);
mvwprintw(ticket_win, 4, 1, "File named Booking_%s.txt has been created!", bookingCode);
mvwprintw(ticket_win, 5, 1, "Press any key to continue"); mvwprintw(ticket_win, 5, 1, "Press any key to continue");
wgetch(ticket_win); wgetch(ticket_win);
wrefresh(ticket_win); wrefresh(ticket_win);
delwin(ticket_win);
} }
int ShowDestinations(MYSQL *con, int *rows, int *cols) int ShowDestinations(MYSQL *con, int *rows, int *cols)
{ {
if (mysql_query(con, "SELECT id,destination,time FROM Flights;")) //Query for all destinations
if (mysql_query(con, "SELECT id,destination,time, flightNum FROM Flights;"))
{ {
finish_with_error(con); finish_with_error(con);
} }
MYSQL_RES *result = mysql_store_result(con); MYSQL_RES *result = mysql_store_result(con);
int num_fields = mysql_num_fields(result); int num_fields = mysql_num_fields(result);
char buffer[BUFFER_MAX];
WINDOW *menu_win; WINDOW *menu_win;
menu_win = create_newwin(28, 100, 1, *cols / 2 - 100/2); menu_win = create_newwin(WIN_LENGTH, WIN_WIDTH, 1, *cols / 2 - WIN_WIDTH/2);
mvwprintw(menu_win, 1, 1, "Available destinations and times"); mvwprintw(menu_win, 1, 1, "Available destinations and times");
mvwprintw(menu_win, 2, 1, "--------------------------------"); mvwprintw(menu_win, 2, 1, "--------------------------------");
MYSQL_ROW row; MYSQL_ROW row;
int count = 0; int count = 0;
//Print out destinations, times and flight numbers
while ((row = mysql_fetch_row(result))) while ((row = mysql_fetch_row(result)))
{ {
wmove(menu_win, count + 4, 1); wmove(menu_win, count + 4, 1);
...@@ -427,32 +428,37 @@ int ShowDestinations(MYSQL *con, int *rows, int *cols) ...@@ -427,32 +428,37 @@ int ShowDestinations(MYSQL *con, int *rows, int *cols)
wprintw(menu_win, "%s ", row[i] ? row[i] : "NULL"); wprintw(menu_win, "%s ", row[i] ? row[i] : "NULL");
} }
count++; count++;
} }
mvwprintw(menu_win, 12, 1, "To select press (1-%d)", count); mvwprintw(menu_win, count + 4, 1, "Select flight");
int selection;
//Ask for selection
int selection = 0;
echo();
do do
{ {
selection = wgetch(menu_win) - 48; mvwprintw(menu_win, count + 5, 1, ">");
if (selection > count || selection < 1) wprintw(menu_win, " "); //Clear typing area
mvwscanw(menu_win, count + 5, 2, "%d", &selection); //Ask for flight
//Check if flight id exists
sprintf(buffer, "SELECT flightNum FROM Flights WHERE id = %d", selection);
if (mysql_query(con, buffer))
{ {
mvwprintw(menu_win, 12, 1, "Invalid selection, retry (1-%d)", count); finish_with_error(con);
} }
}while(selection > count || selection < 1); MYSQL_RES *result = mysql_store_result(con);
row = mysql_fetch_row(result);
char query[512]; //Check if flight exists
sprintf(query, "SELECT destination FROM Flights WHERE Flights.id = %d;", if (row == NULL)
selection);
if (mysql_query(con, query))
{ {
finish_with_error(con); mvwprintw(menu_win, count + 4, 1, "Invalid selection, retry");
} }
result = mysql_store_result(con);
//~ PrintRes(result); }while(row == NULL);
noecho();
wrefresh(menu_win); wrefresh(menu_win);
mysql_free_result(result); mysql_free_result(result);
delwin(menu_win);
clear();
return selection; return selection;
} }
...@@ -498,13 +504,14 @@ void PrintRes(MYSQL_RES *result) ...@@ -498,13 +504,14 @@ void PrintRes(MYSQL_RES *result)
} }
int GetRand(int min, int max) int GetRand(int min, int max)
{ {
//Return random number ranging from provided min to max
return (rand() % (max - min + 1)) + min; return (rand() % (max - min + 1)) + min;
} }
int CheckLetters(char word[STR_MAX]) int CheckLetters(char word[STR_MAX])
{ {
int i; int i;
//Checks if provided string consists of only letters
for (i = 0; i < strlen(word); i++) for (i = 0; i < strlen(word); i++)
{ {
if ((word[i] >= 'a' && word[i] <= 'z') || (word[i] >= 'A' && word[i] <= 'Z')) if ((word[i] >= 'a' && word[i] <= 'z') || (word[i] >= 'A' && word[i] <= 'Z'))
......
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