Professional Documents
Culture Documents
NRP : 4122600044
Kelas : 2 D4 Mekatronika B
#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 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;
};
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 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
}
}
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");
}
}