Templates and Ordered Linked Lists

Hey guys working on this project, I feel like I understand the linked list part of this assignment but I'm not sure what is is asking for the readFrom File method if any one could help me that would be great.

Here is what the problem says

4. A static method named readFromFile that takes a C-string as the first parameter, and an orderedLinkedList<MemberDO> object as the second parameter. The first argument is the filename that contains data for existing members. This method should read the data for each individual member from the input file (one line of data per member), create a new MemberDO object, and insert this object into the linked list specified in the second argument.


How do I take the second parameter in, do I need to create the Linked List first?

Here is the code I have so far

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
167
168
    #include<iostream>
    #include<string>
    #include <fstream>

    using namespace std;

    class MemberDO
    {
    private:
    int key;
    string lastName;
    char firstInitial;
    double dues;
    
    public:
    MemberDO(){};
    MemberDO(int, string, char, double);
    void setKey(int);
    void setLastName(string);
    void setFirstInitial(char);
    void setDues(double);
    int getKey();
    string getLastName();
    char getFirstInitial();
    double getDues();
    static void readFromFile(char*, MemberDO);
    };

    MemberDO::MemberDO(int xKey, string xLastName, char xInitial, double xDues)
    {
    key = xKey;
    lastName = xLastName;
    firstInitial = xInitial;
    dues = xDues;
    }

    void MemberDO::setKey(int xKey)
    {
    key = xKey;
    }

    void MemberDO::setLastName(string xLastName)
    {
    lastName = xLastName;
    }

    void MemberDO::setFirstInitial(char xInitial)
    {
    firstInitial = xInitial;
    }

    void MemberDO::setDues(double xDues)
    {
    dues = xDues;
    }

    int MemberDO::getKey()
    {
    return key;
    }

    string MemberDO::getLastName()
    {
    return lastName;
    }

    char MemberDO::getFirstInitial()
    {
    return firstInitial;
    }

    double MemberDO::getDues()
    {
    return dues;
    }

    void readFromFile(char *fname, MemberDO)
    {
        //Not sure what to do here
    }

    template<class Type>
    class orderedLinkedList
    {
    private:
    Type *first;
    orderedLinkedList<Type>  *last;
    Type count;

    public:
    orderedLinkedList();
    int insert(const Type& newItem);
    Type * find(int location) const;
    int remove(int keyValue);
    };

    template<class Type>
    orderedLinkedList<Type>::orderedLinkedList()
    {
    first = NULL;
    last = NULL;
    count = 0;
    }

    template<class Type>
    int orderedLinkedList<Type>::insert(const Type& newItem)
    {
    // Function to insert newItem in the ordered list
    //  Key values should be used exclusively to find appropriate
    //     location for inserted object
    // Postcondition: first points to the new list, newItem
    //    is inserted at the proper place in the list, and
    //    count is incremented by 1
    // Return: Key value of item inserted on the list
    
    }

    template<class Type>
    Type orderedLinkedList<Type>::*find(int location)
    {
    // Function to find the object at a specific location on the list
    // For example, if location = 1, find the first item on the list
    // If location = 5, find the fifth item on the list
    // Postcondition: Status of list not changed
    // Return: Object if found, NULL if object not found or if
    //    location value is <= 0.
    }

    template<class Type>
    int orderedLinkedList<Type>::remove(int keyValue)
    {
    // Function to remove an item with key == keyValue from the list
    //  Key values should be used exclusively to identify
    //     object to be removed
    // Postcondition: if found the node with key == keyValue is
    //   removed from the list;
    //   first points to the first node of the new list; count is
    //   decremented by 1.
    // Return: If item is in list, return key, else return -1.

    }


    //This helps with testing, do not modify.
    template<class elemType>
    bool checkTest(string testName, elemType whatItShouldBe, elemType whatItIs)             {
    
	if (whatItShouldBe == whatItIs) {
		cout << "Passed " << testName << " Output was: " << whatItIs <<     endl;
		return true;
	}
	else {
		cout << "**Failed test " << testName << " ** " << endl << "       Output was "<< whatItIs << endl << "   Output should have been " <<     whatItShouldBe << endl;
		return false;
	}
    }

    bool NULLCheckTest(string testName, void * input) {
    
	if (input == NULL) {
		cout << "Passed " << testName << " NULL test on find method."     << endl;
		return true;
	}
	else {
		cout << "Failed " << testName << " NULL test on find method." << endl;
		return false;
	}
    }


