Skip to content
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

Paper_Karla T_C15 #54

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions swap_meet/clothing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from swap_meet.item import Item
class Clothing(Item):
pass

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass is a Python placeholder to avoid syntax errors before code is added. Once code is added pass can be removed.

def __init__(self,condition = 0.0):
self.category = "Clothing"
self.condition = condition
Comment on lines +4 to +6

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way to write this is to use super to take advantage of the code in Item's constructor:

def __init__(self, condition = 0.0):
    super().__init__("Clothing", condition)

def __str__(self):
return "The finest clothing you could wear."

8 changes: 8 additions & 0 deletions swap_meet/decor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from swap_meet.item import Item
class Decor(Item):
pass
def __init__(self,condition = 0.0):
self.category = "Decor"
self.condition = condition
def __str__(self):
return "Something to decorate your space."
8 changes: 8 additions & 0 deletions swap_meet/electronics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from swap_meet.item import Item
class Electronics(Item):
pass
def __init__(self,condition = 0.0):
self.category = "Electronics"
self.condition = condition
def __str__(self):
return "A gadget full of buttons and secrets."
23 changes: 23 additions & 0 deletions swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Item:
pass
def __init__(self, category="", condition = 0.0):
self.category = category
self.condition = condition
def __str__(self):
return "Hello World!"
def condition_description(self):
if self.condition == 0:
return "mint condition"
elif self.condition == 1:
return "like new"
elif self.condition == 2:
return "gently used"
elif self.condition == 3:
return "used"
elif self.condition == 4:
return "clean this before using it"
elif self.condition == 5:
return "Please clean 2x over"
Comment on lines +9 to +20

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Condition can have a decimal - what happens if the condition is 3.5? How could you change this section to account for those conditions?




56 changes: 56 additions & 0 deletions swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class Vendor:
pass
def __init__(self, inventory = None):
if inventory == None:
self.inventory= []
else:
self.inventory = inventory
def add(self, add_item):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For readability I recommend placing a blank line between the end of one function and the beginning of the next.

self.inventory.append(add_item)
return add_item
def remove(self, remove_item):
if remove_item not in self.inventory:
return False
else:
self.inventory.remove(remove_item)
return remove_item
def get_by_category(self, category):
item_list = []
for item in self.inventory:
if category == item.category:
item_list.append(item)
return item_list
def swap_items(self, vendor, my_item, their_item):
if my_item not in self.inventory or their_item not in vendor.inventory:
return False
if my_item in self.inventory and their_item in vendor.inventory:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conditional on line 24 will catch the case where an inventory doesn't have the item, so this check isn't needed. If the function gets to this point, each item must be in the correct inventory.

self.remove(my_item)
vendor.add(my_item)
vendor.remove(their_item)
self.add(their_item)
return True
def swap_first_item(self, vendor):
if len(self.inventory) > 0 and len(vendor.inventory) > 0:
my_first_item = self.inventory [0]
their_first_item = vendor.inventory[0]
return self.swap_items(vendor, my_first_item, their_first_item) #adjusted line for "DRY"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent code reuse!


return False
def get_best_by_category(self, category):
items = self.get_by_category(category)
if len(items) == 0:
return None
best_by_category = items[0]
for item in items:
if item.condition > best_by_category.condition:
best_by_category = item
return best_by_category
def swap_best_by_category(self, other, my_priority, their_priority):
my_item = self.get_best_by_category(their_priority)
their_item = other.get_best_by_category(my_priority)
if my_item and their_item:
return self.swap_items(other, my_item, their_item) #adjusted line for "DRY"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!


return False