Note that there are some explanatory texts on larger screens.

plurals
  1. POselecting correct list of Enums from 2 derived classes
    primarykey
    data
    text
    <p>I'm building a book library app, I have a abstract book Class, two types of derived books and two Enums that will save the genre of the book. Each Book can be related to one genre or more.</p> <pre><code> abstract public class Book { public int Price { get; set; } ... } public enum ReadingBooksGenre { Fiction, NonFiction } public enum TextBooksGenre { Math, Science } abstract public class ReadingBook : Book { public List&lt;ReadingBooksGenre&gt; Genres { get; set; } } abstract public class TextBook : Book { public List&lt;TextBooksGenre&gt; Genres { get; set; } } </code></pre> <p>Now i want to save the discounts based on the book genres (no double discounts, only the highest discount is calculated), so i'm thinking about making two dictionaries that will save all the discounts for each genre, like this:</p> <pre><code> Dictionary&lt;ReadingBooksGenre, int&gt; _readingBooksDiscounts; Dictionary&lt;TextBooksGenre, int&gt; _textBooksDiscounts; </code></pre> <p>So now i need to check the genre of each book in order to find the highest discount, is there any better way to do it than:</p> <pre><code> private int GetDiscount(Book b) { int maxDiscount = 0; if (b is ReadingBook) { foreach (var genre in (b as ReadingBook).Genres) { // checking if the genre is in discount, and if its bigger than other discounts. if (_readingBooksDiscounts.ContainsKey(genre) &amp;&amp; _readingBooksDiscounts[genere]&gt;maxDiscount) { maxDiscount = _readingBooksDiscounts[genere]; } } } else if (b is TextBook) { foreach (var genre in (b as TextBook).Genres) { if (_textBooksDiscounts.ContainsKey(genre) &amp;&amp; _textBooksDiscounts[genere]&gt;maxDiscount) { maxDiscount = _textBooksDiscounts[genere]; } } } return maxDiscount; } </code></pre> <p>is there a way to select the correct dictionary without checking for the type? or maybe even a way to do it without the dictionaries, or using one? maybe somehow connect book type with the Enum?</p> <p>will be glad to hear any suggestions for improvement.</p> <p>(there are a lot of more discounts based on books name, date and author. Even some more book types that is why this way doesn't seem right to me)</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload