0% found this document useful (0 votes)
2 views

Project - Colab

The document outlines a Python program that implements a menu system using object-oriented principles, including abstract classes and polymorphism. It features a base class for menu items, subclasses for food and drink items, and an order class that calculates the total bill. The program also includes exception handling for file operations and demonstrates the creation and preparation of menu items.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Project - Colab

The document outlines a Python program that implements a menu system using object-oriented principles, including abstract classes and polymorphism. It features a base class for menu items, subclasses for food and drink items, and an order class that calculates the total bill. The program also includes exception handling for file operations and demonstrates the creation and preparation of menu items.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

from abc import ABC, abstractmethod

# Base class MenuItem with Inheritance


class MenuItem(ABC):
def __init__(self, name, price):
self.name = name
self.price = price

@abstractmethod
def prepare(self):
pass

# Subclasses for FoodItem and DrinkItem


class FoodItem(MenuItem):
def prepare(self):
return f"Preparing food item: {self.name}, which costs {self.price}."

class DrinkItem(MenuItem):
def prepare(self):
return f"Preparing drink item: {self.name}, which costs {self.price}."

# Abstract class Order with methods for adding items and generating bills
class Order(ABC):
def __init__(self):
self.items = []

def add_item(self, item):


self.items.append(item)

@abstractmethod
def generate_bill(self):
pass

# Concrete Order class implementing the generate_bill method


class RestaurantOrder(Order):
def generate_bill(self):
total = 0
for item in self.items:
total += item.price
return f"Total bill amount: {total}."

# Polymorphism in action
def prepare_item(item: MenuItem):
return item.prepare()

# Exception Handling for unavailable items and invalid order operations


class OrderException(Exception):
pass

# Handling file operations


def save_order_to_file(order, filename):
try:
with open(filename, 'w') as file:
for item in order.items:
file.write(f"{item.name} - {item.price}\n")
file.write(order.generate_bill())
except Exception as e:
raise OrderException(f"Failed to save order to file: {e}")

# Main program execution


def main():
# Creating food and drink items
burger = FoodItem("Burger", 8.99)
cola = DrinkItem("Cola", 2.99)

# Creating an order and adding items to it


order = RestaurantOrder()
order.add_item(burger)
order.add_item(cola)

# Display preparation process using polymorphism


print(prepare_item(burger))
print(prepare_item(cola))

# Generating and displaying the bill


print(order.generate_bill())

# Saving order to a file


try:
save_order_to_file(order, "order.txt")
print("Order saved to file successfully.")
except OrderException as e:
print(e)

if __name__ == "__main__":
main()

Preparing food item: Burger, which costs 8.99.


Preparing drink item: Cola, which costs 2.99.
Total bill amount: 11.98.
Order saved to file successfully.

You might also like