Microsoft Word - CustomerAssignment.docx PythonProgramming CustomerAssignment DueMay2,2021 GencoPuraOliveOilhasafilecontainingitscustomers,afilecontainingthe...

adely


Microsoft Word - CustomerAssignment.docx PythonProgramming CustomerAssignment DueMay2,2021 GencoPuraOliveOilhasafilecontainingitscustomers,afilecontainingthe customers’orders,andhasbeguncreatingaprogramtoreadthefilesandprintthe contentsinahuman-readableformat. Filestosubmit:Customer.py runCustomer.py Inputfiles: Therearetwoinputfiles.Thefirstoneiscalledcustomers.txt.Eachlineofthisinput filecontainsacustomernumberandnameseparatedbyacomma.Thesecondinput fileiscalledorders.txt.Thisfilecontainslineseachcorrespondingtoanorder.Each linecontainsacustomernumber,aproductname,quantity,andordertotalall separatedbycommas.Customer.py InafilecalledCustomer.py,createaclasscalledCustomer.MyfilewouldbecalledconwayCustomer.pyandmy classwouldbecalledConwayCustomer. Attributes Theclasswillhavethreeattributes(alsocalledinstancevariables):anintcalled number,astringcalledname,andalistcalledorders.Thelistwillcontainalistof tuples,eachcorrespondingtoanorderforthatcustomer. __init__() Defineaconstructor(init)method.Inadditiontoself,theconstructorwilldefine twomoreparameters,oneforcustomernumber,andoneforcustomername.The constructorshouldusethesevaluestoinitializeattributesnumberandname.Set thelist(calledorders)toanemptylist. addOrder() DefineamethodaddOrder().Inadditiontoself,thismethodwilldefineonemore parameter,atuplecalledorder.Addthistupletothelistattributecalledorders. __str__() Definethe__str__()method.Anobject’sstr()methodbuildsandreturnsastring representationofanobject’sstate.Thisallowsanobjecttobesenttotheprint() function(asisoccurringinthemainmethod).Createastringthatincludesthe customer’snameandnumber.Thenappendtothisstringalltheordersinthe orderslist.Loopoverthelistandaddtheitemsinthetupletothestring.Theitems inatuplecanbeaccessedthesamewaytheitemsinalistcanbeaccessed–using thenameofthetupleandtheindexasasubscript. runCustomer.py AfilecalledrunCustomer.pyisprovidedtoyouandispartiallycompleted.Change thisfilenametorunCustomer.py.Myfilewouldbecalled runConwayCustomer.py.Thisfilecontainsanimportstatement,amainmethod,a functioncalledreadCustomers(),andthefunctionheaderofafunctionyouwill createcalledreadOrders().YouwillhavetomodifytheimportstatementtofromCustomer import Customer.My importstatementwouldbefrom conwayCustomer import ConwayCustomer ThemainmethodcallsafunctioncalledreadCustomers()thatopensaninputfile, readsthecontents,andreturnsadictionary.Itthencallsafunctioncalled readOrders()thattakesthatdictionaryasaparameter;thisfunctionwillreadfrom aninputfilecalledorders.txtandaddtheorderstotheappropriatecustomer.The mainmethodthenloopsthroughthecustomerdictionaryandprintseachcustomer. FunctionreadCustomers() ThefunctionreadCustomers()isalreadywritten.Thisfunctionopenstheinputfile, customers.txt,forreading.Anemptydictionaryiscreated.Thedictionarywillmap acustomer’snumbertoacustomerobject.Aseachlineoftheinputfileisreadin asastring,thestringissplitusingthecommadelimiter.Thisreturnsalist.Thefirst itemintheresultinglististhecustomernumberandtheseconditemisthe customername.ThecustomerobjectiscreatedbycallingtheCustomerclass’s constructor(init)methodusingthesevalues.Anentryismadeintothedictionary usingthecustomernumberasthekeyandtheCustomerobjectasthevalue(you’ll havetomodifythistoreflectyourclassname).Finally,thedictionaryisreturned. FunctionreadOrders() YouarecreatingthereadOrders()function.Thisfunctiontakesasitsparameterthe dictionaryfilledwithcustomernumber/customerobjectkey/valuepairs. Thisfunctionshouldopentheinputfile,orders.txt,forreading.Itshouldthenloop throughthefileinasimilarwayasthereadCustomers()function,asthevaluesare alsodelimitedbycommas. Getthecustomerobjectfromthedictionary.Thiscanbedoneusingthedictionary get()methodandsendingthecustomernumber.Thegetmethodcantakeasecond argumentthatisreturnedifthecustomernumberisnotakeyinthedictionary. Ifthecustomerexistsinthedictionary,createatupleconsistingoftheproduct(a string),thequantity(anint),andtheordertotal(afloat).Addthistupletothe customer’slistofordersbysendingittothecustomer’saddOrder()method. Copyright Policy: The copy in this document has been provided for educational purposes in accord with copyright law. This material is protected by copyrights owned by the instructor. The user of this work is responsible for adhering to copyright law. This material cannot be re-published, uploaded, posted, transmitted or distributed in any way. Hereissampleoutput:
Apr 28, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here