Question: For some reason, the code doesn't want t o read the t x t files, need help fixing that Example ManufacturerList.txt , PriceList.txt and ServiceDatesList.txt
For some reason, the code doesn't want read the files, need help fixing that
Example ManufacturerList.txt PriceList.txt and ServiceDatesList.txt are provided for reference. Your code will be expected to work with any group input files of the appropriate format. Manufacturers can and will likely be different as will the items.
Interactive Inventory Query Capability
Query the user of an item by asking for manufacturer and item type.
Print a message No such item in inventory if either the manufacturer or the item type is not in the inventory, more that one of either type is submitted or the combination is not in the inventory. Ignore any other words, so nice Apple computer is treated the same as Apple computer
Print Your item is: with the item ID manufacturer name, item type, and price on one line. Do not provide items that are past their service date or damaged. If there is more than one item, provide the most expensive item.
Also, print You may, also, consider: and print information about the same item type from another manufacturer that closes in price to the output item. Only print this if the same item from another manufacturer is in the inventory and is not damaged nor past its service date.
After output for one query, query the user again. Allow q to quit.
Using Classes is MANDATORY.
Using Pandas is Prohibited.
ManufacturerList.txt
Apple,phone
Dell,laptop
Dell,tower
Lenovo,laptop,Y
Samsung,phone
Apple,laptop
Lenovo,tower
PriceList.txt
ServiceDatesList.txt
My code below:
from datetime import datetime, date
class Item:
def initself itemid manufacturer, itemtype, isdamagedFalse, priceNone, servicedateNone:
self.itemid itemid
self.manufacturer manufacturer
self.itemtype itemtype
self.isdamaged isdamaged
self.price price
self.servicedate servicedate
def isvalidself:
today date.today
return not self.isdamaged and selfservicedate is None or self.servicedate today
class Inventory:
def initself manufacturerfile, pricefile, servicedatefile:
self.items
self.loadmanufacturerlistmanufacturerfile
self.loadpricelistpricefile
self.loadservicedatelistservicedatefile
def loadmanufacturerlistself filepath:
with openfilepath, r as file:
for line in file:
itemid manufacturer, itemtype, isdamaged line.stripsplit
isdamaged lenisdamaged
self.itemsitemid Itemitemid manufacturer, itemtype, isdamaged
def loadpricelistself filepath:
with openfilepath, r as file:
for line in file:
itemid price line.stripsplit
if itemid in self.items:
self.itemsitemidprice floatprice
def loadservicedatelistself filepath:
with openfilepath, r as file:
for line in file:
itemid servicedate line.stripsplit
if itemid in self.items:
self.itemsitemidservicedate datetime.strptimeservicedate, mdYdate
def queryitemself manufacturer, itemtype:
validitems item for item in self.items.values if item.manufacturer manufacturer and item.itemtype itemtype and item.isvalid
if not validitems:
printNo such item in inventory"
return
validitems.sortkeylambda item: item.price, reverseTrue
bestitem validitems
printfYour item is: bestitem.itemidbestitem.manufacturerbestitem.itemtype $bestitem.price:f
othermanufacturers item for item in validitems: if item.manufacturer bestitem.manufacturer
if othermanufacturers:
othermanufacturers.sortkeylambda item: absitemprice bestitem.price
closestitem othermanufacturers
printfYou may, also, consider: closestitem.itemidclosestitem.manufacturerclosestitem.itemtype $closestitem.price:f
def runinventoryquery:
inventory InventoryManufacturerListtxt 'PriceList.txt 'ServiceDatesList.txt
while True:
userinput inputEnter manufacturer and item type eg Apple computer or q to quit: striplower
if userinput q:
break
parts userinput.split
if lenparts:
printInvalid input. Please enter manufacturer and item type."
continue
manufacturer parts
itemtype join
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
