Interfaces

Ronald Rex 181 Reputation points
2022-11-24T20:33:58.337+00:00

I was wanting to know is there anyone that is well versed in C# Interfaces and OOP design. Here is my problem. Say for instance I have a pizza ordering system and I have different menu items that a customer can order. For example, pizzas, drinks, salads, and desserts. I want to just start with this small piece of the problem and thats that we all know that pizzas, drinks and salads come in different sizes but if I could have a method that all of these items share a common interface with then if I had a method that I could pass a type IMenuItem into for example and it was able to return the price of every item regardless of whether it was a pizza, drink, salad, or dessert. Can someone help me with how this method that takes an Interface as a parameter should be approached, maybe a small code example. Thanks for any help offered.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,866 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,241 questions
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,406 Reputation points
    2022-11-24T22:10:33.443+00:00

    The "size" of a menu item relates more to a purchase rather than the menu item itself, so it makes sense to do the price calculation outside of the actual menu item. In fact you could just use each IMenuItem as a place to store the supported sizes (along with the prices) and calculate it in your business layer, or if you had an ItemPurchase that has two properties: Item (of type IMenuItem) and Size (of type ItemSize), then that calculation could just live inside there.

       Pizza pizza = new Pizza(new Dictionary<ItemSize, decimal>() {  
       	{ ItemSize.Small, 10 },  
       	{ ItemSize.Medium, 20 },  
       	{ ItemSize.Large, 30 },  
       });  
         
       PrintPrice(pizza, ItemSize.Medium);  
         
       decimal? GetItemPrice(IMenuItem item, ItemSize size) {  
       	if (!item.Prices.ContainsKey(size)) {  
       		return null;  
       	}  
         
       	return item.Prices[size];  
       }  
         
       void PrintPrice(IMenuItem item, ItemSize size) {  
       	decimal? price = GetItemPrice(item, size);  
         
       	if (price is null) {  
       		Console.WriteLine($"Can't buy this item in a {size}");  
       		return;  
       	}  
         
       	Console.WriteLine($"That'll cost {price}");  
       }  
         
       enum ItemSize {  
       	Small, Medium, Large  
       }  
         
       interface IMenuItem {  
       	IReadOnlyDictionary<ItemSize, decimal> Prices { get; }  
       }  
         
       class Pizza : IMenuItem {  
       	public Pizza(IReadOnlyDictionary<ItemSize, decimal> prices) {  
       		Prices = prices;  
       	}  
         
       	public IReadOnlyDictionary<ItemSize, decimal> Prices { get; }  
       }  
    

0 additional answers

Sort by: Most helpful