//The function printAll goes through //the list and prints off each book Record. public void printAll() { if (m_pHead == null) //Checks if there is anything in the list, prints message if list is empty { Console.Error.WriteLine("Nothing to print.There are no contents in this list!"); return; } BookRecord temp = new BookRecord(); temp = m_pHead; while (temp != null)//The while loop runs through the list until it ends { temp.printRecord(); temp = temp.getNext(); } }
//The function getNumberInStock searches //for a specific book by its stock number. //If the book is found, its information is printed. public int getNumberInStock(long sn) { if (m_pHead == null) //Checks if there is anything in the list, prints message if list is empty { Console.Error.WriteLine("There are no contents in this list!"); } BookRecord temp = new BookRecord(); temp = m_pHead; //The function searches the list until it ends or the matching stock number is found while ((temp != null) && (sn != temp.getStockNum())) { temp = temp.getNext(); } if (temp == null) //If the match isn't found, return 0 { return 0; } else //If there is a match, the number in stock is printed { return temp.getNumberInStock(); } }
//The function searchByCost searches //for each book that falls within the given //price range. public void searchByCost(double min, double max) { BookRecord temp = new BookRecord(); temp = m_pHead; int tracker = 0; //Tracks every book found if (m_pHead == null) //Checks if there is anything in the list, prints message if list is empty { Console.Error.WriteLine("There are no contents in this list!"); } while ((temp != null)) //The list is searched till end { //Checks to see if the cost of the book falls within the range (min & max) if ((temp.getCost() >= min) && (temp.getCost() <= max)) { temp.printRecord(); //If it does, the information is printed. tracker++; } temp = temp.getNext(); } if (tracker <= 0) //If no book is found in that price range { Console.WriteLine("No item(s) found for this price range!"); } }
//The function searchByClassification searches //for a book by the given classification number //and prints the information. public void searchByClassification(int cl) { if (m_pHead == null) //Checks if there is anything in the list, prints message if list is empty { Console.Error.WriteLine("There are no contents in this list!"); return; } BookRecord temp = new BookRecord(); int tempCL; int tracker = 0; //Tracks every book found temp = m_pHead; tempCL = temp.getClassification(); //This for loop searches the list until it ends while (temp != null) { tempCL = temp.getClassification(); if(tempCL == cl)//if the classification numbers match, { temp.printRecord(); //the information is printed tracker++; } temp = temp.getNext(); // iterate } if (tracker <= 0) { Console.WriteLine("No item(s) with that classification number!"); } }
//The function searchByStockNumber searches the list for books //with the given stock number and prints their information. public BookRecord searchByStockNumber(long stockNum) { if (m_pHead == null)//Checks if there is anything in the list, prints message if list is empty { Console.Error.WriteLine("There are no contents in this list!"); } BookRecord temp = new BookRecord(); Boolean isFound = false; temp = m_pHead; //The function searches the list until it reaches the end while (temp != null) { if (stockNum == temp.getStockNum()) //The book information is printed if it's found { isFound = true; return temp; } temp = temp.getNext(); //Iterate } if (isFound == false) //If the target is not found, return null { return null; } //Function won't ever get here, this is to stop the warning that "not all control paths return a value" return null; }
//The function removeBook searches for the stock number //of a Book Record and removes that book from the list. public Boolean removeBook(long stockNum) { BookRecord temp = new BookRecord(); BookRecord back = new BookRecord(); if (m_pHead == null) //CHecks if there is anything in the list { return false; } temp = m_pHead; back = null; while((temp != null) && (stockNum != temp.getStockNum())) //The function moves down the list { back = temp; temp = temp.getNext(); } //If the list has reached the end and no book is found, a message is printed if(temp == null) { Console.WriteLine("Book not Found."); return false; } else if (back == null) //If the book is at the head, head points to the next node and the book is removed { m_pHead = m_pHead.getNext(); temp = null; } else //If the book is not at the head, the node before the target is set to the node after the target { back.setNext(temp.getNext()); temp = null; //he target is deleted. } return true; }