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
36 changes: 7 additions & 29 deletions tdd-oop-inheritance.CSharp.Main/Article.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,15 @@

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

bool onLoan = false;

public Article(string title) {
this.title = title;
public Article(string title, Author author) : base(title)
{
Author = author;
}

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

}
}
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 Contact { get; set; }
public string Website { get; set; }

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

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

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

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

this.onLoan = true;

return "item has been checked out";
}

}
}
51 changes: 51 additions & 0 deletions tdd-oop-inheritance.CSharp.Main/Item.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace tdd_oop_inheritance.CSharp.Main
{
public class Item
{
public string Title;
public Boolean onLoan = false;


public Item(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";
}
}
}



64 changes: 8 additions & 56 deletions tdd-oop-inheritance.CSharp.Main/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,17 @@
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>();
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);
public void addToStock(Item 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) {
public string checkInItem(string title) {

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

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

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

if (filtered.Count() < 1) {
return "item is not part of the library's collection";
Expand All @@ -46,44 +36,6 @@ public string checkOutArticle(string title) {
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";
}

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

}
}
9 changes: 3 additions & 6 deletions tdd-oop-inheritance.CSharp.Main/Newspaper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@

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

Boolean onLoan = false;

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

public bool isOnLoan() {
Expand Down
24 changes: 20 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");
Author author = new Author("Jane Doe", "jane.doe@example.com", "https://janedoe.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");
Author author = new Author("Jane Doe", "jane.doe@example.com", "https://janedoe.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");
Author author = new Author("Jane Doe", "jane.doe@example.com", "https://janedoe.com");
Article article = new Article("JUnit Rocks", author);
article.checkOut();

Assert.AreEqual("item has been checked in", article.checkIn());
Expand All @@ -33,9 +36,22 @@ public void shouldCheckInIfOnLoan()
[Test]
public void shouldDeclineCheckInIfNotOnLoan()
{
Article article = new Article("JUnit Rocks");
Author author = new Author("Jane Doe", "jane.doe@example.com", "https://janedoe.com");
Article article = new Article("JUnit Rocks", author);

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

[Test]

public void shouldSetAuthorName()
{
Author author = new Author("Jane Doe", "jane.doe@example.com", "https://janedoe.com");
Article article = new Article("JUnit Rocks", author);

string name = article.Author.Name;

Assert.AreEqual("Jane Doe", name);
}
}
}
23 changes: 19 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");
Author author = new Author("Jane Doe", "jane.doe@example.com", "https://janedoe.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");
Author author = new Author("Jane Doe", "jane.doe@example.com", "https://janedoe.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");
Author author = new Author("Jane Doe", "jane.doe@example.com", "https://janedoe.com");
Book book = new Book("JUnit Rocks", author);
book.checkOut();

Assert.AreEqual("item has been checked in", book.checkIn());
Expand All @@ -33,9 +36,21 @@ public void shouldCheckInIfOnLoan()
[Test]
public void shouldDeclineCheckInIfNotOnLoan()
{
Book book = new Book("JUnit Rocks");
Author author = new Author("Jane Doe", "jane.doe@example.com", "https://janedoe.com");
Book book = new Book("JUnit Rocks", author);

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

public void shouldSetAuthorName()
{
Author author = new Author("Jane Doe", "jane.doe@example.com", "https://janedoe.com");
Book book = new Book("JUnit Rocks", author);

string name = book.Author.Name;

Assert.AreEqual("Jane Doe", name);
}
}
}