Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
9.0 kB
4
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#pragma warning(disable:4996)
#define priceperHours 6.00
void addbooking();
void deletebooking();
void displaybooking();
void modifybooking(char *);
void calculation();
void searchbooking();
struct bookingDate {
	int year, month, day;
};

struct bookingTime {
	int hours, minutes, endHours, endMinutes;
	char time;
};

typedef struct booking {
	char memberID[10];
	char facilityID[10];
	int priceBooking;
	struct bookingDate bookDate;
	struct bookingTime bookTime;
};

typedef struct member
{
	booking book;

};

member memberArray[100] = {};
member currentMember;
FILE* managebook;
FILE* memberdata;
booking book;



void main()
{

	SYSTEMTIME x;
	GetLocalTime(&x);
	printf("Date:%2d/%2d/%4d, Time:%d:%d\n\n", x.wDay, x.wMonth, x.wYear, x.wHour, x.wMinute);
	int choice;
	int icount = 0;
	char time;
	char search, modify;
	int option;
	printf("\n**********************************************");
	printf("\nBooking System");
	printf("\n**********************************************");
	printf("\n1. Add Booking");
	printf("\n2. Modify Booking");
	printf("\n3. Search Booking");
	printf("\n4. Delete Booking");
	printf("\n5. Exit");
	printf("\n**********************************************");
	printf("\nPlease enter your option< 1 / 2 / 3 / 4 / 5>: ");
	scanf("%d", &choice);
	switch (choice)
	{
	case 1:
		addbooking();
		break;
	case 2:
		modifybooking(&time);
		break;
	case 3:
		searchbooking();
		break;
		case 4:
			deletebooking();
			break;
		case 5:
			exit(-1);
		break;
	default:
		break;
	}
	

}

void displaybooking() {
	printf("Member ID | Facility ID |	Times\t\n");
	printf("%5s  %11s %12d.%2d to %2d.%2d\n", currentMember.book.memberID, currentMember.book.facilityID,
		currentMember.book.bookTime.hours, currentMember.book.bookTime.minutes, currentMember.book.bookTime.endHours, currentMember.book.bookTime.endMinutes);
	
	
}

void calculation()
{
	int finalPrice_hours, calculation_price_hours, calculation_price_minutes;
	float price_per_minutes, finalPrice, finalPrice_minutes;
	
	if (currentMember.book.bookTime.hours >= 12 && currentMember.book.bookTime.time == 'P')
	{
		currentMember.book.bookTime.hours += 12;
	}
	else if (currentMember.book.bookTime.endHours >= 12 && currentMember.book.bookTime.time == 'P')
	{
		currentMember.book.bookTime.hours += 12;
	}
	if (currentMember.book.bookTime.minutes == 0 || currentMember.book.bookTime.endMinutes == 0)
	{
		price_per_minutes = priceperHours / 60;
		calculation_price_hours = currentMember.book.bookTime.endHours - currentMember.book.bookTime.hours;
		calculation_price_minutes = currentMember.book.bookTime.endMinutes - currentMember.book.bookTime.minutes;
		finalPrice_hours = calculation_price_hours * priceperHours;
		finalPrice_minutes = (float)calculation_price_minutes * price_per_minutes;
	}
	else
	{
		price_per_minutes = priceperHours / 60;
		calculation_price_hours = currentMember.book.bookTime.endHours - currentMember.book.bookTime.hours;
		calculation_price_minutes = currentMember.book.bookTime.endMinutes - currentMember.book.bookTime.minutes;
		finalPrice_hours =(float) calculation_price_hours * priceperHours;
		finalPrice_minutes = (float)calculation_price_minutes * price_per_minutes;
	}
	finalPrice = (float)finalPrice_hours + finalPrice_minutes;
	printf("Total Price:%.2f \n", finalPrice);
}

void addbooking()
{
	static int icount = 0;
	int add;
	char time;
	managebook = fopen("bookingData.bin", "ab");
	if (!managebook)
	{
		printf("Cannot open the file\n");
	}
	do
	{


		printf("The Price of Booking for 1 Facility in 1 Hour is RM5\n");
		printf("Enter your Member ID: ");
		scanf(" %s", &currentMember.book.memberID);


		printf("Enter Facility ID: ");
		scanf(" %s", &currentMember.book.facilityID);
		rewind(stdin);

		printf("Booking Date:  ");
		scanf("%d %d %d", &currentMember.book.bookDate.day, &currentMember.book.bookDate.month, &currentMember.book.bookDate.year);
		printf("\n");

		printf("Booking Time:\n");
		printf("AM(A) or PM(P)?: ");
		scanf(" %c", &time);
		printf("From (Need to input 00): ");
		scanf("%2d:%2d", &currentMember.book.bookTime.hours, &currentMember.book.bookTime.minutes);

		printf("To (Need to input 00): ");
		scanf("%2d:%2d", &currentMember.book.bookTime.endHours, &currentMember.book.bookTime.endMinutes);
		if (time == 'A' && currentMember.book.bookTime.hours >= 12)
		{
			printf("Its closed\n");
			printf("Please enter again: ");
			scanf("%2d:%2d", &currentMember.book.bookTime.hours, &currentMember.book.bookTime.minutes);
			printf("To : ");
			scanf("%2d:%2d", &currentMember.book.bookTime.endHours, &currentMember.book.bookTime.endMinutes);
		}
		else
		{
			calculation();
		}


		displaybooking();
		currentMember = memberArray[icount];
		icount++;
		fwrite(&currentMember, sizeof(member), 1, managebook);
		printf("Do you want to add?No(-1), Yes(1) : ");
		scanf("%d", &add);

	} while (add != -1);
	
	fclose(managebook);
}

