I'm Python newbie, and, as you can see, I'm trying to study Object programming. In the next code, I'm trying to model trucks that transport items: every item has an identifier and a weight. Tracks have max weight capacity. Look at the code below, than I will tell you the problem.
class Item():
'''
Class Item models an object transported in a supply chain
'''
def __init__(self, id, weight):
'''
Constructor of an object of class Item
:param id: Unique identifier of the Item
:param weight: the weight of the Item, expressed in Kg
'''
self.id = id
self.weight = weight
class Truck():
'''
Class Truck models a truck transporting Items
'''
def __init__(self, plate_number, capacity):
'''
Constructor of an object of class Truck
:param plate_number: Unique identifier of the Truck
:param capacity: maximum weight transportable by the Truck
'''
self.plate_number = plate_number
self.capacity = capacity
self.remaining_capacity = capacity
def load(self, items):
'''
This method allows to load a list of items into a truck if their
total weight is less that or equal to the remaining capacity of the truck.
If this is case, True is returned. Otherwise False is returned
:param items:
:return:
'''
total_load = 0
for item in items:
total_load += item.weight
if total_load > self.remaining_capacity:
print('out of range')
return False
else:
for item in items:
items.append(item)
self.remaining_capacity = self.remaining_capacity - item.weight
return True
a = Item('AAA01', 10)
b = Item('BBB01', 20)
c = Item('CCC01', 30)
truck_a = Truck('LE000A', 35)
truck_a.load([a, b])
print('Remaining capcity of Truck ', truck_a.plate_number, ' is ',truck_a.remaining_capacity)
In the load(items)
method of Trucks, you can see the command items.append(item)
: if that command is delated, the averall program works; if not, it goes in loop, but I can't image why.
EDIT
Using the next code, it seems to work, without changing the items.append(item)
statement:
class Item():
'''
Class Item models an object transported in a supply chain
'''
def __init__(self, id, weight):
'''
Constructor of an object of class Item
:param id: a unique code associated to the item
:param weight: the weight of the item expressed in kg
'''
self.id = id
self.weight = weight
class Truck():
'''
This class models a truck
'''
def __init__(self, plate_number, capacity, items):
'''
Clonstructor of an object of class Truck
:param plate_number: a unique number Identifying the truck
:param capacity: maximum weight that the truck can carry
:param items: list of objects of class Item carried by the truck
'''
self.plate_number = plate_number
self.capacity = capacity
self.items = list(items)
self.remaining_capacity = capacity
for item in self.items:
self.remaining_capacity = self.remaining_capacity - item.weight
def load(self, items):
'''
This method allows to load a list of items into a truck if their
total weight is less that or equal to the remaining capacity of the truck.
If this is case, True is returned. Otherwise False is returned
:param items:
:return:
'''
total_load = 0
for item in items:
total_load = item.weight
if total_load > self.remaining_capacity:
return False
else:
for item in items:
self.items.append(item)
self.remaining_capacity = self.remaining_capacity - item.weight
return True
a = Item('AAA01', 10)
b = Item('BBB01', 20)
c = Item('CCC01', 30)
truck_a = Truck('LE000A', 35, [a])
truck_a.load([b])
print('Remaining capacity of Truck ', truck_a.plate_number, ' is ', truck_a.remaining_capacity)
As you can see, main difference in the EDIT code is that class Truck
has a constructor that takes items
as parameter and make a "copy" of it with the self.items = list(items)
. I don't know why this last code has been working