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
19 changes: 5 additions & 14 deletions tdd-oop-inheritance.CSharp.Main/Article.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,11 @@

namespace tdd_oop_inheritance.CSharp.Main
{
public class Article {
public string title;
public class Article : Item
{
public Article(string title, bool onLoan = false) : base(title, onLoan) { }

bool onLoan = false;

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

public bool isOnLoan() {
return onLoan;
}

public string checkIn() {
public override string checkIn() {
if (!this.isOnLoan()) {
return "item is not currently on loan";
}
Expand All @@ -29,7 +20,7 @@ public string checkIn() {
return "item has been checked in";
}

public string checkOut() {
public override string checkOut() {
if (this.isOnLoan()) {
return "item is currently on loan";
}
Expand Down
29 changes: 12 additions & 17 deletions tdd-oop-inheritance.CSharp.Main/Book.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@

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

bool onLoan = false;

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

public bool isOnLoan() {
return onLoan;
}

public string checkIn() {
if (!this.isOnLoan()) {
public class Book : Item
{
public Book(string title, bool onLoan = false) : base(title, onLoan) { }

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

Expand All @@ -29,8 +22,10 @@ public string checkIn() {
return "item has been checked in";
}

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

Expand Down
29 changes: 29 additions & 0 deletions tdd-oop-inheritance.CSharp.Main/Item.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 Item
{
public string title;
public bool onLoan;

protected Item(string title, bool onLoan)
{
this.title = title;
this.onLoan = onLoan;
}

public bool isOnLoan()
{
return onLoan;
}

public abstract string checkIn();

public abstract string checkOut();
}
}
65 changes: 7 additions & 58 deletions tdd-oop-inheritance.CSharp.Main/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,16 @@
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<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) {

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

return filtered[0].checkOut();
}

public string checkInNewspaper(string title) {
List<Newspaper> filtered = (List<Newspaper>)this.newspapers.Where(newspaper => newspaper.title.Equals(title));
public string checkInItem(string title) {
List<Item> filtered = 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 @@ -76,8 +25,8 @@ public string checkInNewspaper(string title) {
return filtered[0].checkIn();
}

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

if (filtered.Count() < 1) {
return "item is not part of the library's collection";
Expand Down
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 : Item
{
public string title;
public Newspaper(string title, bool onLoan = false) : base(title, onLoan) { }

Boolean onLoan = false;

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

public bool isOnLoan() {
return onLoan;
}

public string checkIn() {
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
83 changes: 83 additions & 0 deletions tdd-oop-inheritance.CSharp.Test/AllTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using tdd_oop_inheritance.CSharp.Main;
using NUnit.Framework;

namespace tdd_oop_inheritance.CSharp.Test
{
public class AllTest
{
[Test]
public void shouldCheckOutIfAvailable()
{
Library library = new Library();
Article article = new Article("JUnit Rocks");

Book book = new Book("Book");

Newspaper newspaper = new Newspaper("Newspaper");

Assert.AreEqual("item is not part of the library's collection", library.checkOutItem(article.title));
Assert.AreEqual("item is not part of the library's collection", library.checkOutItem(book.title));
Assert.AreEqual("item is not part of the library's collection", library.checkOutItem(newspaper.title));
}

[Test]
public void shouldDeclineIfNotAvailableToCheckout()
{
Library library = new Library();
Article article = new Article("JUnit Rocks");
library.addToStock(article);
library.checkOutItem(article.title);

Book book = new Book("Book");
library.addToStock(book);
library.checkOutItem(book.title);

Newspaper newspaper = new Newspaper("Newspaper");
library.addToStock(newspaper);
library.checkOutItem(newspaper.title);

Assert.AreEqual("item is currently on loan", library.checkOutItem(article.title));
Assert.AreEqual("item is currently on loan", library.checkOutItem(book.title));
Assert.AreEqual("newspapers are not available for loan", library.checkOutItem(newspaper.title));
}

[Test]
public void shouldCheckInIfOnLoan()
{
Library library = new Library();
Article article = new Article("JUnit Rocks");
library.addToStock(article);
library.checkOutItem(article.title);

Book book = new Book("Book");
library.addToStock(book);
library.checkOutItem(book.title);

Newspaper newspaper = new Newspaper("Newspaper");
library.addToStock(newspaper);
library.checkOutItem(newspaper.title);

Assert.AreEqual("item has been checked in", library.checkInItem(article.title));
Assert.AreEqual("item has been checked in", library.checkInItem(book.title));
Assert.AreEqual("newspapers are not available for loan", library.checkInItem(newspaper.title));
}

[Test]
public void shouldDeclineCheckInIfNotOnLoan()
{
Library library = new Library();
Article article = new Article("JUnit Rocks");
library.addToStock(article);

Book book = new Book("Book");
library.addToStock(book);

Newspaper newspaper = new Newspaper("Newspaper");
library.addToStock(newspaper);

Assert.AreEqual("item is not currently on loan", library.checkInItem(article.title));
Assert.AreEqual("item is not currently on loan", library.checkInItem(book.title));
Assert.AreEqual("newspapers are not available for loan", library.checkInItem(newspaper.title));
}
}
}
41 changes: 0 additions & 41 deletions tdd-oop-inheritance.CSharp.Test/ArticleTest.cs

This file was deleted.

41 changes: 0 additions & 41 deletions tdd-oop-inheritance.CSharp.Test/BookTest.cs

This file was deleted.

Loading