Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# ERROR (This is for only when you exit the code the rest of the code has been working fine) #Traceback (most recent call last):

# ERROR (This is for only when you exit the code the rest of the code has been working fine)

#Traceback (most recent call last): File "C:\Users\Glewi\OneDrive\Desktop\School\CSC-249\Hash\sample.py", line 132, in if (HT.beginChain[n] != None): AttributeError: 'EmployeeHashTable' object has no attribute 'beginChain'

---------------------------

# [Gavin Lewis] # P9 - Employee Hash Table

# The Hash Dictionary points to the first element of the Employee Linked List with its hash (0-11) class HashDictionary: def __init__(self, hashLength): self.beginChain = [] self.endChain = [] for n in range(hashLength): self.beginChain.append(None) self.endChain.append(None)

# Employee Linked List class Employee: def __init__(self, empID, empLastName, empFirstName, empPhone): self.empID = empID self.LastName = empLastName self.FirstName = empFirstName self.phone = empPhone self.nextEmployee = None # corrected variable name self.deleted = False # added deleted flag to mark deleted employees

def __str__(self): # added string representation for Employee return "Employee ID: {}, Last Name: {}, First Name: {}, Phone: {}".format( self.empID, self.LastName, self.FirstName, self.phone)

class EmployeeHashTable: def __init__(self, hashLength): self.hashDict = HashDictionary(hashLength)

def insertEmployee(self, empID, empLastName, empFirstName, empPhone): position = empID % 12 currentNode = self.hashDict.beginChain[position] prevNode = None while currentNode is not None: prevNode = currentNode currentNode = currentNode.nextEmployee emp = Employee(empID, empLastName, empFirstName, empPhone) if prevNode is None: self.hashDict.beginChain[position] = emp else: prevNode.nextEmployee = emp print("Employee with ID {} added to database".format(emp.empID))

def searchEmployee(self, empID): position = empID % 12 currentNode = self.hashDict.beginChain[position] while currentNode is not None: if currentNode.empID == empID and not currentNode.deleted: # check for deleted flag print("Employee Found: ", currentNode) return currentNode currentNode = currentNode.nextEmployee print("Employee with ID {} not found".format(empID)) return None

def deleteEmployee(self, empID): position = empID % 12 currentNode = self.hashDict.beginChain[position] while currentNode is not None: if currentNode.empID == empID and not currentNode.deleted: # check for deleted flag currentNode.deleted = True print("Employee with ID {} marked as deleted".format(empID)) return currentNode = currentNode.nextEmployee print("Employee with ID {} not found".format(empID))

def traverseChain(self, startEMP): print("Employee Chain:") print(" ", startEMP) node = startEMP.nextEmployee while node is not None: print(" ", node) node = node.nextEmployee

# main program from random import randint import csv

hashLength = 12 empList = []

file = open('EmployeeDB.csv') csvreader = csv.reader(file) header = next(csvreader) for employee in csvreader: empList.append(employee)

HT = EmployeeHashTable(hashLength)

for empRecord in empList: empID = int(empRecord[0]) empLastName = empRecord[1] empFirstName = empRecord[2] empPhone = empRecord[3] EMP = Employee(empID, empLastName, empFirstName, empPhone) HT.insertEmployee(empID, empLastName, empFirstName, empPhone)

empIDin = input("Enter Employee ID (001-999, 0 to Exit): ") empID = int(empIDin) while (empID > 0 and empID < 1000): oper = input("Enter Operation Type (S)earch, (I)nsert, (D)elete: ") if (oper == "S"): E = HT.searchEmployee(empID) if (E != None): print(E.empID, E.FirstName, E.LastName, E.phone) else: print(empID, " Doesn't Exist") elif (oper == "I"): E = HT.searchEmployee(empID) if (E != None): print (empID, " Already Exists.", E.LastName, " Cannot Have Duplicates") else: fName = input("Enter Employee First Name: ") lName = input("Enter Employee Last Name: ") phoneNum = input("Enter Employee Phone Number: ") EMP = Employee(empID, lName, fName, phoneNum) HT.insertEmployee(empID, empLastName, empFirstName, empPhone) print (EMP.empID, EMP.LastName, " Record Inserted") elif (oper == "D"): E = HT.searchEmployee(empID) if (E != None): HT.deleteEmployee(empID) print (E.empID, E.LastName, " Deleted") else: print(empID, " Not Found. Cannot Delete") empIDin = input("Enter Employee ID (001-999, 0 to Exit): ") empID = int(empIDin)

for n in range(hashLength): print (n) if (HT.beginChain[n] != None): traverseChain(HD.beginChain[n])

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

=+free to pirate employees from competitors?

Answered: 1 week ago