Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 5 additions & 30 deletions tdd-oop-inheritance.CSharp.Main/Article.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,12 @@

namespace tdd_oop_inheritance.CSharp.Main
{
public class Article {
public string title;

bool onLoan = false;

public Article(string title) {
this.title = title;
}

public bool isOnLoan() {
return onLoan;
}

public string checkIn() {
if (!this.isOnLoan()) {
return "item is not currently on loan";
}

this.onLoan = false;

return "item has been checked in";
public class Article : LibraryItem, IItemExtended
{
public Article(string title) : base(title)
{
}

public string checkOut() {
if (this.isOnLoan()) {
return "item is currently on loan";
}

this.onLoan = true;

return "item has been checked out";
}
public Author Author { get; set; }
}
}
22 changes: 22 additions & 0 deletions tdd-oop-inheritance.CSharp.Main/Author.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace tdd_oop_inheritance.CSharp.Main
{
public class Author
{
public string Name { get; set; }
public string Email { get; set; }
public string Website { get; set; }

public Author(string name, string email, string website)
{
Name = name;
Email = email;
Website = website;
}
}
}
35 changes: 4 additions & 31 deletions tdd-oop-inheritance.CSharp.Main/Book.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,10 @@

namespace tdd_oop_inheritance.CSharp.Main
{
public class Book {
public string title;
public class Book : LibraryItem, IItemExtended
{
public Book(string title) : base(title) { }

bool onLoan = false;

public Book(string title) {
this.title = title;
}

public bool isOnLoan() {
return onLoan;
}

public string checkIn() {
if (!this.isOnLoan()) {
return "item is not currently on loan";
}

this.onLoan = false;

return "item has been checked in";
}

public string checkOut() {
if (this.isOnLoan()) {
return "item is currently on loan";
}

this.onLoan = true;

return "item has been checked out";
}
public Author Author { get; set; }
}
}
13 changes: 13 additions & 0 deletions tdd-oop-inheritance.CSharp.Main/IItemExtended.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace tdd_oop_inheritance.CSharp.Main
{
public interface IItemExtended
{
public Author Author { get; set; }
}
}
84 changes: 23 additions & 61 deletions tdd-oop-inheritance.CSharp.Main/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,83 +7,45 @@
namespace tdd_oop_inheritance.CSharp.Main
{
public class Library {
List<Article> articles = new List<Article>();
List<Book> books = new List<Book>();
List<Newspaper> newspapers = new List<Newspaper>();
public List<LibraryItem> items = new List<LibraryItem>();

public void addToStock(Article item) {
this.articles.Add(item);
}

public void addToStock(Book item) {
this.books.Add(item);
}

public void addToStock(Newspaper item) {
this.newspapers.Add(item);
public void addToStock(LibraryItem item)
{
this.items.Add(item);
}

// The following methods may contain code that you are unfamiliar with. The strange syntax of article -> something
// is called a lambda expression (https://www.w3schools.com/java/java_lambda.asp)
public string checkInArticle(string title) {

List<Article> filtered = (List<Article>)this.articles.Where(article => article.title.Equals(title));

if (filtered.Count() < 1) {
public string checkInItem(string title)
{
List<LibraryItem> filtered = this.items.Where(item => item.title.Equals(title)).ToList();
if (filtered.Count() < 1)
{
return "item is not part of the library's collection";
}

return filtered[0].checkIn();
}

public string checkOutArticle(string title) {
List<Article> filtered = (List<Article>)this.articles.Where(article => article.title.Equals(title));

if (filtered.Count() < 1) {
public string checkOutItem(string title)
{
List<LibraryItem> filtered = this.items.Where(item => item.title.Equals(title)).ToList();
if (filtered.Count() < 1)
{
return "item is not part of the library's collection";
}

return filtered[0].checkOut();
}

public string checkInBook(string title) {
List<Book> filtered = (List<Book>)this.books.Where(book => book.Equals(title));
public List<LibraryItem> GetItemsByAuthor(string authorName)
{
List<LibraryItem> result = new List<LibraryItem>();
List<Article> articles = this.items.Where(item => item is Article).Cast<Article>().ToList();
List<Book> books = this.items.Where(item => item is Book).Cast<Book>().ToList();

if (filtered.Count() < 1) {
return "item is not part of the library's collection";
}
result.AddRange(articles.FindAll(article => article.Author.Name.Equals(authorName)));
result.AddRange(books.FindAll(book => book.Author.Name.Equals(authorName)));

return filtered[0].checkIn();
return result;
}

public string checkOutBook(string title) {
List<Book> filtered = (List<Book>)this.books.Where(book => book.Equals(title));

if (filtered.Count() < 1) {
return "item is not part of the library's collection";
}

return filtered[0].checkOut();
}

public string checkInNewspaper(string title) {
List<Newspaper> filtered = (List<Newspaper>)this.newspapers.Where(newspaper => newspaper.title.Equals(title));

if (filtered.Count() < 1) {
return "item is not part of the library's collection";
}

return filtered[0].checkIn();
}

public string checkOutNewspaper(string title) {
List<Newspaper> filtered = (List<Newspaper>)this.newspapers.Where(newspaper => newspaper.title.Equals(title));

if (filtered.Count() < 1) {
return "item is not part of the library's collection";
}

return filtered[0].checkOut();
}
}
}
}
40 changes: 40 additions & 0 deletions tdd-oop-inheritance.CSharp.Main/LibraryItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace tdd_oop_inheritance.CSharp.Main
{
public abstract class LibraryItem
{
public string title;
bool onLoan = false;
public LibraryItem(string title)
{
this.title = title;
}
public bool isOnLoan()
{
return onLoan;
}
public string checkIn()
{
if (!this.isOnLoan())
{
return "item is not currently on loan";
}
this.onLoan = false;
return "item has been checked in";
}
public string checkOut()
{
if (this.isOnLoan())
{
return "item is currently on loan";
}
this.onLoan = true;
return "item has been checked out";
}
}
}
18 changes: 4 additions & 14 deletions tdd-oop-inheritance.CSharp.Main/Newspaper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,15 @@

namespace tdd_oop_inheritance.CSharp.Main
{
public class Newspaper
public class Newspaper : LibraryItem
{
public string title;
public Newspaper(string title) : base(title) { }

Boolean onLoan = false;

public Newspaper(string title) {
this.title = title;
}

public bool isOnLoan() {
return onLoan;
}

public string checkIn() {
public new string checkIn() {
return "newspapers are not available for loan";
}

public string checkOut() {
public new string checkOut() {
return "newspapers are not available for loan";
}
}
Expand Down
9 changes: 9 additions & 0 deletions tdd-oop-inheritance.CSharp.Test/BookTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,14 @@ public void shouldDeclineCheckInIfNotOnLoan()

Assert.AreEqual("item is not currently on loan", book.checkIn());
}

[Test]
public void TestAuthor()
{
Book book = new Book("JUnit Rocks");
Author author = new Author("Nigel", "nigel@boolean.com", "boolean.com");
book.Author = author;
Assert.AreEqual(author, book.Author);
}
}
}
Loading