Code

/*

* main.c

*

*  Created on: Mar 10, 2015

*      Author: Anonymous

*/

#include <stdio.h>

#include <stdlib.h>

#include <strings.h>

#include <string.h>

#define ROOMS 10

typedef struct room_t {

char* number;

char* customer;

int availability;

} room_t;

void initialize(FILE*, room_t**);

void checkAvailabilitySingle(FILE*, room_t**);

void checkAvailabilityDouble(FILE*, room_t**);

void reserveRoom(char*, char*, room_t**);

void checkoutRoom(char*, room_t**);

void exitAndFree(char*, room_t**);

int main(int argc, char** argv) {

FILE *input, *singleRooms, *doubleRooms;

struct room_t** rooms;

rooms = (room_t**) malloc(100 * sizeof(int));

input = fopen(“rooms.txt”, “r”);

initialize(input, rooms);

char temp[265];

char file[265];

while (1) {

scanf(“%s”, temp);

if (strcmp(temp, “vacancy”) == 0) {

scanf(“%s”, temp);

if (strcmp(temp, “single”) == 0) {

scanf(“%s”, temp);

singleRooms = fopen(temp, “w”);

checkAvailabilitySingle(singleRooms, rooms);

}

else if (strcmp(temp, “double”) == 0) {

scanf(“%s”, temp);

doubleRooms = fopen(temp, “w”);

checkAvailabilityDouble(doubleRooms, rooms);

}

else

printf(“Invalid Argument!”);

}

else if (strcmp(temp, “reserve”) == 0) {

scanf(“%s”, temp);

scanf(“%s”, file);

scanf(“%s”, file);

reserveRoom(temp, file, rooms);

}

else if (strcmp(temp, “checkout”) == 0) {

scanf(“%s”, temp);

checkoutRoom(temp, rooms);

}

else if (strcmp(temp, “exit”) == 0) {

scanf(“%s”, temp);

exitAndFree(temp, rooms);

break;

}

else {

printf(“Invalid Argument!”);

}

}

return 0;

}

void initialize(FILE* input, room_t** rooms) {

if (input == NULL) {

fprintf(stderr, “Could not open file\n”);

return;

}

if (rooms == NULL)

return;

printf(“Initialization\n”);

int i;

for (i = 0; i < ROOMS; i++) {

rooms[i] = (room_t*) malloc(sizeof(room_t));

rooms[i]->number = (char*) malloc(sizeof(char*));

rooms[i]->customer = (char*) malloc(sizeof(char*));

fscanf(input, “%s %s %d”, rooms[i]->number, rooms[i]->customer,

&rooms[i]->availability);

printf(“%s %s %d\n”, rooms[i]->number, rooms[i]->customer,

rooms[i]->availability);

}

fclose(input);

return;

}

void checkAvailabilitySingle(FILE* singleRooms, room_t** rooms) {

if (singleRooms == NULL) {

fprintf(stderr, “Could not open file\n”);

return;

}

fprintf(singleRooms, “Available Single Rooms:\n”);

printf(“Available Single Rooms:\n”);

int i;

for (i = 0; i < (ROOMS / 2); i++) {

if (rooms[i]->availability == 1) {

fprintf(singleRooms, “%s\n”, rooms[i]->number);

printf(“%s\n”, rooms[i]->number);

}

}

fclose(singleRooms);

return;

}

void checkAvailabilityDouble(FILE* doubleRooms, room_t** rooms) {

if (doubleRooms == NULL) {

fprintf(stderr, “Could not open file\n”);

return;

}

fprintf(doubleRooms, “Available Double Rooms:\n”);

printf(“Available Double Rooms:\n”);

int i;

for (i = (ROOMS / 2); i < ROOMS; i++) {

if (rooms[i]->availability == 1) {

fprintf(doubleRooms, “%s\n”, rooms[i]->number);

printf(“%s\n”, rooms[i]->number);

}

}

fclose(doubleRooms);

return;

}

void reserveRoom(char* roomNumber, char* customerName, room_t** rooms) {

int i;

for (i = 0; i < ROOMS; i++) {

if (strcmp(rooms[i]->number, roomNumber) == 0) {

if (rooms[i]->availability == 1) {

rooms[i]->customer = malloc(sizeof(char*));

strcpy(rooms[i]->customer, customerName);

rooms[i]->availability = 0;

printf(“The room %s is reserved to customer %s.\n”,

rooms[i]->number, rooms[i]->customer);

return;

} else {

printf(“the room is occupied!\n”);

return;

}

}

}

printf(“The room doesn’t exist\n”);

return;

}

void checkoutRoom(char* customerName, room_t** rooms) {

int i;

for (i = 0; i < ROOMS; i++) {

if (strcmp(rooms[i]->customer, customerName) == 0) {

rooms[i]->customer = “null”;

rooms[i]->availability = 1;

printf(“%s has check out from room %s\n”, customerName,

rooms[i]->number);

return;

}

}

printf(“%s is not reserved in the hotel\n”, customerName);

return;

}

void exitAndFree(char* out, room_t** rooms) {

FILE* output;

output = fopen(out, “w”);

if (output == NULL) {

fprintf(stderr, “Could not open file\n”);

return;

}

int i;

for (i = 0; i < ROOMS; i++) {

fprintf(output, “%s %s %d\n”, rooms[i]->number, rooms[i]->customer,

rooms[i]->availability);

}

fclose(output);

free(rooms);

return;

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s