@ADoctor/

Barcodes Task 2

Python

Original name: FittingHuskyFolders

fork
loading
Files
  • main.py
  • BreakfastOrder.txt
  • items.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def getItems():
	itemfile = open('items.txt','r').readlines()
	for i in range(0,len(itemfile)):
		itemfile[i] = itemfile[i].strip('\n')
		itemfile[i] = itemfile[i].split(',')
	return(itemfile)

def getOrder(products):
	receipt = []
	orderComplete = False
	while orderComplete == False:
		barcode = str(input("Input the barcode of the item you wish to order, or 'X' to finish your order. ")).upper()
		if barcode == 'X':
			orderComplete = True
		else:
			foundFlag = False
			for i in products:
				if i[0] == barcode:
					foundFlag = True
					while True:
						try:
							quantity = int(input("How many of the item '{0}' would you like to order? If you do not wish to order this item, enter 0. ".format(i[1])))
							if quantity > -1:
								break
							else:
								print("Sorry, that's not a valid number. You need to order at least 1.")
						except ValueError:
							print("Sorry, that's not a valid number. You need to enter an integer.")
					if quantity != 0:
						totalPrice = str(round(float(i[2])*quantity,2))
						lengthDiff = len(totalPrice) - len(str(int(round(float(i[2])*quantity,0))))
						if lengthDiff == 0:
							totalPrice += '.00'
						elif lengthDiff == 2:
							totalPrice += '0'
						receipt.append([barcode,i[1],quantity,i[2],totalPrice])
			if foundFlag == False:
				print("Item not found.")
			print("")
	return receipt

def createReceipt(finalOrder):
	totalPrice = float(0)
	for i in finalOrder:
		totalPrice += float(i[4])
	receiptName = str(input("What do you want your receipt file to be called? Do not add the .txt at the end. "))
	receiptName += '.txt'
	finalReceipt = open(receiptName,'w+')
	for i in finalOrder:
		for j in range(0,len(i)):
			finalReceipt.write(str(i[j]))
			if j == 0:
				finalReceipt.write(' - ')
			elif j == 1:
				finalReceipt.write(' (')
			elif j == 2:
				finalReceipt.write('): £')
			elif j == 3:
				finalReceipt.write(' -> £')
			elif j == 4:
				finalReceipt.write('\n')
			else:
				finalReceipt.write(',')
	finalReceipt.write('\n')
	finalReceipt.write('Final cost: £')
	finalReceipt.write(str(totalPrice))

def main():
	items = getItems()
	while True:
		option = input("Would you like to create a new order? Y/N ").upper()
		if option == 'Y':
			order = getOrder(items)
			createReceipt(order)
		elif option == 'N':
			break
		else:
			print("Sorry, that's not a valid option. Please input 'Y' or 'N'.")
		print("")

main()