I'm making from scratch the website of my agency, an online food order and delivery service.
I'm trying to improve at best the promotion system: every restaurant has its own promotions, and some of them has a certain priority above others.
For example, if the client orders three pizzas and for every ordered pizza there is a free can the site will suggest a big bottle instead of three little cans. There may be more types of promotions:
- if your order is higher than a threshold, you will get something for free (e.g. if order is more than 20$, you get one free beer);
- a certain product has something for free associated, even if you order only one of it (e.g. one free beer for every sausage and pepperoni pizza);
- if you order a generic product belonging to a precise category, you get something for free (e.g. every ordered pizza you get a beer for free, no matter the type of pizza you order);
- if you order more than one product, you get something for free (e.g. if you order more than one pizza or more than one kebab, you get something for free).
Some restaurants provide only one of the four promotions above, or a combination of them: some of them may conflit each other.
The old website was a mess, a pyramid of doom of nested if
and they want me to make it better and easier to handle (insertion and removal of promotions must be easy).
I'm trying to recall what university left me, and I thought about Deterministic finite automata (DFA): the grammar would be the rules of precedence in considering promotions, the tokens of alphabet would be the dishes and the free products, the language would be the set of possible promotions. But:
- every restaurant would have its set of promotions, i.e. its grammar, its DFA (it may be an overkill to develop a DFA for such a small set of free products);
- grammar design may be hard (some promotions might conflit with some others if a restaurant offers more types of promotions; furthermore, the precedence of some promotions over others may be hard to design with grammar rules and easier to describe with some "weight" concept).
What would be the more appropriate algorithm / data structure to use in these cases?