#include #define yes 1 #define no 0 void menu(int); void ExChoice(char); void PrintAssigned(); void PrintEmpty(); void AssignSeat(); void SetIDs(); void RemoveSeat(); void ReadData(FILE *); void WriteData(); struct seat{ char seatID[2]; int assigned; char surname[20]; char firstname[20]; }; struct seat seats[12]; int main() { FILE *seatassignments; if((seatassignments = fopen("seats.txt", "r"))==NULL){ printf("No current data detected.\n\n"); SetIDs(); } else{ReadData(seatassignments);} menu(0); return 0; } void SetIDs(){ int x=0,y=48; for(x=0;x<12;x++){ y++; seats[x].seatID[0] = y; seats[x].seatID[1] = 'a'; x++; seats[x].seatID[0] = y; seats[x].seatID[1] = 'b'; } } void ReadData(FILE *seatassignments){ int i=0,size=0; for(i=0;i<12;i++){ fgets(seats[i].seatID, 3, seatassignments); fscanf(seatassignments, "%d", &seats[i].assigned); fgets(seats[i].surname, 21, seatassignments); fgets(seats[i].firstname, 21, seatassignments); if (seats[i].surname != NULL) { size = (strlen(seats[i].surname) - 1); if (seats[i].surname[size] == '\n') seats[i].surname[size] = '\0'; } if (seats[i].firstname != NULL) { size = (strlen(seats[i].firstname) - 1); if (seats[i].firstname[size] == '\n') seats[i].firstname[size] = '\0'; } } if(fclose(seatassignments) != 0){ printf("Error Closing Data File"); } } void menu(int call){ char choice; printf("Mersey Airlines Booking System\n"); printf("**************************************************"); printf("\n\na) Show assigned seats with passenger names\n"); printf("b) Show list of empty seats\n"); printf("c) Assign A customer to a seat\n"); printf("d) Delete a seat assignment\n"); if(call == 0){ printf("e) Quit\n"); } else{printf("\n");} printf("**************************************************"); printf("\n"); fflush(stdin); printf("Enter choice: "); choice = getchar(); if(choice == 'e' && call == 1){ printf("\n\nInvalid choice, menu feature not available at the present time\n\n"); system("pause"); menu(1); } ExChoice(choice); return; } void ExChoice(char choice){ switch(choice){ case 'a':PrintAssigned(); break; case 'b':PrintEmpty(); break; case 'c':AssignSeat(); break; case 'd':RemoveSeat(); break; case 'e':exit(1); break; default: printf("Incorrect menu choice\n"); system("pause"); system("cls"); menu(0); break; } } void PrintAssigned(){ int i=0; int AnyAssigned=no; printf("\nAssigned Seats:\n---------------\n\n"); for(i=0;i<12;i++){ if(seats[i].assigned == yes) { printf(" Seat ID: %s\nFirst Name: %s\n Last Name: %s\n\n", seats[i].seatID,seats[i].firstname,seats[i].surname); AnyAssigned = yes; } } if(AnyAssigned == no){ printf("\n\nThere are currently no assigned Seats\n\n"); } system("pause"); system("cls"); menu(1); } void PrintEmpty(){ int i=0; int AnyEmpty = no; printf("\nEmpty Seats:\n-------------\n\n"); for(i=0;i<12;i++){ if(seats[i].assigned == no) { printf("Seat ID: %s\n", seats[i].seatID); AnyEmpty = yes; } } if(AnyEmpty == no){ printf("\n\nThere are currently no Empty Seats\n\n"); } system("pause"); system("cls"); menu(1); } void AssignSeat(){ char TempSeatID[2]; int i=0; printf("\nPlease enter the seat ID you wish to assign a passenger to \n(type M to return to menu):\n\n"); scanf("%s",&TempSeatID); if(TempSeatID[0] == 'M'){ system("cls"); menu(1); } for(i=0;i<12;i++){ if(seats[i].seatID[0] == TempSeatID[0] && seats[i].seatID[1] == TempSeatID[1]) { printf("Seat Found\n\n"); break; } if(i==11){ printf("\n\nSeat Not on Record\n\n"); AssignSeat(); } } if(seats[i].assigned == yes) { printf("\n\nSorry but this seat Number is already assigned, please make another choice\n\n"); AssignSeat(); } printf("Please Enter the customers Surname: "); scanf("%s",&seats[i].surname); printf("\n\nPlease Enter the customers First Name: "); scanf("%s",&seats[i].firstname); seats[i].assigned = yes; printf("\n\nThank you, Seat %s Assigned to %s %s\n\n", seats[i].seatID,seats[i].firstname,seats[i].surname); system("pause"); system("cls"); WriteData(); menu(1); } void RemoveSeat(){ char TempSeatID[2]; int i=0; char sure; printf("Please Enter the Seat ID for which you wish to clear data: "); scanf("%s",&TempSeatID); for(i=0;i<12;i++){ if(seats[i].seatID[0] == TempSeatID[0] && seats[i].seatID[1] == TempSeatID[1]) { printf("Seat Found\n\n"); break; } if(i==11){ printf("\n\nSeat Not on Record\n\n"); RemoveSeat(); } } if(seats[i].assigned == no){ printf("This seat is not currently assigned\n\n"); system("pause"); system("cls"); menu(1); } printf("\n\nAre you sure you wish to remove the following data:\n\nSeat %s Assigned to %s %s\n\n", seats[i].seatID,seats[i].firstname,seats[i].surname); printf("Enter Y for yes and N for No: "); fflush(stdin); scanf("%c",&sure); if(sure=='Y'){ seats[i].assigned = no; printf("Seat Cleared, Thank you\n\n"); } system("pause"); system("cls"); WriteData(); menu(1); } void WriteData(){ int i=0; FILE *WriteSeatAssignments; if((WriteSeatAssignments = fopen("seats.txt", "w+"))==NULL) { printf("Error Creating Data File"); menu(1); } for(i=0;i<12;i++) { fputs(seats[i].seatID,WriteSeatAssignments); fprintf(WriteSeatAssignments, "%d", seats[i].assigned); fputs(seats[i].surname,WriteSeatAssignments); fprintf(WriteSeatAssignments,"\n"); fputs(seats[i].firstname,WriteSeatAssignments); fprintf(WriteSeatAssignments,"\n"); } if(fclose(WriteSeatAssignments)!=0){ printf("Error closing File"); } }