Jan-18-2018, 10:47 AM
Hi, I am currently trying to code for this particular question which is
Uncle Tim started his Secondhand Bookstore in 2000. As years passed, the number of books in his store increased significantly. He can no longer keep track of their location in his store. He has approached you to develop a program (in Python) that could help him to keep track of the books.
My Current Codes Are :
Thanks In Advance
Uncle Tim started his Secondhand Bookstore in 2000. As years passed, the number of books in his store increased significantly. He can no longer keep track of their location in his store. He has approached you to develop a program (in Python) that could help him to keep track of the books.
My Current Codes Are :
class Node: def __init__(self,data): self.data = data self.next = None def getData(self): return self.data def setData(self, newData): self.data = newData def getNext(self): return self.next def setNext(self,newNode): self.next=newNode class Book: def __init__(self, author, title, price): self.author = author self.title = title self.price = price def __str__(self): return 'Author: ' + self._author + ', Title: ' + \ self._title + ', Price: ' + str(self._price); def setAuthor(self, author): self._author = author def getAuthor(self): return self.author def setTitle(self, title): self._book = book def getTitle(self): return self.title def setPrice(self, price): self._price = price def getPrice(self): return self._price class LinkedList: def __init__(self): self.head = None self.size = 0 def AddBookToFront(self, data): print("1") newNode = Node(data) newNode.setNext(self.head) self.head = newNode self.size+=1 def DisplayBook(self): print("3") tempo = self.head while tempo is not None: print ("Author Name: " + tempo.getData().getAuthor() +" " +"Title: " + tempo.getData().getTitle()) tempo = tempo.getNext() def RemoveBookAtPosition(self,position): # If linked list is empty if self.head == None: return # Store head node temp = self.head # If head needs to be removed if position == 0: head = temp.next temp = None return # Find previous node of the node to be deleted for i in range(position -1 ): temp = temp.next if temp is None: break # If position is more than number of nodes if temp is None: return if temp.next is None: return # Node temp.next is the node to be deleted # store pointer to the next of node to be deleted next = temp.next.next # Unlink the node from linked list temp.next = None temp.next = next def AddBookAtPosition(self, data , position): cursor= self.head temps = Node(data) index = 0 prev = None if (position ==0): temps.setNext(cursor) self.head = temp else: while (index < position): prev = cursor cursor = cursor.getNext() index +=1 temps.setNext(prev.getNext) prev.setNext(temps) ll=LinkedList() book1 = Book("AAA","BBB","$4") #n1=Node(book1) book2 = Book("CCC", "BBB", "$5") #n2=Node(book2) book3 = Book("DDD", "CCC", "$6") #n3=Node(book3) book4 = Book("GGG", "FFF", "$3") ll.AddBookToFront(book1) ll.AddBookToFront(book2) ll.AddBookToFront(book3) #ll.AddBookAtPosition(book4, 2) ll.DisplayBook() print("After Removing") ll.RemoveBookAtPosition(1) ll.DisplayBook() print("Add Book At Position") ll.AddBookAtPosition(book4, 2) ll.DisplayBook() As Far As I Know This Is The Problem Causing Code [python] def DisplayBook(self): print("3") tempo = self.head while tempo is not None: print ("Author Name: " + tempo.getData().getAuthor() +" " +"Title: " + tempo.getData().getTitle()) tempo = tempo.getNext()This Is My Current Output
Output:1
1
1
3
Author Name: DDD Title: CCC
Author Name: CCC Title: BBB
Author Name: AAA Title: BBB
After Removing
3
Author Name: DDD Title: CCC
Author Name: AAA Title: BBB
Add Book At Position
3
Author Name: DDD Title: CCC
Author Name: AAA Title: BBB
Author Name: GGG Title: FFF
Traceback (most recent call last):
File "c:\Users\TP_baseline\Desktop\DSAG\ProjectTest9-2.py", line 134, in <module>
ll.DisplayBook()
File "c:\Users\TP_baseline\Desktop\DSAG\ProjectTest9-2.py", line 62, in DisplayBook
print ("Author Name: " + tempo.getData().getAuthor() +" " +"Title: " + tempo.getData().getTitle())
AttributeError: 'function' object has no attribute 'getData'
Error CodeError:Traceback (most recent call last):
File "c:\Users\TP_baseline\Desktop\DSAG\ProjectTest9-2.py", line 134, in <module>
ll.DisplayBook()
File "c:\Users\TP_baseline\Desktop\DSAG\ProjectTest9-2.py", line 62, in DisplayBook
print ("Author Name: " + tempo.getData().getAuthor() +" " +"Title: " + tempo.getData().getTitle())
AttributeError: 'function' object has no attribute 'getData'
Any Guidance On Solving This Issue Would Be Greatly Appreciated.Thanks In Advance