Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import string, datetime
- def check_files(): #checks whether the files are there, if they're not, it creates one
- try:
- stockID = open('stock.txt', 'r') #searches for / opens the file
- except FileNotFoundError: #if the file is not found:
- print("Creating stock file...")
- stockID = open('stock.txt', 'w') #make the file
- print ("Stock file created")
- try:
- ordersID = open('orders.txt', 'r')
- except FileNotFoundError:
- print("Creating orders file...")
- ordersID = open('orders.txt', 'w')
- print ("Orders file created")
- try:
- customerID = open('customers.txt', 'r')
- except FileNotFoundError:
- customerID = open('customers.txt', 'w')
- print ("Customers file created")
- try:
- pay = open('paid.txt', 'r')
- except FileNotFoundError:
- pay = open('paid.txt', 'w')
- print ("Unpaid orders file created")
- def mainMenu():
- print("""
- ###############################################################################
- # #
- # Urdd y Clefyddydau Cerameg #
- # Please Login Below! #
- # #
- ###############################################################################
- """)
- validateUser()
- def adminMenu():
- while True:
- print("""
- ###############################################################################
- # #
- # Urdd y Clefyddydau Cerameg #
- # Admin Menu #
- # #
- # 1 - New Customer #
- # 2 - Add to the stock #
- # 3 - Remove item from stock #
- # 4 - Make an order #
- # 5 - Print an invoice #
- # 6 - Cancel an order #
- # 7 - Delete cutsomer #
- # 8 - Unpaid orders #
- # 9 - Pay for an order #
- # #
- # #
- # 10 - Exit #
- # #
- # #
- ###############################################################################
- """)
- adminMenu = input("Which option would you like to choose?:\n ")
- if adminMenu == "1":
- newCustomer() #create a customer function
- break
- elif adminMenu == "2":
- addStock() #adding stock function
- break
- elif adminMenu == "3":
- delStock()#removing stock function
- break
- elif adminMenu == "4":
- orderMenu()#making an order function
- break
- elif adminMenu == "5":
- createInvoice()#producing an invoice function
- break
- elif adminMenu == "6":
- delOrder()#canceling an order function
- break
- elif adminMenu == "7":
- delCustomer()#deleting a customer function
- break
- elif adminMenu == "8":
- check_unpaid_orders()#check for unpaid orders function
- break
- elif adminMenu =="9":
- payOrder()#pay for order function
- break
- elif adminMenu == "10":
- exit() #exit the program
- def validateUser():
- validateUserName = ""
- validatePassword = ""
- UserName = str(input("\nPlease enter your UserName: \n"))
- password = str(input("\nPlease enter your password: \n"))
- while True:
- try:
- readUsers = open("users.txt","r")#opens the file
- validateCounter = 0 #to match both username and password
- while True:
- location = readUsers.readline() #reads the file
- validateUserName = location [0:10] #where the string is located in the file
- validateUserName = validateUserName.strip()#removes any spaces
- validateUserName = validateUserName.lower()
- UserName = UserName.lower() #lowercase
- validatePassword = location[10:20]
- validatePassword = validatePassword.strip()
- validatePassword = validatePassword.lower()
- password = password.lower()
- if location == "":
- readUsers.close()#closes the file
- break
- if validateUserName == UserName and validatePassword == password: #if everything matches up:
- validateCounter = validateCounter + 1
- adminMenu() #sends user through to the admin menu
- break
- if validateUserName == UserName and validatePassword == password:
- validateCounter = validateCounter + 1
- userMenu()
- break
- if validateCounter ==0: #if one of the things didn't match up:
- print("\nNo user in the file with those login details. \n")
- create_user_q = input ("Would you like to create a new user? (Y/N)\n ")
- if create_user_q == ("Y"):
- newUser() #sends you to the create a user function
- if create_user_q == ("N"):
- mainMenu()#sends the user back to the main menu
- except FileNotFoundError: #if there is no file to be found:
- print("There is no Users file for you to search! How about making your first account?")
- newUser()#sends user to the create a user function
- def newUser():
- UserName, password1, password2 = "", "", ""
- while True:
- UserName = input("\nPlease enter the UserName of the new user:\n ")
- if len(UserName) >10: #if the length of the username is more than 10:
- print("\nUserName must be less than 11 characters.\n")
- elif UserName.isalpha() == False: #if the username contains any numbers:
- print("\nThe UserName must only contain alpha.\n")
- else:
- break
- while True:
- password1 = input("\nPlease enter the password for the user:\n ")
- if len(password1) > 10:#if the length of the password is more than 10:
- print("\nPassword must be less than 11 characters.\n")
- password2 = input("\nPlease enter the password again:n\n ")
- if len(password2) > 10:#if the length of the second password is more than 10:
- print("\nPassword must be less than 11 characters.\n")
- if password1 != password2: #if the first password doesn't match the second:
- print("\nPasswords do not match.\n")
- else:
- break #breaks the loop
- UserName=UserName.lower() #lowercase
- password1=password1.lower()#lowercase
- UserNameStore=UserName.ljust(10)#makes the username 10 digits long in the text file
- password1Store=password1.ljust(10)#makes the password 10 digits long in the text file
- store=open("users.txt","a") #opens the file
- iStore=UserNameStore+password1Store+"\n" #what's being stored in the text file
- store.write(iStore) #writes it into the text file
- store.close()#closes the file
- print("\nCreated a New User")
- print("UserName :",UserName)
- print("Password : ********** ")
- mainMenu() #goes back to the login menu
- def orderMenu():
- while True:
- print("""
- ################################################################################
- # #
- # Urdd y Clefyddydau Cerameg #
- # #
- # Create a new order: #
- # 1 - New Customer #
- # 2 - Existing Customer #
- # #
- # 3 - Exit #
- # #
- ################################################################################
- """)
- orderMenu=input("What would like to do? Option '1', '2' or '3':\n ")
- if orderMenu =="1":
- newCustomer() #create a customer function
- break
- if orderMenu =="2":
- createOrder()#the create an order function
- break
- elif orderMenu =="3":
- exit()#exits the program
- else:
- print("\nThat is not a valid entry, you must select '1', '2' or '3'")
- def newCustomer():
- customerID=""
- customerForename=""
- customerSurname=""
- customerTown=""
- customerHouseNumber=""
- customerPostcode=""
- customerPhone=""
- while True:
- customerForename=input("\nInput the Customer Forename:\n ")
- if len(customerForename)>20: #if the length of the customer forename is more than 20:
- print("\nCustomer Forename has to be between 1 and 20 characters")
- elif customerForename.isalpha()==False:#if the customer forename contains any numbers:
- print("\nYou can only use letters for a Customer Forename")
- else:
- break
- while True:
- customerSurname=input("\nInput the Customer Surname:\n ")
- if len(customerSurname)>20:
- print("\nCustomer Surname has to be between 1 and 20 characters")
- elif customerSurname.isalpha()==False:
- print("\nYou can only use letters for a Customer Surname")
- else:
- break
- while True:
- customerTown=input("\nInput the Customer Town:\n ")
- if len(customerTown)>20:
- print("\nCustomer Town has to be between 1 and 20 characters")
- elif customerTown.isalpha()==False:
- print("\nYou can only use letters for a Customer Town")
- else:
- break
- while True:
- customerHouseNumber=input("\nInput the Customer House Number:\n ")
- if len(customerHouseNumber)>5:
- print("\nCustomer House Number cannot be >5 characters")
- else:
- break
- while True:
- customerPostcode=input("\nInput the Customer Postcode:\n ")
- if len(customerPostcode)>8:
- print("\nCustomer Postcode cannot be >8 characters i.e. XX99 9XX")
- else:
- break
- while True:
- customerPhone=input("\nInput the Customer Phone Number:\n ")
- if len(customerPhone)>11:
- print("\nCustomer Phone Number can not be more than 11 numbers")
- elif customerPhone.isdigit()!=True:
- print("\nYou can only use numbers for a Customer Phone Number")
- else:
- break
- customerHouseNumber=customerHouseNumber.strip() #gets rid of spaces
- customerPostcode=customerPostcode.replace(" ", "")#removes the spaces in between
- customerID=(customerHouseNumber)+(customerPostcode) #creates the customer ID
- customerID=customerID.strip()#gets rid of spaces
- customerID=customerID.lower()#makes it lowercase
- customerIDStore=customerID.ljust(12)#makes it 12 digits long in the text file when storing it
- customerForenameStore=customerForename.ljust(12)
- customerSurnameStore=customerSurname.ljust(12)
- customerTownStore=customerTown.ljust(20)
- customerHouseNumberStore=customerHouseNumber.ljust(12)
- customerPostcodeStore=customerPostcode.ljust(12)
- customerPhoneStore=customerPhone.ljust(12)
- store=open("customers.txt","a") #opens the file
- iStore=customerIDStore+customerForenameStore+customerSurnameStore+customerTownStore+customerHouseNumberStore+customerPostcodeStore+customerPhoneStore+"\n" #everything that's being stored
- store.write(iStore) #writes it into the file
- store.close()#closes the file
- print("\nFinished creating a new customer")
- print("Customer ID: ",customerIDStore)
- print("Customer Forename: ",customerForenameStore)
- print("Customer Surname: ",customerSurnameStore)
- print("Customer Town: ",customerTownStore)
- print("Customer House Number: ",customerHouseNumberStore)
- print("Customer Posecode: ",customerPostcodeStore)
- print("Customer Phone: ",customerPhoneStore)
- def addStock():
- StockID = ""
- StockName = ""
- StockPrice = ""
- while True:
- StockName=input("\nInput the name of the item you are adding to the stock:\n ")
- if len(StockName)>20: #if the length is more than 20:
- print("\nStockName has to be between 1 and 20 characters")
- elif StockName.isalpha()==False: #if it contains any numbers:
- print("\nYou can only use letters for a product name")
- else:
- break
- while True:
- StockPrice=float(input("\nInput the price of your product:\n "))
- if StockPrice>150: #if the price is more than £150:
- print("\nPrice must be under £150!")
- elif str(StockPrice).isdigit()!=False: #if it contains any letters:
- print("\nYou can only use numbers for a price")
- else:
- break
- StockID=input("Please enter 5 random letters so we can give you a unique ID:\n ")
- if StockID.isalpha()==False: #if it contains numbers
- print("\nYou can only use letters for your unique ID")
- elif len(StockID) != 5: #if it is more or less than 5 digits
- print("\nYou must use exaclty 5 digits ")
- StockID = ("QF")+ StockID
- StockID=StockID.strip()#removes spaces
- StockID=StockID.lower()#lowercase
- StockPrice = str(StockPrice) #makes it writable
- StockIDStore=StockID.ljust(12) #makes it 12 digits long in the text file
- StockNameStore=StockName.ljust(12)
- StockPriceStore=StockPrice.ljust(12)
- store=open("stock.txt","a") #opens the file
- iStore=StockIDStore+StockNameStore+StockPriceStore+"\n" #everything that's being stored in the file
- store.write(iStore) #writes it into the file
- store.close() #closes the file
- print("\nFinished adding to stock")
- print("Stock ID: ",StockIDStore)
- print("Stock Name: ",StockNameStore)
- print("Stock Item Price: ",StockPriceStore)
- while True:
- print("""
- ################################################################################
- # #
- # Urdd yr Clefyddydau Cerameg #
- # #
- # Would you like to add another item to stock?: #
- # 1 - Add another item #
- # #
- # 2 - Exit to Menu #
- # #
- ################################################################################
- """)
- StockMenu=input("What would like to do? Option '1' or '2':\n ")
- if StockMenu =="1":
- addStock() #repeates th entire process
- break
- elif StockMenu =="2":
- adminMenu() #returns to admin menu
- break
- else:
- print("\nThat is not a valid entry, you must select '1' or '2'")
- def delStock():
- while True:
- del_item = input("Please enter the name of the item you would like to remove from the stock\n ")
- with open("stock.txt","r+") as f:
- new_f = f.readlines() #reads the file
- f.seek(0)
- for line in new_f: #for every line in the file:
- if del_item not in line: #if the item isn't in the line:
- f.write(line)#writes the entire file bcak without the deleted line
- f.truncate()
- a = input("Would you like to remove another item? (Y/N)\n ")
- if a == ("n"):
- break
- elif a == ("y"):
- print("Thank you")
- def createOrder():
- while True:
- customerID = input("Please enter your customer ID (House number + Postcode - Example - 6ab123cd)\n ")
- with open('customers.txt') as f: #opens the file
- if customerID in f.read(): #if the customer ID is in the file:
- print("Thank you")
- break
- else:
- print("Customer ID was not found. Please try again")
- while True:
- orderID = input("Please enter a unique ID for your order (5 random numbers)\n ") #to make the order ID
- if orderID.isdigit()==False: #if it contains letters:
- print("\nYou can only use numbers for your unique ID")
- elif len(orderID) != 5: #if it's more or less than 5 digits long:
- print("\nYou must use exaclty 5 digits ")
- else:
- print("Your unique ID is PQ" + orderID)
- break
- orderID = ("PQ")+ orderID
- orderID=orderID.strip()#removes any spaces
- orderID=orderID.lower()#lowercase
- orderIDStore=orderID.ljust(12) #makes it 12 digits long in the text file
- while True:
- item_name = input("Please enter the name of the item you wish to purchase\n ")
- with open('stock.txt') as f: #opens the file
- if item_name in f.read(): #if it's in the file:
- print("Thank you")
- break
- elif item_name not in f.read(): #if it isn't:
- print("Item was not found. Please try again")
- while True:
- readStock = open('stock.txt','r') #opens the file
- location = readStock.readline()#reads the file
- price = location[24:36]#location of where the string is in the line #makes the price a float (can have decimals)
- price = float(price)
- quantity = int(input("What quantity of this product would you like to purchase?\n "))
- if quantity >100: #if the quantity is more than 100:
- print("You can only have up to 100 items")
- elif quantity <1: #if it's less than 1:
- ("You must give a quantity of 1 or more")
- overallPrice = (price*quantity) #gives the price of each item combined
- readStock.close() #closes the file
- break
- quantityID=str(quantity) #makes the quantity a string
- quantityID=quantityID.ljust(12) #makes it 12 digits long in the text file
- customerID=customerID.strip()#strips any spaces
- customerID=customerID.lower()#lowercase
- customerIDStore=customerID.ljust(12)
- overallPrice=str(overallPrice)#makes the price a string
- overallPrice=overallPrice.ljust(12)
- today = datetime.datetime.now() #the date
- today = str(today)#makes the date a string (so it can be stored in the file)
- today = today.ljust(30) #makes it 12 digits long
- order_file = open('orders.txt', 'a') #opens the order file in append mode
- stock_file = open('stock.txt', 'r') #opens the stock file in read mode
- for line in stock_file: #for each line in the stock file
- if item_name in line: #for each line:
- line = line.ljust(12) #makes sure that there's a gap at the end of the line
- order_store = quantityID + orderIDStore + overallPrice + today + customerIDStore + line #everything that's being stored
- order_file.write(order_store) #writes it into the file
- print("Thank you, your item has been added to your order")
- while True:
- a = input("Would you like to add another item to the order? (y/n)\n ")
- if a == ("n"):
- break
- elif a == ("y"):
- print("Thank you ")
- createOrder() #makes another order
- else:
- print("Invalid input, returning to menu ")
- break
- while True:
- a = input("Would you like to pay for your order now? (y/n)\n ")
- if a == ("y"):
- pay_file = open("paid.txt","a") #opens the paid file
- stock_file = open('stock.txt', 'r')#opens the stock file
- for line in stock_file: #for every line in the stock file:
- if item_name in line: #if the item name is in the line:
- line = line.ljust(12)
- order_store = quantityID + orderIDStore + overallPrice + today + customerIDStore + line + "\n"
- pay_file.write(order_store) #writes the line into the pay file
- print("Paid")
- break
- elif a == ("n"):
- print("Thank you ")
- break
- else:
- print("Invalid input, returning to menu ")
- break
- def delOrder():
- while True:
- z = input("Are you sure you want to cancel an order? (You must pay 20% of the order)")
- if z == "y":
- break
- if z == "n":
- adminMenu()
- break
- else:
- print("Invalid input, returning to menu")
- adminMenu()
- 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 file:
- print("Thank you")
- break
- else:
- print("Order ID was not found. Please try again")
- while True:
- with open("orders.txt","r") as f: #opens the file
- f.seek(0) #reads the file
- total = 0
- for line in f: #for every line in the file:
- if order_ID in line: #if the order ID is in the line:
- total_price = line[24:36] #location of the total
- total += float(total_price) #produces the total
- if total>50: #if it's more than 50:
- total = float(total - (total*0.05)) #discount
- total = int(total) #changes it into an integer
- print("You will be charged £",total*0.2) #20% of the total
- with open("orders.txt","r+") as f:
- new_f = f.readlines() #reads the file
- f.seek(0)
- for line in new_f: #for every line in the file:
- if order_ID not in line: #if the order ID isn't in the line:
- f.write(line) #writes the line back in
- print("Order deleted ")
- f.truncate()
- if total>50: #if the total is more than 50
- total = float(total - (total*0.05))
- total = str(total) #turns it into a string (so it can be stored)
- a = input("Would you like to cancel another order? (Y/N)\n ")
- if a == ("n"):
- break
- elif a == ("y"):
- delOrder()
- def delCustomer():
- while True:
- del_item = input("Please enter customer ID\n ")
- with open("customers.txt","r+") as f:
- new_f = f.readlines() #reads the file
- f.seek(0)
- for line in new_f: #for every line in the file:
- if del_item not in line: #if it's not in the line
- f.write(line)#write the line back in
- print("Customer deleted")
- f.truncate()
- a = input("Would you like to remove another customer? (Y/N)\n ")
- if a == ("n"):
- break
- elif a == ("y"):
- print("Thank you")
- def createInvoice():
- while True:
- order_ID = input("Please enter your order ID\n ")
- with open('orders.txt') as f:
- if order_ID in f.read():
- print("Thank you")
- break
- else:
- print("Order ID was not found. Please try again")
- with open("orders.txt","r+") as f:
- f.seek(0)
- for line in f:
- if order_ID in line:
- quantity = line[0:12]
- orderID = line[12:24]
- ovr_price = line[24:36]
- date = line[36:48]
- customerID = line[48:60]
- stockID = line[60:72]
- stockName = line[72:84]
- itemPrice = line[84:96]
- print("\nInvoice for Order ID: " +orderID)
- print("Date of order: " + date)
- print("Customer ID: " + customerID)
- print("Stock ID: " + stockID)
- print("Item name: " + stockName)
- print("Item price: " + itemPrice)
- print("Stock Quantity: " + quantity)
- ovr_price = float(ovr_price)
- ovr_price = ovr_price + 5.99
- ovr_price = str(ovr_price)
- print("Shipping: £5.99")
- print("Total: " + ovr_price)
- 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 check_unpaid_orders(): #begins the late orders function
- 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 + print expired invoices
- if today > i[0] + datetime.timedelta(28): #checks if the current date is larger 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(): #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
- def main():
- check_files()
- mainMenu()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement