Python - 2 Questions | 40 minutes

 

INSTRUCTION:  Don't look at the answer until you have tried your best to solve the problem. The solution provided here is in case you are stuck in a situation, and you are extremely helpless to solve it out. Hope you will crack the logic. So let's start solving the problem.



TOLLPLAZA

TollPlaza

A toll booth on the way to Bangalore wants to keep the track of the number of vehicles passed through it and total amount collected by them.
Write a python program to implement the class diagram given below.

Class description:
Constructor: Initialize both the instance variables, no_of_vehicle, total_amount to 0

  1. count_vehicle(): Increment total number of vehicle by 1
  2. calculate_amount(vehicle_type): Accept vehicle type and identify toll amount for that vehicle based on details given in the table. Add it to the total_amount instance variable.
  3. collect_toll(owner_type,vehicle_type): Accept owner type and vehicle type of the vehicle for which toll should be collected.

 

Conditions :

If the owner of the vehicle is a "VIP", then toll amount need not be collected but number of vehicles should be updated.
For any other type of owner, calculate the toll amount and update the number of vehicles.
Perform case insensitive string comparison.

Vehicle TypeAmount
Car70
Bus100
Truck150
Any other type of vehicle70

 

(Hint: Invoke appropriate methods to complete the functionality)

Create an object of Tollbooth class, invoke collect_toll() method for different vehicles and test your program.

 

Note :

Refer Sample Input Output for displaying format

[All text in bold corresponds to the input and the rest corresponds to the output]

 

Sample Input and Output 1:

Enter the vehicle count :
2
Enter owner type :
VIP
Enter vehicle type :
Car
Enter owner type :
Normal
Enter vehicle type :
Truck
Total No of Vehicle 2
Total amount 150

 

Sample Input and Output 2:

Enter the vehicle count :
3
Enter owner type :
VIP
Enter vehicle type :
Truck
Enter owner type :
Normal
Enter vehicle type :
Bike
Enter owner type :
Premium
Enter vehicle type :
Bus
Total No of Vehicle 3
Total amount 170




Solution:


Main.py

from TollPlaza import TollPlaza


class Main:
    tb = TollPlaza()
    count = int(input("Enter the vehicle count :\n"))
    for i in range(0, count):
        owner_type = input("Enter owner type :\n")
        vehicle_type = input("Enter vehicle type :\n")
        tb.collect_toll(owner_type, vehicle_type)
    print("Total No of Vehicle", tb.get_no_of_vehicle())
    print("Total amount", tb.get_total_amount())



TollPlaza.py


class TollPlaza:

    def __init__(self):
        self.__total_amount = 0
        self.__no_of_vehicle = 0

    def calculate_amount(self, vehicle_type):
        #Fill your code
        return 0

    def count_vehicle(self):
        self.__no_of_vehicle+=1
        #Fill your code

    def collect_toll(self, owner_type, vehicle_type):
        #Fill your code
        self.count_vehicle()
        if(owner_type!="VIP"):
            if(vehicle_type=="Car"):self.__total_amount+=70
            elif(vehicle_type=="Bus"):self.__total_amount+=100
            elif(vehicle_type=="Truck"):self.__total_amount+=150
            else:self.__total_amount+=70

    def get_no_of_vehicle(self):
        #Fill your code
        return self.__no_of_vehicle

    def get_total_amount(self):
        #Fill your code
        return self.__total_amount





2.  

MOBILE ACCESSORIES

Mobile Accessories

The Chennai Mobiles wants to automate its order taking and delivery process of selling mobile accessories.

Write a python program to implement the class diagram given below.

Class Description :
1. mobile_accessory_dict : 
Static dictionary which stores accessory type (key) and a list (value) which contains the number of items available, price per item and number of days it would take to deliver it.
Initialize it using the sample data given:

{"Case": [750, 200, 5], "Charger": [250, 500, 25], "Headphone": [500, 750, 10], "Powerbank": [150, 800, 30]}

2. Constructor:
 Initialize attribute, order_date to current date (24/05/2019)
Note :  Consider current date is 24/05/2019. Calculate delivery date based on 24/05/2019.

3.calculate_amount(): Calculate and return the total amount to be paid based on the item type and number of items ordered

4. update_stock(): Update stock based on details given below.

  • Set the attribute, delivery_date to order_date + number of days required to deliver the ordered item
  • Update the number of items available for the ordered item type based on number of items ordered
5. take_order(): Take the order, calculate amount and update stock based on rules given below.
  • If the ordered item type and quantity (no_of_item) are available based on details in mobile_accessory_dic
Calculate amount
Update item stock
Return the calculated amount
  • Else, return -1

Conditions :

Perform case sensitive string comparison.
If the order item type and quantity is available, display the cost and the delivery date (as given in the sample input and output 1)
If the order item type and quantity is not available, display "Sorry we are out of stock"

(Hint: Invoke appropriate methods to complete the functionality)
Create an object of MobileAccessories class, invoke take_order() method on the MobileAccessories object, display the details and test your program.

Note :

Refer Sample Input Output for displaying format

[All text in bold corresponds to the input and the rest corresponds to the output]

 

Sample Input and Output 1:

{'Case': [750, 200, 5], 'Charger': [250, 500, 25], 'Powerbank': [150, 800, 30], 'Headphone': [500, 750, 10]}
What do you like to buy :
-Case
-Charger
-Headphone
-Powerbank
Case
Number of Case(s) required :
15
Your order cost Rs.3000 will be delivered by 29/05/2019
Updated Stock Details
{'Powerbank': [150, 800, 30], 'Case': [735, 200, 5], 'Headphone': [500, 750, 10], 'Charger': [250, 500, 25]}

Sample Input and Output 2:

{'Case': [750, 200, 5], 'Charger': [250, 500, 25], 'Powerbank': [150, 800, 30], 'Headphone': [500, 750, 10]}
What do you like to buy :
-Case
-Charger
-Headphone
-Powerbank
Headphone
Number of Headphone(s) required :
1000
Sorry we are out of stock



Solution:

from MobileAccessories import MobileAccessories
print(MobileAccessories.mobile_accessory_dict)
print("What do you like to buy :")
print("-Case")
print("-Charger")
print("-Headphone")
print("-Powerbank")
item_name = input()
item_req = int(input("Number of " + item_name + "(s) required :\n"))
ma = MobileAccessories(item_name, item_req, 0)
details = ma.take_order()
if details != -1:
    print("Your order cost Rs." + details.split(",")[0] + " will be delivered by " + details.split(",")[1])
    print("Updated Stock Details")
    print(MobileAccessories.mobile_accessory_dict)
else:
    print("Sorry we are out of stock")


MobileAccessories.py

import time, datetime
from datetime import timedelta, date


class MobileAccessories:
    mobile_accessory_dict = {"Case": [750, 200, 5], "Charger": [250, 500, 25], "Powerbank": [150, 800, 30],
                             "Headphone": [500, 750, 10]}

    def __init__(self, accessory_type, no_of_item, delivery_date):
        self.__accessory_type = accessory_type
        self.__no_of_item = no_of_item
        self.__delivery_date = delivery_date
        self.__order_date = datetime.datetime(2019, 5, 24)

    def get_accessory_type(self):
        pass

    def get_no_of_item(self):
        # Fill your code
        pass

    def get_delivery_date(self):
        self.__delivery_date = self.__order_date + datetime.timedelta(
            days=self.mobile_accessory_dict[self.__accessory_type][2])
        self.__delivery_date = self.__delivery_date.strftime('%d/%m/%Y')

    def get_order_date(self):
        # Fill your code
        pass

    def take_order(self):
        if (self.mobile_accessory_dict[self.__accessory_type][0] < self.__no_of_item):
            return -1
        else:
            amt = self.calculate_amount()
            self.get_delivery_date()
            return str(amt) + ',' + str(self.__delivery_date)
        # Fill your code

    def calculate_amount(self):
        self.update_stock()
        return self.mobile_accessory_dict[self.__accessory_type][1] * self.__no_of_item

    def update_stock(self):
        self.mobile_accessory_dict[self.__accessory_type][0] = self.mobile_accessory_dict[self.__accessory_type][
                                                                   0] - self.__no_of_item


Comments

Popular posts from this blog

2-D Arrays

String

Conditional Statements