skb461

CardDelarship

Mar 17th, 2024 (edited)
798
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.08 KB | Source Code | 0 0
  1. from abc import ABC, abstractmethod
  2.  
  3. class Card(ABC):
  4.     @abstractmethod
  5.     def get_info(self):
  6.         pass
  7.  
  8. class Type(ABC):
  9.     @abstractmethod
  10.     def get_type(self):
  11.         pass
  12.  
  13. class Style(ABC):
  14.     @abstractmethod
  15.     def get_style(self):
  16.         pass
  17.  
  18. class Size(ABC):
  19.     @abstractmethod
  20.     def get_size(self):
  21.         pass
  22.  
  23. class Cost(ABC):
  24.     @abstractmethod
  25.     def get_cost(self, area):
  26.         pass
  27.  
  28. class BusinessCard(Card):
  29.     def get_info(self):
  30.         return "Business Card"
  31.  
  32. class InvitationCard(Card):
  33.     def get_info(self):
  34.         return "Invitation Card"
  35.  
  36. class MarriageCard(Card):
  37.     def get_info(self):
  38.         return "Marriage Card"
  39.  
  40. class Affordable(Type):
  41.     def get_type(self):
  42.         return "Affordable"
  43.  
  44. class Premium(Type):
  45.     def get_type(self):
  46.         return "Premium"
  47.  
  48. class Color(Style):
  49.     def __init__(self, color):
  50.         self.color = color
  51.  
  52.     def get_style(self):
  53.         return f"Color: {self.color}"
  54.  
  55. class Design(Style):
  56.     def __init__(self, design):
  57.         self.design = design
  58.  
  59.     def get_style(self):
  60.         return f"Design: {self.design}"
  61.  
  62. class SizeSpecification(Size):
  63.     def __init__(self, height, width):
  64.         self.height = height
  65.         self.width = width
  66.  
  67.     def get_size(self):
  68.         area = self.height * self.width
  69.         return f"Size: {area} sq. cm"
  70.  
  71. class CardFactory(ABC):
  72.     @abstractmethod
  73.     def create_card(self):
  74.         pass
  75.  
  76. class AffordableCardFactory(CardFactory):
  77.     def create_card(self):
  78.         card_type = input("Enter card type (1. Business Card, 2. Invitation Card, 3. Marriage Card): ")
  79.         color = input("Enter card color: ")
  80.         design = input("Enter card design: ")
  81.         height = float(input("Enter card height (in cm): "))
  82.         width = float(input("Enter card width (in cm): "))
  83.  
  84.         if card_type == "1":
  85.             card = BusinessCard()
  86.         elif card_type == "2":
  87.             card = InvitationCard()
  88.         elif card_type == "3":
  89.             card = MarriageCard()
  90.         else:
  91.             raise ValueError("Invalid card type")
  92.  
  93.         return Affordable(), Color(color), Design(design), card, SizeSpecification(height, width)
  94.  
  95. class PremiumCardFactory(CardFactory):
  96.     def create_card(self):
  97.         card_type = input("Enter card type (1. Business Card, 2. Invitation Card, 3. Marriage Card): ")
  98.         color = input("Enter card color: ")
  99.         design = input("Enter card design: ")
  100.         height = float(input("Enter card height (in cm): "))
  101.         width = float(input("Enter card width (in cm): "))
  102.  
  103.         if card_type == "1":
  104.             card = BusinessCard()
  105.         elif card_type == "2":
  106.             card = InvitationCard()
  107.         elif card_type == "3":
  108.             card = MarriageCard()
  109.         else:
  110.             raise ValueError("Invalid card type")
  111.  
  112.         return Affordable(), Color(color), Design(design), card, SizeSpecification(height, width)
  113.  
  114. class CardClient:
  115.     def __init__(self, factory):
  116.         self.type, self.style1, self.style2, self.card, self.size = factory.create_card()
  117.  
  118.     def show_card_info(self):
  119.         print("Card Type:", self.type.get_type())
  120.         print(self.style1.get_style())
  121.         print(self.style2.get_style())
  122.         print("Card Name:", self.card.get_info())
  123.         print(self.size.get_size())
  124.         area = self.size.height * self.size.width
  125.         cost_factory = PremiumCostFactory() if isinstance(self.type, Premium) else AffordableCostFactory()
  126.         cost_calculator = CostCalculator(cost_factory)
  127.         cost = cost_calculator.calculate_cost(area)
  128.         print("Cost: $", cost)
  129.  
  130. class CostFactory(ABC):
  131.     @abstractmethod
  132.     def create_cost(self):
  133.         pass
  134.  
  135. class PremiumCostFactory(CostFactory):
  136.     def create_cost(self):
  137.         return PremiumCost()
  138.  
  139. class AffordableCostFactory(CostFactory):
  140.     def create_cost(self):
  141.         return AffordableCost()
  142.  
  143. class CostCalculator:
  144.     def __init__(self, cost_factory):
  145.         self.cost = cost_factory.create_cost()
  146.  
  147.     def calculate_cost(self, area):
  148.         return self.cost.get_cost(area)
  149.  
  150. class PremiumCost(Cost):
  151.     def get_cost(self, area):
  152.         return area * 5  # $5 per sq. cm for Premium cards
  153.  
  154. class AffordableCost(Cost):
  155.     def get_cost(self, area):
  156.         return area * 1.5  # $1.5 per sq. cm for Affordable cards
  157.  
  158. def main():
  159.  
  160.     print("1. Affordable Card")
  161.     print("2. Premium Card")
  162.     print("3. Exit")
  163.     choice = input("Enter your choice (1-3): ")
  164.     if choice == "1":
  165.         factory1 = AffordableCardFactory()
  166.         client1 = CardClient(factory1)
  167.         print()
  168.         print("=== Affordable Card Information ===")
  169.         client1.show_card_info()
  170.     elif choice == "2":
  171.         factory2 = PremiumCardFactory()
  172.         client2 = CardClient(factory2)
  173.         print()
  174.         print("=== Premium Card Information ===")
  175.         client2.show_card_info()
  176.     elif choice == "3":
  177.         print("Exiting. Thank you!")
  178.     else:
  179.         print("Invalid choice. Please enter a valid option.")
  180.  
  181. if __name__ == "__main__":
  182.     main()
  183.  
Advertisement
Add Comment
Please, Sign In to add comment