Skip to content
Snippets Groups Projects
Commit d15d139a authored by Brandon Rodriguez's avatar Brandon Rodriguez
Browse files

Correct some interface bugs regarding potential user input

parent d411bd5c
Branches
No related merge requests found
......@@ -158,30 +158,33 @@ class UserInterface():
print('')
# Attempt to set file contents as new item set.
with open(user_input, 'r') as file:
# Read in file.
json_data = json.load(file)
# Check that JSON data is formatted in way we expect.
if self._knapsack_algorithm.set_item_set(json_data):
# Successfully set JSON data as new item set.
print('Successfully set new Item Set from JSON data.')
self._knapsack_algorithm.display_item_set()
else:
# Validation failure.
print('Failed to validate file contents for Item Set.')
try:
with open(user_input, 'r') as file:
# Read in file.
json_data = json.load(file)
# Check that JSON data is formatted in way we expect.
if self._knapsack_algorithm.set_item_set(json_data):
# Successfully set JSON data as new item set.
print('Successfully set new Item Set from JSON data.')
self._knapsack_algorithm.display_item_set()
else:
# Validation failure.
print('Failed to validate file contents for Item Set.')
except FileNotFoundError:
print('Failed to locate file "{0}".'.format(user_input))
def add_to_item_set(self):
"""
Adds new "Item" to "Item Set" by manual input.
"""
print('Enter a benefit amount (must be a positive integer):')
print('Enter Item "Benefit" amount (must be a positive integer):')
# Get user's input.
benefit_input = input()
print('')
print('Enter a cost amount (must be a positive integer):')
print('Enter Item "Cost" amount (must be a positive integer):')
# Get user's input.
cost_input = input()
......@@ -221,4 +224,8 @@ class UserInterface():
"""
Solves knapsack with current problems and displays result to user.
"""
self._knapsack_algorithm.calculate_fractional_knapsack()
self._knapsack_algorithm.display_max_weight()
self._knapsack_algorithm.display_item_set()
results = self._knapsack_algorithm.calculate_fractional_knapsack()
print('Optimal backpack contents, by weight of each item to take:')
print(results)
......@@ -38,8 +38,13 @@ class FractionalKnapsackAlgorithm():
"""
# First validate item set.
if self.validate_item_set(item_set):
# Validation passed. Set values.
self._item_set_ = item_set
# Validation passed. First clear item set.
self.clear_item_set()
# Now add each item to new set.
for item in item_set:
self._add_item_to_item_set(item)
self._items_populated_ = True
return True
else:
......@@ -91,20 +96,35 @@ class FractionalKnapsackAlgorithm():
def add_item_to_item_set(self, item):
"""
Adds dictionary item to item set.
Validates dictionary item. On success, calls internal function to add to item set.
:param item: Potential new item to add to "item set".
:return: Bool indicating if item was successfully added or not.
"""
# First validate that item matches requirements.
if self.validate_item_set([item]):
# Validation success.
self._item_set_.append(item)
self._items_populated_ = True
self._add_item_to_item_set(item)
return True
else:
# Validation failed. Item not added.
return False
def _add_item_to_item_set(self, item):
"""
Adds item to item set.
:param item: Validated item.
"""
# Clean item (Just because values were parsable as int's doesn't mean they currently are).
cleaned_item = {
'benefit': int(str(item['benefit']).strip()),
'cost': int(str(item['cost']).strip()),
}
# Add to item set.
self._item_set_.append(cleaned_item)
self._items_populated_ = True
def set_max_weight(self, max_weight):
"""
Sets upper bound to limit algorithm choices.
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment