aboutsummaryrefslogtreecommitdiffstats
path: root/ee1301/wk7/hw7_directory/HouseDB.cpp
blob: 69811611642018650a0a18f22b68270d8cb9b5cd (plain) (blame)
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
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
class houseData {
public:
    //id,value,bedrooms,bathrooms,sqft,yr_built,zipcode
    long int id = 0;
    int price = 0;
    int bedrooms = 0;
    int bathrooms = 0;
    int sqft = 0;
    int yr_built = 0;
    int zipcode = 0;
    houseData *nextZip = nullptr;
    void printShort();
    void printRecord();

    };

void ProcHeader(ifstream &file); 
houseData* ReadRecord(ifstream &file);
void DelRecord(houseData *record);

void printAllHousesByZip(houseData* head);

int main(int argc, char* argv[]) {
    // This line must be removed in your final 
    // program (see HINT 1).
    houseData* initHouseData[10];
    
    ifstream fin;
    if(argc > 1) {
        fin.open(argv[1]);  // Small file to test with
    } else {
        fin.open("./house-info-v4-short.csv");  // Small file to test with
        //fin.open("./house-info-v4.csv");  // Use this file for final
    }

    if(fin.is_open()) {
        ProcHeader(fin);  // Just dumps header

        int numRec = 0;
        while(!fin.eof()) {
            houseData* tempHouse = ReadRecord(fin);
            if(tempHouse != nullptr) {
                //tempHouse->printRecord();
                tempHouse->printShort();
                initHouseData[numRec] = tempHouse;  // DO NOT store data like this!
                numRec++;
            }
        }

        // The following is just to show how printAllHousesByZip() works
        // DO NOT use this method to organize your linked list!
        houseData* head = initHouseData[2];
        initHouseData[2]->nextZip = initHouseData[1];
        initHouseData[1]->nextZip = initHouseData[3];
        initHouseData[3]->nextZip = initHouseData[4];
        initHouseData[4]->nextZip = initHouseData[0];

        printAllHousesByZip(head);

        //This loop will also have to change in your 
        // final program (see HINT 1).
        for(int a = 0; a<numRec ; a++) {  
            DelRecord(initHouseData[a]);
        }

        fin.close();
        
    } else {
        cout << "File missing!" << endl;
        return 1;
    }

    return 0;
}

void ProcHeader(ifstream &file) {
    int endOfHeader = 0;
    char curChar;

    while(!endOfHeader) {
        curChar = file.get();

        if (curChar == '\n') {
            //End of line found
            endOfHeader = 1;
        }
    }
}

houseData* ReadRecord(ifstream &file) {
    int b = 0;
    int endOfHeader = 0;
    int c = 0;
    long int tempData[7] = {0};
    houseData* localHouseData = new houseData();
    string tempString = " ";

    while(!endOfHeader) {
        char curChar;
        
        //Get the next character in file
        curChar = file.get();
        
        //Check what the character is
        if (curChar == EOF) {
            return nullptr;
        } else if (curChar == '\n') {
            //Final symbol of row found: put in array and end loop
            tempString.append(1,'\0');
            tempData[c++] = stol(tempString);
            endOfHeader = 1;
        } else if (curChar == ',') {
            //End of current symbol found: put in array
            tempString.append(1,'\0');
            tempData[c++] = stol(tempString);
            tempString.clear();
        } else {
            // Append to array
            tempString.append(1,curChar);
            b++;
        }
    }

    localHouseData->id = tempData[0];
    localHouseData->price = tempData[1];
    localHouseData->bedrooms = tempData[2];
    localHouseData->bathrooms = tempData[3];
    localHouseData->sqft = tempData[4];
    localHouseData->yr_built = tempData[5];
    localHouseData->zipcode = tempData[6];

    return localHouseData;
}

void houseData::printRecord() {
    cout <<  "Printing House Record:" << endl;
    cout << "ID:" << id << endl;
    cout << "Price:" << price << endl;
    cout << "Bedrooms:" << bedrooms << endl;
    cout << "Bathrooms:" << bathrooms << endl;
    cout << "Square Feet:" << sqft << endl;
    cout << "Year Built:" << yr_built << endl;
    cout << "Zipcode:" << zipcode << endl;
}

void DelRecord(houseData *record) {
    delete record;
}

void houseData::printShort() {
    cout << id << "," << zipcode << endl;
}

void printAllHousesByZip(houseData* head){
    cout << endl << "Printing List of All Houses by Zip:" << endl;
    houseData* current = head;
    while(current != nullptr) {
        current->printShort();
        current = current->nextZip;
    }
}