void modifybooking(char * times)
{
	char askformodify;
	managebook = fopen("bookingData.bin", "ab");
	memberdata = fopen("member.dat", "rb");
	if (!managebook)
	{
		printf("Cannot open the file\n");
	}
	if (!memberdata)
	{
		printf("Cannot open the file\n");
	}
	printf("Do you want to change?Yes (Y), No(N): ");
	scanf(" %c", &askformodify);
	if (askformodify == 'N' || askformodify == 'n')
	{
		exit(-1);
	}
	printf("Enter your member ID: ");
	scanf(" %s", &currentMember.book.memberID);
	while (fread(&currentMember, sizeof(member), 1, managebook) != 0)
	{
		for (int i = 0; i < 100; i++) {
			if (strcmp(currentMember.book.memberID, memberArray[i].book.memberID) == 0) {
				memberArray[i] = currentMember ;
			
			}
			
		}
	}
	printf("Enter your new details:\n");
	printf("FACILITY ID: ");
	scanf(" %s", currentMember.book.facilityID);
	printf("Booking Date:  ");
	scanf("%d %d %d", &currentMember.book.bookDate.day, &currentMember.book.bookDate.month, &currentMember.book.bookDate.year);
	printf("AM(A) or PM(P)?: ");
	scanf(" %c", &times);
	printf("From (Need to input 00): ");
	scanf("%2d:%2d", &currentMember.book.bookTime.hours, &currentMember.book.bookTime.minutes);
	printf("To (Need to input 00): ");
	scanf("%2d:%2d", &currentMember.book.bookTime.endHours, &currentMember.book.bookTime.endMinutes);
	printf("   Member ID |         Facility ID |	      Times\t\n");
	printf("%10s  %19s %13d.%2d to %2d.%2d\n", currentMember.book, currentMember.book.facilityID, currentMember.book.bookTime.hours, currentMember.book.bookTime.minutes
		, currentMember.book.bookTime.endHours, currentMember.book.bookTime.endMinutes);
	fwrite(&currentMember, sizeof(member), 1, managebook);
	fclose(managebook);
	fclose(memberdata);

}
	void searchbooking() {
		managebook = fopen("bookingData.bin", "ab");
		memberdata = fopen("member.dat", "rb");
		if (!memberdata)
		{
			printf("Cannot open the file\n");
		}
		if (!managebook)
		{
			printf("Cannot open the file\n");
		}
			printf("Enter your member ID: ");
			scanf(" %s", &currentMember.book.memberID);
		while (fread(&currentMember, sizeof(member), 1, managebook) != 0)
		{
			for (int i = 0; i < 100; i++) {
				if (strcmp(currentMember.book.memberID, memberArray[i].book.memberID)== 0) {
					memberArray[i] = currentMember  ;
					printf("   Member ID |         Facility ID |	      Times\t\n");
					printf("%10s  %19s %13d.%2d to %2d.%2d\n", currentMember.book, currentMember.book.facilityID, currentMember.book.bookTime.hours, currentMember.book.bookTime.minutes
						, currentMember.book.bookTime.endHours, currentMember.book.bookTime.endMinutes);
				}
			}
		}
		fclose(managebook);
		fclose(memberdata);
	}

	void deletebooking() {
		managebook = fopen("bookingData.bin", "ab+");
		memberdata = fopen("member.dat", "rb");
		if (!memberdata)
		{
			printf("Cannot open the file\n");
		}
		if (!managebook)
		{
			printf("Cannot open the file\n");
		}
		char askfordelete;
		printf("If you want to delete(Y  = YES)(N = NO): ");
		scanf(" %c", &askfordelete);
		printf("Enter your Member ID: ");
		scanf(" %s", &currentMember.book.memberID);
		while (fread(&currentMember, sizeof(member), 1, managebook) != 0)
		{
			for (int i = 0; i < 100 - 1; i++) {
				if (strcmp(currentMember.book.memberID, memberArray[i].book.memberID) == 0) {
					memberArray[i + 1] = memberArray[i];
					memberArray[i + 2] = memberArray[i + 1];
					memberArray[i + 3] = memberArray[i + 2];
					memberArray[i + 4] = memberArray[i + 3];
					memberArray[i + 5] = memberArray[i + 4];
					memberArray[i + 6] = memberArray[i + 5];
				}
			}
			printf("Delete Successfully\n");
		}
		fwrite(&currentMember, sizeof(member), 1, managebook);
		fclose(managebook);
		fclose(memberdata);
	}