-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment 5
82 lines (75 loc) · 3.44 KB
/
Assignment 5
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
'''
Instructions
Solve the following problems and submit your solutions before the deadline.
Problems
Write the following programs in Python language:
A program which allows the user to write and read product data from a file. The program should ask the user to enter product data (name, unit price and amount) and write the data of each product to a separate line so that details are separated By semicolon(;). For the search part the application should ask the user to enter the criteria (name, unit price or amount) and search the product with the criteria equal to the given value.
A program which returns th number of products and calculates the total value of each product (amount*unit price) and the sum of total values of each product.
Improve your program for problem 2 so that you catch possible exceptions and print a user-friendly message both to the screen and to a log file.
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.
'''
from sys import exit
import json
#To execute the program successfully,firstly you have to write a product data to the database by choose the *write option.
class Product(object):
def __init__(self):
self.name = ""
self.unitprice = ""
self.amount = ""
self.product = {}
self.product1={}
def writing(self):
f = open('c:/TEMP/memo.txt', 'r+')
f.truncate()
self.product1={}
if(eval(p.unitprice)<0 or eval(p.amount)<0):
print('The amount and unitprice should be positive numbers.')
else:
self.product[p.name] = p.unitprice, p.amount
self.product1.update(self.product)
json.dump(self.product1, open('c:/TEMP/memo.txt','a'))
print(self.product1)
def reading(self):
s = open('c:/TEMP/memo.txt', 'r').read()
self.product = eval(s)
print(self.product)
name = input("Please type the product name,unit price or amount to search:\n")
for k in self.product:
if name in self.product:
print(name,self.product[name])
else:
for v in self.product[k]:
if name in self.product[k]:
print(k,self.product[k])
else:
print('This product information is not in the file.')
def calculating(self):
s = open('c:/TEMP/memo.txt', 'r').read()
self.product = eval(s)
li=[]
s=0
for k in self.product:
for v in self.product[k]:
li.append(v)
for i in range(len(li)):
if(i%2==0):
a=eval(li[i])*eval(li[i+1])
s+=a
x=len(li)/2
print('There are '+str(x)+' products in total.\n',
'The total price of the products are '+str(s)+'.')
p = Product()
while True:
print("Please type:\n\t*read to search for the data base\n\t*write to write to the data base\n\t*calc to calculate\n\t*exit to exit")
action = input("\n> ")
if "write" in action:
p.name = input("Please type a product name:\n> ")
p.unitprice = input("Please type the unit price of the product:\n> ")
p.amount = input("Please type the amount of the product:\n> ")
p.writing()
elif "read" in action:
p.reading()
elif "calc" in action:
p.calculating()
elif "exit" in action:
exit(0)