-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddcard.c
98 lines (90 loc) · 3.39 KB
/
addcard.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <malloc.h>
#include <time.h>
#include "addcard.h"
#include "safeinput.h"
#include "card.h"
#include "cardlist.h"
#include "adminmenu.h"
#include "fileio.h"
// Function to get a valid 4-digit card ID.
int getValidCardId() {
int cardId;
do {
GetInputInt("\nEnter new Card-ID (4 digits): ", &cardId);
if (cardId < 1000 || cardId > 9999) {
printf("\nERROR: Card-ID must be exactly 4 digits. Please try again.\n");
}
} while (cardId < 1000 || cardId > 9999);
return cardId;
}
void addCard(CARDLIST *cardList) {
// Showing the current time, that will be implemented as a timestamp in the newCard.
time_t current_time;
struct tm* time_info;
time(¤t_time);
time_info = localtime(¤t_time);
printf("\nSystem's current date: %d/%02d/%02d\n",
time_info->tm_year + 1900, time_info->tm_mon + 1, time_info->tm_mday);
// Initiate a new Card struct.
CARD newCard;
// Initiate a bool, the confirm that the new card was added successfully at the end.
bool success = true;
// Initiate a bool to check if the input CardID already exists.
bool idExists;
do {
// Use the function to get 4 valid ID-digits.
newCard.cardId = getValidCardId();
// Loop through the cardlist, to check if the input matches an already existing cardid.
idExists = false;
for (int i = 0; i < cardList->count; i++) {
if (cardList->list[i].cardId == newCard.cardId) {
idExists = true;
printf("\nERROR: Card-ID %d already exists. Please choose a different ID.\n", newCard.cardId);
break;
}
}
} while (idExists);
// Asks the user if the new card should have access granted or not.
newCard.accessGranted = GetBooleanInput("Should access be granted? (yes/no): ");
// Setting current system timestamp.
strftime(newCard.timeStamp, sizeof(newCard.timeStamp), "%Y-%m-%d", time_info);
// Resize the list with realloc to accommodate the new card.
cardList->list = realloc(cardList->list, (cardList->count + 1) * sizeof(CARD));
if (cardList->list == NULL) {
// Handle memory allocation error.
free(cardList->list);
success = false;
return;
}
// Add the new card to the list and increment the total count.
cardList->list[cardList->count] = newCard;
cardList->count++;
// Save the updated card list to the .txt file.
FILE *file = fopen("cardlist.txt", "a");
if (file == NULL) {
fprintf(stderr, "ERROR: Cannot open the file for writing.\n");
free(cardList->list); // Free memory if file opening fails.
success = false;
return;
}
for (int i = 0; i < cardList->count; i++) {
size_t write_size = fwrite(&(cardList->list[i]), sizeof(CARD), 1, file);
if (write_size != 1) {
fprintf(stderr, "ERROR: Cannot write card to file.\n");
free(cardList->list); // Free memory if writing fails.
success = false;
break;
}
}
if(success == true){
// Report success to the user.
printf("\nNew card added successfully.\n");
printf("\nTotal amount of cards in the system: %d\n", cardList->count);
} else if (success == false){
printf("\nERROR: Cannot add new card. Please try again.\n");
}
fclose(file);
}