Travel Monitoring System Program in C code

Travel Monitoring System in C:

In C, the travel monitoring system is based on the concept of maintaining tour packages for tourists. Before stepping into the main system a user has to pass through the login system to get access, The purpose of the Travel Monitoring System Program in C efficiently organizes the booking of tickets.

Travel Management System Program in C:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
enum state{menu,loggedin};
enum state currentstate=menu;
typedef struct user
{
    char username[100];
    char password[100];
    char place[100];
    float price;
    int numtick;
    struct user *next;
}user;
void ShowBrochure();
user* InitializeList(user*);
user* AddUser(user*);
void LoginUser(user*);
void BookTicket(user*);
void PrintTicket(user*);
void CancelTicket(user*);
void ChangePassword(user*);
void LogoutUser();
void CheckTicket(user*);
void DisplayAll(user*);
void WriteToFile(user*);
void ExitProgram();
char currentuser[100];
int main()
{
    printf("\t\t\t**** WEBEDUCLICK TRAVEL MONITORING SYSTEM ****\n");
    user *h=NULL;
    int ch1,ch2;
    h=InitializeList(h);
    while (1)
    {
    	
        if(currentstate==menu)
        {
        	
            printf("\n\t\t\t\tAdd User - 1\n\t\t\t\tLogin User - 2\n\t\t\t\tBrochure - 3\n\t\t\t\tExit - 4\n\n\t\t\t\tEnter:");
            scanf("%d",&ch1);
            switch(ch1)
            {
                case 1:
                    h=AddUser(h);
                    break;
                case 2:
                    LoginUser(h);
                    break;
                case 3:
                    ShowBrochure();
                    break;
                case 4:
                    ExitProgram();
                    exit(0);
                    break;
                default:
                    printf("Not a valid input at this stage\n");
            }
        }
        else if(currentstate==loggedin)
        {
        system("CLS");
        printf("\n\t\t\t\t************************************");
		printf("\n\t\t\t\tWEBEDUCLICK TRAVEL MONITORING SYSTEM");
		printf("\n\t\t\t\t************************************\n");	
            printf("\n\t\t\t\tBook Package - 1\n\t\t\t\tCheck Ticket - 2\n\t\t\t\tPrint Ticket - 3\n\t\t\t\tCancel Ticket - 4\n\t\t\t\tChange Password - 5"
                   "\n\t\t\t\tLogout User - 6\n\t\t\t\tBrochure - 7\n\t\t\t\tExit - 8\n");
            scanf("%d",&ch2);
            switch(ch2)
            {
                case 1:
                    BookTicket(h);
                    system("PAUSE");
                    system("CLS");
                    break;
                case 2:
                    CheckTicket(h);
                    system("PAUSE");
                    system("CLS");
                    break;
                case 3:
                    PrintTicket(h);
                    system("PAUSE");
                    system("CLS");
                    break;
                case 4:
                    CancelTicket(h);
                    system("PAUSE");
                    system("CLS");
                    break;
                case 5:
                    ChangePassword(h);
                    system("PAUSE");
                    system("CLS");
                    break;
                case 6:
                    LogoutUser(h);
                    system("PAUSE");
                    system("CLS");
                    break;
                case 7:
                    ShowBrochure();
                    system("PAUSE");
                    system("CLS");
                    break;
                case 8:
                    ExitProgram();
                    exit(0);
                    break;
                default:
                    printf("Not a valid input at this stage\n");
            }
        }
    }
    return 0;
}
user* InitializeList(user *h)
{
    user* t,*ptr,temp;
    FILE *fp;
    int cc=0,x;
    float ff;
    fp=fopen("users.txt","r");

    if(fp==NULL)
        return NULL;

    if(fgetc(fp)==EOF)
        return NULL;

    rewind(fp);
	while(fscanf(fp,"%s %s %s %f %d",temp.username,temp.password,temp.place,&temp.price,&temp.numtick)!=EOF)
	{
		ptr=(user*)malloc(sizeof(user));
		strcpy(ptr->username,temp.username);
		strcpy(ptr->password,temp.password);
		strcpy(ptr->place,temp.place);
		ptr->price=temp.price;
		ptr->numtick=temp.numtick;
		ptr->next=NULL;

		if(h==NULL)
            h=t=ptr;
		else
		{
			h->next=ptr;
			h=ptr;
		}
	}
	fclose(fp);
    return t;
}
void WriteToFile(user *h)
{
    FILE *fp;
    fp=fopen("users.txt","w");
    while(h!=NULL)
    {
        fprintf(fp,"%s %s %s %f %d\n",h->username,h->password,h->place,h->price,h->numtick);
        h=h->next;
    }
    fclose(fp);
}
void ShowBrochure()
{
	system("CLS");
    printf("\tPRICE LIST\n=============================\n\n1. TM - Agra Taj Mahal Tours  - Rs 10000\n2. GC - Grand Canyon Tours - Rs 50000\n3. ZR - Zurich Tours - Rs 65000\n4. MM - Miami Vacation - Rs 40000\n"
           "5. SG - Singapore Tours - Rs 40000\n6. LV - Las Vegas Tours - Rs 70000\n7. SF - San Francisco Tours - Rs 50000\n8. PR - Paris Tours - Rs 45000\n9. LD - London Vacation - Rs 40000\n10. CT - Cape Town Tours - Rs 35000\n");
}
void CheckTicket(user *h)
{
    while(h!=NULL)
    {
        if(!strcmp(h->username,currentuser))
            break;
        h=h->next;
    }
    if(!strcmp(h->place,"\0") || h->price==0.0 || h->numtick==0)
    {
        printf("You do not have a ticket booked yet\n");
        return;
    }
    float total=0.0;
    total=(h->price)*(h->numtick);
    printf("You have booked %d tickets for a sum total of Rs %f for tour code %s\n",h->numtick,total,h->place);
}
user* AddUser(user* h)
{
    user *t;
    t=h;
    user *nw;
    nw=(user*)malloc(sizeof(user));
    fflush(stdin);
    printf("Enter username or email\n");
    scanf("%s",nw->username);
    while(h!=NULL)
    {
        if(!strcmp(h->username,nw->username))
        {
            printf("That email already exists\n");
            return t;
        }
        h=h->next;
    }
    h=t;
    fflush(stdin);
    printf("Enter password\n");
    scanf(" %[^\n]s",&nw->password);
    nw->next=NULL;
    strcpy(nw->place,"N/A");
    nw->price=0.0;
    nw->numtick=0;

    if(h==NULL)
    {
        h=t=nw;
    }
    else
    {
        while(h->next!=NULL)
        {
            h=h->next;
        }
        h->next=nw;
    }
    WriteToFile(t);
    return t;
}
void LoginUser(user* h)
{
    char username[100];
    char password[100];
    fflush(stdin);
    printf("\n\n");
    printf("\t\tEnter Email/Username:\n\t\t");
    scanf("%s",username);
    fflush(stdin);
    printf("\n\t\tEnter Password:\n\t\t");
    scanf(" %[^\n]s",password);
    while(h!=NULL)
    {
        if((!strcmp(h->username,username)) && (!strcmp(h->password,password)))
        {
            currentstate=loggedin;
            strcpy(currentuser,username);
            
            printf("\n\t\tLogin successful!\n");
            system("PAUSE");
            return;
        }
        else if((!strcmp(h->username,username)) && (strcmp(h->password,password)))
        {
            printf("Password mismatch\n");
            return;
        }
        h=h->next;
    }
    printf("Sorry, no such user record was found\n");
}
void BookTicket(user *h)
{
    user *t=h;
    char place[100];
    while(h!=NULL)
    {
        if(!strcmp(h->username,currentuser))
            break;
        h=h->next;
    }
    if(h==NULL)
        return;
    if(h->price!=0.0)
    {
        printf("You must cancel your previous ticket before buying a new one\n");
        return;
    }
    ShowBrochure();
    float pricelist[]={10000.0,50000.0,65000.0,40000.0,40000.0,70000.0,50000.0,45000.0,40000.0,350000.0};
    fflush(stdin);
    printf("\nEnter place code (eg: ZR, LV)\n");
    scanf(" %[^\n]s",place);
    char choice;
    fflush(stdin);
    printf("\nWould You Like to Confirm Booking?\n[1] - Yes\n[2] - No\n");
    scanf("%c",&choice);
    float price;
    if(choice!='1')
        return;
    if(strcmp(place,"TM")==0)
        price=pricelist[0];
    else if(strcmp(place,"GC")==0)
        price=pricelist[1];
    else if(strcmp(place,"ZR")==0)
        price=pricelist[2];
    else if(strcmp(place,"MM")==0)
        price=pricelist[3];
    else if(strcmp(place,"SG")==0)
        price=pricelist[4];
    else if(strcmp(place,"LV")==0)
        price=pricelist[5];
    else if(strcmp(place,"SF")==0)
        price=pricelist[6];
    else if(strcmp(place,"PR")==0)
        price=pricelist[7];
    else if(strcmp(place,"LD")==0)
        price=pricelist[8];
    else if(strcmp(place,"CT")==0)
        price=pricelist[9];
    else
    {
        printf("That tour code doesn't exist\n");
        return;
    }
    printf("Enter the number of tickets you want to book?\n");
    scanf("%d",&h->numtick);
    if(h->numtick==0)
        return;
    strcpy(h->place,place);
    h->price=price;
    WriteToFile(t);
    printf("Bookings Done!!\n");
    system("PAUSE");
    
}
void PrintTicket(user *h)
{
    while(h!=NULL)
    {
        if(!strcmp(h->username,currentuser))
            break;
        h=h->next;
    }
    if(!strcmp(h->place,"\0") || h->price==0.0 || h->numtick==0)
    {
        printf("You do not have a ticket booked yet\n");
        return;
    }
    float total=0.0;
    total=(h->price)*(h->numtick);
    FILE *fp;
    char filename[50];
    strcpy(filename,h->username);
    strcat(filename,"_ticket.txt");
    fp=fopen(filename,"w");
    if(fp==NULL)
    {
        printf("Problem opening the file\n");
        return;
    }
    if(fgetc(fp)==EOF)
    {
        fprintf(fp,"TOURISM TICKET\n===============\n\n");
    }
    fprintf(fp,"Email ID: %s\nTour Code: %s\nTicket Cost: Rs %f\nNumber of tickets: %d\nTotal Cost: Rs %f\n",h->username,h->place,h->price,h->numtick,total);
    fclose(fp);
}
void CancelTicket(user *h)
{
    user *t=h;
    while(h!=NULL)
    {
        if(!strcmp(h->username,currentuser))
            break;
        h=h->next;
    }

    int flag=-1;

    if(h==NULL)
        printf("No such user\n");

    if(strcmp(h->place,"LL")==0)
        flag++;
    else if(strcmp(h->place,"JK")==0)
        flag++;
    else if(strcmp(h->place,"SK")==0)
        flag++;
    else if(strcmp(h->place,"SHM")==0)
        flag++;
    else if(strcmp(h->place,"AND")==0)
        flag++;
    else if(strcmp(h->place,"BHB")==0)
        flag++;
    else if(strcmp(h->place,"AG")==0)
        flag++;
    else if(strcmp(h->place,"ND")==0)
        flag++;
    else if(strcmp(h->place,"RJ")==0)
        flag++;
    else if(strcmp(h->place,"SI")==0)
        flag++;
    else
    {
        printf("You haven't booked a tour yet\n");
        return;
    }
    if(flag==0)
    {
        printf("Your ticket has been successfully cancelled\nYour refund of Rs %f for Tour Code %s for %d tickets will soon be credited to your account\n",h->price,h->place,h->numtick);
        strcpy(h->place,"N/A");
        h->price=0.0;
        h->numtick=0;
        WriteToFile(t);
    }
}
void ChangePassword(user *h)
{
    user *t=h;
    char passcurr[100];
    fflush(stdin);
    printf("Enter your current password to continue:\n");
    scanf(" %[^\n]s",passcurr);
    while(h!=NULL)
    {
        if(!strcmp(h->username,currentuser))
            break;
        h=h->next;
    }
    if(h==NULL)
        return;
    if(!strcmp(passcurr,h->password))
    {
        printf("Enter new password:\n");
        scanf(" %[^\n]s",h->password);
    }
    WriteToFile(t);
}
void LogoutUser()
{
    if(currentstate==menu || strcmp(currentuser,"\0")==0)
    {
        printf("You must be logged in to logout\n");
        return;
    }
    strcpy(currentuser,"\0");
    currentstate=menu;
    printf("You have been successfully logged out\n");
}
void ExitProgram()
{
    printf("Press \"Enter/Return\" to exit");
    char exitprog;
    fflush(stdin);
    scanf("%c",&exitprog);
}

Output-1:

Travel Monitoring System Program in C code

Output-2:

Travel Monitoring System Program in C code

Output-3:

Travel Monitoring System Program in C code

Output-4:

Travel Monitoring System Program in C code

Output-5:

Travel Monitoring System Program in C code

Leave a Reply

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