//inserts movies in a binary tree based on the title. public void Insert(DVD dvd, BTreeNode ptr) { if (dvd.getTitle().CompareTo(ptr.Item.getTitle()) < 0) { if (ptr.LChild == null) { ptr.LChild = new BTreeNode(dvd); } else { Insert(dvd, ptr.LChild); } } else if (dvd.getTitle().CompareTo(ptr.Item.getTitle()) == 0) //if title is same then increase quantity by 1 { ptr.Item.increaseQuantity(); } else { if (ptr.RChild == null) { ptr.RChild = new BTreeNode(dvd); } else { Insert(dvd, ptr.RChild); } } }
public void Delete(DVD dvd) { //search for dvd and its parent BTreeNode ptr = root; // search reference BTreeNode parent = null; // parent of ptr while ((ptr != null) && (dvd.getTitle().CompareTo(ptr.Item.getTitle()) != 0)) { parent = ptr; if (dvd.getTitle().CompareTo(ptr.Item.getTitle()) < 0) // move to the left child of ptr { ptr = ptr.LChild; } else { ptr = ptr.RChild; } } if (ptr != null) // if the search was successful { // case 3: item has two children if ((ptr.LChild != null) && (ptr.RChild != null)) { // find the right-most node in left subtree of ptr if (ptr.LChild.RChild == null) // a special case: the right subtree of ptr.LChild is empty { ptr.Item = ptr.LChild.Item; ptr.LChild = ptr.LChild.LChild; } else { BTreeNode p = ptr.LChild; BTreeNode pp = ptr; // parent of p while (p.RChild != null) { pp = p; p = p.RChild; } // copy the item at p to ptr ptr.Item = p.Item; pp.RChild = p.LChild; } } else // cases 1 & 2: item has no or only one child { BTreeNode c; if (ptr.LChild != null) { c = ptr.LChild; } else { c = ptr.RChild; } // remove node ptr if (ptr == root) //need to change root { root = c; } else { if (ptr == parent.LChild) { parent.LChild = c; } else { parent.RChild = c; } } } } }
public void LendMovie() { Console.Clear(); Console.WriteLine("------------------Lend a Movie------------------\r"); Console.WriteLine("Title of the movie that customer wants to borrow:"); string movieTitleInput = Console.ReadLine(); //check if there is a movie with the inputed title. if (DVDCollection.searchMyCollection(movieTitleInput) != null) { Console.WriteLine("Customer First Name:"); string firstNameInput = Console.ReadLine(); Console.WriteLine("Customer Last Name:"); string lastNameInput = Console.ReadLine(); Console.WriteLine("Customer Contact Number:"); string phoneInput = Console.ReadLine(); //check if movie with that title exists. else tell inform that movie doesnt exist. //check if customer with those details exist -> append just the borrower's DVDArr //else if no customer exist -> add details with the movie to borrowerArr //the dvd that will be added to the borrower's DVDArr. DVD movieBorrowed = DVDCollection.searchMyCollection(movieTitleInput); Console.WriteLine(movieBorrowed.getTitle()); Borrower customer = new Borrower(firstNameInput, lastNameInput, phoneInput, new DVD[10]); //check if borrower's details already exist in DVDCollection if (DVDCollection.checkBorrower(customer) != 100) { Console.WriteLine(DVDCollection.checkBorrower(customer)); DVDCollection.appendBorrowerMovie(DVDCollection.getBorrower(DVDCollection.checkBorrower(customer)), movieTitleInput); } //else add the customer to the management system and //then add the movie to that customers borrowed movies array. else { DVDCollection.addBorrower(customer); DVDCollection.appendBorrowerMovie(customer, movieTitleInput); Console.WriteLine(); Console.WriteLine("Customer has been added. Press enter to continue."); Console.ReadLine(); } //check if borrower exists /* if (DVDCollection.checkBorrower(customer) != 100) * { * // append the movie of the borrower that exists in the datastructre. * DVDCollection.appendBorrowerMovie( * DVDCollection.getBorrower(DVDCollection.checkBorrower(customer)), * movieTitleInput); * } * //else add the customer to the management system and * //then add the movie to that customers borrowed movies array. * else * { * DVDCollection.addBorrower(customer); * DVDCollection.appendBorrowerMovie(customer, movieTitleInput); * }*/ displayLendMovie = false; displayBorrower = true; } /*foreach (Borrower oldCustomer in DVDCollection.getBorrowerArr()) // O(n) * { * if (oldCustomer.getPhoneNum().CompareTo(contactInput) == 0) //as the phone number is unique * { * * * //oldCustomer.addMoreMovies(movieTitleInput); * } * else * { * DVDCollection.addBorrower(newBorrower); * DVDCollection.addMovie(newBorrower, movieTitleInput); * } * }*/ }