Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:11:52+00:00 2026-06-15T13:11:52+00:00

I’m building a book library app, I have a abstract book Class, two types

  • 0

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.

    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<ReadingBooksGenre> Genres { get; set; }
    }

    abstract public class TextBook : Book
    {   
        public List<TextBooksGenre> Genres { get; set; }
    }

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:

    Dictionary<ReadingBooksGenre, int> _readingBooksDiscounts;
    Dictionary<TextBooksGenre, int> _textBooksDiscounts;

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:

    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) && _readingBooksDiscounts[genere]>maxDiscount)
                {
                    maxDiscount = _readingBooksDiscounts[genere];
                }
            }
        }
        else if (b is TextBook)
        {
            foreach (var genre in (b as TextBook).Genres)
            {
                if (_textBooksDiscounts.ContainsKey(genre) && _textBooksDiscounts[genere]>maxDiscount)
                {
                    maxDiscount = _textBooksDiscounts[genere];
                }
            }
        }
        return maxDiscount;
    }

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?

will be glad to hear any suggestions for improvement.

(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)

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:11:53+00:00Added an answer on June 15, 2026 at 1:11 pm

    Your GetDiscount method is classic example of Open/Closed principle violation. When you add new book type you have to add new if block to GetDiscount.

    Better way is to use some existing techinques which allow you to add new functionality without necessity to modify existing code. For example, Composite pattern. I’ll write some draft implementation of composite discount evaluator. You can add new discount evaluators based on any book proerties (date, price, etc.) easily.

    Also, I’ll use interfaces instead of inheritance. Inheritance is a very strong link between two entities and in this case it is excessive.

    Listing is 167 lines long, so here is more comfort pastebin copy

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main()
            {
                var compositeDiscountEvaluator = ConfigureEvaluator();
                var scienceBook = new TextBook
                                   {
                                       Date = DateTime.Now,
                                       Price = 100,
                                       Genres = new[] {TextBooksGenre.Math}
                                   };
                var textBook = new TextBook
                                   {
                                       Date = DateTime.Now,
                                       Price = 100,
                                       Genres = new[] {TextBooksGenre.Math, TextBooksGenre.Science}
                                   };
                var fictionBook = new ReadingBook
                            {
                                Date = DateTime.Now,
                                Price = 200,
                                Genres = new[] {ReadingBooksGenre.Fiction}
                            };
                var readingBook = new ReadingBook
                                      {
                                          Date = DateTime.Now,
                                          Price = 300,
                                          Genres = new[] {ReadingBooksGenre.Fiction, ReadingBooksGenre.NonFiction}
                                      };
                Console.WriteLine(compositeDiscountEvaluator.GetDiscount(scienceBook));
                Console.WriteLine(compositeDiscountEvaluator.GetDiscount(textBook));
                Console.WriteLine(compositeDiscountEvaluator.GetDiscount(fictionBook));
                Console.WriteLine(compositeDiscountEvaluator.GetDiscount(readingBook));
            }
    
            private static IDiscountEvaluator ConfigureEvaluator()
            {
                var evaluator = new CompositeDiscountEvaluator();
                evaluator.AddEvaluator(new ReadingBookDiscountEvaluator());
                evaluator.AddEvaluator(new TextBookDiscountEvaluator());
                return evaluator;
            }
        }
    
        class CompositeDiscountEvaluator : IDiscountEvaluator
        {
            private readonly ICollection<IDiscountEvaluator> evaluators;
    
            public CompositeDiscountEvaluator()
            {
                evaluators = new List<IDiscountEvaluator>();
            }
    
            public void AddEvaluator(IDiscountEvaluator evaluator)
            {
                evaluators.Add(evaluator);
            }
    
            public bool CanEvaluate<TGenre>(IBook<TGenre> book)
            {
                return evaluators.Any(e => e.CanEvaluate(book));
            }
    
            public int GetDiscount<TGenre>(IBook<TGenre> book)
            {
                if (!CanEvaluate(book))
                    throw new ArgumentException("No suitable evaluator");
                return evaluators.Where(e => e.CanEvaluate(book)).Select(e => e.GetDiscount(book)).Max();
            }
        }
    
        interface IDiscountEvaluator
        {
            bool CanEvaluate<TGenre>(IBook<TGenre> book);
            int GetDiscount<TGenre>(IBook<TGenre> book);
        }
    
        class ReadingBookDiscountEvaluator : IDiscountEvaluator
        {
            private readonly IDictionary<ReadingBooksGenre, int> discounts;
    
            public ReadingBookDiscountEvaluator()
            {
                discounts = new Dictionary<ReadingBooksGenre, int>
                                {
                                    {ReadingBooksGenre.Fiction, 3},
                                    {ReadingBooksGenre.NonFiction, 4}
                                };
            }
    
            public bool CanEvaluate<TGenre>(IBook<TGenre> book)
            {
                return book is ReadingBook;
            }
    
            public int GetDiscount<TGenre>(IBook<TGenre> book)
            {
                var readingBook = (ReadingBook) book;
                return readingBook.Genres.Select(g => discounts[g]).Max();
            }
        }
    
        class TextBookDiscountEvaluator : IDiscountEvaluator
        {
            private readonly IDictionary<TextBooksGenre, int> discounts;
    
            public TextBookDiscountEvaluator()
            {
                discounts = new Dictionary<TextBooksGenre, int>
                                {
                                    {TextBooksGenre.Math, 1},
                                    {TextBooksGenre.Science, 2}
                                };
            }
    
            public bool CanEvaluate<TGenre>(IBook<TGenre> book)
            {
                return book is TextBook;
            }
    
            public int GetDiscount<TGenre>(IBook<TGenre> book)
            {
                var textBook = (TextBook) book;
                return textBook.Genres.Select(g => discounts[g]).Max();
            }
        }
    
        interface IBook<TGenre>
        {
            int Price { get; set; }
            DateTime Date { get; set; }
            TGenre[] Genres { get; set; }
        }
    
        class ReadingBook : IBook<ReadingBooksGenre>
        {
            public int Price { get; set; }
            public DateTime Date { get; set; }
            public ReadingBooksGenre[] Genres { get; set; }
        }
    
        class TextBook : IBook<TextBooksGenre>
        {
            public int Price { get; set; }
            public DateTime Date { get; set; }
            public TextBooksGenre[] Genres { get; set; }
        }
    
        enum TextBooksGenre
        {
            Math,
            Science
        }
    
        public enum ReadingBooksGenre
        {
            Fiction,
            NonFiction
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.