#include #include #include "cdlist2.h" // ************************************************************* // // first member functions, and other functions // for CDinfo struct // // ************************************************************* CDinfo::CDinfo() // postcondition: all fields initialized to '0' { id_number = 0; title = ""; price = 0.0; group = ""; } CDinfo::CDinfo(int id, const string & t, double p, const string & g) // postcondition: all fields initialized { id_number = id; title = t; price = p; group = g; } ostream & operator << (ostream & output, const CDinfo & cd) // postcondition: all information for cd inserted into output stream { long int saveFlags = output.flags(); output.setf(ios::fixed); output.precision(2); output << cd.id_number << " " << "$" << cd.price << "\t" << cd.group << "\t" << cd.title; output.setf(saveFlags); // restore original } // ************************************************************* // // CDlist member fucntions come here // // ************************************************************* CDlist::CDlist() : myList(0) // postcondition: list is empty, has 0 CDs stored { myCount = 0; } CDlist::CDlist(int size) : myList(size) // postcondition: list is empty, has myCapacity size { myCount = 0; myIndex = 0; } CDlist::~CDlist() // postcondition: list is destroyed { myCount = 0; myIndex = 0; } void CDlist::First() // postcondition: current item reset to beginning of list { myIndex = 0; } void CDlist::Next() // precondition: !IsDone // postcondition: current item is advanced { myIndex++; } bool CDlist::IsDone() // postcondition: returns true if current item off end of list // else returns false (current item is accessible) { return myIndex >= myCount; // items numbered 0, 1, ..., myCount - 1 } CDinfo CDlist::Current() // precondition: !IsDone // postcondition: returns current item in list { if (myIndex < 0 || IsDone()) // index out of range? { cerr << "error, current item not accessible" << endl; exit(1); } return myList[myIndex]; } int CDlist::Size() // postcondition: returns count of # items in list { return myCount; } void CDlist::SetSize(int size) // postcondition: capacity of list is set to size { myList.SetSize(size); } bool CDlist::IsFull() // postcondition: returns true if list is full, else returns false { return myCount >= myList.Length(); } void CDlist::Add(const CDinfo & cd) // postcondition: cd is added at end of list { if (IsFull()) { SetSize(myList.Length() * 2); // grow vector to double its size } myList[myCount] = cd; myCount++; } void CDlist::Delete(int id) // precondition: id is id_number of some item in list // postcondition: item in list with id_number == id deleted, // size of list decreased by one { // add code here, remove line below cerr << "warning, Delete not implemented" << endl; } void CDlist::Print() { int k; for(k=0; k < myCount; k++) { cout << myList[k] << endl; } } void CDlist::Search(string CDinfo::* str, const string & name, CDlist & match) // postcondition: match contains all (and only) items that contain // str as a substring { int k; string down; match.DeleteAll(); string namedown = name.Downcase(); for(k=0; k < myCount; k++) { down = (myList[k].*str).Downcase(); if (down.Contains(namedown)) { match.Add(myList[k]); } } } void CDlist::DeleteAll() // postcondition: all entries deleted, # entries = 0 { myCount = 0; }