Upcoming SlideShare
×

# Week 1 Assignment Q2 Solution

572 views

Published on

Published in: Education, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
572
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
42
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Week 1 Assignment Q2 Solution

1. 1. BookStore.CSusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;namespace ConsoleApplication1{ public delegate void EditionHandler(); public class Book { public string Title; // Title of the book. public string Author; // Author of the book. public string BookType; //Type of Book public decimal Price; // Price of the book. public bool Paperback; // Is it paperback? public EditionHandler AddEdition; public Book(string title, string author, decimal price, boolpaperBack) { Title = title; Author = author; Price = price; Paperback = paperBack; } public void ShowEditions() { if (AddEdition != null) AddEdition(); } } public delegate void PrintBook(Book book); public class BookDB { // List of all books in the database: ArrayList list = new ArrayList(); // Add a book to the database: public void AddBook(string title, string author, decimal price, boolpaperBack) { list.Add(new Book(title, author, price, paperBack)); } // Call a passed-in delegate on each paperback book to process it: public ArrayList List
2. 2. { get { return list; } } public void PrintPaperBackBooks() { decimal TotalPrice = 0, AveragePrice = 0, ctr = 0; foreach (Book b in list) { if(b.Paperback) { Console.WriteLine(" Book Name {0}", b.Title); Console.WriteLine(" Book Author {0}", b.Author); Console.WriteLine(" Book Price {0:C}", b.Price); Console.WriteLine(" Book Paperback {0}", b.Paperback); Console.WriteLine("n"); TotalPrice = TotalPrice + b.Price; ctr++; } } AveragePrice = TotalPrice / ctr; Console.WriteLine("Average of Paper Back Books is : {0:C}",AveragePrice); } public void PrintAllBooks() { foreach (Book b in list) { Console.WriteLine(" Book Name {0}", b.Title); Console.WriteLine(" Book Author {0}", b.Author); Console.WriteLine(" Book Price {0}", b.Price); Console.WriteLine(" Book Paperback {0}", b.Paperback); Console.WriteLine("n"); } } }}