-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssigment08.py
More file actions
251 lines (212 loc) · 8.67 KB
/
Assigment08.py
File metadata and controls
251 lines (212 loc) · 8.67 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# ------------------------------------------------------------------------ #
# 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
# jcarnes,12.06.2020,Modified code to complete assignment 8
# ------------------------------------------------------------------------ #
# Data -------------------------------------------------------------------- #
strFileName = 'products.txt'
lstOfProductObjects = []
menu_option = None
product_to_add = None
price_to_add = None
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:
to_string: returns the product_name and product_price as string separated by comma
__str__: returns the product_name and product_price as string separated by comma
__doc__: returns the class information of the Product Class
is_float: returns a True/False for if the value can be converted to a float
changelog: (When,Who,What)
RRoot,1.1.2030,Created Class
jcarnes,12.6.2020,Modified code to complete assignment 8
"""
# DONE: Add Code to the Product class
#
# Constructor
# -- Constructor --
def __init__(self, product_name, product_price):
"""Sets Product Name and Product Price for new object"""
# Attributes
try:
self.__product_name = product_name
self.__product_price = float(product_price)
except Exception as e:
raise Exception('Issue setting initial values: \n\n' + str(e))
# -- Properties --
# product_name
@property # DON'T USE NAME for this directive!
def product_name(self): # (getter or accessor)
return str(self.__product_name).title() # Title case
@product_name.setter # The NAME MUST MATCH the property's!
def product_name(self, value): # (setter or mutator)
if str(value).isnumeric() is False:
self.__product_name = value
else:
raise Exception("Names cannot be numbers")
# product_price
@property # DON'T USE NAME for this directive!
def product_price(self): # (getter or accessor)
return str(self.__product_price) # Title case
@product_price.setter # The NAME MUST MATCH the property's!
def product_price(self, value): # (setter or mutator)
if Product.is_float(value):
self.__product_price = value
else:
raise Exception("Value must be a number")
@staticmethod
def is_float(value):
"""Tests to see if the value entered is convertible to a float
:param value: (str) from user input
:return: Boolean True or False"""
try:
float(value)
return True
except ValueError:
return False
# -- Methods --
def to_string(self):
"""Returns the object as a string"""
return self.product_name + "," + str(self.product_price)
def __str__(self):
"""Returns the object as a string"""
return self.product_name + "," + str(self.product_price)
def __doc__(self):
"""Information on the Product class"""
return 'This class holds product and price data'
# 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_product_objects):
read_data_from_file(file_name): -> (a list of product objects)
changelog: (When,Who,What)
RRoot,1.1.2030,Created Class
jcarnes,12.06.2020,Modified code to complete assignment 8
"""
# DONE: Add Code to process data from a file
@staticmethod
def read_data_from_file(file_name):
"""This function reads data from a text file and adds the contents to a list of Product objects.
If the file doesn't exist then the file is created and the list is empty.
:param file_name: text file containing products and prices
:return: list_of_rows containing Product objects
"""
list_of_rows = []
try:
f = open(file_name, 'r')
for line in f:
product, price = line.split(",")
list_of_rows.append(Product(product, price.strip()))
except:
f = open(file_name, 'w')
f.close()
return list_of_rows
# TODO: Add Code to process data to a file
@staticmethod
def write_data_to_file(file_name, list_of_rows):
"""This function takes a list containing Product objects and writes the list to a text file
:param file_name: text file to write to
:param list_of_rows: list containing Product objects
:return: str success status
"""
f = open(file_name, 'w')
for obj in list_of_rows:
f.write(obj.product_name + ',' + obj.product_price + '\n')
f.close()
return 'Success'
# Processing ------------------------------------------------------------- #
# Presentation (Input/Output) -------------------------------------------- #
class IO:
# DONE: Add docstring
"""Display information to a user and receive user input:
methods:
print_menu()
input_menu_choic()
display_current_data(list_of_rows): -> a list of Product objects
get_product_data()
changelog: (When,Who,What)
jcarnes,12.06.2020,Created IO Class
"""
# DONE: Add code to show menu to user
@staticmethod
def print_menu():
""" Display a menu of choices to the user
:return: nothing
"""
print('''
Menu of Options
1) Display Current Data
2) Add Product to List
3) Save Data to File & Exit Program
''')
print() # Add an extra line for looks
# DONE: Add code to get user's choice
@staticmethod
def input_menu_choice():
""" Gets the menu choice from a user
:return: string containing the user's choice
"""
choice = str(input("Which option would you like to perform? [1 to 3] - ")).strip()
print() # Add an extra line for looks
return choice
# DONE: Add code to show the current data from the file to user
@staticmethod
def display_current_data(list_of_rows):
""" Prints Product objects in the list to the user
:param list_of_rows: list containing Product objects
"""
if list_of_rows == []:
print('\nThe product list is currently empty!')
else:
print('\nCurrent Products and Prices in List:')
for obj in list_of_rows:
print(obj.product_name + ', ' + obj.product_price)
# DONE: Add code to get product data from user
@staticmethod
def get_product_data():
""" Get product and price information from user then create new Product object
:param: None
:return: product object"""
try:
item = input('Please enter a product: ').strip()
price = input('Please enter a price: ').strip()
product_obj = Product(item, price)
except Exception as e:
print(e)
return product_obj
# Presentation (Input/Output) -------------------------------------------- #
# Main Body of Script ---------------------------------------------------- #
# DONE: Add Data Code to the Main body
# 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
# Main Body of Script ---------------------------------------------------- #
try:
# Load data from file into a list of product objects
list_of_objects = FileProcessor.read_data_from_file(strFileName)
# Show user a menu of options
# Get user's menu option choice
while menu_option != '3':
IO.print_menu()
menu_option = IO.input_menu_choice()
if menu_option == '1': # Display current data
IO.display_current_data(list_of_objects)
elif menu_option == '2': # Add product to list
list_of_objects.append(IO.get_product_data())
elif menu_option == '3':
FileProcessor.write_data_to_file(strFileName, list_of_objects)
break
else:
continue
except Exception as e:
print(e)