Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import operating system and the date
- import os
- from datetime import date
- def main():
- check_files()
- loginMenu()
- def check_files():
- #check to see if the files exists.
- for i in ["stock","users","orders","customers"]:
- #If it does'nt, create the missing file(s)
- if not os.path.isfile(i+".txt"):
- #Let the user know that the files are being created
- print ("Creating the " + i + " file...")
- stockID = open(i+".txt", 'w')
- #Let the user know that the file(s) have been created
- print(i + "file created!")
- def loginMenu():
- #Show the user a home screen
- print("""
- ##########################################################################
- # #
- # Guild of the Ceramic Arts #
- # Login Menu Below! #
- # #
- ##########################################################################
- """)
- #Run the verifyUser function
- verifyUser()
- def verifyUser():
- #Assign independant variables
- verifyUsername,verifyPassword = "",""
- #Allow the user to enter their username and password
- username = str(input("\nPlease enter your username: \n"))
- password = str(input("\nPlease enter your password: \n"))
- while True:
- readUsers = open("users.txt","r")
- #Read the files to verify user authentication
- lines = readUsers.readlines()
- #Tell the user if the file is empty
- if len(lines) == 0:
- print("There are no Users in the file for you to search!\n")
- #Sart the function newUser as there are no existing users
- newUser()
- continue
- verifyCounter = 0
- i = 0
- #If there are users created, verify if the username and password match
- while i < len(lines):
- location = lines[i]
- verifyUsername = location [0:10]
- verifyUsername = verifyUsername.strip()
- verifyUsername = verifyUsername.lower()
- username = username.lower()
- verifyPassword = location[10:20]
- verifyPassword = verifyPassword.strip()
- verifyPassword = verifyPassword.lower()
- password = password.lower()
- if location == "":
- readUsers.close()
- break
- #Allow the user to continue
- if verifyUsername == username and verifyPassword == password:
- verifyCounter = verifyCounter + 1
- mainMenu()
- break
- i += 1
- #If the credentials are incorrect, allow the user to re-enter their username and password
- if verifyCounter ==0:
- print("\nThere is no user in the Users file with those login details.\n")
- loginMenu()
- else:
- break
- def mainMenu():
- while True:
- #Print a menu for the user
- print("""
- ##########################################################################
- # #
- # Guild of Ceramic Arts #
- # Main Menu #
- # #
- # What would you like to do? #
- # #
- # 1 - Add new User #
- # 2 - Add new customer #
- # 3 - Add new product #
- # 4 - Delete product #
- # 5 - Add new order #
- # 6 - Review an Invoice
- # 7 - Review an unpayed order
- # #
- # 7 - Logout #
- # 8 - Exit #
- # #
- ##########################################################################
- """)
- #Allow the user to choose an option
- mainMenu=input("What would like to do? Option '1', '2', '3', '4', '5' , '6' or '7'? ")
- if mainMenu =="1":
- newUser()
- break
- elif mainMenu =="2":
- newCustomer()
- break
- elif mainMenu =="3":
- newProduct()
- break
- elif mainMenu =="4":
- removeProduct()
- break
- elif mainMenu =="5":
- newOrder()
- break
- elif mainMenu =="6":
- printOrderInvoice()
- break
- elif mainMenu =="7":
- quit()
- elif mainMenu =="8":
- #If the user enters an invalid option, tell the
- else:
- print("\nThat is not a valid entry, you must select '1', '2', '3', '4', '5' , '6' or '7'")
- def newUser():
- #Assign independant variables
- userName, password1, password2 = "", "", ""
- while True:
- #Allow the user to enter a new username
- userName=input("\nInsert a Username for the new user (input '-1' to cancel and exit): ")
- #Allow the user to leave the newUser function if they wish to
- if userName =="-1":
- mainMenu()
- #Make sure the username is not over 10 characters long
- if len(userName)>10:
- print("\nUsername has to be between 1 and 10 characters!")
- #Make sure the username only uses letters
- elif userName.isalpha()==False:
- print("\nYou need to use letters only!")
- else:
- break
- while True:
- password1=input("\nInput a password for the user (input '-1' to cancel and exit): ")
- #Allow the user to leave the newUser function if they wish to
- if password1 =="-1":
- mainMenu()
- #Make sure the password is not over 10 characters long
- if len(password1)>10:
- print("\nPassword has to be between 1 and 10 charcters!")
- #Allow the user to re-enter their password
- password2=input("\nInput the password again (input '-1' to cancel and exit): ")
- #Allow the user to leave the newUser function if they wish to
- if password2 =="-1":
- mainMenu()
- #Make sure the second entry of the password is not over 10 characters long
- if len(password2)>10:
- print("\nPassword has to be between 1 and 10 charcters!")
- #Ensure that both passwords match before creating a user
- if password1 != password2:
- print("\nThe passwords did not match!")
- else:
- break
- #Change the username and password into lower case characters if possible
- userName=userName.lower()
- password1=password1.lower()
- #Ensure that the username and password take up exactly ten spaces
- userNameStore=userName.ljust(10)
- password1Store=password1.ljust(10)
- #Write the user credentials in the users text file
- store=open("users.txt","a")
- iStore=userNameStore+password1Store+"\n"
- store.write(iStore)
- store.close()
- #Let the user know that a new user has been
- print("\n" + userName + " has been created!")
- print("Username :",userName)
- print("Password : ********** ")
- mainMenu()
- def newOrder():
- while True:
- #Allow the user to insert a customer ID
- customerID = input("Please enter your customer ID: ")
- with open('customers.txt') as f:
- #Verify if the user exists
- if customerID in f.read():
- #Let the user know if the ID is valid or not
- print("\nThe customer ID is valid")
- break
- else:
- print("\nThat Customer ID does not exist. Please try again")
- #Ask the user to input their order ID
- while True:
- orderID = input("Please enter a unique ID for your order (5 numbers): ")
- if orderID.isdigit()==False:
- print("\nYou can only use numbers for your unique ID")
- elif len(orderID) != 5:
- print("\nYou must use exactly 5 digits")
- #Create a unique order ID for the customer
- else:
- print("Your unique ID is OR" + orderID)
- break
- #Asks the user to input what product the customer would like to buy and ensures there are no errors
- while True:
- item_name = input("\nPlease type the name of the item the customer would like to purchase: ")
- with open('stock.txt') as f:
- if item_name in f.read():
- #Tells the reader that the item has been added
- print("\nItem Added")
- break
- #Lets the user know that they've spelt the item incorrectly
- elif item_name not in f.read():
- print("\nItem does not exist. Make sure it is spelt correctly!")
- while True:
- #calculates the price of a single item
- readStock = open('stock.txt','r')
- location = readStock.readline()
- price = location[33:50]
- price = float(price)
- #Allows the user to add more than one of the same item
- quantity = int(input("\nHow many of the singular product would the customer like to purchase?: "))
- if quantity >100:
- print("\nThe customer cannot buy more than 100 items")
- break
- elif quantity <1:
- ("\nThe customer must buy a minimum of 1 item!")
- #Calculates the final price
- finalPrice = (price*quantity)
- break
- #If the price is over £50, adds a 5% discount
- if finalPrice >= 50:
- finalPrice *= 0.95
- readStock.close()
- break
- #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
- orderID = ("OR")+ orderID
- orderID=orderID.strip()
- orderID=orderID.lower()
- orderIDStore=orderID.ljust(13)
- customerID=customerID.strip()
- customerID=customerID.lower()
- customerIDStore=customerID.ljust(13)
- finalPrice=str(finalPrice)
- finalPrice=finalPrice.ljust(7)
- today = date.today()
- today = str(today)
- today = today.ljust(15)
- quantity = str(quantity)
- quantityStore = quantity.ljust(5)
- paid = " 0 "
- #Stores the above into the orders file
- order_file = open('orders.txt', 'a')
- stock_file = open('stock.txt', 'r')
- for line in stock_file:
- if item_name in line:
- line = line.ljust(12)
- order_store = finalPrice + today + customerIDStore + quantityStore + orderIDStore + paid + line
- order_file.write(order_store + "\n")
- #Shows the user that the chosen items hae been succesfully added
- print("\nThe chosen item(s) have been added to the order")
- order_file.close()
- stock_file.close()
- mainMenu()
- def unpaidOrders():#checks if an order has been payed or not
- while True:
- order_ID = input("Please enter your order ID\n ")
- with open('orders.txt') as f: #opens the file
- if order_ID in f.read(): #if the orderID is in the file:
- print("Thank you")
- break
- today = datetime.datetime.now() #detecting Current Date
- with open('orders.txt') as orders:
- expired = []
- dates = [[datetime.datetime.strptime(i[36:62].strip(), '%Y-%m-%d %H:%M:%S.%f'),i[12:24]] for i in orders.readlines()]
- for i in dates: #check and print expired invoices
- if today > i[0] + datetime.timedelta(28): #checks if the date is later than the date saved in the file
- expired.append(i)
- if len (expired) >0:
- 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
- else:
- print("There are currently no late orders")
- mainMenu()
- def payOrder():
- while True:
- order_ID = input("Please enter your order ID\n ")
- with open('orders.txt') as f: #opens the file
- if order_ID in f.read(): #Checks if the order is in the file
- print("Thank you")
- break
- while True:
- pay_file = open("paid.txt","a")
- orders_file = open('orders.txt', 'r')
- for line in orders_file:
- if order_ID in line:
- line = line.ljust(12) #gives room at the end of the line
- pay_file.write(line + "\n") #writes the line into the paid flie
- break
- while True:
- with open("orders.txt","r") as f:
- f.seek(0)
- total = 0
- for line in f:
- if order_ID in line:
- total_price = line[24:36]
- total += float(total_price)
- if total>50:
- total = float(total-(total*0.05))
- total = str(total)
- print("\nYour order total is: ",total + " (Including 5% discount)")
- break
- else:
- print("\nYour order total is: ",total)
- break
- def newCustomer():
- customerID,customerForeName,customerLastName,customerTown,customerHouseNumber,customerPostcode,customerPhoneNumberNumber ="","","","","","",""
- while True:
- #Allows the user to input and save the customers forename, surname, town, building number, postcode and phone number
- customerForeName=input("\nInput the Customers Forename: ")
- if len(customerForeName)>20:
- print("\nThe Customers Forename must be between 1 and 20 characters")
- elif customerForeName.isalpha()==False:
- print("\nYou can only use letters for the customers Forename.")
- else:
- break
- while True:
- customerLastName=input("\nInput the customers Surname: ")
- if len(customerLastName)>20:
- print("\nCustomer Surname has to be between 1 and 20 characters")
- elif customerLastName.isalpha()==False:
- print("\nYou must only use letters for the customers Surname")
- else:
- break
- while True:
- customerTown=input("\nInput the customers Town: ")
- if len(customerTown)>20:
- print("\nThe Customers Town name must be between 1 and 20 characters")
- else:
- break
- while True:
- customerHouseNumber=input("\nInput the Customers Building Number: ")
- if len(customerHouseNumber)>5:
- print("\nThe Customers Building Number must be less than 5 characters")
- else:
- break
- while True:
- customerPostcode=input("\nInput the Customers Postcode: ")
- if len(customerPostcode)>8:
- print("\nThe customers Postcode must be less than 8 characters")
- else:
- break
- while True:
- customerPhoneNumber=input("\nInput the customers Phone Number: ")
- if len(customerPhoneNumber)>11:
- print("\nThe Customer Phone Number must be less than 11 numbers")
- elif customerPhoneNumber.isdigit()!=True:
- print("\nYou can only use numbers for the customers Phone Number")
- else:
- break
- #Stores the above in the customers file
- customerHouseNumber=customerHouseNumber.strip()
- customerPostcode=customerPostcode.replace(" ", "")
- customerID=(customerHouseNumber)+(customerPostcode)
- customerID=customerID.strip()
- customerID=customerID.lower()
- customerIDStore=customerID.ljust(12)
- customerForeNameStore=customerForeName.ljust(20)
- customerLastNameStore=customerLastName.ljust(20)
- customerTownStore=customerTown.ljust(20)
- customerHouseNumberStore=customerHouseNumber.ljust(5)
- customerPostcodeStore=customerPostcode.ljust(8)
- customerPhoneNumberStore=customerPhoneNumber.ljust(11)
- store=open("customers.txt","a")
- iStore=customerIDStore+customerForeNameStore+customerLastNameStore+customerTownStore+customerHouseNumberStore+customerPostcodeStore+customerPhoneNumberStore+"\n"
- store.write(iStore)
- store.close()
- #Lets the user know that the customer has been stored succesfully
- print("\nA new customer has been succesfully created!")
- print("Customer ID: ",customerIDStore)
- print("Customer Forename: ",customerForeNameStore)
- print("Customer Surname: ",customerLastNameStore)
- print("Customer Town: ",customerTownStore)
- print("Customer House Number: ",customerHouseNumberStore)
- print("Customer Posecode: ",customerPostcodeStore)
- print("Customer Phone Number: ",customerPhoneNumberStore)
- mainMenu()
- def newProduct():
- productID,productPrice,productName = "","",""
- #Allows the user to create a product name and a product ID
- while True:
- productName = input("\nInput the product name you would like to add: ")
- productName.strip()
- productName.lower()
- if len(productName)>25:
- print("\nProduct name must be between 1 and 25 characters! Try Again! ")
- else:
- break
- while True:
- productID = input("\nInput the product ID of the product you want to add (5 Numbers): ")
- productID.strip()
- if len(productID)>5:
- print("\nThe product ID, '"+str(productID)+"' exceeds the allowed amount of digits")
- elif productID.isdigit()!=True:
- print("\nYou can only use numbers for the product ID!")
- else:
- break
- productPrice=float(input("\nInput the price of the your product(numbers only!): "))
- productPrice=str(productPrice)
- productID = "PT"+productID
- productID.strip()
- productID.lower()
- productIDStore = productID.ljust(8)
- productName.strip()
- productName.lower()
- productNameStore = productName.ljust(25)
- #Stores the new product in the stock file
- store= open("stock.txt","a")
- istore= productNameStore + productIDStore + productPrice+ "\n"
- store.write(istore)
- store.close()
- #tells the user that the new product has been added and repeats the details
- print("\nFinished adding the new product")
- print("Product ID: ",productIDStore)
- print("Product Name: ",productNameStore)
- print("Product Price: ",productPrice)
- mainMenu()
- def removeProduct():
- #Asks the user what product they would like to delete, locates the product and deletes it
- product = input("What product would you like to delete (Insert Name)? ")
- f = open("stock.txt","r+")
- r = open("stock.txt","w")
- deleted = 0
- for line in f:
- stock = line [0:25]
- stock = stock.lower()
- stock = stock.strip()
- if product == stock:
- deleted = deleted + 1
- line = ""
- r.write(line+'\n')
- f.close()
- r.close()
- if deleted == 1:
- print("\nProduct name '"+str(product)+"' has been deleted!")
- tidyUpProduct()
- def printOrderInvoice():
- #Allows the user to chose what order they would like to view
- while True:
- orderID = input("Please enter the orderID of the invoice you wish to view\n")
- with open('orders.txt') as f:
- if orderID in f.read():
- print("Thank you")
- break
- else:
- print("The orderID entered was invalid")
- printOrderInvoice()
- #Locates the order and prints all the details based on line location
- with open("orders.txt","r+") as f:
- f.seek(0)
- for line in f:
- if orderID in line:
- finalPrice = line[0:7]
- date = line[7:22]
- customerID = line[22:35]
- quantity = line[35:40]
- orderID = line[40:53]
- paid = line[53:55]
- orderName = line[55:80]
- productID = line[80:88]
- #shows the user the invoice that has been chosen
- print("\n#########################################")
- print("# Invoice for Order ID: " +orderID+(" #"))
- print("# Date of order: " +date+(" #"))
- print("# Customer ID: " +customerID+(" #"))
- print("# Product quantity: "+quantity+(" #"))
- print("# Paid or unpaid? (1:0): "+paid+(" #"))
- print("# Order Name: "+orderName+("#"))
- print("# Product ID: "+productID+(" #"))
- print("#########################################")
- mainMenu()
- def payOrder():
- while True:
- order_ID = input("Please enter your order ID\n ")
- with open('orders.txt') as f: #opens the file
- if order_ID in f.read(): #if the order ID is in the text file:
- print("Thank you")
- break
- while True:
- pay_file = open("paid.txt","a") #opens the paid file in append mode
- orders_file = open('orders.txt', 'r') #opens the orders file in read mode
- for line in orders_file: #for each line in the orders file:
- if order_ID in line: #if the order ID is in the line:
- line = line.ljust(12) #gives room at the end of the line
- pay_file.write(line + "\n") #writes the line into the paid flie
- break
- while True:
- with open("orders.txt","r") as f:
- f.seek(0)
- total = 0
- for line in f:
- if order_ID in line:
- total_price = line[24:36]
- total += float(total_price)
- if total>50:
- total = float(total-(total*0.05))
- total = str(total)
- print("\nYour order total is: ",total + " (Including 5% discount)")
- break
- else:
- print("\nYour order total is: ",total)
- break
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement