Homework Help Question & Answers

9.12 Ch 9 Program: The Evil Register Python 3.7 "The ItemToPurchase class If you haven't done so already, make s...

9.12 Ch 9 Program: The Evil Register Python 3.7

"The ItemToPurchase class

If you haven't done so already, make sure to complete zyLab 9.11 before this one. Otherwise, you will have to implement the ItemToPurchase class from scratch and this zyLab won't make much sense".

ItemToPurchase class from zyLab 9.11 refers to:

(1) Build the ItemToPurchase class with the following specifications:

  • Attributes (3 pts)
  • item_name (string)
  • item_price (int)
  • item_quantity (int)
  • Default constructor (1 pt)
  • Initializes item's name = "none", item's price = 0, item's quantity = 0
  • Method
  • print_item_cost()

9.12 Ch 9 Program: The Evil Register Background Think about a receipt that you have gotten from a recent shopping trip. (I juCustomizing the ItemToPurchase class Calculating the total price of an item is tedious, so implement a get_item_cost methodMethods 1. Create a default constructor that can take a customer name as an arqument, but if it gets no customer name, it wilThen the receipt should be printed in sorted order like we discussed earlier, but whether or not it starts with the highest c(Just be glad you dont have to handle yeah no.) Okay enough horsing around. (GET IT? Aggies?! Horsing!) Next, in the main(9.12: Ch 9 Program: The Evil Register Enter Customer Name: Nate Enter Todays Date: 12/20/2019 Are you evil? bwahahahaha yesAre you evil? bwahahahaha yes Enter the item name: Bottled Student Tears Enter the item price: 2 Enter the item quantity: 299

9.12 Ch 9 Program: The Evil Register Background Think about a receipt that you have gotten from a recent shopping trip. (I just know you all read your receipts and file them if they contain any warranty-bearing goods!) You'll notice that the order of the items on the receipt is usually just the order the items were scanned in. Target sorts the items by department. But, if you think about it, the line items on the receipt should be sorted in descending order of price That way, if the cashier or self-checkout system made a mistake, you would spot the biggest mistake first. On the other hand, if a store wanted to be evil they would put the least expensive items first, to remind you of all the good deals you got while forgetting about the costlier scanning mistakes at the register. The ItemToPurchase class If you haven't done so already, make sure to complete zyLab 9.11 before this one. Otherwise, you will have to implement the ItemToPurchase class from scratch and this zyLab won't make much sense. Customizing the ItemToPurchase class pe here to search
Customizing the ItemToPurchase class Calculating the "total price" of an item is tedious, so implement a get_item_cost method that just returns the quantity times the price for an item. By the way, the technical term for this kind of instance method is an accessor method, but you'll hear developers calling them getters because they always start with "get and they get some value from instance attributes. In order to make the items sortable by their total total price, we need to customize our class. Search the lectures slides for 'magic" to see how to do this. See Section 9.8 for an additional reference The Receipt class This will be the class that defines our Receipt type. Obviously, a receipt will consist of the Items on the receipt. This is called the composition design pattern. and it is very powerful. Instance Attributes: 1. customer_name It is very important to always know everything you can about your customers for "analytics", so you will keep track of a string customer name in objects of type Receipt. 2. date The legal team has required that you keep track of the dates that purchases happen for "legal reasons", so you will also keep track of the string date in objects of type Receipt. 3. cart items : This will be a list of the items in the cart and hence end up on the receipt Methods here to search
Methods 1. Create a default constructor that can take a customer name as an arqument, but if it gets no customer name, it will just put "Real Human for the customer_name attribute. It should also accept a date argument, but will just use the value "Today" for the date instance attribute if no date is given. The parameters should be named the same as the instance attributes to keep things simple. 2. add item Self-descriptive. Takes a parameter which we hope beyond hope is of type ItemToPurchase and adds it to the cart_items. Returns None. 3. print receipt Takes a single parameter isevil, with default value True. Returns a total cost of all the items on the receipt (remember to factor in the quantity). Prints the receipt based on the following specification: For example, if isevil is True, and customer_name and date are the default values: Welcome to EvilMart, Real Human Today Have an EVIL day! Otherwise, it should print: Welcome to GoodCo, Real Human Today Have a GOOD day! Then the receipt should be printed in sorted order like we discussed earlier, but whether or not it starts with the highest cost (think e here to search
Then the receipt should be printed in sorted order like we discussed earlier, but whether or not it starts with the highest cost (think reverse), depends on the value of isevil. If it is evil, then the lowest cost items should print first, but if it is good, then it will print the highest cost items first. (Cost meaning price*quantity). Remember to return the total cost regardless Your main ) function: The main flow of control of your program should go in a main () function or the program will fail all the unit tests. Get the name of the customer with the prompt: Enter Customer Name: Get the date with the prompt: Enter Today's Date Then, ask the question: Are you evil? Your program should consider the following as True: Yeah yup Let's face it: yes Hint: What do these strings all have in common? Your program should consider all the following as False: No nah perhaps but I'm leaning no (Just be glad you don't have to handle 'yeah no.") Okay enough horsing around. (GET IT? Aggies?! Horsing!) here to search
(Just be glad you don't have to handle "yeah no.") Okay enough horsing around. (GET IT? Aggies?! Horsing!) Next, in the main() function, you will have to create a Receipt object and start adding things into it using an input-while loop. The loop will prompt the user for the item name exactly as in the previous zyLab (9.11). But unlike the previous zyLab, the loop will terminate only if an empty string is entered for the item name. Then, the price and the quantity will be prompted for exactly as in the previous zyLab. Create the ItemToPurchase objects in the same manner as the previous zyLab, but don't forget to add them to the receipt using your add_item instance method. Then, the items on the receipt should be printed with the same formatting as in the previous zyLab, of course with either "good" or "evil" ordering However, on the last line, the total should be printed as follows: Total: $10 Where 10 is replaced by the actual total. Sample Run Here is what a sample run of the final program should look like: Enter Customer Name: Nate Enter Today's Date: ere to search
9.12: Ch 9 Program: The Evil Register Enter Customer Name: Nate Enter Today's Date: 12/20/2019 Are you evil? bwahahahaha yes Enter the item name: Bottled Student Tears Enter the item price: 2 Enter the item quantity: 299 Enter the item name: Salt Enter the item price: 2 Enter the item quantity: 1 Enter the item name Welcome to EvilMart, Nate 12/20/2019 Have an EVIL day! Salt 1 a s2 S2 Type here to search
Are you evil? bwahahahaha yes Enter the item name: Bottled Student Tears Enter the item price: 2 Enter the item quantity: 299 Enter the item name Salt Enter the item price: 2 Enter the item quantity: 1 Enter the item name: Welcome to EvilMart, Nate 12/20/2019 Have an EVIL day! Salt 1 $2 = $2 Bottled Student Tears [email protected] $2 = $598 Total: $600 Type here to search
0 0
Next > < Previous
ReportAnswer #1

Screenshot

dEvIReqsteinPython - Micresoft Visual Studio Ouck Launch (Ct) Prmjert RuildDehu Team Tocls deepthionganattu D File Feit Tart---------------------------------------------------

Program

from datetime import date
#Create a class for items
class Itemtopurchase:
    #Attributes
    item_name="none"
    item_price=0
    item_quantity=0
    #constructor
    def __init__(self,item_name,item_price,item_quantity):
        self.item_name=item_name
        self.item_price=item_price
        self.item_quantity=item_quantity
    #Method to print item details
    def print_item_cost(self):
        print(self.item_name+" "+str(self.item_quantity)+" @ $"+str(self.item_price)+" = $"+str(self.item_price*self.item_quantity))
    #Getters
    def get_item_price(self):
        return self.item_price
    def get_item_quantity(self):
        return self.item_quantity

#Create receipt class
class Receipt:
    #attributes
    customer_name=""
    purchase_date=""
    cart_items=[]
    #constructor
    def __init__(self,customer_name,purchase_date):
        if customer_name=="":
            self.customer_name="Real Human"
        else:
            self.customer_name=customer_name
        if purchase_date=="":
            self.purchase_date=str(date.today())
        else:
            self.purchase_date=purchase_date
    #add items
    def add_item(self,item):
        self.cart_items.append(item)
    #Print details
    def print_receipt(self,isevil):
        total=0
        if isevil==True:
            self.cart_items=sorted(self.cart_items, key=lambda cart:(cart.get_item_quantity()*cart.get_item_price()))
            print("Welcome to EvilMart, "+self.customer_name)
            print(self.purchase_date)
            print("Have an EVIL day!")
          
        else:
            self.cart_items=sorted(self.cart_items, key=lambda cart:(cart.get_item_quantity()*cart.get_item_price()),reverse=True)
            print("Welcome to GoodCo, "+self.customer_name)
            print(self.purchase_date)
            print("Have an GOOD day!")
          
        for x in self.cart_items:
                x.print_item_cost()
                total+=x.get_item_price()*x.get_item_quantity()
        print("Total: $"+str(total))

#Main method
def main():
    #Promt for customer name and date
    customer_name=input("Enter Customer Name:\n")
    purchase_date=input("Enter Today's Date: \n")
    #Evil prompt
    isEvil=input("Are you evil?\n")
    isEvil=isEvil.upper()
    evilCheck=False
    if("YEAH" in isEvil or "YES" in isEvil or "YUP" in isEvil):
        evilCheck=True
        #Receipt object
    receipt=Receipt(customer_name,purchase_date)
    #Loop unti user stop purchase
    while True:
        item_name=input("Enter the item name: \n")
        if item_name!="":
            item_price=int(input("Enter the item price: \n"))
            item_quantity=int(input("enter the item quantity: \n"))
            receipt.add_item(Itemtopurchase(item_name,item_price,item_quantity))
        else:
          print()
          receipt.print_receipt(evilCheck)
          break
#star
main()

---------------------------------------------------------------------

output

Enter Customer Name:
Nate
Enter Today's Date:

Are you evil?
bwahahah yes
Enter the item name:
Bottles Children Tears
Enter the item price:
2
enter the item quantity:
299
Enter the item name:
Salt
Enter the item price:
2
enter the item quantity:
1
Enter the item name:


Welcome to EvilMart, Nate
2019-06-02
Have an EVIL day!
Salt 1 @ $2 = $2
Bottles Children Tears 299 @ $2 = $598
Total: $600

-------------------------------------------------

Program Screenshot

from datetire irport date Create a class for itens #add items def add_item(self,item) class Itertopurchase: tems.append(item)

Add Homework Help Answer
Add Answer of:
9.12 Ch 9 Program: The Evil Register Python 3.7 "The ItemToPurchase class If you haven't done so already, make s...
Your Answer: Your Name: What's your source?
Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
More Homework Help Questions Additional questions in this topic.
Need Online Homework Help?
Ask a Question
Get FREE Expert Answers
WITHIN MINUTES
Related Questions