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
37 changes: 7 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,14 @@

namespace tdd_oop_inheritance.CSharp.Main
{
public class Article {
public string title;
public class Article : LibraryItem
{
public Author Author { get; }

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 string checkOut() {
if (this.isOnLoan()) {
return "item is currently on loan";
}

this.onLoan = true;

return "item has been checked out";
public Article(string title, Author author) : base(title)
{
Author = author;
}

}
}
16 changes: 16 additions & 0 deletions tdd-oop-inheritance.CSharp.Main/Author.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace tdd_oop_inheritance.CSharp.Main
{
public class Author
{
public string Name { get; }
public string ContactInfo { get; }
public string Website { get; }

public Author(string name, string contactInfo, string website)
{
Name = name;
ContactInfo = contactInfo;
Website = website;
}
}
}
37 changes: 7 additions & 30 deletions tdd-oop-inheritance.CSharp.Main/Book.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,14 @@

namespace tdd_oop_inheritance.CSharp.Main
{
public class Book {
public string title;
public class Book : LibraryItem
{
public Author Author { get; }

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 Book(string title, Author author) : base(title)
{
Author = author;
}

}
}
68 changes: 10 additions & 58 deletions tdd-oop-inheritance.CSharp.Main/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,19 @@

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 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 class Library
{
List<LibraryItem> items = new List<LibraryItem>();

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) {
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";
}
public string checkInItem(string title) {

return filtered[0].checkOut();
}

public string checkInBook(string title) {
List<Book> filtered = (List<Book>)this.books.Where(book => book.Equals(title));
List<LibraryItem> filtered = (List<LibraryItem>)this.items.Where(item => item.title.Equals(title)).ToList();

if (filtered.Count() < 1) {
return "item is not part of the library's collection";
Expand All @@ -56,8 +27,8 @@ public string checkInBook(string title) {
return filtered[0].checkIn();
}

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

if (filtered.Count() < 1) {
return "item is not part of the library's collection";
Expand All @@ -66,24 +37,5 @@ public string checkOutBook(string title) {
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();
}
}
}
48 changes: 48 additions & 0 deletions tdd-oop-inheritance.CSharp.Main/LibraryItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace tdd_oop_inheritance.CSharp.Main
{
public class LibraryItem
{
bool onLoan = false;

public string title;

public LibraryItem(string title)
{
this.title = title;
}
public bool isOnLoan()
{
return onLoan;
}

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

this.onLoan = false;

return "item has been checked in";
}

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

this.onLoan = true;

return "item has been checked out";
}
}
}
19 changes: 5 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,16 @@

namespace tdd_oop_inheritance.CSharp.Main
{
public class Newspaper
public class Newspaper : LibraryItem
{
public string title;

Boolean onLoan = false;

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

public bool isOnLoan() {
return onLoan;
}

public string checkIn() {
public Newspaper(string title) : base(title) { }

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

public string checkOut() {
public override string checkOut() {
return "newspapers are not available for loan";
}
}
Expand Down
12 changes: 8 additions & 4 deletions tdd-oop-inheritance.CSharp.Test/ArticleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ class ArticleTest
[Test]
public void shouldCheckOutIfAvailable()
{
Article article = new Article("JUnit Rocks");
var author = new Author("John Smith", "john@example.com", "www.johnsmith.com");
Article article = new Article("JUnit Rocks", author);
Assert.AreEqual("item has been checked out", article.checkOut());
}

[Test]
public void shouldDeclineIfNotAvailableToCheckout()
{
Article article = new Article("JUnit Rocks");
var author = new Author("John Smith", "john@example.com", "www.johnsmith.com");
Article article = new Article("JUnit Rocks", author);
article.checkOut();

Assert.AreEqual("item is currently on loan", article.checkOut());
Expand All @@ -24,7 +26,8 @@ public void shouldDeclineIfNotAvailableToCheckout()
[Test]
public void shouldCheckInIfOnLoan()
{
Article article = new Article("JUnit Rocks");
var author = new Author("John Smith", "john@example.com", "www.johnsmith.com");
Article article = new Article("JUnit Rocks", author);
article.checkOut();

Assert.AreEqual("item has been checked in", article.checkIn());
Expand All @@ -33,7 +36,8 @@ public void shouldCheckInIfOnLoan()
[Test]
public void shouldDeclineCheckInIfNotOnLoan()
{
Article article = new Article("JUnit Rocks");
var author = new Author("John Smith", "john@example.com", "www.johnsmith.com");
Article article = new Article("JUnit Rocks", author);

Assert.AreEqual("item is not currently on loan", article.checkIn());
}
Expand Down
12 changes: 8 additions & 4 deletions tdd-oop-inheritance.CSharp.Test/BookTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ public class BookTest
[Test]
public void shouldCheckOutIfAvailable()
{
Book book = new Book("JUnit Rocks");
var author = new Author("John Smith", "john@example.com", "www.johnsmith.com");
Book book = new Book("JUnit Rocks", author);
Assert.AreEqual("item has been checked out", book.checkOut());
}

[Test]
public void shouldDeclineIfNotAvailableToCheckout()
{
Book book = new Book("JUnit Rocks");
var author = new Author("John Smith", "john@example.com", "www.johnsmith.com");
Book book = new Book("JUnit Rocks", author);
book.checkOut();

Assert.AreEqual("item is currently on loan", book.checkOut());
Expand All @@ -24,7 +26,8 @@ public void shouldDeclineIfNotAvailableToCheckout()
[Test]
public void shouldCheckInIfOnLoan()
{
Book book = new Book("JUnit Rocks");
var author = new Author("John Smith", "john@example.com", "www.johnsmith.com");
Book book = new Book("JUnit Rocks", author);
book.checkOut();

Assert.AreEqual("item has been checked in", book.checkIn());
Expand All @@ -33,7 +36,8 @@ public void shouldCheckInIfOnLoan()
[Test]
public void shouldDeclineCheckInIfNotOnLoan()
{
Book book = new Book("JUnit Rocks");
var author = new Author("John Smith", "john@example.com", "www.johnsmith.com");
Book book = new Book("JUnit Rocks", author);

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