Here is the main

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
    int main()
    {
    orderedLinkedList<MemberDO> memberList;
    MemberDO::readFromFile("Member.dat", memberList);
	
	// Test 1  - Key of 1st member on list
	checkTest("Test 1", 1123, (memberList.find(1))->getKey());
	// Test 2  - Lastname of 2nd member on list
	checkTest("Test 2", string("Johns"), (memberList.find(2))-  >getLastName());
	// Test 3  - First initial of 3rd member on list
	checkTest("Test 3", 'B', (memberList.find(3))->getFirstInitial());
	// Test 4  - Dues of 4th member on list
	checkTest("Test 4", 99.99, (memberList.find(4))->getDues());
	// Test 5  - Key of 5th member on list
	checkTest("Test 5", 6789, (memberList.find(5))->getKey());
	// Test 6 - Testing proper NULL response from find method
	NULLCheckTest("Test 6", memberList.find(6));
	// Test 7 - Testing proper NULL response from find method
	NULLCheckTest("Test 7", memberList.find(-3));
	
	// insert a new member
	memberList.insert(MemberDO(4488, "Triton", 'Z', 832.10));
	// Test 8  - Key of 1st member on list
	checkTest("Test 8", 1123, (memberList.find(1))->getKey());
	// Test 9  - Lastname of 3rd member on list
	checkTest("Test 9", string("Triton"), (memberList.find(3))->getLastName());
	// Test 10  - First initial of 4th member on list
	checkTest("Test 10", 'B', (memberList.find(4))->getFirstInitial());
    
	// insert a new member
	memberList.insert(MemberDO(1100, "Ramsey", 'P', 45.45));
	// Test 11  - Key of 1st member on list
	checkTest("Test 11", 1100, (memberList.find(1))->getKey());
	// Test 12  - Lastname of 2nd member on list
	checkTest("Test 12", string("Stevens"), (memberList.find(2))->getLastName());
	// Test 13  - First initial of 6th member on list
	checkTest("Test 13", 'R', (memberList.find(6))->getFirstInitial());
    
	// insert a new member
	memberList.insert(MemberDO(8888, "Finkel", 'F', 123.12));
	// Test 14  - Key of 7th member on list
	checkTest("Test 14", 6789, (memberList.find(7))->getKey());
	// Test 15  - Lastname of 8th member on list
	checkTest("Test 15", string("Finkel"), (memberList.find(8))->getLastName());
    
	// remove a member
	memberList.remove(4489);
	// Test 16  - Key of 4th member on list
	checkTest("Test 16", 4488, (memberList.find(4))->getKey());
	// Test 17  - Lastname of 5th member on list
	checkTest("Test 17", string("Pryor"), (memberList.find(5))->getLastName());
	// Test 18  - First initial of 6th member on list
	checkTest("Test 18", 'J', (memberList.find(6))->getFirstInitial());
    
	// remove a member
	memberList.remove(8888);
	// Test 19  - Key of 5th member on list
	checkTest("Test 19", 5555, (memberList.find(5))->getKey());
	// Test 20  - Lastname of 6th member on list
	checkTest("Test 20", string("Towson"), (memberList.find(6))->getLastName());
	
	// remove a member
	memberList.remove(1100);
	// Test 21  - Key of 1st member on list
	checkTest("Test 21", 1123, (memberList.find(1))->getKey());
	// Test 22  - Lastname of 2nd member on list
	checkTest("Test 22", string("Johns"), (memberList.find(2))->getLastName());
	// Test 23  - First initial of 4th member on list
	checkTest("Test 23", 'R', (memberList.find(4))->getFirstInitial());
    
	// Test 24 - Testing proper NULL response from find method
	NULLCheckTest("Test 24", memberList.find(6));
	// Test 25  - Testing proper return value from remove method
	checkTest("Test 25", -1, memberList.remove(6001));
    return 0;
   }


I know this is a lot of code but I just wanted to post it all, I only need help with what to do with the readFromFile static method? Thanks
Last edited on
If you want to see what the data is in the Member.dat file

1
2
3
4
5
6789 Towson J 65.25
3456 Johns K 200.00
1123 Stevens M 112.35
4489 Ellwood B 700.25
5555 Pryor R 99.99
Topic archived. No new replies allowed.