// pre: ptr != null // post: item is inserted to the binary search tree rooted at ptr private void Insert(iMember aMember, BTreeNode ptr) { IComparable fName = aMember.FirstName; IComparable lName = aMember.LastName; if ((fName.CompareTo(ptr.FName) < 0) || (fName.CompareTo(ptr.FName) == 0 && lName.CompareTo(ptr.LName) < 0)) { if (ptr.LChild == null) { ptr.LChild = new BTreeNode(aMember); } else { Insert(aMember, ptr.LChild); } } else { if (ptr.RChild == null) { ptr.RChild = new BTreeNode(aMember); } else { Insert(aMember, ptr.RChild); } } }
public void delete(iMember aMember) { registerMembers.delete(aMember); Console.WriteLine(""); Console.WriteLine("Hit enter to continue"); Console.ReadLine(); }
public string mobileNumber(iMember aMember) { if (this.FindContact(aMember, this.Root) != null) { return("Phone number is :" + this.FindContact(aMember, this.Root).member.ContactNumber); } return("Invalid entry"); }
public void add(iMember aMember) { registerMembers.add(aMember); Console.WriteLine($"{aMember.FirstName + aMember.LastName} successfully added to membercollection"); Console.WriteLine(""); Console.WriteLine("Hit enter to continue"); Console.ReadLine(); }
private BTreeNode rchild; // reference to its right child public BTreeNode(iMember aMember) { fName = aMember.FirstName; lName = aMember.LastName; this.aMember = aMember; lchild = null; rchild = null; }
public void deleteBorrower(iMember aMember) //delte a member from the borrower list { if (Borrowers.Remove(aMember) == false) { Console.Write("The member selected is not borroweer of this tool, press anykey to return..."); Console.ReadKey(); } }
public bool search(iMember aMember) { if (this.Find(aMember, this.Root) != null) { return(true); } return(false); }
public void Insert(iMember aMember) { if (root == null) { root = new BTreeNode(aMember); } else { Insert(aMember, root); } }
private int compareTo(iMember memberOne, iMember memberTwo) { if (Convert.ToInt32(memberOne.ContactNumber) < Convert.ToInt32(memberTwo.ContactNumber)) { return(-1); } else { return(1); } }
private iMember MinValue(Node node) { iMember minv = node.member; while (node.LeftNode != null) { minv = node.LeftNode.member; node = node.LeftNode; } return(minv); }
public void add(iMember aMember) //add a new memeber to the system { bool found = Program.aCollection.GetMemberCollection.search(aMember); if (found) { Console.Write("Error the given member is already registered, press any key to return..."); Console.ReadKey(); } else { Program.aCollection.GetMemberCollection.add(aMember); } }
public void delete(iMember aMember) //delete a member from the system { bool found = Program.aCollection.GetMemberCollection.search(aMember); if (!found || aMember.Tools.Length > 0) { Console.Write("Error the given member is not registered or has outstanding tools, " + "press any key to return..."); Console.ReadKey(); } else { Program.aCollection.GetMemberCollection.delete(aMember); } }
private Node Remove(Node parent, iMember aMember) { Console.WriteLine("Deleting member"); Console.WriteLine("aMember name: " + aMember.LastName); Console.WriteLine("parent.member name: " + parent.member.LastName); if (parent == null) { return(parent); } if (aMember.FirstName == parent.member.FirstName && aMember.LastName == parent.member.LastName && aMember.ContactNumber == parent.member.ContactNumber && aMember.PIN == parent.member.PIN) { Console.Write("Member to delete: " + parent.member.LastName); // node with only one child or no child if (parent.LeftNode == null) { return(parent.RightNode); } else if (parent.RightNode == null) { return(parent.LeftNode); } // node with two children: Get the inorder successor (smallest in the right subtree) parent.member = MinValue(parent.RightNode); // Delete the inorder successor parent.RightNode = Remove(parent.RightNode, parent.member); } else if (compareTo(aMember, parent.member) == -1) { Console.WriteLine("left"); parent.LeftNode = Remove(parent.LeftNode, aMember); } else if (compareTo(aMember, parent.member) == 1) { Console.WriteLine("right"); parent.RightNode = Remove(parent.RightNode, aMember); } return(parent); }
private Node FindContact(iMember aMember, Node parent) { if (parent != null) { if (aMember.FirstName == parent.member.FirstName && aMember.LastName == parent.member.LastName) { return(parent); } if (compareTo(aMember, parent.member) < 0) { return(Find(aMember, parent.LeftNode)); } else { return(Find(aMember, parent.RightNode)); } } return(null); }
public void borrowTool(iMember aMember, iTool aTool) //a member borrows a tool from the tool library { if (aMember.Tools.Length >= 3) { Console.Write("You currently have three(3) tools on loan, please return one before renting another. " + "Press anykey to return..."); Console.ReadKey(); return; } else if (aTool.AvailableQuantity <= 0) { Console.Write("The tool selected is unavalible, please wait for someone to return this tool. " + "Press anykey to return..."); Console.ReadKey(); return; } else { aMember.addTool(aTool); aTool.addBorrower(aMember); } }
public void add(iMember aMember) { Node before = null, after = this.Root; while (after != null) { before = after; if (compareTo(aMember, after.member) < 0) { after = after.LeftNode; } else { after = after.RightNode; } } Node newNode = new Node(); newNode.member = aMember; if (this.Root == null)//Tree is empty { this.Root = newNode; } else { if (compareTo(aMember, before.member) < 0) { before.LeftNode = newNode; } else { before.RightNode = newNode; } } //Number++; }
public void displayBorrowingTools(iMember aMember) //given a member, display all the tools that the member are currently renting { Console.Clear(); int num = 1; string[] borrowedTools = listTools(aMember); Console.WriteLine($"{aMember.FirstName}'s Borrowed Tools\n" + "========================="); if (borrowedTools.Length == 0) { Console.WriteLine("\nNothing to show"); } else { foreach (string tool in borrowedTools) { Console.WriteLine($"{num}\n{tool}"); num++; } } Console.Write("=========================\nPress anykey to continue..."); Console.ReadKey(); }
public void displayBorrowingTools(iMember aMember) { throw new NotImplementedException(); }
public void delete(iMember aMember) { this.Root = Remove(this.Root, aMember); }
public Boolean search(iMember aMember) //search a given member in this member collection. Return true if this memeber is in the member collection; return false otherwise. { return(memberCollection.Search(aMember)); }
public void addBorrower(iMember aMember) //add a member to the borrower list { Borrowers.Add(aMember); NoBorrowings = 1; }
public string[] listTools(iMember aMember) //get a list of tools that are currently held by a given member { return(aMember.Tools); }
// there are three cases to consider: // 1. the node to be deleted is a leaf // 2. the node to be deleted has only one child // 3. the node to be deleted has both left and right children public void Delete(iMember aMember) { IComparable fName = aMember.FirstName; IComparable lName = aMember.LastName; // search for item and its parent BTreeNode ptr = root; // search reference BTreeNode parent = null; // parent of ptr while ((ptr != null) && (fName.CompareTo(ptr.FName) != 0)) { parent = ptr; if ((fName.CompareTo(ptr.FName) < 0) || (fName.CompareTo(ptr.FName) == 0 && lName.CompareTo(ptr.LName) < 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.FName = ptr.LChild.FName; ptr.LName = ptr.LChild.LName; ptr.AMember = ptr.LChild.AMember; 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.FName = p.FName; ptr.LName = p.LName; ptr.AMember = p.AMember; 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; } } } } else { Console.Write("Sorry the member given wasn't found in the Data base, press anykey to return..."); Console.ReadKey(); } }
public void returnTool(iMember aMember, iTool aTool) { throw new NotImplementedException(); }
public void delete(iMember aMember) { throw new NotImplementedException(); }
public bool Search(iMember aMember) { return(Search(aMember.FirstName, aMember.LastName, root)); }
public string[] listTools(iMember aMember) { throw new NotImplementedException(); }
public void add(iMember aMember) //add a new member to this member collection, make sure there are no duplicates in the member collection { memberCollection.Insert(aMember); count++; }
public static void RunCommand(int[] selection) { int staff, menuSelection1, menuSelection2; int[] changeTool; bool remove, noQuantity; iTool returnedTool; string toolTypeStr; menuSelection1 = 0; staff = menuSelection2 = 1; iToolLibrarySystem funcs = new ToolLibrarySystem(); if (selection[menuSelection1] == staff)//staff { switch (selection[menuSelection2]) { case (int)StaffCommands.AddNewTool: navigateToolMenu(out toolTypeStr, funcs); returnedTool = CreateNewTool(); funcs.add(returnedTool); funcs.displayTools(toolTypeStr); break; case (int)StaffCommands.AddExisitingTool: navigateToolMenu(out toolTypeStr, funcs); changeTool = PickTool(); if (changeTool != null) { returnedTool = currentToolType.toArray()[changeTool[0] - 1]; funcs.add(returnedTool, changeTool[1]); } funcs.displayTools(toolTypeStr); break; case (int)StaffCommands.RemoveTool: navigateToolMenu(out toolTypeStr, funcs); bool removeExisting = true; changeTool = PickTool(removeExisting); if (changeTool != null) { returnedTool = currentToolType.toArray()[changeTool[0] - 1]; if (returnedTool.Quantity == changeTool[1]) { funcs.delete(returnedTool); } else { funcs.delete(returnedTool, changeTool[1]); } } funcs.displayTools(toolTypeStr); break; case (int)StaffCommands.RegisterMember: printMembers(); iMember newMember = CreateMember(); funcs.add(newMember); printMembers(); Console.Write("Press any Key to continue..."); Console.ReadKey(); break; case (int)StaffCommands.RemoveMember: printMembers(); iMember removeMember = PickMember(); funcs.delete(removeMember); printMembers(); Console.Write("Press any Key to continue..."); Console.ReadKey(); break; case (int)StaffCommands.ContactNumMember: getContact(); break; default: Console.WriteLine("Error: switch failed :("); break; } } else //member { switch (selection[menuSelection2]) { case (int)MemberCommands.DisplayAllToolsType: navigateToolMenu(out toolTypeStr, funcs); break; case (int)MemberCommands.BorrowTool: navigateToolMenu(out toolTypeStr, funcs); remove = false; noQuantity = true; changeTool = PickTool(remove, noQuantity); if (changeTool != null) { funcs.borrowTool(Program.aCollection.CurrentMember, currentToolType.toArray()[changeTool[0] - 1]); } funcs.displayTools(toolTypeStr); break; case (int)MemberCommands.ReturnTool: funcs.displayBorrowingTools(Program.aCollection.CurrentMember); remove = false; noQuantity = true; Member replace = (Member)Program.aCollection.CurrentMember; changeTool = PickTool(remove, noQuantity, Program.aCollection.CurrentMember.Tools.Length); if (changeTool != null) { funcs.returnTool(Program.aCollection.CurrentMember, replace.ToolsBorrowed[changeTool[0] - 1]); } funcs.displayBorrowingTools(Program.aCollection.CurrentMember); break; case (int)MemberCommands.ListToolsRenting: funcs.displayBorrowingTools(Program.aCollection.CurrentMember); break; case (int)MemberCommands.DisplayTopThree: funcs.displayTopTHree(); break; default: Console.WriteLine("Error: switch failed :("); break; } } }
public void delete(iMember aMember) //delete a given member from this member collection, a member can be deleted only when the member currently is not holding any tool { memberCollection.Delete(aMember); count--; }