Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Put in tkinter please. Python 3 ''' HousemateHelper: Creates a database of housem ''' class HousemateHelper: Class stores a Database of Items entered with

Put in tkinter please. Python 3 ''' HousemateHelper: Creates a database of housem ''' class HousemateHelper: """ Class stores a Database of Items entered with cost and quantities. also stores a dictionary of persons and their individual purchases. """ def __init__(self): self.inventory = {}#The Maluable inventory. self.names = {}#Stores a dictionary of all of the Person Objects connected to the name which are keys

def addPurchase(self, name, item, quantity, cost): """ Add Individual purchases to the database - Very general (Accepts any item or cost, etc). - Every new person is saved as a new person object. - Every item entry that is disimilar from the previous will create a new entry in the dictionary. """ if name in self.names: pass else: self.names[name]=Person()#if the person has not entered anything previously, they will have a new person object created for them and be added to the dictionary self.names[name].addPurchase(item,quantity,cost,name)#Functions in the same way as the current method, but stores a static dictionary that can not be affected unless equalized. if item in self.inventory: #If item exists, simply add to the already entered quantities self.inventory[item][0]+= quantity self.inventory[item][1]+= cost else: self.inventory[item]= [self.names[name].getItems()[item][0],self.names[name].getItems()[item][1]] def getInventory(self,s): """ Return the Quantity of dictionary key [s] """ return self.inventory[s][0] def useItem(self,s,k): """ Remove a certain number of Items for Key value S. """ if self.inventory[s][0] <= k: del self.inventory[s] else: self.inventory[s][0] -= k def personValue(self,s): """ How far below or above a person is to the Average of all purchases """ person = self.names[s].getTotal() return round(person -(self.getAverage()),2) def equalize(self): """ Returns a List of tupples containing (A,B,x) (A owes B: X amount of money to bring them closer to converging at the average) """ ower = [] owed = [] Shpay = [] for i in self.names: if self.personValue(i) < 0: ower.append([i,(self.personValue(i))]) if self.personValue(i) > 0: owed.append([i,(self.personValue(i))]) #print (ower,owed) for i in ower: for b in owed: while not i[1] >= 0 and not b[1] <= 0: x = b[1] + i[1] if x <= 0: Shpay.append((i[0],b[0],round(abs(b[1]),2))) i[1] = x b[1] = 0 if x > 0: Shpay.append((i[0],b[0],round(abs(i[1]),2))) b[1] = x i[1] = 0 #print(b[1],i[1]) return Shpay

def getAverage(self): #The average is essentially the limit value where all purchases converge to. This is based on adding a summation of any number sequence. Those numbers Averaged and multiplied by n amount of them divided by 2. """ Based on the individual purchases to escape the possibility of missing items. returns the average cost between purchasers. """ total = 0 count = 0 for i in self.names: total += self.names[i].getTotal() count += 1 #print(count) if count == 0: return 0 return round(total/count,2)

def __str__(self): ''' Returns a string of who owes who what. ''' string = "" if len(self.names)> 1: for i in self.equalize(): string += "{} owes {} ${}. ".format(i[0],i[1],i[2]) string += "To balance the spending to an average of ${}.".format(self.getAverage()) string += " " return string

class Person: def __init__(self): self.items = {} self.total = 0 self.name = "" def addPurchase(self,item,quantity,cost,name): """ Add New purchases to the individuals list of items """ if item in self.items: self.items[item][0] += quantity self.items[item][1] += cost else: self.items[item]=[quantity,cost] self.name = name def getItems(self): return self.items

def __str__(self): string = '' if self.items != {}: for i in self.items: string += "{}:\tItem:{}\tQuantity:{}\tCost:{} ".format(self.name,i,self.items[i][0],self.items[i][1]) string += " " return string + "Valued at: {}".format(self.getTotal()) def getTotal(self): Total = 0 for i in self.items: Total += self.items[i][1] #print(v) self.total = Total return self.total

# TESTER def testHelper():

H = HousemateHelper() H.addPurchase('Juan', 'food', 7, 50) H.addPurchase('Alice', 'food', 3, 30) print("Should return 10: ", H.personValue('Juan') ) print("Should return -10: ", H.personValue('Alice') ) print("Equalize: " ) print(H.equalize())

H = HousemateHelper() H.addPurchase('Juan', 'food', 7, 50) H.addPurchase('Alice', 'food', 3, 5) H.addPurchase('Bob', 'toy', 18, 35) H.addPurchase('Liz', 'utility', 2, 10)

H.useItem('food', 4) print("Should return 6: ", end='') print(H.getInventory('food')) # returns 6 (i.e. 7 + 3 - 4)

print("Should return 25: ", H.personValue('Juan') ) # returns 25 print("Should return 10: ", H.personValue('Bob') ) # returns 10 print("Should return -20: ", H.personValue('Alice') ) # returns -20 (note negative) print("Should return -15: ", H.personValue('Liz') ) # returns -15 (note negative)

print("Equalize: " ) print(H.equalize()) # The following line is a possible return #[ ('Alice', 'Bob', 10), ('Alice', 'Juan', 10), ('Liz', 'Juan', 15) ]

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

Graph Databases In Action

Authors: Dave Bechberger, Josh Perryman

1st Edition

1617296376, 978-1617296376

More Books

Students also viewed these Databases questions

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago