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

Rock - Priscille #50

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
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):
def __init__(self, category="Clothing", condition=0, age=0):
super().__init__(category, condition, age)
Comment on lines +4 to +5

Choose a reason for hiding this comment

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

👍 this works perfectly! But it still lets the user change the category, and we don't want that. class Clothing will always have the category "clothing." So let's try this:

Suggested change
def __init__(self, category="Clothing", condition=0, age=0):
super().__init__(category, condition, age)
def __init__(self, condition=0, age=0):
super().__init__("clothing", condition, age)


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

10 changes: 10 additions & 0 deletions swap_meet/decor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from swap_meet.item import Item

class Decor(Item):

Choose a reason for hiding this comment

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

👍 We can do the same thing here as we did above!


def __init__(self, category="Decor", condition=0, age=0):
super().__init__(category, condition, age)

def __str__(self):
return "Something to decorate your space."

10 changes: 10 additions & 0 deletions swap_meet/electronics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from swap_meet.item import Item

class Electronics(Item):

Choose a reason for hiding this comment

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

👍 We can do the same thing here as we did above!


def __init__(self, category="Electronics", condition=0, age=0):
super().__init__(category, condition, age)

def __str__(self):
return "A gadget full of buttons and secrets."

39 changes: 39 additions & 0 deletions swap_meet/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Item:

def __init__(self, category = "", condition = 0, age = 0):
if category is None:
self.category = ""
else:
self.category = category
if condition is None:
self.condition = 0
else:
self.condition = condition
if age is None:
self.age = 0
else:
self.age = age
Comment on lines +3 to +15

Choose a reason for hiding this comment

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

this works good! But our default parameters, like category="" already do what the if-else statements do. so let's let the default parameters do all the work.

Suggested change
def __init__(self, category = "", condition = 0, age = 0):
if category is None:
self.category = ""
else:
self.category = category
if condition is None:
self.condition = 0
else:
self.condition = condition
if age is None:
self.age = 0
else:
self.age = age
def __init__(self, category = "", condition = 0, age = 0):
self.category = category
self.condition = condition
self.age = age

now category will be assigned "" if the user doesn't add in an argument. It will do what if-else statements are doing for us!



def __str__(self):
return "Hello World!"

def condition_description(self):

Choose a reason for hiding this comment

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

👍


if self.condition == 0:
return "Poor"
elif self.condition == 1:
return "Fair"
elif self.condition == 2:
return "Good"
elif self.condition == 3:
return "Very good"
elif self.condition == 4:
return "Like new"
elif self.condition == 5:
return "Brand new"





97 changes: 97 additions & 0 deletions swap_meet/vendor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from swap_meet.item import Item

class Vendor:

Choose a reason for hiding this comment

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

👍


def __init__(self, inventory = None):
if inventory is None:
self.inventory = []
else:
self.inventory = inventory
Comment on lines +5 to +9

Choose a reason for hiding this comment

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

👍 this time the if statement is necessary! we shouldn't use lists and dictionaries as default parameters because then all instances of Vendor would use the same list or dictionary in our memory space.

But with the if-else statement, we an make sure that each instance gets its own unique and independent list! perfect!


def add(self, item):

Choose a reason for hiding this comment

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

👍

self.inventory.append(item)
return item

def remove(self, item):

Choose a reason for hiding this comment

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

👍

if item in self.inventory:
self.inventory.remove(item)
return item
return False

def get_by_category(self, category_name):

Choose a reason for hiding this comment

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

👍

items = []
for item in self.inventory:
if category_name == item.category:
items.append(item)
return items

def swap_items(self, vendor_friend, my_item, their_item):

Choose a reason for hiding this comment

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

👍

if my_item not in self.inventory or their_item not in vendor_friend.inventory:
return False
else:
self.remove(my_item)
vendor_friend.remove(their_item)
vendor_friend.add(my_item)
self.add(their_item)
return True

def swap_first_item(self, vendor_friend):
if len(self.inventory) == 0 or len(vendor_friend.inventory) == 0:
return False
else:
item_a = self.inventory[0]
item_d = vendor_friend.inventory[0]
result = self.swap_items(vendor_friend, item_a, item_d)
Comment on lines +42 to +44

