From d15d139af8e07723c538e934d383121863969f6c Mon Sep 17 00:00:00 2001 From: brodriguez8774 <brodriguez8774@gmail.com> Date: Thu, 26 Sep 2019 21:45:56 -0400 Subject: [PATCH] Correct some interface bugs regarding potential user input --- resources/interface.py | 37 ++++++++++++++++++++++--------------- resources/knapsack.py | 30 +++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/resources/interface.py b/resources/interface.py index da02867..36181a3 100644 --- a/resources/interface.py +++ b/resources/interface.py @@ -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) diff --git a/resources/knapsack.py b/resources/knapsack.py index 332021a..cfb4c5f 100644 --- a/resources/knapsack.py +++ b/resources/knapsack.py @@ -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. -- GitLab