-
Notifications
You must be signed in to change notification settings - Fork 1
/
display.c
215 lines (198 loc) · 8.78 KB
/
display.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* Handles functions related to displaying information on the console.
* This could include functions for printing tables and data information.
*
* @file Display.c
* @author Pranav Kale pkale
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "display.h"
#include "flights.h"
#include "input.h"
/**
Compare countries a and b
This method is used as a comparator for the QSort() method
@param a void constant pointer to first country
@param b void constant pointer to second country
@return string comparison of both
*/
int compareCountries(const void *a, const void *b) {
return strcmp((*(CountryNode **)a)->country, (*(CountryNode **)b)->country);
}
/**
* Display all the flights as a row in the table based on given 2d array
*
*/
void displayFlightTableRow(FlightDatabase *fdatab)
{
for (int i = 0; i < fdatab->count; i++) {
Flight *flight = fdatab->flight[i];
printf("| %-21s | %-5s | %-21s | %-10s | %-9s | %-20s |\n",
flight->operatorName,
flight->flightID,
flight->departureAirport,
flight->departTime,
flight->arrivalTime,
flight->arrivalAirport);
}
}
/**
* Displays the Table Header for all flights
* Includes: Airline of flight, flight ID, Origin, Depart time, Arrival, Flight Total
*/
void displayFlightTableHeader()
{
printf("| Airline | ID | Origin | Departed | Arrival | Destination |\n");
printf("|-----------------------+-------+-----------------------+------------+-----------+----------------------+\n");
}
/**
* Clears the console for the user
* This is called every time user runs one of the show methdos
* Called after show flight table option, show flight data option, show airports
*/
void clearConsole()
{
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
/**
* Display's all the menu options to user
*/
void displayMenuOptions(const char options[][50])
{
for (int i = 0; i < 6; i++) {
printf("%s\n", options[i]);
}
}
/**
* Display specific information for One Flight (NOT A TABLE)
* Includes ID, # of seats, engine type, Plane (Ex. Boeing 777), Pilot,
* Total miles, Total trips, and Operator (Ex.American Airlines)
*/
void displaySingleFlightData(FlightDatabase *fdatab, char const *str) {
bool exists = false;
for (int i = 0; i < fdatab->count; i++) {
Flight *flight = fdatab->flight[i];
if (strcmp(flight->flightID, str) == 0) {
exists = true;
printf("Flight found!\n\n");
printf("[FLIGHT DATE]: %s\n", flight->date);
printf("\n.------------------------------------------------------.\n");
int val = 25 - strlen(flight->operatorName) - strlen(flight->flightID);
printf("|%s %s", flight->operatorName, flight->flightID);
for (int i = 0; i < val; i++) {
putchar(' ');
}
printf("| |\n");
printf("| | |\n");
printf("| .-'-. |\n");
printf("| ' === ' |\n");
printf("| \\-----' .-. '-----/ |\n");
printf("| ——————————————————' '-' '———————————————————— |\n");
printf("| '''''-|--/ \\==^'--T--'^==// \\--|-''''' |\n");
printf("| \\___/ O \\___/ |\n");
printf("| |\n");
printf(".------------------------------------------------------.\n");
printf("\nFlight Departing At: %s\n", flight->departTime);
printf("Flight Arriving At: %s\n", flight->arrivalTime);
printf("-------------------------\n");
printf("Departure Airport: %s\n", flight->departureAirport);
printf("Arrival Airport: %s\n", flight->arrivalAirport);
printf("-------------------------\n");
printf("Pilot Name: %s\n", flight->pilot);
printf("Seats Available: %d\n", flight->seats);
printf("Aircraft Type: %s\n", flight->aircraftType);
printf("-------------------------\n");
printf("\nEnter \" more data \" for additional information.\n\n");
while (true) {
printf("cmd> ");
char * adding;
adding = readLine(stdin);
if (strcmp(adding, "more data") == 0) {
double avgMile = calculateAverageMilesPerFlight(flight->totalMiles, flight->totalTrips);
char *fTime = calculateFlightTime(flight->departTime, flight->arrivalTime);
double earnings = calculateTotalRevenue(flight->seats, 409.99);
printf("\nThis plane's average miles per trip --> %.2f miles\n", avgMile);
printf("Total Travel Duration --> %s\n", fTime);
printf("Total Expected Earnings --> $%.2f\n", earnings);
free(fTime);
}
break;
}
break;
}
}
if (!exists) {
printf("Unknown Flight ID, please Re-Enter.\n\n");
}
}
/**
* Display the Header for the Airport Information to Console
*/
void dispAirportHeader()
{
printf("| Country | Airport |\n");
printf("|-----------------+-----------------------+\n");
}
/**
Display all available airports in the current flight database
@param fdatab the database to check
*/
void displayAllAirports(FlightDatabase *fdatab) {
HashTable *table = createTable(TABLE_SIZE);
for (int i = 0; i < fdatab->count; i++) {
Flight *flight = fdatab->flight[i];
insertAirport(table, flight->departureCountry, flight->departureAirport);
}
// Collect all countries into an array for sorting
CountryNode *countryArray[table->size];
int countryCount = 0;
for (int i = 0; i < table->size; i++) {
CountryNode *node = table->buckets[i];
while (node != NULL) {
countryArray[countryCount++] = node;
node = node->next;
}
}
// Sort the array of countries
qsort(countryArray, countryCount, sizeof(CountryNode *), compareCountries);
// Print the sorted list of countries and their airports
dispAirportHeader();
for (int i = 0; i < countryCount; i++) {
CountryNode *countryNode = countryArray[i];
AirportNode *airportNode = countryNode->airports;
while (airportNode != NULL) {
printf("| %-15s | %-21s |\n",
countryNode->country,
airportNode->airport);
airportNode = airportNode->next;
}
}
freeHashTable(table);
}
void displayApp() {
printf("\n.————————————————————————————————————————————————————————————————.\n");
printf("| __ __ __ __ |\n");
printf("| \\ \\ / /___ | | __ ___ _ __ ___ | |_ ___ |\n");
printf("| \\ \\//\\/ // -_)| |/ _|/ _ \\| ' \\ / -_) | _|/ _ \\ |\n");
printf("| \\_//\\_/ \\___||_|\\__|\\___/|_|_|_|\\___| \\__|\\___/ |\n");
printf("| _____ |\n");
printf("| ___ _ _ _ _ | \\ |\n");
printf("| | __|| |(_) __ _ | |_ | |_ | \\_________________ |\n");
printf("| | _| | || |/ _` || ' \\| _| |______ \\_____\\___ |\n");
printf("| |_| |_||_|\\__, ||_||_|\\__| \\____/__,-------,________) |\n");
printf("| |___/ \\ / |\n");
printf("| __ __ |____/__ |\n");
printf("| | \\/ | __ _ _ _ __ _ __ _ ___ _ _ |\n");
printf("| | |\\/| |/ _` || ' \\ / _` |/ _` |/ -_)| '_| |\n");
printf("| |_| |_|\\__,_||_||_|\\__,_|\\__, |\\___||_| |\n");
printf("| |___/ |\n");
printf(".————————————————————————————————————————————————————————————————.\n");
printf("License and Copyright @ 2024 - Pranav Kale - [email protected]\n\n");
}