SHARE
TWEET

Untitled

a guest Jan 24th, 2020 142 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import operating system and the date
  2. import os
  3. from datetime import date
  4.  
  5. def main():
  6.     check_files()
  7.     loginMenu()
  8.  
  9. def check_files():
  10. #check to see if the files exists.
  11.     for i in ["stock","users","orders","customers"]:
  12. #If it does'nt, create the missing file(s)
  13.         if not os.path.isfile(i+".txt"):
  14. #Let the user know that the files are being created
  15.             print ("Creating the " + i + " file...")
  16.             stockID = open(i+".txt", 'w')
  17. #Let the user know that the file(s) have been created
  18.             print(i + "file created!")
  19.  
  20. def loginMenu():
  21. #Show the user a home screen
  22.     print("""
  23. ##########################################################################
  24. #                                                                        #
  25. #                   Guild of the Ceramic Arts                            #
  26. #                       Login Menu Below!                                #
  27. #                                                                        #
  28. ##########################################################################
  29.  
  30. """)
  31. #Run the verifyUser function
  32.     verifyUser()
  33.  
  34. def verifyUser():
  35. #Assign independant variables
  36.     verifyUsername,verifyPassword = "",""
  37. #Allow the user to enter their username and password
  38.     username = str(input("\nPlease enter your username: \n"))
  39.     password = str(input("\nPlease enter your password: \n"))
  40.  
  41.     while True:
  42.         readUsers = open("users.txt","r")
  43. #Read the files to verify user authentication
  44.         lines = readUsers.readlines()
  45. #Tell the user if the file is empty
  46.         if len(lines) == 0:
  47.             print("There are no Users in the file for you to search!\n")
  48. #Sart the function newUser as there are no existing users        
  49.             newUser()
  50.             continue
  51.         verifyCounter = 0
  52.         i = 0
  53. #If there are users created, verify if the username and password match
  54.         while i < len(lines):
  55.             location = lines[i]
  56.  
  57.             verifyUsername = location [0:10]
  58.             verifyUsername = verifyUsername.strip()
  59.             verifyUsername = verifyUsername.lower()
  60.             username = username.lower()
  61.  
  62.             verifyPassword = location[10:20]
  63.             verifyPassword = verifyPassword.strip()
  64.             verifyPassword = verifyPassword.lower()
  65.             password = password.lower()
  66.  
  67.             if location == "":
  68.                 readUsers.close()
  69.                 break
  70. #Allow the user to continue
  71.             if verifyUsername == username and verifyPassword == password:
  72.                 verifyCounter = verifyCounter + 1
  73.                 mainMenu()
  74.                 break
  75.             i += 1
  76. #If the credentials are incorrect, allow the user to re-enter their username and password
  77.         if verifyCounter ==0:
  78.             print("\nThere is no user in the Users file with those login details.\n")
  79.             loginMenu()
  80.         else:
  81.             break
  82.  
  83. def mainMenu():
  84.     while True:
  85. #Print a menu for the user
  86.         print("""
  87. ##########################################################################
  88. #                                                                        #
  89. #                          Guild of Ceramic Arts                         #
  90. #                                Main Menu                               #
  91. #                                                                        #
  92. #  What would you like to do?                                            #
  93. #                                                                        #
  94. #  1 - Add new User                                                      #
  95. #  2 - Add new customer                                                  #
  96. #  3 - Add new product                                                   #
  97. #  4 - Delete product                                                    #
  98. #  5 - Add new order                                                     #
  99. #  6 - Review an Invoice
  100. #  7 - Review an unpayed order                                                                    
  101. #                                                                        #
  102. #  7 - Logout                                                            #
  103. #  8 - Exit                                                              #
  104. #                                                                        #
  105. ##########################################################################
  106. """)
  107. #Allow the user to choose an option
  108.         mainMenu=input("What would like to do? Option '1', '2', '3', '4', '5' , '6' or '7'? ")
  109.         if mainMenu =="1":
  110.             newUser()
  111.             break
  112.         elif mainMenu =="2":
  113.             newCustomer()
  114.             break
  115.         elif mainMenu =="3":
  116.             newProduct()
  117.             break
  118.         elif mainMenu =="4":
  119.             removeProduct()
  120.             break
  121.         elif mainMenu =="5":
  122.             newOrder()
  123.             break
  124.         elif mainMenu =="6":
  125.             printOrderInvoice()
  126.             break
  127.         elif mainMenu =="7":
  128.             quit()
  129.         elif mainMenu =="8":
  130. #If the user enters an invalid option, tell the
  131.         else:
  132.             print("\nThat is not a valid entry, you must select '1', '2', '3', '4', '5' , '6' or '7'")    
  133.  
  134.  
  135.  
  136. def newUser():
  137. #Assign independant variables
  138.     userName, password1, password2 = "", "", ""
  139.     while True:
  140. #Allow the user to enter a new username
  141.         userName=input("\nInsert a Username for the new user (input '-1' to cancel and exit): ")
  142. #Allow the user to leave the newUser function if they wish to
  143.         if userName =="-1":
  144.             mainMenu()
  145. #Make sure the username is not over 10 characters long
  146.         if len(userName)>10:
  147.             print("\nUsername has to be between 1 and 10 characters!")
  148. #Make sure the username only uses letters
  149.         elif userName.isalpha()==False:
  150.             print("\nYou need to use letters only!")
  151.         else:
  152.             break
  153.     while True:
  154.         password1=input("\nInput a password for the user (input '-1' to cancel and exit): ")
  155. #Allow the user to leave the newUser function if they wish to
  156.         if password1 =="-1":
  157.             mainMenu()
  158. #Make sure the password is not over 10 characters long
  159.         if len(password1)>10:
  160.             print("\nPassword has to be between 1 and 10 charcters!")
  161. #Allow the user to re-enter their password
  162.         password2=input("\nInput the password again (input '-1' to cancel and exit): ")
  163. #Allow the user to leave the newUser function if they wish to
  164.         if password2 =="-1":
  165.             mainMenu()
  166. #Make sure the second entry of the password is not over 10 characters long
  167.         if len(password2)>10:
  168.             print("\nPassword has to be between 1 and 10 charcters!")
  169. #Ensure that both passwords match before creating a user
  170.         if password1 != password2:
  171.             print("\nThe passwords did not match!")
  172.         else:
  173.             break
  174. #Change the username and password into lower case characters if possible
  175.     userName=userName.lower()
  176.     password1=password1.lower()
  177. #Ensure that the username and password take up exactly ten spaces
  178.     userNameStore=userName.ljust(10)
  179.     password1Store=password1.ljust(10)
  180. #Write the user credentials in the users text file
  181.     store=open("users.txt","a")
  182.     iStore=userNameStore+password1Store+"\n"
  183.     store.write(iStore)
  184.  
  185.     store.close()
  186. #Let the user know that a new user has been
  187.     print("\n" + userName + " has been created!")
  188.     print("Username  :",userName)
  189.     print("Password  : ********** ")
  190.  
  191.     mainMenu()
  192.  
  193. def newOrder():
  194.  
  195.     while True:
  196. #Allow the user to insert a customer ID
  197.         customerID = input("Please enter your customer ID: ")
  198.         with open('customers.txt') as f:
  199. #Verify if the user exists
  200.             if customerID in f.read():
  201. #Let the user know if the ID is valid or not
  202.                 print("\nThe customer ID is valid")
  203.                 break
  204.             else:
  205.                 print("\nThat Customer ID does not exist. Please try again")
  206. #Ask the user to input their order ID
  207.     while True:
  208.         orderID = input("Please enter a unique ID for your order (5 numbers): ")
  209.         if orderID.isdigit()==False:
  210.             print("\nYou can only use numbers for your unique ID")
  211.         elif len(orderID) != 5:
  212.                 print("\nYou must use exactly 5 digits")
  213. #Create a unique order ID for the customer
  214.         else:
  215.             print("Your unique ID is OR" + orderID)
  216.             break
  217. #Asks the user to input what product the customer would like to buy and ensures there are no errors
  218.     while True:
  219.         item_name = input("\nPlease type the name of the item the customer would like to purchase: ")
  220.         with open('stock.txt') as f:
  221.             if item_name in f.read():
  222. #Tells the reader that the item has been added
  223.                 print("\nItem Added")
  224.                 break
  225. #Lets the user know that they've spelt the item incorrectly
  226.             elif item_name not in f.read():
  227.                 print("\nItem does not exist. Make sure it is spelt correctly!")
  228.  
  229.     while True:
  230. #calculates the price of a single item
  231.         readStock = open('stock.txt','r')
  232.         location = readStock.readline()
  233.         price = location[33:50]
  234.         price = float(price)
  235. #Allows the user to add more than one of the same item
  236.         quantity = int(input("\nHow many of the singular product would the customer like to purchase?:  "))
  237.         if quantity >100:
  238.             print("\nThe customer cannot buy more than 100 items")
  239.             break
  240.         elif quantity <1:
  241.             ("\nThe customer must buy a minimum of 1 item!")
  242. #Calculates the final price
  243.         finalPrice = (price*quantity)
  244.         break
  245. #If the price is over £50, adds a 5% discount
  246.         if finalPrice >= 50:
  247.             finalPrice *= 0.95
  248.             readStock.close()
  249.             break
  250.            
  251. #Ensures the order ID, final price, the date, whether it's been payed or not and the quantity are all ready to be stored in the orders file      
  252.     orderID = ("OR")+ orderID
  253.     orderID=orderID.strip()
  254.     orderID=orderID.lower()
  255.     orderIDStore=orderID.ljust(13)    
  256.     customerID=customerID.strip()
  257.     customerID=customerID.lower()
  258.     customerIDStore=customerID.ljust(13)
  259.     finalPrice=str(finalPrice)
  260.     finalPrice=finalPrice.ljust(7)
  261.     today = date.today()
  262.     today = str(today)
  263.     today = today.ljust(15)
  264.     quantity = str(quantity)
  265.     quantityStore = quantity.ljust(5)
  266.     paid = " 0 "
  267. #Stores the above into the orders file
  268.     order_file = open('orders.txt', 'a')
  269.     stock_file = open('stock.txt', 'r')
  270.     for line in stock_file:
  271.         if item_name in line:
  272.             line = line.ljust(12)
  273.             order_store = finalPrice + today + customerIDStore + quantityStore + orderIDStore + paid + line
  274.             order_file.write(order_store + "\n")
  275. #Shows the user that the chosen items hae been succesfully added
  276.             print("\nThe chosen item(s) have been added to the order")
  277.             order_file.close()
  278.             stock_file.close()
  279.             mainMenu()
  280.  
  281. def unpaidOrders():#checks if an order has been payed or not
  282.     while True:
  283.             order_ID = input("Please enter your order ID\n ")
  284.             with open('orders.txt') as f: #opens the file
  285.                 if order_ID in f.read(): #if the orderID is in the file:
  286.                     print("Thank you")
  287.                     break
  288.  
  289.     today = datetime.datetime.now() #detecting Current Date
  290.  
  291.     with open('orders.txt') as orders:
  292.         expired = []
  293.         dates = [[datetime.datetime.strptime(i[36:62].strip(), '%Y-%m-%d %H:%M:%S.%f'),i[12:24]] for i in orders.readlines()]
  294.         for i in dates: #check and print expired invoices
  295.             if today > i[0] + datetime.timedelta(28): #checks if the date is later than the date saved in the file
  296.                 expired.append(i)
  297.         if len (expired) >0:
  298.             print("Late orders:\n" + "\n".join([("Order ID: " + i[1] + " Date: " + i[0]) for i in[[str(i[0]),i[1]] for i in expired]]))#prints the order which is late and the date of  the order
  299.         else:
  300.             print("There are currently no late orders")
  301.  
  302.     mainMenu()
  303.    
  304. def payOrder():
  305.     while True:
  306.         order_ID = input("Please enter your order ID\n ")
  307.         with open('orders.txt') as f: #opens the file
  308.             if order_ID in f.read(): #Checks if the order is in the file
  309.                 print("Thank you")
  310.                 break
  311.     while True:
  312.         pay_file = open("paid.txt","a")
  313.         orders_file = open('orders.txt', 'r')
  314.         for line in orders_file:
  315.             if order_ID in line:
  316.                 line = line.ljust(12) #gives room at the end of the line
  317.                 pay_file.write(line + "\n") #writes the line into the paid flie        
  318.         break
  319.  
  320.     while True:      
  321.         with open("orders.txt","r") as f:
  322.             f.seek(0)
  323.             total = 0
  324.             for line in f:
  325.                 if order_ID in line:
  326.                     total_price = line[24:36]
  327.                     total += float(total_price)
  328.  
  329.         if total>50:
  330.             total = float(total-(total*0.05))
  331.             total = str(total)
  332.             print("\nYour order total is:     ",total + " (Including 5% discount)")
  333.             break
  334.         else:
  335.             print("\nYour order total is:     ",total)
  336.             break
  337.  
  338. def newCustomer():
  339.     customerID,customerForeName,customerLastName,customerTown,customerHouseNumber,customerPostcode,customerPhoneNumberNumber ="","","","","","",""
  340.     while True:
  341. #Allows the user to input and save the customers forename, surname, town, building number, postcode and phone number
  342.         customerForeName=input("\nInput the Customers Forename: ")
  343.         if len(customerForeName)>20:
  344.             print("\nThe Customers Forename must be between 1 and 20 characters")
  345.         elif customerForeName.isalpha()==False:
  346.             print("\nYou can only use letters for the customers Forename.")
  347.         else:
  348.             break
  349.     while True:
  350.         customerLastName=input("\nInput the customers Surname: ")
  351.         if len(customerLastName)>20:
  352.             print("\nCustomer Surname has to be between 1 and 20 characters")
  353.         elif customerLastName.isalpha()==False:
  354.             print("\nYou must only use letters for the customers Surname")
  355.         else:
  356.             break
  357.     while True:
  358.         customerTown=input("\nInput the customers Town: ")
  359.         if len(customerTown)>20:
  360.             print("\nThe Customers Town name must be between 1 and 20 characters")
  361.         else:
  362.             break
  363.     while True:
  364.         customerHouseNumber=input("\nInput the Customers Building Number: ")
  365.         if len(customerHouseNumber)>5:
  366.             print("\nThe Customers Building Number must be less than 5 characters")
  367.         else:
  368.             break
  369.     while True:
  370.         customerPostcode=input("\nInput the Customers Postcode: ")
  371.         if len(customerPostcode)>8:
  372.             print("\nThe customers Postcode must be less than 8 characters")
  373.         else:
  374.             break
  375.     while True:
  376.         customerPhoneNumber=input("\nInput the customers Phone Number: ")
  377.         if len(customerPhoneNumber)>11:
  378.             print("\nThe Customer Phone Number must be less than 11 numbers")
  379.         elif customerPhoneNumber.isdigit()!=True:
  380.             print("\nYou can only use numbers for the customers Phone Number")
  381.         else:
  382.             break
  383. #Stores the above in the customers file
  384.     customerHouseNumber=customerHouseNumber.strip()
  385.     customerPostcode=customerPostcode.replace(" ", "")
  386.     customerID=(customerHouseNumber)+(customerPostcode)
  387.     customerID=customerID.strip()
  388.     customerID=customerID.lower()
  389.     customerIDStore=customerID.ljust(12)
  390.     customerForeNameStore=customerForeName.ljust(20)
  391.     customerLastNameStore=customerLastName.ljust(20)
  392.     customerTownStore=customerTown.ljust(20)
  393.     customerHouseNumberStore=customerHouseNumber.ljust(5)
  394.     customerPostcodeStore=customerPostcode.ljust(8)
  395.     customerPhoneNumberStore=customerPhoneNumber.ljust(11)
  396.     store=open("customers.txt","a")
  397.     iStore=customerIDStore+customerForeNameStore+customerLastNameStore+customerTownStore+customerHouseNumberStore+customerPostcodeStore+customerPhoneNumberStore+"\n"
  398.     store.write(iStore)
  399.     store.close()
  400. #Lets the user know that the customer has been stored succesfully
  401.     print("\nA new customer has been succesfully created!")
  402.     print("Customer ID:             ",customerIDStore)
  403.     print("Customer Forename:       ",customerForeNameStore)
  404.     print("Customer Surname:        ",customerLastNameStore)
  405.     print("Customer Town:           ",customerTownStore)
  406.     print("Customer House Number:   ",customerHouseNumberStore)
  407.     print("Customer Posecode:       ",customerPostcodeStore)
  408.     print("Customer Phone Number:   ",customerPhoneNumberStore)
  409.     mainMenu()
  410.    
  411. def newProduct():
  412.     productID,productPrice,productName = "","",""
  413.  
  414. #Allows the user to create a product name and a product ID
  415.     while True:
  416.         productName = input("\nInput the product name you would like to add: ")
  417.         productName.strip()
  418.         productName.lower()
  419.         if len(productName)>25:
  420.             print("\nProduct name must be between 1 and 25 characters! Try Again! ")
  421.         else:
  422.             break
  423.     while True:
  424.         productID = input("\nInput the product ID of the product you want to add (5 Numbers): ")
  425.         productID.strip()
  426.         if len(productID)>5:
  427.             print("\nThe product ID, '"+str(productID)+"' exceeds the allowed amount of digits")
  428.         elif productID.isdigit()!=True:
  429.             print("\nYou can only use numbers for the product ID!")
  430.         else:
  431.             break
  432.        
  433.        
  434.     productPrice=float(input("\nInput the price of the your product(numbers only!): "))
  435.     productPrice=str(productPrice)
  436.     productID = "PT"+productID
  437.     productID.strip()
  438.     productID.lower()
  439.     productIDStore = productID.ljust(8)
  440.     productName.strip()
  441.     productName.lower()
  442.     productNameStore = productName.ljust(25)
  443. #Stores the new product in the stock file  
  444.     store= open("stock.txt","a")
  445.     istore= productNameStore + productIDStore + productPrice+ "\n"
  446.     store.write(istore)
  447.  
  448.     store.close()
  449. #tells the user that the new product has been added and repeats the details
  450.     print("\nFinished adding the new product")
  451.     print("Product ID:             ",productIDStore)
  452.     print("Product Name:       ",productNameStore)
  453.     print("Product Price:        ",productPrice)
  454.     mainMenu()
  455.  
  456. def removeProduct():
  457. #Asks the user what product they would like to delete, locates the product and deletes it
  458.     product = input("What product would you like to delete (Insert Name)? ")
  459.     f = open("stock.txt","r+")
  460.     r = open("stock.txt","w")
  461.     deleted = 0
  462.     for line in f:
  463.         stock = line [0:25]
  464.         stock = stock.lower()
  465.         stock = stock.strip()
  466.         if product == stock:
  467.             deleted = deleted + 1
  468.             line = ""
  469.         r.write(line+'\n')
  470.     f.close()
  471.     r.close()
  472.     if deleted == 1:
  473.         print("\nProduct name '"+str(product)+"' has been deleted!")
  474.     tidyUpProduct()
  475.    
  476. def printOrderInvoice():
  477. #Allows the user to chose what order they would like to view
  478.     while True:
  479.         orderID = input("Please enter the orderID of the invoice you wish to view\n")
  480.         with open('orders.txt') as f:
  481.             if orderID in f.read():
  482.                 print("Thank you")
  483.                 break
  484.             else:
  485.                 print("The orderID entered was invalid")
  486.                 printOrderInvoice()
  487. #Locates the order and prints all the details based on line location
  488.     with open("orders.txt","r+") as f:
  489.         f.seek(0)
  490.         for line in f:
  491.             if orderID in line:
  492.                 finalPrice = line[0:7]
  493.                 date = line[7:22]
  494.                 customerID = line[22:35]
  495.                 quantity = line[35:40]
  496.                 orderID = line[40:53]
  497.                 paid = line[53:55]
  498.                 orderName = line[55:80]
  499.                 productID = line[80:88]
  500. #shows the user the invoice that has been chosen
  501.                 print("\n#########################################")
  502.                 print("#  Invoice for Order ID: " +orderID+("  #"))
  503.                 print("#  Date of order: " +date+("       #"))
  504.                 print("#  Customer ID: " +customerID+("           #"))
  505.                 print("#  Product quantity: "+quantity+("              #"))
  506.                 print("#  Paid or unpaid? (1:0): "+paid+("            #"))
  507.                 print("#  Order Name: "+orderName+("#"))
  508.                 print("#  Product ID: "+productID+("                 #"))
  509.                 print("#########################################")
  510.                 mainMenu()
  511.  
  512. def payOrder():
  513.     while True:
  514.         order_ID = input("Please enter your order ID\n ")
  515.         with open('orders.txt') as f: #opens the file
  516.             if order_ID in f.read(): #if the order ID is in the text file:
  517.                 print("Thank you")
  518.                 break
  519.     while True:
  520.         pay_file = open("paid.txt","a") #opens the paid file in append mode
  521.         orders_file = open('orders.txt', 'r') #opens the orders file in read mode
  522.         for line in orders_file: #for each line in the orders file:
  523.             if order_ID in line: #if the order ID is in the line:
  524.                 line = line.ljust(12) #gives room at the end of the line
  525.                 pay_file.write(line + "\n") #writes the line into the paid flie        
  526.         break
  527.     while True:      
  528.         with open("orders.txt","r") as f:
  529.             f.seek(0)
  530.             total = 0
  531.             for line in f:
  532.                 if order_ID in line:
  533.                     total_price = line[24:36]
  534.                     total += float(total_price)
  535.         if total>50:
  536.             total = float(total-(total*0.05))
  537.             total = str(total)
  538.             print("\nYour order total is:     ",total + " (Including 5% discount)")
  539.             break
  540.         else:
  541.             print("\nYour order total is:     ",total)
  542.             break
  543.  
  544. main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top