-
Notifications
You must be signed in to change notification settings - Fork 21
Ada-ac2 Ocelots Soumya S #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4df6934
d631936
bac22de
ae850f4
f9182d4
4f9feb0
77e1959
6f47283
eb3034d
e747891
1d4dd93
ecd2202
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,11 @@ | ||
| class Clothing: | ||
| pass | ||
| from swap_meet.item import Item | ||
| class Clothing(Item): | ||
| def __init__(self, id=None, fabric="Unknown", condition=0): | ||
| super().__init__(id, condition) | ||
| self.fabric = fabric if fabric else "Unknown" | ||
|
|
||
| def get_category(self): | ||
| return f"Clothing" | ||
|
|
||
| def __str__(self): | ||
| return f"An object of type {self.get_category()} with id {self.id}. It is made from {self.fabric} fabric." |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,12 @@ | ||
| class Decor: | ||
| pass | ||
| from swap_meet.item import Item | ||
| class Decor(Item): | ||
| def __init__(self, id=None, width=0, length=0, condition=0): | ||
| super().__init__(id, condition) | ||
| self.width = width if width else 0 | ||
| self.length = length if length else 0 | ||
|
|
||
| def get_category(self): | ||
| return f"Decor" | ||
|
|
||
| def __str__(self): | ||
| return f"An object of type {self.get_category()} with id {self.id}. It takes up a {self.width} by {self.length} sized space." | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,11 @@ | ||
| class Electronics: | ||
| pass | ||
| from swap_meet.item import Item | ||
| class Electronics(Item): | ||
| def __init__(self, id=None, type="Unknown", condition=0): | ||
| super().__init__(id, condition) | ||
| self.type = type if type else "Unknown" | ||
|
|
||
| def get_category(self): | ||
| return f"Electronics" | ||
|
|
||
| def __str__(self): | ||
| return f"An object of type {self.get_category()} with id {self.id}. This is a {self.type} device." |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,31 @@ | ||
| import uuid | ||
| class Item: | ||
| pass | ||
| def __init__(self, id=None, condition=0): | ||
| unique_identifier = uuid.uuid4() | ||
| self.id = id if id else unique_identifier.int | ||
| if not isinstance(self.id, int): | ||
| raise TypeError("Item id should be an int type object") | ||
| self.condition = condition if condition else 0 | ||
|
|
||
| def get_category(self): | ||
| return self.__class__.__name__ | ||
|
|
||
| def __str__(self): | ||
| return f"An object of type {self.get_category()} with id {self.id}" | ||
|
|
||
| def condition_description(self): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well organized code! At this point, you might see that a |
||
| condition_rating = round(self.condition) | ||
| if condition_rating == 0: | ||
| return "user risk involved" | ||
| elif condition_rating == 1: | ||
| return "may not be working" | ||
| elif condition_rating == 2: | ||
| return "use at your own discretion" | ||
| elif condition_rating == 3: | ||
| return "used condition" | ||
| elif condition_rating == 4: | ||
| return "as good as new" | ||
| elif condition_rating == 5: | ||
| return "mint condition" | ||
| else: | ||
| return "Unknown condition" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,95 @@ | ||
| class Vendor: | ||
| pass | ||
| def __init__(self, inventory=None): | ||
| self.inventory = inventory if inventory else [] | ||
|
|
||
| if not isinstance(self.inventory, list): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice error checking |
||
| raise TypeError("Inventory should be a list type object") | ||
|
|
||
| def add(self, item): | ||
| if not item: | ||
| return None | ||
| self.inventory.append(item) | ||
| return item | ||
|
|
||
| def remove(self, item): | ||
| if item not in self.inventory: | ||
| return None | ||
| self.inventory.remove(item) | ||
| return item | ||
|
|
||
| def get_by_id(self, id): | ||
| if not id or not self.inventory: | ||
| return None | ||
| return next((item for item in self.inventory if item.id == id), None) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. excellent list comprehension! |
||
|
|
||
| def swap_items(self, other_vendor, my_item, their_item): | ||
| if not my_item or not their_item: | ||
| return False | ||
| if not self.inventory or not other_vendor.inventory: | ||
| return False | ||
| if my_item not in self.inventory or their_item not in other_vendor.inventory: | ||
| return False | ||
| self.add(other_vendor.remove(their_item)) | ||
| other_vendor.add(self.remove(my_item)) | ||
| return True | ||
|
|
||
| def swap_first_item(self, other_vendor): | ||
| if not self or not other_vendor: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice example of guard statements. No need to indent the rest of the function as an |
||
| return False | ||
| if not self.inventory or not other_vendor.inventory: | ||
| return False | ||
| self.swap_items(other_vendor, self.inventory[0], other_vendor.inventory[0]) | ||
| return True | ||
|
|
||
| def get_by_category(self, category): | ||
| if not category or not self.inventory: | ||
| return None | ||
| return [item for item in self.inventory if item.get_category() == category] | ||
|
|
||
| def get_best_by_category(self, category): | ||
| if not category: | ||
| return None | ||
| category_items = self.get_by_category(category) | ||
| if not category_items: | ||
| return None | ||
| return max(category_items, key=lambda item: item.condition) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice use of |
||
|
|
||
| def swap_best_by_category(self, other_vendor, my_priority, their_priority): | ||
| if not self.inventory or not other_vendor.inventory: | ||
| return False | ||
| my_best_item = self.get_best_by_category(their_priority) | ||
| their_best_item = other_vendor.get_best_by_category(my_priority) | ||
| if not my_best_item or not their_best_item: | ||
| return False | ||
| return self.swap_items(other_vendor, my_best_item, their_best_item) | ||
|
|
||
| def display_inventory(self, category=None): | ||
| if not self.inventory: | ||
| print("No inventory to display.") | ||
| return | ||
| items = self.get_by_category(category) if category else self.inventory | ||
| if items: | ||
| for index, item in enumerate(items): | ||
| print(f"{index+1}. {str(item)}") | ||
| else: | ||
| print("No inventory to display.") | ||
|
|
||
| def swap_by_id(self, other_vendor, my_item_id, their_item_id): | ||
| if not self.inventory or not other_vendor.inventory: | ||
| return False | ||
| if (not self.get_by_id(my_item_id)) or (not other_vendor.get_by_id(their_item_id)): | ||
| return False | ||
| return self.swap_items(other_vendor, self.get_by_id(my_item_id), other_vendor.get_by_id(their_item_id)) | ||
|
|
||
| def choose_and_swap_items(self, other_vendor, category=None): | ||
| if not self.inventory or not other_vendor.inventory: | ||
| print("No inventory to display.") | ||
| return False | ||
| print("Inventory 1:\n") | ||
| self.display_inventory(category) | ||
| print("Inventory 2:\n") | ||
| other_vendor.display_inventory(category) | ||
| my_item_id = int(input(f"Enter the item id from first inventory to swap: ")) | ||
| their_item_id = int(input(f"Enter the item id from second inventory to swap: ")) | ||
| return self.swap_by_id(other_vendor, my_item_id, their_item_id) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice use of ternary operators