Data Engineers regularly collect, process and store data. In this task you will develop a deeper…

Question:

Data Engineers regularly collect, process and store data. In this task you will develop a deeper understanding of how C programming language can be used for collecting, processing and storing data. In this assignment you get the opportunity to build an interactive program that can manage the list of flights departing Sydney Airport.

The list is stored as an array of flight_t type structures flight_t flights [MAX_NUM_FLIGHTS];

The flight_t is a structure typedef for struct flight. The struct flight contains the following fields

  • flightcode – array of MAX_FLIGHTCODE_LEN+1 chars (string)
  • departure_dt – a structure of date_time_t type as defined below
  • arrival_city – array of MAX_CITYCODE_LEN+1 chars (string)
  • arrival_dt – a structure of date_time_t type as defined below

Note that we now have a struct nested within a struct. The date_time_t is a structure typedef for struct date_time. The struct date_time contains the following fields,

  • month – integer between 1 and 12 (inclusive)
  • day – integer between 1 and 31 (inclusive)
  • hour – integer between 0 and 23 (inclusive)
  • minute – integer between 0 and 59 (inclusive)

Your program interacts with the nested struct array in your memory (RAM) and simple database file in your hard disk. It should provide the following features:

1. add a flight

Add a new flight to the flights through the terminal. You should collect the input by asking multiple questions from the user.

Enter flight code>
Enter departure info for the flight leaving SYD.
Enter month, date, hour and minute separated by spaces>
Enter arrival city code>
Enter arrival info.
Enter month, date, hour and minute separated by spaces>

2. display all flights to a destination

Prompt the following question

Enter arrival city code or enter * to show all destinations>

The user may enter the abbreviation of MAX_CITYCODE_LEN characters for the arrival city. The program should display all flights to the requested destination. If the user input is *, display all flights.

The display format should be similar to this, do not copy&paste from this webpage. Please follow the sample executable for the exact display format, including white spaces.

Flight Origin Destination
—— ————— —————
VA1 SYD 11-26 09:54 LAX 11-26 18:26

Pay attention to the strict formatting guide:

  • Flight – left aligned, MAX_FLIGHTCODE_LEN (i.e. 6) chars at most.
  • Origin and Destination
  • City – left aligned, MAX_CITYCODE_LEN (i.e. 3) chars at most.
  • Month, day, hour, minute – two digits with leading zeros

3. save the flights to the database file

Save the flights in the hard disk as a binary/text file named database. You may use your own format to save the data. You should overwrite if database file already exists.

4. load the flights from the database file

Read the database file and put the data into flights. You may only read the data files created by your own program. You should overwrite the flights array you had in memory when loading from the file.

5. exit the program

Exit the interactive program.

Careless Users

Your program may assume that the input data type is always the expected type i.e. when the program expects an integer the user must enter an integer. However, a careless user may enter an input that is outside the expected range (but still of the expected data type). Your program is expected to handle careless users. e.g.

Enter choice (number between 1-5)>
-1
Invalid choice

Or a careless user may try to add 365 as the month (month should be between 1 and 12). Or try to add a flight to the flights array when it already contains MAX_NUM_FLIGHTS flights, etc.

Run the sample executable to futher understand the expected behaviour.

Check the formatting of the flightcode

WARNING: Attempting this feature is recommended only for advanced students who enjoy a small challenge. You may need to do your own research, but more than that you may have to be creative. By using incorrect techniques you could very well introduce more bugs in your code and it could be time consuming. The special techniques required for this purpose will not be assessed in the final exam.

Your program should be able to check the format of the flightcode. The first two characters of the flightcode should be uppercase letters (A-Z) representing the airline. The rest of the flightcode should be numerals (0-9) representing the flight number. There must be 1-4 numerals as the flight number part of the flightcode. No spaces in the flightcode.

Run the sample executable to further understand the expected behaviour.

The database file

It is up to you to create your own data storage format for the database file. Your program should be able to read the database that was created by itself. You can create the database as a text or binary file.

You do NOT need to be able to create a database identical to the database of the sample executable. You do NOT need to be able to read the database of the sample executable.

Answer:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*******************************************************************************
* List preprocessing directives - you may define your own.
*******************************************************************************/

#define MAX_FLIGHTCODE_LEN 6
#define MAX_CITYCODE_LEN 3
#define MAX_NUM_FLIGHTS 5
#define DB_NAME "database"
#define MAX_ARRIVECODE_LEN 100

/*******************************************************************************
* List structs - you may define struct date_time and struct flight only. Each
* struct definition should have only the fields mentioned in the assignment
* description.
*******************************************************************************/

struct date_time {
int month;
int day;
int hour;
int minute;
};
typedef struct date_time date_time_t;

struct flights {
char flightcode[MAX_FLIGHTCODE_LEN+1];
date_time_t departure_dt;
char arrival_city[MAX_CITYCODE_LEN+1];
date_time_t arrival_dt;
};
typedef struct flights flight_t;

/*******************************************************************************
* Function prototypes - do NOT change the given prototypes. However you may
* define your own functions if required.
*******************************************************************************/
void print_menu (void);
int addFlight(flight_t flights[], int flightCount);
void displayFlightsToDest(flight_t flights[], int flightCount);
void saveFlights(flight_t flights[MAX_NUM_FLIGHTS], int flightCount);
int loadFlights(flight_t flights[MAX_NUM_FLIGHTS], int flightCount);

/*******************************************************************************
* Main
*******************************************************************************/

int main(void)
{
  
  
int option;
flight_t flights[MAX_NUM_FLIGHTS];
int flightCount;
flightCount = 0;
_Bool check;
  
do
{   
option = 0;
check = 1;
do
{

print_menu();
scanf("%d", &option);
  
if (option < 1 || option > 5)
{
check = 1;
printf("Invalid choice\n");
}
else
{
check = 0;
}

} while (check == 1);
  
switch (option)
{
case 1:
flightCount = addFlight(flights, flightCount);
break;
case 2:
displayFlightsToDest(flights, flightCount);
break;
case 3:
saveFlights(flights, flightCount);
break;
case 4:
flightCount = loadFlights(flights, flightCount);
break;
case 5:
break;
}
} while (option != 5);

return 0;
  
}

/*******************************************************************************
* This function prints the initial menu with all instructions on how to use
* this program.
* inputs:
* - none
* outputs:
* - none
*******************************************************************************/
void print_menu (void)
{
printf("\n"
"1. add a flight\n"
"2. display all flights to a destination\n"
"3. save the flights to the database file\n"
"4. load the flights from the database file\n"
"5. exit the program\n"
"Enter choice (number between 1-5)>\n");
}
/*******************************************************************************
* This function adds a new flight to the flights through the terminal.
* inputs:
* - flights(Current), flightCount
* outputs:
* - flights(Updated)
*******************************************************************************/
int addFlight(flight_t flights[MAX_NUM_FLIGHTS], int flightCount)
{

_Bool check;
check = 0;

/******************************************************************************
* Flight Code
*******************************************************************************/
if (flightCount <= MAX_NUM_FLIGHTS-1) {
  
do
{
printf("Enter flight code>\n");
scanf("%s", flights[flightCount].flightcode);
  
if (strlen(flights[flightCount].flightcode) <= 6 && strlen(flights[flightCount].flightcode) >= 3)
{   
if (flights[flightCount].flightcode[0] >= 'A' && flights[flightCount].flightcode[0] <= 'Z')
{
if (flights[flightCount].flightcode[1] >= 'A' && flights[flightCount].flightcode[1] <= 'Z')
{
if (flights[flightCount].flightcode[2] >= '0' && flights[flightCount].flightcode[2] <= '9'){
check = 1;
}
else
{
check = 0;
printf("Invalid input\n");
}
}
else
{
check = 0;
printf("Invalid input\n");
}
}
else
{
check = 0;
printf("Invalid input\n");
}
}
else
{
check = 0;
printf("Invalid input\n");
}

} while (check == 0);

/******************************************************************************
* Depart Info
*******************************************************************************/

check = 0;
printf("Enter departure info for the flight leaving SYD.\n");

do
{

printf("Enter month, date, hour and minute separated by spaces>\n");
scanf("%d %d %d %d", &flights[flightCount].departure_dt.month, &flights[flightCount].departure_dt.day, &flights[flightCount].departure_dt.hour, &flights[flightCount].departure_dt.minute);

if (flights[flightCount].departure_dt.month >= 1 && flights[flightCount].departure_dt.month <= 12) {
if (flights[flightCount].departure_dt.day >= 1 && flights[flightCount].departure_dt.day <= 31) {
if (flights[flightCount].departure_dt.hour >= 0 && flights[flightCount].departure_dt.hour <= 23) {
if (flights[flightCount].departure_dt.minute >= 0 && flights[flightCount].departure_dt.minute <= 59) {
check = 1;
}
else
{
check = 0;
printf("Invalid input\n");
}
}
else
{
check = 0;
printf("Invalid input\n");
}
}
else
{
check = 0;
printf("Invalid input\n");
}
}
else
{
check = 0;
printf("Invalid input\n");
}
  
} while (check == 0);

/******************************************************************************
* Arrival City Code
*******************************************************************************/
  
char arrival_code[100];
printf("Enter arrival city code>\n");
scanf("%s", arrival_code);
strcpy(flights[flightCount].arrival_city, arrival_code);
flights[flightCount].arrival_city[strlen(flights[flightCount].arrival_city)+1] = '\0';
  
/******************************************************************************
* Arrival Info
*******************************************************************************/

check = 0;
printf("Enter arrival info.\n");

do
{

printf("Enter month, date, hour and minute separated by spaces>\n");
scanf("%d %d %d %d", &flights[flightCount].arrival_dt.month, &flights[flightCount].arrival_dt.day, &flights[flightCount].arrival_dt.hour, &flights[flightCount].arrival_dt.minute);
  
if (flights[flightCount].arrival_dt.month >= 1 && flights[flightCount].arrival_dt.month <= 12) {
if (flights[flightCount].arrival_dt.day >= 1 && flights[flightCount].arrival_dt.day <= 31) {
if (flights[flightCount].arrival_dt.hour >= 0 && flights[flightCount].arrival_dt.hour <= 23) {
if (flights[flightCount].arrival_dt.minute >= 0 && flights[flightCount].arrival_dt.minute <= 59) {
check = 1;
flightCount = flightCount + 1;
}
else
{
check = 0;
printf("Invalid input\n");
}
}
else
{
check = 0;
printf("Invalid input\n");
}
}
else
{
check = 0;
printf("Invalid input\n");
}
}
else
{
check = 0;
printf("Invalid input\n");
}
} while(check == 0);
}
else
{
printf("Cannot add more flights - memory full\n");
}

return flightCount;

}

/*******************************************************************************
* This function adds a new flight to the flights through the terminal.
* inputs:
* - flights, flightcount
* outputs:
* - A print of all the data stored in flights
*******************************************************************************/
void displayFlightsToDest(flight_t flights[MAX_NUM_FLIGHTS], int flightCount)
{

char searchArrivalCity[20];
int i,i2;
int space;
_Bool check;
_Bool check2;

check2 = 0;
printf("Enter arrival city code or enter * to show all destinations>\n");
scanf("%s", searchArrivalCity);
  
if (flightCount >= 0)
{

if (strcmp(searchArrivalCity, "*")== 0)
{

printf("Flight Origin Destination\n");
printf("------ --------------- ---------------\n");

for(i = 0; i < flightCount; i++)
{
printf("%s", flights[i].flightcode);
  
space = MAX_FLIGHTCODE_LEN - strlen(flights[i].flightcode);
  
for( i2 = 0; i2 <= space; i2++)
{
printf(" ");
}
printf("SYD %.2d-%.2d %.2d:%.2d %.3s",flights[i].departure_dt.month, flights[i].departure_dt.day, flights[i].departure_dt.hour, flights[i].departure_dt.minute, flights[i].arrival_city);
  
if (strlen(flights[i].arrival_city)> 3)
{
space = MAX_CITYCODE_LEN - 3;
}
else
{
space = MAX_CITYCODE_LEN - strlen(flights[i].arrival_city);
}
  
for( i2 = 0; i2 <= space; i2++)
{
printf(" ");
}
printf("%.2d-%.2d %.2d:%.2d\n", flights[i].arrival_dt.month, flights[i].arrival_dt.day, flights[i].arrival_dt.hour, flights[i].arrival_dt.minute);
check = 0;
}
}
else
{
  
for( i = 0; i < flightCount; i++)
{
  
if (strcmp(searchArrivalCity, flights[i].arrival_city)== 0)
{
if (check2 == 0)
{
printf("Flight Origin Destination\n");
printf("------ --------------- ---------------\n");
check2 = 1;
}
  
printf("%s", flights[i].flightcode);
space = MAX_FLIGHTCODE_LEN - strlen(flights[i].flightcode);

for( i2 = 0; i2 <= space; i2++)
{
printf(" ");
}

printf("SYD %.2d-%.2d %.2d:%.2d %.3s",flights[i].departure_dt.month, flights[i].departure_dt.day, flights[i].departure_dt.hour, flights[i].departure_dt.minute, flights[i].arrival_city);
space = MAX_CITYCODE_LEN - strlen(flights[i].arrival_city);

for( i2 = 0; i2 <= space; i2++)
{
printf(" ");
}

printf("%.2d-%.2d %.2d:%.2d\n", flights[i].arrival_dt.month, flights[i].arrival_dt.day, flights[i].arrival_dt.hour, flights[i].arrival_dt.minute);
check = 0;

}
else
{
check = 1;
}
}
}
}
else
{
check = 1;
}
  
if (check == 1)
{
printf("No flights\n");
}
  
}

/*******************************************************************************
* This function adds a new flight to the flights through the terminal.
* inputs:
* - flights, flightCount
* outputs:
* - file containing flightCount and the array of structures: flights
*******************************************************************************/
void saveFlights(flight_t flights[MAX_NUM_FLIGHTS], int flightCount)
{
  
FILE *fp=NULL;
int i;
fp = fopen("database.txt", "w");

if (fp==NULL)
{
printf("Write error\n");
return;
}

fprintf(fp,"%d\n",flightCount);
for( i = 0; i < flightCount; i++)
{
fprintf(fp,"%s\t%d\t%d\t%d\t%d\t%s\t%d\t%d\t%d\t%d\n", flights[i].flightcode, flights[i].departure_dt.month, flights[i].departure_dt.day, flights[i].departure_dt.hour, flights[i].departure_dt.minute, flights[i].arrival_city, flights[i].arrival_dt.month, flights[i].arrival_dt.day, flights[i].arrival_dt.hour, flights[i].arrival_dt.minute);
}

fclose(fp);

}

/*******************************************************************************
* This function adds a new flight to the flights through the terminal.
* inputs:
* - flights, fLightCount
* outputs:
* - flights(Loaded), fLightCount(Loaded)
*******************************************************************************/
int loadFlights(flight_t flights[MAX_NUM_FLIGHTS], int flightCount)
{

FILE *fp;
int i;
fp = fopen("database.txt", "r");

if (fp==NULL)
{
printf("Read error\n");
return flightCount;
}

fscanf(fp,"%d\n", &flightCount);
  
for( i = 0; i <= flightCount; i++)
{
if (i > MAX_NUM_FLIGHTS)
{
printf("Read error\n");
fclose(fp);
return flightCount;
}
fscanf(fp,"%s\t%d\t%d\t%d\t%d\t%s\t%d\t%d\t%d\t%d\n", &*flights[i].flightcode, &flights[i].departure_dt.month, &flights[i].departure_dt.day, &flights[i].departure_dt.hour, &flights[i].departure_dt.minute, &*flights[i].arrival_city, &flights[i].arrival_dt.month, &flights[i].arrival_dt.day, &flights[i].arrival_dt.hour, &flights[i].arrival_dt.minute);
}

fclose(fp);
return flightCount;
}

Leave a Comment

Your email address will not be published. Required fields are marked *