In: Computer Science
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
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,
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 is as follows.
Flight
Origin Destination
------ --------------- ---------------
VA1 SYD 11-26 09:54 LAX 11-26 18:26
Pay attention to the strict formatting guide:
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.
You are free to use the following C libraries in this program ONLY:
stdio.h
stdlib.h
string.h
code
solution
//output
//copyable code
//header file
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//define the variables
#define flightdataCODE_LENGTH 6
#define CITYCODE_LENGTH 3
#define MAXIMUM_number_flightdatas 5
#define DATABASENAME "database"
//create struct method
struct datetime
{
//declare the month,day,hour and minute
int mnth;
int day;
int hour;
int minute;
};
typedef struct datetime datetime_t;
//create flight method
struct flightdata
{ //declare the variables
char flightdataCode[flightdataCODE_LENGTH+1];
datetime_t departure_date;
char arrivalcity[CITYCODE_LENGTH+1];
datetime_t arrival_dt;
};
typedef struct flightdata flightdata_t;
//define function
void displaymenu (void);
int get_select();
void inputdata(int input);
void add_flightdata();
void display_flightdatas();
void data_Operation(char * o);
int Valid_Date(int mnth, int day, int hour, int minute);
flightdata_t flightdatas [MAXIMUM_number_flightdatas];
int number_flightdatas = 0;
//main method
int main(void)
{
displaymenu();
int select = get_select();
if(select == -1)
{
main();
}
inputdata(select);
return 0;
}
//display menu
void displaymenu (void)
{
printf("\n"
"1. add flightdata\n"
"2. print all flightdatas to a destination\n"
"3. save the flightdatas to the database \n"
"4. load the flightdatas \n"
"5. end program\n"
"please Enter choice (please enter number between 1-5)>\n");
}
//get select method
int get_select(){
int input = 0;
if (scanf("%d", &input) == 1)
{
if(input < 1 || input > 5)
{
printf("Invalid \n");
return -1;
}
else
{
return input;
}
}
else
{
printf("Invalid \n");
return -1;
}
return -1;
}
//input data function
void inputdata(int input)
{
char * write = (char*)"w";
char * read = (char*)"r";
switch(input) {
//case statement
case 1: add_flightdata(); break;
case 2: display_flightdatas(); break;
case 3: data_Operation(write); break;
case 4: data_Operation(read); break;
case 5: exit(0); break;
}
}
//add the flight method
void add_flightdata()
{
//check the condition
if(number_flightdatas != MAXIMUM_number_flightdatas)
{
flightdata_t nflightdata;
int mnth, day, hour, minute;
int validator = -1;
//while method process
while(validator == -1)
{
printf("please Enter flightdata code>\n");
scanf("%s", nflightdata.flightdataCode);
if(strlen(nflightdata.flightdataCode) > 6)
{
printf("Invalid\n");
}
else
{
validator = 0;
}
}
//display the data
printf("please Enter departure information for leaving SYD.\n");
validator = -1;
while(validator == -1)
{
printf("please Enter mnth, enter date, enter hour and enter minute >\n");
scanf("%d %d %d %d", &mnth, &day, &hour, &minute);
validator = Valid_Date(mnth, day, hour, minute);
}
nflightdata.departure_date.mnth = mnth;
nflightdata.departure_date.day = day;
nflightdata.departure_date.hour = hour;
nflightdata.departure_date.minute = minute;
printf("please Enter arrival city code>\n");
scanf("%s", nflightdata.arrivalcity);
nflightdata.arrivalcity[3] = '\0';
printf("please Enter arrival information.\n");
validator = -1;
while(validator == -1)
{
printf("please Enter mnth, enter date, enter hour and enter minute \n");
scanf("%d %d %d %d", &mnth, &day, &hour, &minute);
validator = Valid_Date(mnth, day, hour, minute);
}
nflightdata.arrival_dt.mnth = mnth;
nflightdata.arrival_dt.day = day;
nflightdata.arrival_dt.hour = hour;
nflightdata.arrival_dt.minute = minute;
flightdatas[number_flightdatas] = nflightdata;
++number_flightdatas;
}
else
{
printf("Could not found memory full\n");
}
main();
}
//display flight data
void display_flightdatas()
{
char arrivalcity[CITYCODE_LENGTH+1];
printf("please Enter arrivalcity code \n");
scanf("%s", arrivalcity);
if(strcmp(arrivalcity, "*" ) == 0)
{
if(number_flightdatas == 0)
{
printf("No flightdatas\n");
}
else{
printf("flightdata Origin Destination\n");
printf("------ --------------- ---------------\n");
int i = 0;
while(i < number_flightdatas)
{
printf("%-6s ", flightdatas[i].flightdataCode);
printf("SYD %02d-%02d %02d:%02d ", flightdatas[i].departure_date.mnth,
flightdatas[i].departure_date.day, flightdatas[i].departure_date.hour,
flightdatas[i].departure_date.minute );
printf("%-3s %02d-%02d %02d:%02d\n", flightdatas[i].arrivalcity,
flightdatas[i].arrival_dt.mnth, flightdatas[i].arrival_dt.day,
flightdatas[i].arrival_dt.hour, flightdatas[i].arrival_dt.minute);
++i;
}
}
}
else
{
int i = 0;
int matches = 0;
while(i < number_flightdatas)
{
if(strcmp(arrivalcity, flightdatas[i].arrivalcity) == 0)
{
matches++ ;
}
++i;
}
if(matches == 0)
{
printf("No flightdatas\n");
}
else{
i = 0;
printf("flightdata Origin Destination\n");
printf("------ --------------- ---------------\n");
while(i < number_flightdatas)
{
if(strcmp(arrivalcity, flightdatas[i].arrivalcity) == 0)
{
printf("%-6s ", flightdatas[i].flightdataCode);
printf("SYD %02d-%02d %02d:%02d ", flightdatas[i].departure_date.mnth, flightdatas[i].departure_date.day, flightdatas[i].departure_date.hour, flightdatas[i].departure_date.minute );
printf("%-3s %02d-%02d %02d:%02d\n", flightdatas[i].arrivalcity, flightdatas[i].arrival_dt.mnth, flightdatas[i].arrival_dt.day, flightdatas[i].arrival_dt.hour, flightdatas[i].arrival_dt.minute);
}
++i;
}
}
}
main();
}
//valid the date
int Valid_Date(int mnth, int day1, int hour1, int min)
{
if(mnth < 1 || mnth > 12 || day1 < 1 || day1 > 31 || hour1 < 0 || hour1 > 23 || min < 0 || min > 59)
{
printf("Invalid \n");
return -1;
}
return 0;
}
//date operation
void data_Operation(char * o)
{
FILE *fp;
if(strcmp(o, "w" ) == 0)
{
fp = fopen(DATABASENAME, "w");
int i = 0;
while(i < number_flightdatas)
{
fprintf(fp, "%s %02d %02d %02d %02d %s %02d %02d %02d %02d\n",
flightdatas[i].flightdataCode, flightdatas[i].departure_date.mnth,
flightdatas[i].departure_date.day, flightdatas[i].departure_date.hour,
flightdatas[i].departure_date.minute, flightdatas[i].arrivalcity,
flightdatas[i].arrival_dt.mnth, flightdatas[i].arrival_dt.day,
flightdatas[i].arrival_dt.hour, flightdatas[i].arrival_dt.minute);
++i;
}
fclose(fp);
}
//compare the function
else if(strcmp(o, "r" ) == 0)
{
fp = fopen(DATABASENAME, "r");
char currentline[33];
if(!fp)
{
flightdata_t databaseflightdatas[MAXIMUM_number_flightdatas];
int currentflightdatas = 0;
while (fgets(currentline, sizeof(currentline), fp) != NULL) {
int tracker = 0;
char * parser = strtok(currentline, " ");
char *delimitedWord[50];
while(parser != NULL)
{
delimitedWord[tracker++] = parser;
parser = strtok (NULL, " ");
}
flightdata_t nflightdata;
strcpy(nflightdata.flightdataCode, delimitedWord[0]);
int charToInt = atoi(delimitedWord[1]);
nflightdata.departure_date.mnth = charToInt;
charToInt = atoi(delimitedWord[2]);
nflightdata.departure_date.day = charToInt;
charToInt = atoi(delimitedWord[3]);
nflightdata.departure_date.hour = charToInt;
charToInt = atoi(delimitedWord[4]);
nflightdata.departure_date.minute = charToInt;
strcpy(nflightdata.arrivalcity,delimitedWord[5]);
charToInt = atoi(delimitedWord[6]);
nflightdata.arrival_dt.mnth = charToInt;
charToInt = atoi(delimitedWord[7]);
nflightdata.arrival_dt.day = charToInt;
charToInt = atoi(delimitedWord[8]);
nflightdata.arrival_dt.hour = charToInt;
charToInt = atoi(delimitedWord[9]);
nflightdata.arrival_dt.minute = charToInt;
databaseflightdatas[currentflightdatas++] = nflightdata;
}
if(number_flightdatas != 0){
memset(flightdatas, 0, sizeof(flightdatas));
}
int i = 0;
while(i < currentflightdatas)
{
flightdatas[i] = databaseflightdatas[i];
++i;
}
number_flightdatas = currentflightdatas;
}
else
{
printf(" error\n");
}
fclose(fp);
}
main();
}