Question

## create a class Fruit (attributes: self, fname, fprice) ## create a class Customer (attributes: self,...

## create a class Fruit (attributes: self, fname, fprice)

## create a class Customer (attributes: self, cname)

## create a class ShopCart(Customer) that inherits class Customer

## also, class ShopCart has attribute: self, customerName

## in class ShopCart, create methods such as:

## addItem(self, item, quantity)

## getQ(self, item), returns self.quantity

## balance(self), calculates the total purchased

## Still in ShopCart class, you should create these variables:

## total_purchase = 0

## items = []

## quantity = []

## Main program:

customerList = ["John", "Mary", "Bob", "Alice", "Joe", "Taylor"]

fruitList = ["apple, 1.00", "kiwi, 0.50", "grape, 1.99", "mango, 1.50", "strawberry, 0.75"]

Situation:

  1. John purchases 5 apples, 3 lbs of grapes, 2 mangos
  2. Mary purchases 2 apples, 10 mangos, 20 strawberries
  3. Alice purchases 10 kiwis, 2 lbs of grapes

The program should print:

  1.     The total for John is $xx.xx
  2.     The total for Mary is $xx.xx
  3.     The total for Alice is $xx.xx
0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Fruit:
    """
    Fruit class
    """
    def __init__(self, fname, fprice):
        self.fname = fname
        self.fprice = float(fprice)

    # getters ans setters
    def get_name(self):
        return self.fname

    def get_price(self):
        return self.fprice


# customer class
class Customer:
    def __init__(self, cname):
        self.cname = cname

    def get_name(self):
        return self.cname


class ShopCart(Customer):
    def __init__(self, customerName):
        super().__init__(customerName)
        self.customerName = customerName
        self.total_purchase = 0
        self.items = []
        self.quantity = []

    def addItem(self, item, quantity):
        self.items.append(item)
        self.quantity.append(quantity)

    def getQ(self, item):
        ret = 0
        for c, i in enumerate(self.items):
            if i.get_name() == item.get_name():
                ret = self.quantity[c]
                break
        return ret

    def balance(self):
        total = 0
        for c, i in enumerate(self.items):
            total += self.quantity[c] * i.get_price()
        self.total_purchase = total
        return total


def main():
    customerList = ["John", "Mary", "Bob", "Alice", "Joe", "Taylor"]
    fruitList = ["apple, 1.00", "kiwi, 0.50", "grape, 1.99", "mango, 1.50", "strawberry, 0.75"]
    # creating fruits object
    fruit_obj = [Fruit(*x.split(',')) for x in fruitList]
    print(fruit_obj)
    cart1 = ShopCart(customerList[0])
    cart1.addItem(fruit_obj[0], 5)
    cart1.addItem(fruit_obj[2], 3)
    cart1.addItem(fruit_obj[3], 2)
    # create another object and try
    print(' The total for John is {}'.format( cart1.balance()))

if __name__ == '__main__':
    main()

#OUT

The total for John is 13.969999999999999

Add a comment
Know the answer?
Add Answer to:
## create a class Fruit (attributes: self, fname, fprice) ## create a class Customer (attributes: self,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Create a Governor class with the following attributes: name : String party - char (the...

    JAVA Create a Governor class with the following attributes: name : String party - char (the character will be either D, R or I) ageWhenElected : int Create a State class with the following attributes: name : String abbreviation : String population : long governor : Governor Your classes will have all setters, getters, typical methods to this point (equals(), toString()) and at least 3 constructors (1 must be the default, 1 must be the copy). You will be turning...

  • This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).

    Ch 7 Program: Online shopping cart (continued) (Java)This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).(1) Extend the ItemToPurchase class per the following specifications:Private fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)Public member methodssetDescription() mutator & getDescription() accessor (2 pts)printItemCost() - Outputs the item name followed by the quantity, price, and subtotalprintItemDescription() - Outputs the...

  • 8.7 LAB*: Program: Online shopping cart (Part 2)

    8.7 LAB*: Program: Online shopping cart (Part 2)Note: Creating multiple Scanner objects for the same input stream yields unexpected behavior. Thus, good practice is to use a single Scanner object for reading input from System.in. That Scanner object can be passed as an argument to any methods that read input.This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).(1) Extend the ItemToPurchase class per the following specifications:Private fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized...

  • Zybooks 11.12 LAB*: Program: Online shopping cart (continued) Python 3 is the code needed and this...

    Zybooks 11.12 LAB*: Program: Online shopping cart (continued) Python 3 is the code needed and this is in Zybooks Existing Code # Type code for classes here class ItemToPurchase: def __init__(self, item_name="none", item_price=0, item_quantity=0): self.item_name = item_name self.item_price = item_price self.item_quantity = item_quantity # def __mul__(self): # print_item_cost = (self.item_quantity * self.item_price) # return '{} {} @ ${} = ${}' .format(self_item_name, self.item_quantity, self.item_price, print_item_cost) def print_item_cost(self): self.print_cost = (self.item_quantity * self.item_price) print(('{} {} @ ${} = ${}') .format(self.item_name, self.item_quantity, self.item_price,...

  • 4.18 Ch 7 Program: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping...

    4.18 Ch 7 Program: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping cart" program. (solution from previous lab assignment is provided in Canvas). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by the quantity, price, and subtotal PrintItemDescription() -...

  • ​I have to create two classes one class that accepts an object, stores the object in...

    ​I have to create two classes one class that accepts an object, stores the object in an array. I have another that has a constructor that creates an object. Here is my first class named "ShoppingList": import java.util.*; public class ShoppingList {    private ShoppingItem [] list;    private int amtItems = 0;       public ShoppingList()    {       list=new ShoppingItem[8];    }       public void add(ShoppingItem item)    {       if(amtItems<8)       {          list[amtItems] = ShoppingItem(item);...

  • I need help with this assignment, can someone HELP ? This is the assignment: Online shopping...

    I need help with this assignment, can someone HELP ? This is the assignment: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT