-
Notifications
You must be signed in to change notification settings - Fork 3
/
Assignment5_3.py
119 lines (101 loc) · 3.97 KB
/
Assignment5_3.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
'''
Created on 19.5.2015
Improve your program for problem 1 so that if negative numbers are given as the amount
or unit price the program will raise an Arithmetic exception.
@author: e1201757
'''
import os
DB_PATH = 'product_db.txt'
def writeToFile(content, filePath):
file = open(filePath, 'a+')
file.write(content + '\n')
file.close()
def readFromFile(filePath):
if os.path.exists(filePath):
file = open(filePath, 'r')
content = file.read().split('\n')
content.pop()
productsList = []
for product in content:
productDict = {}
details = product.split(';')
productDict['name'] = details[0]
productDict['unit_price'] = details[1]
productDict['amount'] = details[2]
productsList.append(productDict)
file.close()
return productsList
return []
def inputProductsFromConsole():
text = ''
while True:
print('Please enter product data (name;unit price;amount), and use \';\' to separate(enter \'q\' to quit).')
print('For example: Apple;2;100')
text = input('New product:').replace(' ', '')
if validateProduct(text):
writeToFile(text, DB_PATH)
input('New data have been saved. Enter \'ENTER\' to continue.')
print('---------------------------------------')
elif text == 'q':
print('---------------------------------------')
break
else:
print('Wrong Format!')
input('Enter \'ENTER\' to continue.')
print('---------------------------------------')
def validateProduct(text):
details = text.split(';')
if len(details) == 3:
try:
if eval(details[1]) < 0 or eval(details[2]) < 0:
raise ArithmeticError
except ArithmeticError:
print ('Error: negative numbers given as the amount or unit price are not allowed!')
else:
return True
return False
def searchProducts(criteria, value):
productsList = readFromFile(DB_PATH)
correctProducts = []
print('Search Result(s):')
for productDict in productsList:
if productDict[criteria] == value:
correctProducts.append(productDict)
if correctProducts != []:
for productDict in correctProducts:
printProduct(productDict)
else: print('No Product has been found!\n')
input('Enter \'ENTER\' to continue.')
print('---------------------------------------')
def showAllProducts(filePath):
productsList = readFromFile(filePath)
if productsList!=[]:
for productDict in productsList:
printProduct(productDict)
else: print('No product has been found!\n')
input('Enter \'ENTER\' to continue.')
print('---------------------------------------')
def printProduct(productDict):
print('name: ' + productDict['name'])
print('unit price: ' + productDict['unit_price'])
print('amount: ' + productDict['amount'])
print('---------------------------------------')
if __name__ == '__main__':
while True:
print('******Products Information System******\n\n1. Enter new product data.\n2. Search by criteria in products database \n3. Show all products infomation.\n4. Quit.\n')
choice = input('Please select:')
print('---------------------------------------')
if choice == '1':
inputProductsFromConsole()
elif choice == '2':
criteria = input('Enter the criteria to search(name/unit_price/amount):')
value = input('Enter the value:')
searchProducts(criteria, value)
elif choice == '3':
showAllProducts(DB_PATH)
elif choice == '4':
print('Bye bye!')
break
else:
print('Error input! Please enter a number from 1 to 4!\n')
print('---------------------------------------')