forked from zhanggiene/CZ4031
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.cpp
More file actions
119 lines (102 loc) · 3.52 KB
/
Copy pathBlock.cpp
File metadata and controls
119 lines (102 loc) · 3.52 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
#include <vector>
#include <iostream>
#include "Record.cpp"
using namespace std;
class Block {
private:
Record ** records;
int MAX_NUM_RECORDS;
//Get the next empty spot to add record to
int findEmptyIndex(){
for( int i=0; i<this->MAX_NUM_RECORDS; ++i ) {
if (!records[i]){ //if pointer is null
return i;
}
}
return -1;
}
public:
Block(int maxNumRecords){
this->MAX_NUM_RECORDS = maxNumRecords;
this->records = new Record*[100]();
}
//Get total number of records in a block
int getSize(){
int size = 0;
for( int i=0; i<this->MAX_NUM_RECORDS; ++i ) {
if (records[i]){ //if pointer is not null
size++;
}
}
return size;
}
//Add new record and return recordIndex of newly added record
int addRecord(string tconst, double averageRating, int numVotes){
if (this->getSize()<this->MAX_NUM_RECORDS){
int emptyIndex = this->findEmptyIndex();
if (emptyIndex != -1){
records[emptyIndex] = new Record(tconst,averageRating,numVotes);
return emptyIndex; //return index so that we can reference it later
} else {
cout << "There are not empty spaces in this block!\n";
return -1;
}
}
else {
cout << "The block you are trying to add to is full!\n";
return -1;
}
}
//Delete record using the recordIndex
void deleteRecord(int recordIndex){
delete this->records[recordIndex];
this->records[recordIndex] = NULL;
}
//Get the record pointer using recordIndex
Record * getRecord(int recordIndex){
if (recordIndex < this->MAX_NUM_RECORDS){
return records[recordIndex];
}
else {
return NULL;
}
}
//To check if we can add anymore records to this block
bool isFull(){
return (getSize()>=MAX_NUM_RECORDS);
}
~Block(){
for( int i=0; i<this->MAX_NUM_RECORDS; ++i ) {
if (records[i]){ //if pointer is not null
delete records[i];
}
}
delete records;
}
};
//Checks
int main(){
Block block1(3);
//Test adding record to a block
cout << "size: " << block1.getSize()<<"\n";
block1.addRecord("sdfas", 123.123, 1232314);
cout << "size: " << block1.getSize()<<"\n";
cout << block1.getRecord(0)->toString();
block1.addRecord("sdfas2", 123.123, 1232314);
cout << "size: " << block1.getSize()<<"\n";
cout << block1.getRecord(1)->toString();
//Test deleting a record
block1.deleteRecord(0);
cout << "size: " << block1.getSize()<<"\n";
cout << block1.getRecord(1)->toString();
//Test that it adds to the first empty spot
block1.addRecord("sdfas3", 123.123, 1232314);
cout << "size: " << block1.getSize()<<"\n";
cout << block1.getRecord(0)->toString();
block1.addRecord("sdfas4", 123.123, 1232314);
cout << "size: " << block1.getSize()<<"\n";
//Make sure that it doesn't add more than the limit
block1.addRecord("sdfa5", 123.123, 1232314);
cout << "size: " << block1.getSize()<<"\n";
return 0;
}