0% found this document useful (0 votes)
16 views6 pages

44 - Muhammad Nabil Asrofi Vehicle Parking Management System

This C program implements a vehicle parking management system with the following key features: - It defines the maximum number of parking levels, slots per floor, and log entries that can be stored. - Structures are used to store parking slot data including vehicle ID, entry/exit time, level, slot number, and fee. - Functions are included to park a new vehicle, retrieve a parked vehicle, display the parking status and logs. - The main menu allows the user to select these options and a random parking function is also included.

Uploaded by

nasrofi264
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

44 - Muhammad Nabil Asrofi Vehicle Parking Management System

This C program implements a vehicle parking management system with the following key features: - It defines the maximum number of parking levels, slots per floor, and log entries that can be stored. - Structures are used to store parking slot data including vehicle ID, entry/exit time, level, slot number, and fee. - Functions are included to park a new vehicle, retrieve a parked vehicle, display the parking status and logs. - The main menu allows the user to select these options and a random parking function is also included.

Uploaded by

nasrofi264
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Nama : Muhammad Nabil Asrofi

NRP : 4122600044
Kelas : 2 D4 Mekatronika B

// Vehicle Parking Management


#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

#define MAX_LEVEL 3
#define MAX_SLOT_PER_FLOOR 10
#define MAX_LOGS 1000
#define MAX_LOG_LENGTH 100

char logs[MAX_LOGS][MAX_LOG_LENGTH];
char log_buffer[MAX_LOG_LENGTH];
int log_count = 0;

void add_log(const char* log) {


if (log_count < MAX_LOGS) {
strncpy(logs[log_count], log, MAX_LOG_LENGTH - 1);
logs[log_count][MAX_LOG_LENGTH - 1] = '\0'; // ensure null
termination
log_count++;
}
}

void display_logs() {
for (int i = 0; i < log_count; i++) {
printf("%s\n", logs[i]);
}
}

struct parking_slot {
char name[50];
time_t entry_time;
time_t exit_time;
int parked_level;
int parked_slot;
double parking_fee;
bool is_parked;
};

struct parking_slot slot[MAX_LEVEL][MAX_SLOT_PER_FLOOR];


int park_new() {
for (int i = 0; i < MAX_LEVEL; ++i) {
for (int j = 0; j < MAX_SLOT_PER_FLOOR; ++j) {
if (slot[i][j].is_parked == false) {
bool conflict;
char new_name[50];
do {
conflict = false;
printf(" _______________________________________\n");
printf("> Enter vehicle ID \t: ", i, j);
scanf(" %49s", &new_name); // read the vehicle ID
for (int k = 0; k < MAX_LEVEL; ++k) {
for (int l = 0; l < MAX_SLOT_PER_FLOOR; ++l) {
if (slot[k][l].is_parked && strcmp(slot[k]
[l].name, new_name) == 0) { // check for duplicate vehicle ID
printf("Vehicle ID already exists!\n");
conflict = true;
break;
}
}
if (conflict) break; // break the outer loop if a
conflict is found
}
} while (conflict);
strcpy(slot[i][j].name, new_name); // copy the vehicle ID
from the temporary variable to the slot
slot[i][j].is_parked = true; // mark the slot as occupied
slot[i][j].parked_level = i+1; // set the parking level
slot[i][j].parked_slot = j+1; // set the parking slot
slot[i][j].entry_time = time(NULL); // set the entry time

//logs for parking vehicle


sprintf(log_buffer, "%s: Vehicle '%s' is Parked in Level
%d Slot %d", ctime(&slot[i][j].entry_time), slot[i][j].name, slot[i]
[j].parked_level, slot[i][j].parked_slot);
add_log(log_buffer);
return 0;
}
}
}
}

