Reputation: 25
import ShoppingClass as SC
class ShoppingCartPrinter:
shoppingCart = SC.ShoppingCart()
while(True):
item = SC.ItemToPurchase()
item.setName(input('\nEnter Name: '))
item.setDescription(input('\nSet Description: '))
item.setPrice(input('\nSet price: '))
item.setQuantity(input('\nSet Quantity: '))
shoppingCart.addItem(item)
for shoppingCart.cartItems.getName() in shoppingCart.cartItems:
print(shoppingCart.cartItems.getName())
I am trying to teach myself python coming from java and wrote this simple program. I have another class which I use to create the objects 'item' and 'shopping cart'. In the constructor of shopping cart I create an empty list to which I add objects to it. I don't know how the syntax should be if I am trying to print an object's attribute. What I have above is clearly wrong. Sorry if this is a simple answer, any help is appreciated. Thanks
class ItemToPurchase:
def __init__(self,itemName ='none',itemDescription = 'none',itemPrice =
0,itemQuantity = 0):
self.itemName = itemName
self.itemDescription = itemDescription
self.itemPrice = itemPrice
self.itemQuantity = itemQuantity
def setName(self,newName):
self.itemName = newName
def getName(self):
return self.itemName
def setPrice(self, newPrice):
self.itemPrice = newPrice
def getPrice(self):
return self.itemPrice
def setQuantity(self, newQuantity):
self.itemQuantity = newQuantity
def getQuantity(self):
return self.itemQuantity
def setDescription(self,description):
self.itemDescription = description
def getDescription(self):
return self.itemDescription
def printItemCost():
print(itemName+" "+itemQuantity+" @ $"+itemPrice+" =
"+itemPrice*itemQuantity)
def printItemDescription():
print(itemName+" "+itemDescription)
class ShoppingCart:
def __init__(self,customerName = 'none', currentDate = 'January 1, 2016'):
self.customerName = customerName
self.currentDate = currentDate
self.cartItems = []
def addItem(self, item):
self.cartItems.append(item)
def getDate(self):
return self.currentDate
def getCustomerName(self):
return self.customerName
Upvotes: 0
Views: 152
Reputation: 1299
One issue with your code is the excessive use of setter functions in your ItemToPurchase class. It's better to set the values with the constructor? Courses in Java often teach you need to build getters and setters for all attributes, but it's not the best idea, and makes the code rather bloated. Only if absolutely required should they be implemented (in your case, changing itemQuantity is all that is needed, and maybe itemPrice for a coupon or something like that). Also, in Python you can return a tuple of all ItemToPurchase attributes, rather than have indivual getters:
def getAttributes(self):
return (self.itemName, self.itemDescription, self.itemPrice, self.itemQuantity)
Then you don't need all these accesses back in your primary class / main function, you just assign to local variables and construct the ItemToPurchase that way. Then you have many fewer class accesses / function calls, it's a lot cleaner.
Another issue is how ShoppingCart is constructed. While Python doesn't exactly have 'private' attributes like Java or C++, you might want to make the cartItems list 'private' (mangled, but hey, close enough) in __init___ in that class, and build a retriever:
self.__cartItems = []
def getCartSize():
return len(self.__cartItems)
def getCartItem(pos):
return self.__cartItems[pos].getAttributes()
Then to recover the attributes of the first ItemToPurchase in the ShoppingCart:
myItem = getCartItem(0) #returns a tuple of all the data about that ItemToPurchase
print(myItem[0]) #prints the name of that item in the cart
Probably the biggest difference between Python classes and Java classes is how the concept of 'private' is implemented, and the greater flexibility of Python return statements.
Upvotes: 0
Reputation: 39354
There are lots of things wrong with the code you have posted, but your loop to print items should look like this:
for item in shoppingCart.cartItems:
print(item.getName())
Upvotes: 0
Reputation: 95
I can't surely say anything without seeing the other class. But I think the problem can be caused by the contructor. What I see is you are not passing any values to constructor but you are trying to set their attributes with setter functions. And if you want other people to assign it just do something like this:
name = input("Enter name")
description = input("Enter description")
item = foo(name, description)
Use this kind of pattern to assign variables which typed by other users. And for the printing part use this.
for i in shoppingCart.cartItems:
print(i.getName())
If you have any questions feel free to ask. Hope it helps :)
PS: I was writing my answer when you updated. My answer still counts though. I think you are overcomplicating it.
Upvotes: 2