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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With