def dropStudent(file, enrollTable: EnrollTable, queue: PriorityQueue) -> None: """This helper method reads in a file and drops students from the class with a given ID.""" pc = PrintColour() assert isinstance(enrollTable, EnrollTable), "Not an EnrollTable." assert isinstance(queue, PriorityQueue), "Not a PriorityQueue" for line in file.readlines(): studentId, faculty, fname, lname = lineSplitter(line) if studentId is None: pass else: student = enrollTable.remove(studentId) if student is False: pc.printout( f"WARNING:There is no student {fname} {lname}, (ID: {studentId}) currently enrolled and cannot be dropped!", color="RED") else: try: if not queue.isEmpty(): item = queue.dequeue() item.setNext(None) item.setPrev(None) enrollTable.insert(item) except AssertionError as e: pc.printout(e.args[0], color="RED")
def test_cmputIndex(self): student1 = StudentNode("123456", "ART", "Tosin", "Kuye") student2 = StudentNode("555555", "ART", "Matthew", "Jones") student3 = StudentNode("654321", "ART", "Matthew", "Jones") table = EnrollTable(35) self.assertEqual(table.cmputIndex(student1.getID()), 32) self.assertEqual(table.cmputIndex(student2.getID()), 20) self.assertEqual(table.cmputIndex(student3.getID()), 24)
def test_enroll_student(self): queue = PriorityQueue() table = None for i in range(1, 51): table = EnrollTable(i) with open("input.txt") as f: enrollStudentData(f, table, queue) f.close() self.pc.printout(queue, color="WHITE") self.pc.printout(table, color="WHITE") self.assertEqual(table.size(), 50)
def test_drop_student(self): queue = PriorityQueue() table = EnrollTable(51) with open("input.txt") as f: enrollStudentData(f, table, queue) f.close() with open("input.txt") as f: dropStudent(f, table, queue) self.assertEqual(queue.size(), 0)
def enrollStudentData(file, enrollTable: EnrollTable, queue: PriorityQueue) -> None: """Helper method to enroll a student in a class or if it is full then add to queue""" pc = PrintColour() assert isinstance(enrollTable, EnrollTable), "Not an EnrollTable." assert isinstance(queue, PriorityQueue), "Not a PriorityQueue" for line in file.readlines(): studentId, faculty, fname, lname = lineSplitter(line) if studentId is None: pass else: node = StudentNode(studentId, faculty, fname, lname) try: enrollTable.insert(node) except AssertionError as e: pc.printout(e.args[0], color="RED") try: if enrollTable.isFull(): queue.enqueue(node) except AssertionError: pc.printout( f"Cannot insert student {fname} {lname} (ID:{studentId}) as they are already on the waitlist!", color="RED")
def test_insert_row(self): table = EnrollTable(35) student = StudentNode("999670", "ART", "Tosin", "Kuye") student1 = StudentNode("999635", "SCI", "Satoshi", "Nakamoto") student2 = StudentNode("999600", "ART", "Levi", "Ackermann") student3 = StudentNode("999599", "ART", "Tosin", "Kuye") student4 = StudentNode("555555", "SCI", "Satoshi", "Nakamoto") student5 = StudentNode("534125", "SCI", "Satoshi", "Monkery") student6 = StudentNode("211122", "SCI", "Satoshi", "Masaaas") table.insert(student) table.insert(student1) table.insert(student2) table.insert(student3) table.insert(student4) table.insert(student5) table.insert(student6) temp = table.enrollTable[20] count = 0 while temp is not None: temp = temp.getNext() count += 1 self.assertEqual(count, 5)
def test_insert(self): student = StudentNode("123456", "ART", "Tosin", "Kuye") student1 = StudentNode("555555", "SCI", "Satoshi", "Nakamoto") student2 = StudentNode("654321", "ART", "Levi", "Ackermann") table = EnrollTable(35) table.insert(student) table.insert(student1) table.insert(student2) self.assertEqual(table.tableSize, 3) self.assertEqual(table.enrollTable[32].getID(), "123456") self.assertEqual(table.enrollTable[20].getID(), "555555") self.assertEqual(table.enrollTable[24].getID(), "654321")
def main(): """THE MAIN FUNCTION\n Used for the full and total implementation of the classes and code for this project. """ enrolled = "enrolled.txt" waitlist = "waitlist.txt" LOGOTOP = """ ________ ________ ________ | |\ | | \ / \ | | | | \ | | | | __ | | | |________ | \ | |________/ | / \ | | | | | \ | | \ | \__/ | | | | | \ | | \ | | | | |________ | \| | \ \_______ / |________ |________""" LOGOBOTTOM = """ . _________ __________ /| / | / | / | / | / |_________ / | / | | / | ____|____ / __________| """ linebreak = "-" * 75 noFile = "There is no file with that name please try again." enrollTable = EnrollTable(51) queue = PriorityQueue() pc = PrintColour() entries = ["R", "D", "Q"] running = True pc.printout(LOGOTOP, color="YELLOW") pc.printout(LOGOBOTTOM, color="YELLOW") pc.printout("Author: Tosin Kuye\n", color="YELLOW") inputText = "Hello! Please enter one of the following: " cursor = "::> " goodbye = "Thank you for joining the program, have a nice day!" pc.printout(inputText, color="CYAN") try: while running: pc.printout("R - To register for a course", color="WHITE") pc.printout("D - To drop a course", color="WHITE") pc.printout("Q - To exit the program", color="WHITE") inputs = pc.stdinput(cursor, color="YELLOW") if inputs not in entries: pc.printout("That is not a valid input! Try again...", color="RED") elif inputs == "Q": pc.printout(goodbye, color="CYAN") running = False elif inputs == "R": pc.printout( "Please enter the file name containing the students you with to register:", color="BLUE") fileNameInvalid = True while fileNameInvalid: file = pc.stdinput(cursor, color="YELLOW") try: with open(file, "r") as f: try: enrollStudentData(f, enrollTable, queue) except AssertionError as e: pc.printout(e.args[0], color="RED") pc.printout( "The file's contents has been successfully added to the enrollment table.\nHere are the contents in the table:", color="GREEN") pc.printout(enrollTable, bold=False, color="MAGENTA") f.close() with open(enrolled, "w") as f: f.write(str(enrollTable)) f.close() pc.printout( "The waitlist currently contains the following: ", color="GREEN") pc.printout(queue, bold=False, color="MAGENTA") with open(waitlist, "w") as f: f.write(str(queue)) f.close() pc.printout( "The contents of the enrollment table and the waitlist\nhave been save to files named 'enrolled.txt' and 'waitlist.txt' respectively.", color="WHITE") pc.printout(linebreak, color="WHITE") fileNameInvalid = False except FileNotFoundError: pc.printout(noFile, color="RED") elif inputs == "D": pc.printout( "Please enter the file name for students you wish to drop.", color="BLUE") fileNameInvalid = True while fileNameInvalid: file = pc.stdinput(cursor, color="YELLOW") try: with open(file, "r") as f: try: dropStudent(f, enrollTable, queue) except AssertionError as e: pc.printout(e.args[0], color="RED") f.close() with open(waitlist, "a") as f: f.write(str(queue)) f.close() pc.printout( "After updating the contents of the waitlist, here is what it looks like now:", color="GREEN") pc.printout(str(queue), bold=False, color="MAGENTA") pc.printout( "The updated waitlist has been appended to file 'waitlist.txt'.", color="WHITE") pc.printout(linebreak) fileNameInvalid = False except FileNotFoundError: pc.printout(noFile, color="RED") except KeyboardInterrupt: pc.printout('\n' + goodbye, color="CYAN")
def test_remove_table(self): table = EnrollTable(35) student = StudentNode("999670", "ART", "Tosin", "Kuye") student1 = StudentNode("999635", "SCI", "Satoshi", "Nakamoto") student2 = StudentNode("999600", "ART", "Levi", "Ackermann") student3 = StudentNode("999599", "ART", "Tosin", "Kuye") student4 = StudentNode("555555", "SCI", "Satoshi", "Nakamoto") student5 = StudentNode("534125", "SCI", "Satoshi", "Monkery") student6 = StudentNode("211122", "SCI", "Satoshi", "Masaaas") table.insert(student) table.insert(student1) table.insert(student2) table.insert(student3) table.insert(student4) table.insert(student5) table.insert(student6) t = table.remove(student1.getID()) self.assertEqual(table.tableSize, 6) self.assertTrue(t) table.remove("233433") table.remove(student6.getID()) self.assertEqual(table.tableSize, 5)
def test_init_enrollment(self): table = EnrollTable(35) self.assertEqual(table.tableSize, 0) self.assertEqual(table.MAX_CAPACITY, 51) self.assertEqual(len(table.enrollTable), 35)
def test_assert_empty(self): table = EnrollTable(35) student = StudentNode("999670", "ART", "Tosin", "Kuye") student1 = StudentNode("999635", "SCI", "Satoshi", "Nakamoto") student2 = StudentNode("999600", "ART", "Levi", "Ackermann") student3 = StudentNode("999599", "ART", "Tosin", "Kuye") table.insert(student) table.insert(student1) table.insert(student2) table.insert(student3) self.assertFalse(table.isEmpty()) table.remove(student.getID()) table.remove(student1.getID()) table.remove(student2.getID()) table.remove(student3.getID()) self.assertTrue(table.isEmpty())
def test_is_enrolled(self): table = EnrollTable(35) student = StudentNode("999670", "ART", "Tosin", "Kuye") student1 = StudentNode("999635", "SCI", "Satoshi", "Nakamoto") student2 = StudentNode("999600", "ART", "Levi", "Ackermann") student3 = StudentNode("999599", "ART", "Tosin", "Kuye") student4 = StudentNode("555555", "SCI", "Satoshi", "Nakamoto") student5 = StudentNode("534125", "SCI", "Satoshi", "Monkery") student6 = StudentNode("211122", "SCI", "Satoshi", "Masaaas") table.insert(student) table.insert(student1) table.insert(student2) table.insert(student3) table.insert(student4) table.insert(student5) table.insert(student6) self.assertTrue(table.isEnrolled(student.getID())) self.assertTrue(table.isEnrolled(student1.getID())) self.assertTrue(table.isEnrolled(student2.getID())) self.assertTrue(table.isEnrolled(student3.getID())) self.assertTrue(table.isEnrolled(student4.getID())) self.assertTrue(table.isEnrolled(student5.getID())) table.remove(student.getID()) table.remove(student1.getID()) self.assertFalse(table.isEnrolled(student.getID())) self.assertFalse(table.isEnrolled(student1.getID())) self.assertEqual(table.size(), 5)
from enrollStudent import EnrollTable table = EnrollTable(35) finder = 20 i = 999999 c = 0 while i > 100000 and c < 10: if table.cmputIndex(str(i)) == 20: print(i) c += 1 i -= 1