Python Forum
Want a solution to this
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Want a solution to this
#1
Hi,
Please I am having problem to create this project.

Create a class called ShoppingCart.
Create a constructor that takes no arguments and sets the total attribute to zero, and initializes an empty dict attribute named items.

Create a method add_item that requires item_name, quantity and price arguments. This method should add the cost of the added items to the current value of total. It should also add an entry to the items dict such that the key is the item_name and the value is the quantity of the item.

Create a method remove_item that requires similar arguments as add_item. It should remove items that have been added to the shopping cart and are not required. This method should deduct the cost of the removed items from the current total and also update the items dict accordingly.

If the quantity of an item to be removed exceeds the current quantity of that item in the cart, assume that all entries of that item are to be removed.

Create a method checkout that takes in cash_paid and returns the value of balance from the payment. If cash_paid is not enough to cover the total, return "Cash paid not enough".

Create a class called Shop that has a constructor which takes no arguments and initializes an attribute called quantity at 100.

Make sure Shop inherits from ShoppingCart.

In the Shop class, override the remove_item method, such that calling Shop's remove_item with no arguments decrements quantity by one.

Anyone can help please?
Reply
#2
What code have you got so far?
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
this is what i have tried
class ShoppingCart:

   items = {}
   def __init__(self, total = 0):
       self.total = total
   def add_item(self, item_name, quantity, price):
       self.item_name = item_name
       self.quantity = quantity
       self.price = price
       if not item_name in self.items:
           self.items[item_name] = quantity
       total = self.price*self.quantity + self.total
       self.items[self.item_name] = self.quantity
   def remove_item(self, item_name, quantity, price):
       self.item_name = item_name
       self.quantity = quantity
       self.price = price
       total = self.total - price*quantity
       if item_name in self.items:
           del self.items[item_name]
           return self.items
   def checkout(self, cash_paid):
       self.cash_paid = cash_paid
       return self.total - cash_paid
       if cash_paid < self.total:
           return "Cash paid not enough"
class Shop(ShoppingCart):
   def __init__(self):
       self.quantity = 100

   def remove_item(self, quantity):
       self.quantity -= quantity
       return self.quantity
Reply
#4
When you copy a code from some IDE it's done with all formatting. To paste it here use CTRL+SHIFT+v
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
this is what i come up with

class ShoppingCart:
    items = {}
    def __init__(self, total = 0):
        self.total = total
    def add_item(self, item_name, quantity, price):
        self.item_name = item_name
        self.quantity = quantity
        self.price = price
        if not item_name in self.items:
            self.items[item_name] = quantity
        total = self.price*self.quantity + self.total
        self.items[self.item_name] = self.quantity
    def remove_item(self, item_name, quantity, price):
        self.item_name = item_name
        self.quantity = quantity
        self.price = price
        total = self.total - price*quantity
        if item_name in self.items:
            del self.items[item_name]
            return self.items
    def checkout(self, cash_paid):
        self.cash_paid = cash_paid
        return self.total - cash_paid
        if cash_paid < self.total:
            return "Cash paid not enough"
class Shop(ShoppingCart):
    def __init__(self):
        self.quantity = 100

    def remove_item(self, quantity):
        self.quantity -= quantity
        return self.quantity
Reply
#6
(Feb-05-2017, 07:01 AM)wavic Wrote: When you copy a code from some IDE it's done with all formatting. To paste it here use CTRL+SHIFT+v

Within python tags
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
Do you have question?
To give something to think about below is extract from your code:

class ShoppingCart:

   items = {}
   def __init__(self, total = 0):
       self.total = total
   def add_item(self, item_name, quantity, price):
       self.item_name = item_name
       self.quantity = quantity
       self.price = price
       if not item_name in self.items:
           self.items[item_name] = quantity
       total = self.price*self.quantity + self.total
       self.items[self.item_name] = self.quantity

if __name__ =='__main__':
    john_cart = ShoppingCart()
    tom_cart = ShoppingCart()
    john_cart.add_item('milk', 2, 2.50)
    print "John's cart ->{}".format(john_cart.items)
    print "John's total ->{}".format(john_cart.total)
    print "Tom's cart ->{}".format(tom_cart.items)
Output:
John's cart ->{'milk': 2} John's total ->0 Tom's cart ->{'milk': 2}
Do you see the problem? Also note that your class constructor should not take any arguments as per the assignment.
Reply
#8
I see.
Can you assist please
Reply
#9
What's wrong with what you already have? What does it not do that it should, or what is it doing that it shouldn't?
Reply
#10
(Feb-05-2017, 10:35 AM)Shazily Wrote: Can you assist please
yes, but I will not do it for you.
1. regarding the items - you should understand the difference class attributes vs instance attributes. In your code items is class attribute and as such it is available to all instances of the class, incl. to change it. You should make it instance attribute.
2.regarding total - you should understand the difference between instance attributes and names local to the respective function/method (i.e. self.total vs. total). Also do you think you need to have item_name, quantity and price as instance attributes (i.e. they will change with every item you add to the car)?
3.Note that your assignment request that your class constructor need not to take any arguments. Yours may take total...
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020