-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatronDatabase.cpp
More file actions
114 lines (102 loc) · 3.06 KB
/
Copy pathpatronDatabase.cpp
File metadata and controls
114 lines (102 loc) · 3.06 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
/**
* @file patronDatabase.h
* @author Alex Lambert
*
* Description:
* - PatronDatabase holds all of the patrons for a library
* - It can retrieve patrons and insert patrons
* - There is no functionality for removing patrons from the database
*
* Assumptions/Implementation:
* - Creates patrons based on the string, which can only have 1 format
* - Uses a BSTree to store the patrons
*/
#include "patronDatabase.h"
using namespace std;
//---------------------------------------------------------------------------
/** PatronDatabase()
* Default Constructor
*
* Constructs an empty PatronDatabase
* @pre None
* @post PatronDatabase is initialized with a BSTree for Patrons
*/
PatronDatabase::PatronDatabase()
{
patronBST = new BSTree();
}
//---------------------------------------------------------------------------
/** ~PatronDatabase()
* Default Destructor
*
* Destroys the PatronDatabase and every item inside it
* @pre None
* @post All memory used by the PatronDatabase and its parts is
* deallocated
*/
PatronDatabase::~PatronDatabase()
{
delete patronBST;
}
//---------------------------------------------------------------------------
/** insertPatron()
* Insert Patron Function
*
* Inserts the given Patron into the BSTree
* @param patronInfo is a string holding the new patron's ID and name
* @pre patronInfo is properly formatted
* @post the new patron is added, if the Patron is valid
* @return if the patron was successfully inserted
*/
bool PatronDatabase::insertPatron(string patronInfo)
{
Patron* newPatron = createPatron(patronInfo);
if (!newPatron)
return false;
return patronBST->insert(newPatron);
}
//---------------------------------------------------------------------------
/** getPatron()
* Get Patron Function
*
* Return a Patron object based on the information that is passed in if
* it is in a BSTree
* @param patronInfo is a string holding the patron's ID that you're
* searching for
* @pre patronInfo is properly formatted
* @post None. No changes made
* @return Pointer to the Patron object that they are looking for, if it
* wasn't found, returns nullptr.
*/
Patron* PatronDatabase::getPatron(string patronInfo) const
{
Patron* newPatron = createPatron(patronInfo);
if (!newPatron)
return nullptr;
Patron* toReturn = (Patron*)patronBST->retrieve(newPatron);
delete newPatron;
newPatron = nullptr;
return toReturn;
}
//---------------------------------------------------------------------------
/** createPatron()
* Create Patron Function
*
* Creates a patron based on patronInfo
* @param patronInfo is a string holding the patron's ID and name
* @pre patronInfo is properly formatted
* @post None
* @return Pointer to the new Patron, or nullptr if it was invalid
*/
Patron* PatronDatabase::createPatron(string patronInfo) const
{
stringstream patronStream(patronInfo);
int id;
string name = "";
patronStream >> id;
if (id == 9999)
return nullptr;
patronStream.get();
getline(patronStream, name);
return new Patron(id, name);
}