diff --git a/shopApp.py b/shopApp.py index cc6af3363cbed7510f66776df01eea1108b14e87..127fad1d2a8f2feafd543a68da488415ea87901f 100644 --- a/shopApp.py +++ b/shopApp.py @@ -67,7 +67,7 @@ class ShoppingCart: print(f"Total paid: {total}") -# Interface Segregation Principle (ISP): Create small, specific interfaces for different features. +# Interface Segregation Principle (ISP): Small interfaces for location services. class LocationService(ABC): @abstractmethod def find_nearby_stores(self, user_location: str) -> List[Store]: @@ -83,7 +83,7 @@ class StoreLocator(LocationService): return [store for store in self.stores if store.location == user_location] -# Dependency Inversion Principle (DIP): High-level modules depend on abstractions (interfaces). +# Dependency Inversion Principle (DIP): High-level modules depend on abstractions. class LocationBasedShop: def __init__(self, user: User, store_locator: LocationService): self.user = user @@ -101,19 +101,66 @@ class LocationBasedShop: cart.checkout(payment_method) -# Example Usage -stores = [ - Store("1", "Store A", "Location1", {"Item1": 10.0, "Item2": 20.0}), - Store("2", "Store B", "Location2", {"Item1": 15.0, "Item3": 25.0}), -] +# Authentication Providers: Handle user authentication through SSO or cookies. +class AuthenticationProvider(ABC): + @abstractmethod + def authenticate(self, user_id: str) -> bool: + pass + + +class GoogleAuthProvider(AuthenticationProvider): + def authenticate(self, user_id: str) -> bool: + print(f"Authenticating user {user_id} via Google SSO...") + return True # Mock successful authentication + + +class FacebookAuthProvider(AuthenticationProvider): + def authenticate(self, user_id: str) -> bool: + print(f"Authenticating user {user_id} via Facebook SSO...") + return True # Mock successful authentication -user = User("1", "Alice", "Location1") -store_locator = StoreLocator(stores) -location_based_shop = LocationBasedShop(user, store_locator) -# Show nearby stores -location_based_shop.show_nearby_stores() +class CookieAuthProvider(AuthenticationProvider): + def authenticate(self, user_id: str) -> bool: + print(f"Authenticating user {user_id} via Cookies...") + return True # Mock successful authentication -# Shopping and checkout -store = stores[0] # Store A -location_based_shop.shop_at_store(store, "Item1", CreditCardPayment()) + +# Authentication Manager to handle authentication flow +class AuthManager: + def __init__(self, provider: AuthenticationProvider): + self.provider = provider + + def login(self, user: User): + if self.provider.authenticate(user.user_id): + print(f"User {user.name} authenticated successfully!") + else: + print(f"Authentication failed for user {user.name}.") + + +# Example Usage +if __name__ == "__main__": + # Initialize stores + stores = [ + Store("1", "Store A", "Location1", {"Item1": 10.0, "Item2": 20.0}), + Store("2", "Store B", "Location2", {"Item1": 15.0, "Item3": 25.0}), + ] + + # Create a user + user = User("1", "Alice", "Location1") + + # Authentication flow + google_auth = GoogleAuthProvider() + auth_manager = AuthManager(google_auth) + auth_manager.login(user) + + # Location and shopping flow + store_locator = StoreLocator(stores) + location_based_shop = LocationBasedShop(user, store_locator) + + # Show nearby stores + location_based_shop.show_nearby_stores() + + # Shopping and checkout + store = stores[0] # Store A + location_based_shop.shop_at_store(store, "Item1", CreditCardPayment())