int retrieve_vehicle() {
char name[50]; // store the vehicle ID to be searched
printf(" _______________________________________\n");
printf("> Enter vehicle ID\t: ");
scanf(" %49s", &name); // read the vehicle ID
for (int i = 0; i < MAX_LEVEL; ++i) {
for (int j = 0; j < MAX_SLOT_PER_FLOOR; ++j) {
if (slot[i][j].is_parked && strcmp(slot[i][j].name, name) ==
0) {
slot[i][j].is_parked = false; // mark the slot as empty
slot[i][j].exit_time = time(NULL); // set the exit time
slot[i][j].parking_fee = difftime(slot[i][j].exit_time,
slot[i][j].entry_time) * 0.1; // calculate the parking fee
//logs for retrieving vehicle
sprintf(log_buffer, "%s: Vehicle '%s' is retrieved from
Level %d Slot %d", ctime(&slot[i][j].exit_time), slot[i][j].name, slot[i]
[j].parked_level, slot[i][j].parked_slot);
add_log(log_buffer);
sprintf(log_buffer, ": Vehicle '%s' parking fee = Rp.
%.3f", slot[i][j].name, slot[i][j].parking_fee);
add_log(log_buffer);
// notification for retrieving vehicle
printf("Vehicle %s is retrieved from Level %d Slot %d\n",
slot[i][j].name, slot[i][j].parked_level, slot[i][j].parked_slot);
printf("Parking fee = Rp. %.3f\n", slot[i]
[j].parking_fee);
// clear the slot
strcpy(slot[i][j].name, "none");
return 0;
}
}
}
printf("Vehicle ID not found!\n"); // if the vehicle ID is not found
}

void display_status() { // display the graphical interface of the parking


level and slot
for (int i = MAX_LEVEL-1; i >= 0; --i) {
printf("\n---floor %d of %d----------\n", i+1,MAX_LEVEL);
for (int j = MAX_SLOT_PER_FLOOR*4/5; j < MAX_SLOT_PER_FLOOR; j++)
{
printf("|%d|%s \t", j+1,slot[i][j].name);
}
printf("\n");
for (int j = MAX_SLOT_PER_FLOOR*3/5; j < MAX_SLOT_PER_FLOOR*4/5;
j++) {
printf("|%d|%s \t", j+1,slot[i][j].name);
}
printf("\n");
for (int j = MAX_SLOT_PER_FLOOR*2/5; j < MAX_SLOT_PER_FLOOR*3/5;
j++) {
printf("|%d|%s \t", j+1,slot[i][j].name);
}
printf("\n");
for (int j = MAX_SLOT_PER_FLOOR*1/5; j < MAX_SLOT_PER_FLOOR*2/5;
j++) {
printf("|%d|%s \t", j+1,slot[i][j].name);
}
printf("\n");
for (int j = 0; j < MAX_SLOT_PER_FLOOR*1/5; j++) {
printf("|%d|%s \t", j+1,slot[i][j].name);
}
printf("\n\n");
}
}

void park_random() {
char id[10];
for (int i = 0; i < 9; i++) {
id[i] = 'A' + rand() % 26;
}
id[9] = '\0';

while (1) {
int level = rand() % MAX_LEVEL;
int slot_num = rand() % MAX_SLOT_PER_FLOOR;
if (slot[level][slot_num].is_parked == false) {
strcpy(slot[level][slot_num].name, id);
slot[level][slot_num].is_parked = true;
slot[level][slot_num].parked_level = level+1;
slot[level][slot_num].parked_slot = slot_num+1;
slot[level][slot_num].entry_time = time(NULL);
sprintf(log_buffer, "%s: Vehicle '%s' is Parked in Level %d
Slot %d", ctime(&slot[level][slot_num].entry_time), slot[level]
[slot_num].name, slot[level][slot_num].parked_level, slot[level]
[slot_num].parked_slot);
add_log(log_buffer);
break;
// check the duplicate input
// check if the parking lot is full and show the notification
}
}

void clear_input_buffer() { // clear the input buffer to check for invalid


input
int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
}
int main()
{
for (int i = 0; i < MAX_LEVEL; ++i) { // initialize the parking
level and slot
for (int j = 0; j < MAX_SLOT_PER_FLOOR; ++j) {
strcpy(slot[i][j].name, "none");
}
}

char input[10];
while(1) { // Main menu
printf(" _______________________________________\n");
printf("| --[ MENU ]-- |\n");
printf("|---------------------------------------|\n");
printf("| 1. Park a new vehicle. |\n");
printf("| 2. Retrieve a vehicle. |\n");
printf("| 3. Display the current status |\n");
printf("| of parking level. |\n");
printf("| 4. View parking records. |\n");
printf("| 5. Park a vehicle with random ID. |\n");
printf("| 6. Exit. |\n");
printf("|_______________________________________|\n");
printf("> Enter your choice\t: ");
if (scanf("%s", &input) != 1) { // check for invalid input
printf("Invalid choice!\n");
clear_input_buffer();
continue;
}
char* end;
long choice = strtol(input, &end, 10);
if (*end != '\0' || choice < 1 || choice > 6) {
printf("Invalid choice!\n");
continue;
}
switch ((int)choice) { // switch case for the menu
case 1:
park_new();
break;
case 2:
// call function to retrieve a vehicle
retrieve_vehicle();
break;
case 3:
// call function to display the current status of parking
level
display_status();
break;
case 4:
// call function to view parking records
display_logs();
break;
case 5:
park_random();
break;
case 6:
return 0; // exit the program
default:
printf("Invalid choice!\n");
}
}

You might also like