-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.h
143 lines (118 loc) · 4.38 KB
/
library.h
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
#include <iostream>
#include <memory>
#include <vector>
#pragma once
/* The different types of documents stored in the library */
typedef enum { DOC_NOVEL, DOC_COMIC, DOC_MAGAZINE } DocType;
/* Parent class for all types of documents */
class Document {
public:
/* return the document type (abstract method) */
virtual DocType getDocType() = 0;
Document(std::string title, int year, int quantity);
/* print the document attributes in a single line on the standard output
* (abstract method), the format for the various document types is as
* follows:
* Novel, title: <title>, author: <author>, year: <year>, quantity: <quantity>
* Comic, title: <title>, author: <author>, issue: <issue>, year: <year>,
* quantity: <quantity> Magazine, title: <title>, issue: <issue>, year:
* <year>, quantity: <quantity>
*/
virtual void print() = 0;
/* setters and getters */
void updateTitle(const std::string newTitle);
void updateYear(int newYear);
void updateQuantity(int newQuantity);
std::string getTitle();
int getYear();
int getQuantity();
/* Used when someone tries to borrow a document, should return true on success
* and false on failure */
bool borrowDoc();
/* Used when someone returns a document */
void returnDoc();
protected:
/* These need to be protected to be inherited by the child classes */
std::string _title; // document title
int _year; // year of parution
int _quantity; // quantity held in the library, should be updated on
// borrow (-1) and return (+1)
};
class Novel : public Document {
public:
Novel(const std::string title, const std::string author, int year, int quantity);
~Novel();
DocType getDocType();
void print();
/* getters and setters */
void updateAuthor(const std::string newAuthor);
std::string getAuthor();
private:
/* In addition to the base Document's attributes, a novel has an author */
std::string _author;
};
class Comic : public Document {
public:
Comic(const std::string title, const std::string author, int issue, int year, int quantity);
~Comic();
DocType getDocType();
void print();
/* getters, setters */
void updateAuthor(const std::string newAuthor);
void updateIssue(int newIssue);
std::string getAuthor();
int getIssue();
private:
/* In addition to the base Document's attributes, a comic has an author as
* well as an issue number */
std::string _author;
int _issue;
};
class Magazine : public Document {
public:
Magazine(const std::string title, int issue, int year, int quantity);
~Magazine();
DocType getDocType();
void print();
/* getters, setters */
void updateIssue(int newIssue);
int getIssue();
private:
/* In addition to the base Document's attributes, a magazine has an issue
* number */
int _issue;
};
/* One instance of that class represents a library */
class Library {
public:
Library();
/* print the entire library on the standard output, one line per document,
* in the order they were inserted in the library, using the print()
* methods implemented by the document objects */
void print();
/* Dump the library in a CSV file. The format is:
* 1 line per file:
* <document type>,<title>,<author>,<issue>,<year>,<quantity>
* A field not relevant for a given document type (e.g. issue for novel)
* is left blank. Return true on success, false on failure. */
bool dumpCSV(const std::string filename);
//bool Library::dumpCSV(const char *filename)
/* search for a document in the library, based on the title. We assume that
* a title identifies uniquely a document in the library, i.e. there cannot
* be 2 documents with the same title. Returns a pointer to the document if
* found, NULL otherwise */
Document *searchDocument(const std::string title);
/* Add/delete a document to/from the library, return true on success and
* false on failure. */
bool addDocument(DocType t, const std::string title, const std::string author, int issue, int year, int quantity);
bool addDocument(Document *d);
bool delDocument(const std::string title);
/* Count the number of document of a given type present in the library */
int countDocumentOfType(DocType t);
/* Borrow/return documents, return true on success, false on failure */
bool borrowDoc(const std::string title);
bool returnDoc(const std::string title);
private:
/* Holds all documents in the library */
std::vector<Document*> _docs;
};