Home > database >  The great god tell me what is need to modify and change
The great god tell me what is need to modify and change

Time:09-27

Working class Goods (object) :
Def __init__ (self, id, name, price) :
The self. The id=id
The self. The name=name
Self. Price=price

Def the __str__ (self) :
Info="\ t the commodity name: number: % s to % s \ \ t t price: % d" % (self. The id, the self. The name, the self price)
Return the info



The class ShopManager (object) :

Def __init__ (self, path) :
# path: read the file path shopdic said: said memory storage containers
The self. The path=path
Self. Shopdic=self. ReadFileToDic ()

Def readFileToDic (self) :
# read a file, write to the dictionary
F=open (self. Path, 'a +' encoding="utf-8")
Clist=f.r eadlines ()
F. lose ()
The index=0
Shopdic={}
While the index & lt; Len (clist) :
Each line # will string segmentation, deposit to the new list
Ctlist=clist [index]. Replace (' \ n ', ""). The split (" |")
# for the contents of each row to an object in the
Good=Goods (ctlist [0], ctlist [1], int (ctlist [2]))
# will be to the store to the collection
Shopdic [good. Id]=good
The index=index + 1
Return shopdic

Def writeContentFile (self) :
# of memory of information written to the file
Str1='
For the key in the self. Shopdic. Keys () :
Good=self. Shopdic [key]
Ele=good. Id + "|" + good. Name + "|" + STR (good. Price) + "\ n"
Str1=str1 + ele
F=open (self. Path, 'w', encoding="utf-8")
F.w rite (str1)
F. lose ()

Def addGoods (self) :
The method of adding goods #
Id=input (" please enter the add goods number: & gt;" )
If self. Shopdic. Get (id) :
Print (" article number already exists, please choose again!" )
Return
Name=input (" please enter the add the name of commodity: & gt;" )
Price=int (input (" please input add commodity prices: & gt;" ))
Good=Goods (id, name, price)
Self. Shopdic [id]=good
Print (" add success!" )

Def deleteGoods (self) :
The method of # remove goods
Id=input (" please enter remove goods number: & gt;" )
If self. Shopdic. Get (id) :
Del self. Shopdic [id]
Print (" deleted successful!" )
The else:
Print (" article number does not exist!" )

Def showGoods (self) :
# show all commodity information
Print ("="* 40)
For the key in the self. Shopdic. Keys () :
Good=self. Shopdic [key]
Print (good)
Print ("="* 40)

Def adminWork (self) :
Info="" "
==========welcome to good oh sea shopping mall==========
Function of the input number, you can choose the following functions:
Input "1" : show the commodity information
Input "2" : add commodity information
Input "3" : delete the commodity information
Input "4" : exit system function
==========================================
"" "
Print (info)
While True:
Code=input (" please input function number: & gt;" )
If code=="1" :
Self. ShowGoods ()
Elif code=="2" :
Self. AddGoods ()
Elif code=="3" :
Self. DeleteGoods ()
Elif code=="4" :
Print (" thank you for your use, are exit system!!!!!
")Self. WriteContentFile ()
Break
The else:
Print (" input number is wrong, please input again!!!!!
")
Def userWork (self) :
Print ("==============welcome to good oh sea shopping mall==============")
Print (" you can enter the serial number and purchase quantity of choose and buy goods, input Numbers for n the checkout ")
Self. ShowGoods ()
Total=0
While True:
Id=input (" please enter the purchase goods number: & gt;" )
If id=="n" :
Print (" % d yuan in buying goods consumption, thank you for coming!" % (total))
Break
If self. Shopdic. Get (id) :
Good=self. Shopdic [id]
Num=int (input (" please enter the purchase quantity: & gt;" ))
Total=total + good. Price * num
The else:
Print (" input number is wrong, please check and enter again!" )



Def login (self) :
# login
Print ("==========welcome login good sea shopping mall==========")
Uname=input (" please enter the user name: & gt;" )
Password=input (" please enter the password: & gt;" )
If the uname=="admin" :
If the password=="123456" :
Print (" welcome, "uname," administrator ")
Self. AdminWork ()
The else:
Print (" administrator password mistake, login failed!" )
The else:
Print (" user welcome, % s "% (uname))
# perform user purchase function
Self. UserWork ()


If __name__=="__main__ ':
ShopManage=ShopManager (" shop. TXT ")
ShopManage. The login ()
  • Related