Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @file
- *
- * @course CS1521
- * @section 1
- * @project 3
- * @purpose DiscList class method definitions.
- *
- * @author Your Name Here!!!!!
- *
- * @date 2011 Sep 29
- * @version 5.5 */
- #include <cctype>
- #include <cstdlib>
- using namespace std;
- #include "DiscList.h"
- #include "Track.h"
- namespace Project3 {
- /////////////////////////
- // Implement default ctor
- /////////////////////////
- DiscList::DiscList(): numberOfDiscs(0), head(0){}
- ///////////////////////
- // Implement destructor
- ///////////////////////
- DiscList::~DiscList()
- {
- while(!isEmpty()) removeDisc( head->disc );
- }
- ////////////////////
- // Implement isEmpty
- ////////////////////
- bool DiscList::isEmpty() const
- {
- if( !numberOfDiscs ) return true;
- else return false;
- }
- /////////////////////////////
- // Implement getNumberOfDiscs
- /////////////////////////////
- int DiscList::getNumberOfDiscs() const
- {
- return numberOfDiscs;
- }
- ///////////////////////
- // Implement insertDisc
- ///////////////////////
- void DiscList::insertDisc( Disc* disc)
- {
- DiscListNode* newDisc = new DiscListNode;
- if(findDiscSlot(disc) == head)
- {
- newDisc->next = head;
- head = newDisc;
- }
- else{
- newDisc->next = findDiscSlot( disc )->next;
- findDiscSlot( disc )->next = newDisc;
- }
- numberOfDiscs++;
- }
- ///////////////////////
- // Implement removeDisc
- ///////////////////////
- void DiscList::removeDisc( Disc* disc)
- {
- DiscListNode* newDisc = findDisc(disc);
- if( head = newDisc) head = newDisc->next;
- else findDiscSlot(disc)->next = newDisc->next;
- delete newDisc;
- numberOfDiscs--;
- }
- /////////////////////////
- // Implement retrieveDisc
- /////////////////////////
- Disc* DiscList::retrieveDisc( int n ) const
- {
- return findDiscByNumber(n)->disc;
- }
- void DiscList::editDisc(Disc* disc) {
- cout << "Current contents:" << endl;
- disc->printDisc();
- char command[3];
- do {
- cout << "\n(E)dit disc information." << endl
- << "(1-xx) to edit track information." << endl
- << "(A)dd a track to the disc." << endl
- << "(Q)uit editing." << endl
- << "Enter your choice: ";
- cin >> command;
- if (isdigit(command[0]) ) {
- if (isdigit(command[1]) ) {
- command[2] = '\0';
- }
- else {
- command[1] = '\0';
- }
- int trackNumber = atoi(command);
- if (trackNumber < 1 || trackNumber > disc->getNumberOfTracks() ) {
- cerr << "Invalid track number." << endl;
- }
- else {
- Track* track = disc->retrieveTrackByNumber(trackNumber);
- track->editTrack();
- }
- }
- else {
- // command is not a digit
- switch(command[0]) {
- case 'e': case 'E': {
- // remove the disc from the list in case the title
- // is edited. Cannot use 'removeDisc' because it
- // will delete the disc and we just want to edit it
- // and insert it back into the list
- DiscListNode* discListNode = findDisc(disc);
- if (!discListNode) return; // disc not in list...
- if (head == discListNode) {
- head = discListNode->next;
- }
- else {
- DiscListNode* prev = findDiscSlot(disc);
- prev->next = prev->next->next;
- }
- numberOfDiscs--;
- disc->editDisc();
- insertDisc(disc);
- break;
- }
- case 'a': case 'A': {
- Track* track = new Track(disc);
- track->readTrack();
- disc->insertTrack(track);
- break;
- }
- default: {
- break;
- }
- }
- }
- } while (command[0] != 'q' && command[0] != 'Q');
- }
- DiscList::DiscListNode* DiscList::findDisc(const Disc* const disc) const {
- DiscListNode* returnNode = 0, *currentNode = head;
- for (int i = 1;
- i <= getNumberOfDiscs();
- i++) {
- if (disc == currentNode->disc) {
- returnNode = currentNode;
- break;
- }
- currentNode = currentNode->next;
- }
- return returnNode;
- }
- DiscList::DiscListNode* DiscList::findDiscByNumber(int number) const {
- DiscListNode* currentNode = 0;
- if (isValidDiscNumber(number) ) {
- currentNode = head;
- for (int i = 1;
- i < number;
- i++) {
- currentNode = currentNode->next;
- }
- }
- return currentNode;
- }
- DiscList::DiscListNode* DiscList::findDiscSlot(const Disc* const disc) const {
- DiscListNode* currentNode = 0;
- if (head && *(head->disc) < *disc) {
- for (currentNode = head;
- currentNode;
- currentNode = currentNode->next) {
- if (!currentNode->next || *disc <= *(currentNode->next->disc) ) {
- break;
- }
- }
- }
- return currentNode;
- }
- bool DiscList::isValidDiscNumber(const int number) const {
- return number >= 1 && number <= getNumberOfDiscs();
- }
- }
Add Comment
Please, Sign In to add comment