Repository navigation
Expand file tree
/
Copy pathAssignment08.py
More file actions
223 lines (186 loc) · 7.72 KB
/
Copy pathAssignment08.py
File metadata and controls
223 lines (186 loc) · 7.72 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# ------------------------------------------------------------------------ #
# Title: Assignment 08
# Description: Working with classes
# ChangeLog (Who,When,What):
# RRoot,1.1.2030,Created started script
# RRoot,1.1.2030,Added pseudo-code to start assignment 8
# Tao Ye,6.2.2020,Modified code to complete assignment 8
# Tao Ye,6.3.2020,revision
# ------------------------------------------------------------------------ #
# Data -------------------------------------------------------------------- #
strFileName = 'products.txt'
lstOfProductObjects = []
strChoice = ""
class Product:
"""Stores data about a product:
properties:
product_name: (string) with the products's name
product_price: (float) with the products's standard price
methods:
changelog: (When,Who,What)
RRoot,1.1.2030,Created Class
Tao Ye,6.2.2020,Modified code to complete assignment 8
"""
pass
# Constructor -----------------
def __init__(self):
self.__product_name = ""
self.__product_price = 0.0
# Properties ------------------
# product_name
@property # (getter or accessor)
def product_name(self):
return str(self.__product_name)
@product_name.setter # (setter or mutator)
def product_name(self, name):
self.__product_name = name
# product_price
@property # (getter or accessor)
def product_price(self):
return self.__product_price
@product_price.setter # (setter or mutator)
def product_price(self, value):
self.__product_price = value
# Data -------------------------------------------------------------------- #
# Processing ------------------------------------------------------------- #
class FileProcessor:
"""Processes data to and from a file and a list of product objects:
methods:
save_data_to_file(file_name, list_of_objects):
read_data_from_file(file_name): -> (a list of product objects)
changelog: (When,Who,What)
RRoot,1.1.2030,Created Class
Tao Ye,6.3.2020,Modified code to complete assignment 8
"""
pass
@staticmethod
def read_data_from_file(file_name):
"""
Desc - Reads data from a file into a list of products
:param file_name: (string) with name of file
:return: (list) of product objects
"""
list_of_objects=[]
try:
file = open(file_name, "r")
for line in file:
objProduct = Product() # create an object of Product class
data = line.split(",") # data has the list of name and price strings
objProduct.product_name = data[0]
objProduct.product_price = float(data[1])
list_of_objects.append(objProduct) # add the object to the list
file.close()
return list_of_objects
except FileNotFoundError:
print("File", file_name, "does not exist yet.")
return list_of_objects
@staticmethod
def save_data_to_file(file_name, list_of_objects):
"""
Desc - Save a list of products to a file
:param file_name: (string) with name of file
:param list_of_objects: (list) you want to save to the file
"""
file = open(file_name, "w")
for objProduct in list_of_objects:
file.write(objProduct.product_name + "," + str(objProduct.product_price) + "\n")
file.close()
# Processing ------------------------------------------------------------- #
# Presentation (Input/Output) -------------------------------------------- #
class IO:
""" A class for perform Input and Output
methods:
OutputMenuItems():
InputMenuChoice():
ShowCurrentObjectsInList(list_of_objects):
InputProductAndPrice():
changelog: (When,Who,What)
RRoot,1.1.2030,Created Class
Tao Ye,6.3.2020,Modified code to complete assignment 8
"""
pass
@staticmethod
def OutputMenuItems():
""" Display a menu of choices to the user
:return: nothing
"""
print('''
Menu of Options
1) Show current data
2) Add a new product data.
3) Save Data to File and Exit Program
''')
print() # Add an extra line for looks
@staticmethod
def InputMenuChoice():
""" Gets the menu choice from a user
:return: string
"""
choice = str(input("Which option would you like to perform? [1 to 3] - ")).strip()
print() # Add an extra line for looks
return choice
@staticmethod
def ShowCurrentObjectsInList(list_of_objects):
""" Shows the current list of products
:param list_of_objects: (list) of product objects you want to display
:return: nothing
"""
print("******* The current product lists are: *******")
for objProduct in list_of_objects:
print(objProduct.product_name + " (price: " + str(objProduct.product_price) + ")")
print("*******************************************")
print() # Add an extra line for looks
@staticmethod
def InputProductAndPrice():
""" Ask the user to enter product name and price
:param none:
:return: (string) product name and price
"""
product = input("What is the product? - ").strip()
while True: # Ask user for price as a floating number
try:
price = float(input("What is the price? - ").strip())
break
except ValueError:
print("Price must be a floating number; try again...")
continue
return product, price
# Presentation (Input/Output) -------------------------------------------- #
# Main Body of Script ---------------------------------------------------- #
# Load data from file into a list of product objects when script starts
# Show user a menu of options
# Get user's menu option choice
# Show user current data in the list of product objects
# Let user add data to the list of product objects
# let user save current data to file and exit program
# When the program starts, Load data from products.txt.
# If the file already exists, load data from the file
lstOfProductObjects = FileProcessor.read_data_from_file(strFileName)
# Display a menu of choices to the user
while True:
IO.OutputMenuItems() # Shows menu
strChoice = IO.InputMenuChoice() # Get menu option
# Process user's menu choice
if (strChoice.strip() == '1'): # Show current list of product objects
IO.ShowCurrentObjectsInList(lstOfProductObjects)
continue # to show the menu
elif (strChoice.strip() == '2'): # Add a new product to the list of the objects
# Ask user for new product and price
strProduct, floatPrice = IO.InputProductAndPrice()
print() # Add an extra line for looks
# Add new object to the List
objProduct = Product() # A new product object
objProduct.product_name = strProduct # setter property for product_name
objProduct.product_price = floatPrice # setter property for product_price
lstOfProductObjects.append(objProduct) # add the new object to the list
# Show current list of product objects
IO.ShowCurrentObjectsInList(lstOfProductObjects)
continue # to show the menu
elif (strChoice.strip() == '3'): # save the data to the file and exit
FileProcessor.save_data_to_file(strFileName, lstOfProductObjects)
input("Data saved to file! Press the [Enter] key to return to exit.")
break # exit
else:
print("Invalid choice; try again...")
continue
# Main Body of Script ---------------------------------------------------- #