Guest User

Untitled

a guest
Jan 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.48 KB | None | 0 0
  1. /** @file
  2.  *
  3.  *  @course CS1521
  4.  *  @section 1
  5.  *  @project 3
  6.  *  @purpose DiscList class method definitions.
  7.  *
  8.  *  @author Your Name Here!!!!!
  9.  *
  10.  *  @date 2011 Sep 29
  11.  *  @version 5.5 */
  12.  
  13. #include <cctype>
  14. #include <cstdlib>
  15.  
  16. using namespace std;
  17.  
  18. #include "DiscList.h"
  19. #include "Track.h"
  20.  
  21. namespace Project3 {
  22.  
  23.    /////////////////////////
  24.    // Implement default ctor
  25.    /////////////////////////
  26.     DiscList::DiscList(): numberOfDiscs(0), head(0){}
  27.    ///////////////////////
  28.    // Implement destructor
  29.    ///////////////////////
  30.     DiscList::~DiscList()
  31.     {
  32.         while(!isEmpty()) removeDisc( head->disc );
  33.     }
  34.    ////////////////////
  35.    // Implement isEmpty
  36.    ////////////////////
  37.     bool DiscList::isEmpty() const
  38.     {
  39.         if( !numberOfDiscs ) return true;
  40.         else                 return false;
  41.     }
  42.    /////////////////////////////
  43.    // Implement getNumberOfDiscs
  44.    /////////////////////////////
  45.     int DiscList::getNumberOfDiscs() const
  46.     {
  47.         return numberOfDiscs;
  48.     }
  49.    ///////////////////////
  50.    // Implement insertDisc
  51.    ///////////////////////
  52.     void DiscList::insertDisc( Disc* disc)
  53.     {
  54.         DiscListNode* newDisc = new DiscListNode;
  55.         if(findDiscSlot(disc) == head)
  56.         {
  57.             newDisc->next = head;
  58.             head = newDisc;
  59.         }
  60.         else{
  61.             newDisc->next = findDiscSlot( disc )->next;
  62.             findDiscSlot( disc )->next = newDisc;
  63.         }
  64.         numberOfDiscs++;
  65.     }
  66.    ///////////////////////
  67.    // Implement removeDisc
  68.    ///////////////////////
  69.     void DiscList::removeDisc( Disc* disc)
  70.     {
  71.         DiscListNode* newDisc = findDisc(disc);
  72.         if( head = newDisc) head = newDisc->next;
  73.         else findDiscSlot(disc)->next = newDisc->next;
  74.         delete newDisc;
  75.         numberOfDiscs--;
  76.                  
  77.     }
  78.    /////////////////////////
  79.    // Implement retrieveDisc
  80.    /////////////////////////
  81.     Disc* DiscList::retrieveDisc( int n ) const
  82.     {
  83.         return findDiscByNumber(n)->disc;
  84.     }
  85.  
  86.    void DiscList::editDisc(Disc* disc) {
  87.       cout << "Current contents:" << endl;
  88.       disc->printDisc();
  89.       char command[3];
  90.       do {
  91.          cout << "\n(E)dit disc information." << endl
  92.               << "(1-xx) to edit track information." << endl
  93.               << "(A)dd a track to the disc." << endl
  94.               << "(Q)uit editing." << endl
  95.               << "Enter your choice: ";
  96.          cin >> command;
  97.          if (isdigit(command[0]) ) {
  98.             if (isdigit(command[1]) ) {
  99.                command[2] = '\0';
  100.             }
  101.             else {
  102.                command[1] = '\0';
  103.             }
  104.             int trackNumber = atoi(command);
  105.             if (trackNumber < 1 || trackNumber > disc->getNumberOfTracks() ) {
  106.                cerr << "Invalid track number." << endl;
  107.             }
  108.             else {
  109.                Track* track = disc->retrieveTrackByNumber(trackNumber);
  110.                track->editTrack();
  111.             }
  112.          }
  113.          else {
  114.             // command is not a digit
  115.             switch(command[0]) {
  116.                 case 'e': case 'E': {
  117.                    // remove the disc from the list in case the title
  118.                    // is edited. Cannot use 'removeDisc' because it
  119.                    // will delete the disc and we just want to edit it
  120.                    // and insert it back into the list
  121.  
  122.                    DiscListNode* discListNode = findDisc(disc);
  123.                    if (!discListNode) return; // disc not in list...
  124.  
  125.                    if (head == discListNode) {
  126.                       head = discListNode->next;
  127.                    }
  128.                    else {
  129.                       DiscListNode* prev = findDiscSlot(disc);
  130.                       prev->next = prev->next->next;
  131.                    }
  132.  
  133.                    numberOfDiscs--;
  134.  
  135.                    disc->editDisc();
  136.  
  137.                    insertDisc(disc);
  138.                    break;
  139.                 }
  140.                 case 'a': case 'A': {
  141.                    Track* track = new Track(disc);
  142.                    track->readTrack();
  143.                    disc->insertTrack(track);
  144.                    break;
  145.                 }
  146.                 default: {
  147.                    break;
  148.                 }
  149.             }
  150.          }
  151.       }  while (command[0] != 'q' && command[0] != 'Q');
  152.    }
  153.  
  154.    DiscList::DiscListNode* DiscList::findDisc(const Disc* const disc) const {
  155.       DiscListNode* returnNode = 0, *currentNode = head;
  156.       for (int i = 1;
  157.            i <= getNumberOfDiscs();
  158.            i++) {
  159.          if (disc == currentNode->disc) {
  160.             returnNode = currentNode;
  161.             break;
  162.          }
  163.          currentNode = currentNode->next;
  164.       }
  165.       return returnNode;
  166.    }
  167.  
  168.    DiscList::DiscListNode* DiscList::findDiscByNumber(int number) const {
  169.       DiscListNode* currentNode = 0;
  170.       if (isValidDiscNumber(number) ) {
  171.          currentNode = head;
  172.          for (int i = 1;
  173.               i < number;
  174.               i++) {
  175.             currentNode = currentNode->next;
  176.          }
  177.       }
  178.       return currentNode;
  179.    }
  180.  
  181.    DiscList::DiscListNode* DiscList::findDiscSlot(const Disc* const disc) const {
  182.       DiscListNode* currentNode = 0;
  183.       if (head && *(head->disc) < *disc) {
  184.          for (currentNode = head;
  185.               currentNode;
  186.               currentNode = currentNode->next) {
  187.             if (!currentNode->next || *disc <= *(currentNode->next->disc) ) {
  188.                break;
  189.             }
  190.          }
  191.       }
  192.       return currentNode;
  193.    }
  194.  
  195.    bool DiscList::isValidDiscNumber(const int number) const {
  196.       return number >= 1 && number <= getNumberOfDiscs();
  197.    }
  198.  
  199. }
Add Comment
Please, Sign In to add comment