0

The question is actually derived from this link.

Suppose I have a problem like this:


A book-shop buys and sells two types of books: (1) Non-technical {Title, Author, Price} (2) Technical {Title, Author, Price, CD}

Also, customer gets a CD when he buys a Technical book. A CD object is defined as, CD {Title, Price}.

A Non-technical book’s price will be only the price of the book. A Technical book’s price will be the sum of the price of the book and the CD.

Create a C# program to show the following info:

Total number of book Bought & Price: XXX & XXX.XX
Total number of book Sold & Price: XXX & XXX.XX
Total Technical Book Sold & Price: XXX & XXX.XX
Total Non-technical Book sold & Price: XXX & XXX.XX

abstract class Publication
{
    public virtual string Title { get; set; }
    public virtual double Price { get; set; }
}

class CD : Publication
{
}

abstract class Book : Publication
{
    public virtual string Author { get; set; }
}

class TechnicalBook : Book
{
    public CD Cd { get; set; }
    public override double Price
    {
        get
        {
            return (base.Price + Cd.Price);
        }
    }
}

class NonTechnicalbook : Book
{
}

class Shop
{
    private IDictionary<string, Book> boughtDictionary;
    private IDictionary<string, Book> soldDictionary;

    public Shop()
    {
        boughtDictionary = new Dictionary<string, Book>();
        soldDictionary = new Dictionary<string, Book>();
    }

    public virtual void Buy(Book item)
    {
        boughtDictionary.Add(item.Title, item);
    }

    public virtual void Sell(string title)
    {
        Book book = boughtDictionary[title];
        boughtDictionary.Remove(book.Title);
        soldDictionary.Add(book.Title, book);
    }

    public virtual int GetBoughtBookCount()
    {
        return boughtDictionary.Count;
    }

    public virtual double GetBoughtBookPrice()
    {
        double price = 0.0;

        foreach (string title in boughtDictionary.Keys)
        {
            price = price + boughtDictionary[title].Price;
        }
    }

    public virtual int GetSoldBookCount()
    {
        return boughtDictionary.Count;
    }

    public virtual double GetSoldBookPrice()
    {
        double price = 0.0;

        foreach (string title in soldDictionary.Keys)
        {
            price = price + soldDictionary[title].Price;
        }
    }

    public virtual double GetTotalBookCount()
    {
        return this.GetBoughtBookCount() + this.GetSoldBookCount();
    }

    public virtual double GetTotalBookPrice()
    {
        return this.GetBoughtBookPrice() + this.GetSoldBookPrice();
    }

    public virtual void Show()
    {
        Console.WriteLine("Total number of books Bought & Price: ", this.GetTotalBookCount() + " & " + this.GetTotalBookPrice());
        Console.WriteLine("Total number of books Sold & Price: ", this.GetSoldBookCount() + " & " + this.GetSoldBookPrice());
    }
}

How to show the prices of Technical and Non-technical books while keeping the Open-Closed Principle?

Deriving Shop-class doesn't make any sense.

If I code like the following:

 if(book is TechnicalBook) {
    // ...
 } else if(book is NonTechnicalBook) {
    // ...
 }

I don't think it keeps OCP.

Then what to do?

Community
  • 1
  • 1
user366312
  • 16,949
  • 65
  • 235
  • 452

1 Answers1

0

you can have a method

getTotal(Option op)

where Option is a delagte that takes a book and returns true if it meets your criteria in your case if book is TechnicalBook or NonTechnicalBook.

the getTotal Method would go over all books summing only those that Option delegate returns true. and returning the sum.

hope this is what you meant.

james
  • 1,758
  • 1
  • 16
  • 26