Shopping cart-user and merchant portal, shopping cart merchant portal
'''
This module requires the user login module code
User Portal:
1. Enter the user name and password
2. The product information is stored in the file.
3. the purchased goods and balances are recorded in the document
Note: After a user purchases a product, the user exits. the user's balance is automatically read when the user logs in again.
Merchant portal:
1. Add Product
2. Modify the product name and price
3. delete a product
Note: You need to create a shop file and a shopping cart file, where the shop file stores the product information:
'''
Import OS
Import userLogin
Import types
Def read_info (_ name ):
If OS. path. getsize ("Shopping Cart "):
Remaining = 0
With open ("Shopping Cart", "r", encoding = "UTF-8") as f:
For line in f:
If line. strip () = "":
Continue
Line = eval (line)
If line ["name"] = _ name:
Remaining = line ["shopping"] [2]
If remaining> line ["shopping"] [2]:
Remaining = line ["shopping"] [2]
If remaining = 0:
Return None
Else:
Return remaining
Else:
Return None
Def memu (num ):
If OS. path. getsize ("shop "):
With open ("shop", "r", encoding = "UTF-8") as f:
Print ("------------ commodity ------------")
For index, line in enumerate (f ):
If line. strip () = "":
Continue
Index-= 1
Line = eval (line)
Info2 = '''
{_ Index}, {_ name}, price: {_ price}
'''. Format (_ index = index + 1,
_ Name = line ["name"],
_ Price = line ["price"])
Print (info2)
Else:
Print ("no items currently ")
If num = 1:
Exit ()
Def shopping_list (name ):
Sum = 0
Flag = 0
With open ("Shopping Cart", "r +", encoding = "UTF-8") as f:
For temp in f:
If temp. strip () = "":
Continue
Temp = eval (temp)
If temp ["name"] = name:
Sum + = temp ["shopping"] [1]
Info2 = '''
Item: {_ goods}, price: {_ price}
'''. Format (_ goods = temp ["shopping"] [0],
_ Price = temp ["shopping"] [1])
If flag = 0:
Print ("--------- shopping list ---------")
Print (info2)
Flag = 1
Print ("Total Amount: % d" % sum)
Def user (_ momey, name ):
Choose = input ("Enter the number of the product you want to purchase >> :")
If choose. isdigit ():
Choose = int (choose)
Elif choose = 'q ':
Shopping_list (name)
Exit ()
Else:
Print ("input error ")
Res = read_info (name)
If res! = None:
_ Momey = res
F = open ("shop", "r", encoding = "UTF-8 ")
Content = f. readlines ()
For I in range (len (content )):
If choose-1 = I:
Content [I] = eval (content [I])
Surplus = _ momey-content [I] ["price"]
If surplus> = 0:
_ Momey = surplus
Print ("\ 033 [33; 1 m % s \ 033 [0 m has been added to the shopping cart. Your current balance is: \ 033 [32; 1 m % d \ 033 [0 m "% (content [I] [" name "], _ momey ))
Save (content [I], name, _ momey)
Return _ momey
Else:
Print ("\ 033 [31; 1 m your balance is insufficient, the current balance is: % d \ 033 [0 m" % _ momey)
Return _ momey
F. close ()
Def save (d, name, momey ):
Dict = {}
Dict ["name"] = name
Dict ["shopping"] = [d ["name"], d ["price"], momey]
With open ("Shopping Cart", "a +", encoding = "UTF-8") as f:
F. write (str (dict) + '\ n ')
Def deposit (name ):
While True:
Res = read_info (name)
If res = None:
Momey = input ("Enter your deposit amount >> :")
Else:
Momey = res
Print ("\ 033 [34; 1 m % s hello, your current balance is: % d \ 033 [0 m" % (name, momey ))
Return momey
If type (momey )! = Type (1 ):
If momey. isdigit ():
Momey = int (momey)
Return momey
Elif momey = 'q ':
Exit ()
Else:
Print ("deposit input error ")
Continue
Def add_shop ():
With open ("shop", "a +", encoding = "UTF-8") as f:
Dict = {}
Goods = input ("enter the name of the product you want to add >>> ")
Price = input ("price of the input item (positive integer) >>> ")
If price. isdigit ():
Price = int (price)
Dict ["goods"] = goods
Dict ["price"] = price
F. write (str (dict) + '\ n ')
Print ("\ 033 [31; 1 m added successfully \ 033 [0 m ")
Def modify_shop ():
List = []
F = open ("shop", "r +", encoding = "UTF-8 ")
Content = f. readlines ()
Num = input ("Enter the product number you want to modify >>> ")
If num. isdigit ():
Num = int (num)
Else:
Print ("input error ")
For I in range (len (content )):
If num-1 = I:
Content [I] = eval (content [I])
Choose = input ("enter the information about the product you want to modify. 1. Name, 2. Price >>> ")
If choose. isdigit ():
Choose = int (choose)
Else:
Print ("input error ")
If choose = 1:
Goods = input ("Enter the modified product name >>> ")
Content [I] ["goods"] = goods
Elif choose = 2:
Price = input ("Enter the modified product price >>> ")
If price. isdigit ():
Price = int (price)
Content [I] ["price"] = price
Else:
Print ("input error ")
List. append (content [I])
F. truncate (0)
F. close ()
With open ("shop", "a +", encoding = "UTF-8") as f1:
For temp in list:
F1.write (str (temp ))
Print ("\ 033 [31; 1 m successfully modified the product \ 033 [0 m ")
Def del_shop ():
List = []
F = open ("shop", "r +", encoding = "UTF-8 ")
Content = f. readlines ()
Num = input ("Enter the product number you want to delete >>> ")
If num. isdigit ():
Num = int (num)
Else:
Print ("input error ")
For I in range (len (content )):
If num-1 = I:
Continue
List. append (content [I])
F. truncate (0)
F. close ()
With open ("shop", "a +", encoding = "UTF-8") as f1:
For temp in list:
F1.write (temp)
Print ("\ 033 [31; 1 m deleted successfully \ 033 [0 m ")
Def main1 (name ):
Res = input ("Enter your options: 1. User, 2. Merchant >>> ")
If res. isdigit ():
Res = int (res)
If res = 1:
Momey = deposit (name)
While True:
Memu (res)
Momey = user (momey, name)
Elif res = 2:
While True:
Memu (res)
Res1 = input ("Enter your options: 1. Add, 2. Modify, 3. Delete >>> ")
If res1.isdigit ():
Res1 = int (res1)
If res1 = 1:
Add_shop ()
Elif res1 = 2:
Modify_shop ()
Elif res1 = 3:
Del_shop ()
Else:
Print ("input error ")
Elif res1 = 'q ':
Exit ()
Else:
Print ("input error ")
Elif res = 'q ':
Exit ()
Else:
Print ("input error ")
If _ name _ = "_ main __":
While True:
UserLogin. main ()
'''
------- User login module code ------
'''
'''
Create a user and lock file, and store the account and password of the user. This file is used for Logon judgment. The lock file is frozen three times after an account error occurs.
User Content format:
{'Name': 'hangsan ', 'passwd': '000000 '}
{'Name': 'lisi', 'passwd': '000000 '}
{'Name': 'hangw', 'passwd': '000000 '}
'''
Import OS
Import shopping
Def lock (_ name ):
''' Determine whether a user is frozen '''
If OS. path. getsize ("lock "):
With open ("lock", "r") as f:
For line in f:
If line. strip () = "":
Continue
Line = eval (line)
If line ["name"] = _ name and line ["num"] = 3:
Return 0
Else:
Return-1
Else:
Return-1
Def clear_lock ():
'''When the program exits, the non-frozen account information will be deleted '''
List = []
With open ("lock", "r +") as f:
For line in f:
If line. strip () = "":
Continue
Line = eval (line)
If line ["num"] = 3:
List. append (line)
F. truncate (0)
With open ("lock", "w") as f1:
For I in list:
F1.write (str (I) + '\ n ')
Def login (_ name, _ passwd ):
'''User login '''
With open ("user", "r") as f:
Flag = 0
For line in f:
If line. strip () = "":
Continue
Line = eval (line)
If line ["name"] = _ name:
If line ["name"] = _ name and line ["passwd"] = _ passwd:
Return 0
Else:
Return 1
Return-1
Def write_lock (_ name ):
'''Write the wrong account into the lock file '''
# The file is not empty.
If OS. path. getsize ("lock "):
List = []
With open ("lock", "r +") as f:
Flag = 0
For line in f:
If line. strip () = "":
Continue
Line = eval (line)
# Determine whether an account exists
If line ["name"] = _ name:
Line ["num"] + = 1
List. append (line)
Else:
Dict2 = {}
Dict2 ["name"] = _ name
Dict2 ["num"] = 1
List. append (dict2)
With open ("lock", "w") as f1:
For I in list:
F1.write (str (I) + '\ n ')
# Writing Empty files directly
Else:
List1 = []
Dict1 = {}
Dict1 ["name"] = _ name
Dict1 ["num"] = 1
List1.append (str (dict1 ))
With open ("lock", "w") as f2:
For j in list1:
F2.write (str (j) + '\ n ')
Def main ():
Name = input ("User name :")
Res = lock (name)
If res = 0:
Print ("% s has been frozen, please contact the Administrator" % name)
Clear_lock ()
Exit ()
Passwd = input ("Password :")
Res1 = login (name, passwd)
If res1 = 0:
Print ("Welcome % s to the CBD Shopping Center" % name)
While True:
Shopping. main1 (name)
Elif res1 = 1:
Print ("Incorrect password ")
Write_lock (name)
Else:
Print ("account does not exist ")
Res2 = lock (name)
If res2 = 0:
Print ("% s has been frozen, please contact the Administrator" % name)
Clear_lock ()
Exit ()
If _ name _ = "_ main __":
While True:
Main ()