Choose a reason for hiding this comment

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

we could get rid of these two variables if we wanted!

Suggested change
item_a = self.inventory[0]
item_d = vendor_friend.inventory[0]
result = self.swap_items(vendor_friend, item_a, item_d)
result = self.swap_items(vendor_friend, self.inventory[0], vendor_friend.inventory[0])

return result

def get_best_by_category(self, category_name):

Choose a reason for hiding this comment

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

👍

highest_condition = 0
best_item = None

for item in self.get_by_category(category_name):
if item.condition > highest_condition:
highest_condition = item.condition
best_item = item
return best_item

def swap_best_by_category(self, other, my_priority, their_priority):

Choose a reason for hiding this comment

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

👍

my_best_item = self.get_best_by_category(their_priority)
their_best_item = other.get_best_by_category(my_priority)
result = self.swap_items(other, my_best_item, their_best_item)
return result

# Optional Enhancements: item by age

def get_newest_item(self):

Choose a reason for hiding this comment

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

👍

lowest_value = self.inventory[0].age
newest_item = None
for item in self.inventory:
if item.age < lowest_value:
lowest_value = item.age
newest_item = item
return newest_item

def swap_by_newest(self, business_partner, my_newest_item, their_newest_item):

Choose a reason for hiding this comment

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

👍


if len(self.inventory) == 0 or len(business_partner.inventory) == 0:
return False
else:
my_newest_item = self.get_newest_item()
their_newest_item = business_partner.get_newest_item()
result = self.swap_items(business_partner, my_newest_item, their_newest_item)
return result















95 changes: 95 additions & 0 deletions tests/test_wave_07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import pytest
from swap_meet.vendor import Vendor
from swap_meet.item import Item
from swap_meet.electronics import Electronics

def test_get_newest_item():
item_a = Item(age = 3)
item_b = Item(age = 1)
item_c = Item(age = 2)
fatimah = Vendor(
inventory=[item_a, item_b, item_c]
)
newest_item = fatimah.get_newest_item()

assert newest_item.age == pytest.approx(1.0)


def test_get_newest_item_with_duplicates():
item_a = Item(age = 3)
item_b = Item(age = 1)
item_c = Item(age = 1)
fatimah = Vendor(
inventory=[item_a, item_b, item_c]
)
newest_item = fatimah.get_newest_item()

assert newest_item.age == pytest.approx(1.0)

def test_swap_by_newest():
item_a = Item(age = 3)
item_b = Item(age = 1)
item_c = Item(age = 2)
fatimah = Vendor(
inventory=[item_a, item_b, item_c]
)

item_d = Item(age = 4)
item_e = Item(age = 5)
item_f = Item(age = 2)
jolie = Vendor(
inventory=[item_d, item_e, item_f]
)

result = fatimah.swap_by_newest(
business_partner = jolie,
my_newest_item = item_b,
their_newest_item = item_f)

assert result is True
assert len(fatimah.inventory) is 3
assert len(jolie.inventory) is 3
assert item_b not in fatimah.inventory
assert item_f in fatimah.inventory
assert item_f not in jolie.inventory
assert item_b in jolie.inventory

def test_swap_by_newest_from_my_empty_returns_false():
fatimah = Vendor(
inventory=[]
)

item_d = Item(age = 4)
item_e = Item(age = 5)
item_f = Item(age = 2)
jolie = Vendor(
inventory=[item_d, item_e, item_f]
)

nobodys_item = Item(age = 3)

result = fatimah.swap_by_newest(jolie, nobodys_item, item_f)

assert len(fatimah.inventory) is 0
assert len(jolie.inventory) is 3
assert result is False

def test_swap_by_newest_from_their_empty_returns_false():
item_a = Item(age = 3)
item_b = Item(age = 1)
item_c = Item(age = 2)
fatimah = Vendor(
inventory=[item_a, item_b, item_c]
)

jolie = Vendor(
inventory=[]
)

nobodys_item = Item(age = 3)

result = fatimah.swap_by_newest(jolie, item_b, nobodys_item)

assert len(fatimah.inventory) is 3
assert len(jolie.inventory) is 0
assert result is False