-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibrary.c
158 lines (141 loc) · 3.85 KB
/
Library.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include "Library.h"
Library* initLibrary()
{
Library* library = (Library*)malloc(sizeof(Library));
if (!library)
{
printf("problem occured during system initialization\n");
return NULL;
}
printf("initializing Library system\n");
library->bookManager = initBookManager();
library->loanManager = initLoanManager();
library->memberManager = initMemberManager();
return library;
}
void refreshLibraryLoans(LoanManager* manager)
{
ListNode* head = (manager->loanList).head->next;
if (!head)
{
return;
}
while (!head)
{
if (isOverdue(&((Loan*)head->data)->dateOfReturn))
{
((Loan*)head->data)->status = OVERDUE;
}
}
}
void loadLibraryFromFile(const char* textFile, const char* binFile, Library* library)
{
int choice;
do
{
printf("=========================================== Load menu 2 =========================================\n\n");
printf("[1] - Load system from a text file\n");
printf("[2] - Load system from a binary file\n");
printf("Please enter your choice: ");
scanf("%d", &choice);
getchar();
switch (choice)
{
case 1:
readLibraryFromTextFile(textFile, library);
printf("=================================================================================================\n\n");
return;
case 2:
readLibraryFromBinFile(binFile, library);
printf("=================================================================================================\n\n");
return;
case 3:
printf("Exit\n");
break;
default:
handleError("Not valid choice! Try again!\n\n");
break;
}
} while (1);
}
int writeLibraryToTextFile(const char* textFile, Library* library)
{
FILE* file = fopen(textFile, "w");
if (!file)
{
return 0;
}
int books, members, loans;
books = writeBookManagerToText(file, library->bookManager);
members = writeMemberManagerToText(file, library->memberManager);
loans = writeLoanManagerToText(file, library->loanManager);
if (!books || !loans || !loans)
{
handleError("Problem occured during system writing the system\n");
fclose(file);
return 0;
}
return 1;
}
int readLibraryFromTextFile(const char* textFile, Library* library)
{
FILE* file = fopen(textFile, "r");
if (!file)
{
return 0;
}
int books, members, loans;
books = readBookManagerFromText(file, library->bookManager);
members = readMemberManagerFromText(file, library->memberManager);
loans = readLoanManagerFromText(file, library->loanManager, library->bookManager, library->memberManager);
if (!books || !members || !loans)
{
handleError("problem occured during system loading the system\n");
return 0;
}
getMemberLoanArr(library->loanManager, library->memberManager);
printf("Loading sucsessfull!\n");
return 1;
}
int writeLibraryToBinFile(const char* binFile, Library* library)
{
FILE* file = fopen(binFile, "wb");
if (!file)
{
return 0;
}
int books, members, loans;
books = writeBookManagerToBinary(file, library->bookManager);
members = writeMemberManagerToBinary(file, library->memberManager);
loans = writeLoanManagerToBinary(file, library->loanManager);
if (!books || !loans || !loans)
{
handleError("Problem occured during system writing the system\n");
fclose(file);
return 0;
}
return 1;
}
int readLibraryFromBinFile(const char* binFile, Library* library)
{
FILE* file = fopen(binFile, "rb");
if (!file)
{
return 0;
}
int books, members, loans;
books = readBookManagerFromBinary(file, library->bookManager);
members = readMemberManagerFromBinary(file, library->memberManager);
loans = readLoanManagerFromBinary(file, library->loanManager, library->bookManager, library->memberManager);
if (!books || !loans || !loans)
{
handleError("problem occured during system loading the system\n");
return 0;
}
getMemberLoanArr(library->loanManager, library->memberManager);
printf("Loading sucsessfull!\n");
return 1;
}