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
42 changes: 0 additions & 42 deletions tdd-oop-inheritance.CSharp.Main/Article.cs

This file was deleted.

42 changes: 0 additions & 42 deletions tdd-oop-inheritance.CSharp.Main/Book.cs

This file was deleted.

10 changes: 10 additions & 0 deletions tdd-oop-inheritance.CSharp.Main/Items/Article.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Items;

public class Article : Item
{
public Author Author { get; }
public Article(string title, Author author) : base(title)
{
Author = author;
}
}
13 changes: 13 additions & 0 deletions tdd-oop-inheritance.CSharp.Main/Items/Author.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Author
{
public string Name { get; }
public string ContactInformation { get; }
public string Website { get; }

public Author(string name, string contactInformation, string website)
{
Name = name;
ContactInformation = contactInformation;
Website = website;
}
}
10 changes: 10 additions & 0 deletions tdd-oop-inheritance.CSharp.Main/Items/Book.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Items;

public class Book : Item
{
public Author Author { get; }
public Book(string title, Author author) : base(title)
{
Author = author;
}
}
35 changes: 35 additions & 0 deletions tdd-oop-inheritance.CSharp.Main/Items/Item.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Items
{
public abstract class Item
{
public string Title { get; set; }
public bool OnLoan { get; set; }

public Item(string title)
{
Title = title;
}

public string CheckIn()
{
if (!OnLoan)
{
return "Item is not currently on loan";
}

OnLoan = false;
return "Item has been checked in";
}

public string CheckOut()
{
if (OnLoan)
{
return "Item is currently on loan";
}

OnLoan = true;
return "Item has been checked out";
}
}
}
18 changes: 18 additions & 0 deletions tdd-oop-inheritance.CSharp.Main/Items/Newspaper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Items;

public class Newspaper : Item
{
public Newspaper(string title) : base(title)
{
}

public new string CheckIn()
{
return "Newspapers are not available for loan";
}

public new string CheckOut()
{
return "Newspapers are not available for loan";
}
}
97 changes: 18 additions & 79 deletions tdd-oop-inheritance.CSharp.Main/Library.cs
Original file line number Diff line number Diff line change
@@ -1,89 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Items;

namespace tdd_oop_inheritance.CSharp.Main
public class Library
{
public class Library {
List<Article> articles = new List<Article>();
List<Book> books = new List<Book>();
List<Newspaper> newspapers = new List<Newspaper>();
public List<Item> items = new List<Item>();

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);
}

// 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) {
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) {
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));

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

return filtered[0].checkIn();
}

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";
}
public void AddToStock(Item item)
{
items.Add(item);
}

return filtered[0].checkOut();
public string CheckInOut<T>(string title, bool checkOut) where T : Item
{
if (typeof(T) == typeof(Newspaper))
{
return "Newspapers are not available for loan";
}

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";
}
T item = items.OfType<T>().FirstOrDefault(i => i.Title.Equals(title));

return filtered[0].checkIn();
if (item == null)
{
return "Item is not part of the library's collection";
}

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();
}
return checkOut ? item.CheckOut() : item.CheckIn();
}
}
}
31 changes: 0 additions & 31 deletions tdd-oop-inheritance.CSharp.Main/Newspaper.cs

This file was deleted.

57 changes: 30 additions & 27 deletions tdd-oop-inheritance.CSharp.Test/ArticleTest.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
using tdd_oop_inheritance.CSharp.Main;
using NUnit.Framework;
using NUnit.Framework;
using Items;

namespace tdd_oop_inheritance.CSharp.Test
{
class ArticleTest
{
[Test]
public void shouldCheckOutIfAvailable()
{
Article article = new Article("JUnit Rocks");
Assert.AreEqual("item has been checked out", article.checkOut());
}

[Test]
public void shouldDeclineIfNotAvailableToCheckout()
{
Article article = new Article("JUnit Rocks");
article.checkOut();
private Author author;
private Article article;

Assert.AreEqual("item is currently on loan", article.checkOut());
}
[SetUp]
public void SetUp()
{
author = new Author("Dan Brown", "dan@brown.com", "www.danbrown.com");
article = new Article("JUnit Rocks", author);
}

[Test]
public void shouldCheckInIfOnLoan()
{
Article article = new Article("JUnit Rocks");
article.checkOut();
public void ShouldCheckOutIfAvailable()
{
Assert.That(article.CheckOut(), Is.EqualTo("Item has been checked out"));
}

Assert.AreEqual("item has been checked in", article.checkIn());
}
[Test]
public void ShouldDeclineIfNotAvailableToCheckout()
{
article.CheckOut();
Assert.That(article.CheckOut(), Is.EqualTo("Item is currently on loan"));
}

[Test]
public void shouldDeclineCheckInIfNotOnLoan()
{
Article article = new Article("JUnit Rocks");
public void ShouldCheckInIfOnLoan()
{
article.CheckOut();
Assert.That(article.CheckIn(), Is.EqualTo("Item has been checked in"));
}

Assert.AreEqual("item is not currently on loan", article.checkIn());
}
[Test]
public void ShouldDeclineCheckInIfNotOnLoan()
{
Assert.That(article.CheckIn(), Is.EqualTo("Item is not currently on loan"));
}
}
}
Loading