-
Notifications
You must be signed in to change notification settings - Fork 103
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
Sapphire - April Zhang #68
base: main
Are you sure you want to change the base?
Changes from all commits
09c740a
d07854e
af5b00c
bfc92c0
c25d21a
4cdb926
b57492b
647e52d
7412a5f
f41c819
590aa94
f1a51b4
3cd0f99
bd89d60
be5fad2
4a23fcb
d168067
e3c26a4
de8843f
e37b322
f516d05
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,12 @@ | ||
class Clothing: | ||
pass | ||
import uuid | ||
from swap_meet.item import Item | ||
class Clothing(Item): | ||
""" | ||
A subclass of Item class, representing clothing item. | ||
""" | ||
def __init__(self, id=None, fabric="Unknown", condition=0, age=0): | ||
super().__init__(id, condition, age) | ||
self.fabric = fabric | ||
|
||
def __str__(self): | ||
return super().__str__() + f" It is made from {self.fabric} fabric." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,13 @@ | ||
class Decor: | ||
pass | ||
import uuid | ||
from swap_meet.item import Item | ||
class Decor(Item): | ||
""" | ||
A subclass of Item class, representing Decor item. | ||
""" | ||
def __init__(self, id=None, width=0, length=0, condition=0, age=0): | ||
super().__init__(id, condition, age) | ||
self.width = width | ||
self.length = length | ||
|
||
def __str__(self): | ||
return super().__str__() + f" It takes up a {self.width} by {self.length} sized space." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,12 @@ | ||
class Electronics: | ||
pass | ||
import uuid | ||
from swap_meet.item import Item | ||
class Electronics(Item): | ||
""" | ||
A subclass of Item class, representing electronic item. | ||
""" | ||
def __init__(self, id=None, type="Unknown", condition=0, age=0): | ||
super().__init__(id, condition, age) | ||
self.type = type | ||
|
||
def __str__(self): | ||
return super().__str__() + f" This is a {self.type} device." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,52 @@ | ||
import uuid | ||
class Item: | ||
pass | ||
""" | ||
A class that represents item. | ||
Each Item have: | ||
an attribute named id, which is an unique integer as default; | ||
an attribute named condition, which is an integer representing the item's condition, default value is 0 (the poorest); | ||
an attribute named age, which is an integer representing the item's age, default value is 0. | ||
""" | ||
def __init__(self, id=None, condition=0, age=0): | ||
# when manually set id, check if it's an integer, if not: raise TypeError. | ||
if id: | ||
if isinstance(id, int): | ||
self.id = id | ||
else: | ||
raise TypeError("Id must be an integer.") | ||
# when id is not provided, generate an id with integer. | ||
else: | ||
self.id = uuid.uuid4().int | ||
# If condition out of range, raise ValueError. | ||
if condition > 5: | ||
raise ValueError("Condition value range from 0 to 5.") | ||
else: | ||
self.condition = condition | ||
|
||
self.age = age | ||
|
||
def get_category(self): | ||
""" | ||
Return the class name as a string. | ||
""" | ||
return self.__class__.__name__ | ||
|
||
def __str__(self): | ||
return f"An object of type {self.get_category()} with id {self.id}." | ||
|
||
def condition_description(self): | ||
""" | ||
Return funny description based on the item's condition. | ||
""" | ||
if self.condition == 5: | ||
return "This item is in tip-top shape!" | ||
elif self.condition == 4: | ||
return "This item still has plenty of life left and character to spare." | ||
elif self.condition == 3: | ||
return "It's not down for the count - it's still got some fight left in it!" | ||
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. 💪 !!! |
||
elif self.condition == 2: | ||
return "Don't judge this item by its appearance. It's still got some tricks up its sleeve and plenty of use left." | ||
elif self.condition == 1: | ||
return "This item may not have much life left in it, but it's not quite ready to give up the ghost yet." | ||
else: | ||
return "Oh... :'( This item has seen better days." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,130 @@ | ||
class Vendor: | ||
pass | ||
""" | ||
A class that represents Vendor. | ||
Each Vendor has an attribute named inventory, which is an empty list by default. | ||
""" | ||
def __init__(self, inventory=None): | ||
if inventory is None: | ||
inventory = [] | ||
self.inventory = inventory | ||
|
||
def add(self, item): | ||
""" | ||
Add item to inventory, return item | ||
""" | ||
self.inventory.append(item) | ||
return item | ||
|
||
def remove(self, item): | ||
""" | ||
Remove the matching item in the inventory, return the item. | ||
If no matching item, return False. | ||
""" | ||
if item in self.inventory: | ||
self.inventory.remove(item) | ||
return item | ||
else: | ||
return False | ||
|
||
def get_by_id(self, id): | ||
""" | ||
Return the item with a matching id from the inventory. | ||
If no matching item return None. | ||
|
||
Args: | ||
id (integer): Item's id. | ||
""" | ||
for item in self.inventory: | ||
if id == item.id: | ||
return item | ||
return None | ||
|
||
def swap_items(self, other_vendor, my_item, their_item): | ||
""" | ||
Swap this Vendor's item with the other Vendor's item, return True. | ||
If either Vendor does not have a matching item, return False. | ||
|
||
Args: | ||
other_vendor (an instance of Vendor) | ||
my_item (an instance of Item): representing the item this Vendor instance plans to give. | ||
their_item (an instance of Item): representing the item the other Vendor plans to give. | ||
""" | ||
if my_item not in self.inventory or their_item not in other_vendor.inventory: | ||
return False | ||
else: | ||
self.remove(my_item) | ||
other_vendor.add(my_item) | ||
other_vendor.remove(their_item) | ||
self.add(their_item) | ||
return True | ||
Comment on lines
+52
to
+59
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. Nicely done! |
||
|
||
def swap_first_item(self, other_vendor): | ||
""" | ||
Swap the first item in this Vendor with the first item in the other Vendor, return True. | ||
If either Vendor does not have a matching item, return False. | ||
""" | ||
if not self.inventory or not other_vendor.inventory: | ||
return False | ||
else: | ||
my_first = self.inventory[0] | ||
their_first = other_vendor.inventory[0] | ||
self.swap_items(other_vendor, my_first, their_first) | ||
return True | ||
|
||
def get_by_category(self, category): | ||
""" | ||
Returns a list of objects in the inventory with that category. | ||
If no matching item, return empty list. | ||
|
||
Args: | ||
category (string): representing a category. | ||
""" | ||
category_items = [] | ||
for item in self.inventory: | ||
if item.get_category() == category: | ||
category_items.append(item) | ||
return category_items | ||
|
||
def get_best_by_category(self, category): | ||
""" | ||
Return the item with the best condition in a certain category. | ||
If no matching item, return None. | ||
|
||
Args: | ||
category (string): representing a category. | ||
""" | ||
if not self.get_by_category(category): | ||
return None | ||
else: | ||
return max(self.get_by_category(category), key=lambda item: item.condition) | ||
|
||
def swap_best_by_category(self, other_vendor, my_priority, their_priority): | ||
""" | ||
Swap the best item in this Vendor that matches their_priority category with the best item in the other Vendor that matches my_priority, return True. | ||
If either Vendor has no matching item, return False. | ||
|
||
Args: | ||
other_vendor (an instance of Vendor): _description_ | ||
my_priority (string): represents a category that this Vendor wants to receive. | ||
their_priority (string): represents a category that the other Vendor wants to receive. | ||
""" | ||
my_item = self.get_best_by_category(their_priority) | ||
their_item = other_vendor.get_best_by_category(my_priority) | ||
if not my_item or not their_item: | ||
return False | ||
else: | ||
self.swap_items(other_vendor, my_item, their_item) | ||
return True | ||
|
||
def swap_by_newest(self, other_vendor): | ||
""" | ||
Swap the newest item in this Vendor with the newest item in the other Vendor, return True. | ||
If either Vendor does not have a matching item, return False. | ||
""" | ||
my_newest = min(self.inventory, key=lambda item: item.age, default=False) | ||
their_newest = min(other_vendor.inventory, key=lambda item: item.age, default=False) | ||
Comment on lines
+124
to
+125
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. Nicely done! |
||
if not my_newest or not their_newest: | ||
return False | ||
else: | ||
self.swap_items(other_vendor, my_newest, their_newest) | ||
return True |
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.
I really like your decision to raise errors in these cases. I wonder what the unit tests for these cases would look like 😄 😄 😄