-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.cpp
More file actions
150 lines (137 loc) · 4.81 KB
/
Library.cpp
File metadata and controls
150 lines (137 loc) · 4.81 KB
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Book {
public:
string ISBN;
string title;
string author;
int copyright_date;
bool checked_out;
enum Genre {fiction, nonfiction, periodical, biography, children};
Genre genre;
public:
Book(string i, string t, string a, int c, bool co, Genre g) : ISBN{i}, title{t}, author{a}, copyright_date{c},
checked_out{co}, genre{g} {}
string get_ISBN() const { return ISBN; }
string get_title() const { return title; }
string get_author() const { return author; }
int get_copyright_date() const { return copyright_date; }
bool get_checked_out() const { return checked_out; }
Genre get_genre() const { return genre; }
void check_out() { checked_out = true; }
void check_in() { checked_out = false; }
friend bool operator==(const Book& b1, const Book& b2) {
return b1.ISBN == b2.ISBN;
}
friend bool operator!=(const Book& b1, const Book& b2) {
return !(b1 == b2);
}
friend ostream& operator<<(ostream& os, const Book& b) {
os << "Title: " << b.title << endl;
os << "Author: " << b.author << endl;
os << "ISBN: " << b.ISBN << endl;
os << "Genre: ";
switch(b.genre) {
case fiction:
os << "Fiction";
break;
case nonfiction:
os << "Non-fiction";
break;
case periodical:
os << "Periodical";
break;
case biography:
os << "Biography";
break;
case children:
os << "Children";
break;
}
os << endl;
return os;
}
};
class Patron {
public:
string user_name;
string card_number;
int owed_fees;
public:
Patron(string u, string c) : user_name{u}, card_number{c}, owed_fees{0} {}
string get_user_name() const { return user_name; }
string get_card_number() const { return card_number; }
int get_owed_fees() const { return owed_fees; }
bool owes_fees() const { return owed_fees > 0; }
void set_owed_fees(int f) { owed_fees = f; }
};
class Transaction {
public:
Book book;
Patron patron;
enum Activity {check_out, check_in};
Activity activity;
public:
Transaction(Book b, Patron p, Activity a) : book{b}, patron{p}, activity{a} {}
};
class Library {
public:
vector<Book> books;
vector<Patron> patrons;
vector<Transaction> transactions;
public:
void add_book(Book b) { books.push_back(b); }
void check_out_book(Book b, Patron p) {
if (find(books.begin(), books.end(), b) == books.end()) {
cout << "Error: book not found in library" << endl;
return;
}
if (p.owes_fees()) {
cout << "Error: patron owes fees" << endl;
return;
}
b.check_out();
transactions.push_back(Transaction(b, p, Transaction::check_out));
cout << "Book checked out successfully" << endl;
}
vector<string> patrons_owing_fees() {
vector<string> owing_patrons;
for (Patron p : patrons) {
if (p.owes_fees()) {
owing_patrons.push_back(p.get_user_name());
}
}
return owing_patrons;
}
};
int main() {
// Example usage
Book b1("1111111111", "Wari uzi ko!", "Hirwa Nixon", 2002, false, Book::fiction);
Book b2("2222222222", "Inzira y'ibihe", "Ndizeye Yvan", 2003, false, Book::nonfiction);
Book b3("3333333333", "Ibirari by'ubutegetsi", "Rugaba Nx", 2001, true, Book::biography);
Book b4("4444444444", "Menya n'ibi", "Beza Yv", 2004, true, Book::periodical);
Patron p1("Rukundo", "0001");
Patron p2("Olivier", "0002");
Patron p3("Bamenya", "0003");
Patron p4("Sebulikoko", "0004");
Library lib;
lib.add_book(b1);
lib.add_book(b2);
lib.add_book(b3);
lib.add_book(b4);
lib.check_out_book(b1, p1);
lib.check_out_book(b2, p2);
vector<string> owing_patrons = lib.patrons_owing_fees();
if (owing_patrons.empty()) {
cout << "No patrons owe fees" << endl;
} else {
cout << "Patrons owing fees:" << endl;
for (string s : owing_patrons) {
cout << s << endl;
}
}
return 0;
}