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

namespace tdd_oop_inheritance.CSharp.Main
{
public class Article {
public string title;
public class Article : LibraryItem
{
private Author _author;

bool onLoan = false;

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

public bool isOnLoan() {
return onLoan;
public Article(Author author, string title) : base(title)
{
_author = author;
}

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";
}
}
}
23 changes: 23 additions & 0 deletions tdd-oop-inheritance.CSharp.Main/Author.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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 ContactInfo { get; set; }
public string Website { get; set; }

public Author(string name, string contactInfo, string website)
{
Name = name;
ContactInfo = contactInfo;
Website = website;
}

}
}
36 changes: 6 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,13 @@

namespace tdd_oop_inheritance.CSharp.Main
{
public class Book {
public string title;
public class Book : LibraryItem
{
private Author _author;

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(Author author, string title) : base(title)
{
_author = author;
}
}
}
73 changes: 14 additions & 59 deletions tdd-oop-inheritance.CSharp.Main/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,79 +7,34 @@
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);
}
List<LibraryItem> items = new List<LibraryItem>();

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) {
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<LibraryItem> filtered = (List<LibraryItem>)this.items.Where(item => item.title.Equals(title));

if (filtered.Count() < 1) {
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));
public string checkOutItem(string title)
{
List<LibraryItem> filtered = (List<LibraryItem>)this.items.Where(items => items.Equals(title));

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

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


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

Boolean onLoan = false;

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


}
}
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 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
7 changes: 5 additions & 2 deletions tdd-oop-inheritance.CSharp.Main/Program.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
using tdd_oop_inheritance.CSharp.Main;


Author author = new Author("Name", "info", "website");
Book book = new Book(author, "BookName");
46 changes: 27 additions & 19 deletions tdd-oop-inheritance.CSharp.Test/ArticleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,45 @@ namespace tdd_oop_inheritance.CSharp.Test
{
class ArticleTest
{
Author author;

[SetUp]
public void SetUp()
{
this.author = new Author("Bob Bagel", "+4712345678", "http://bobsemail.com");
}

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

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

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

[Test]
public void shouldCheckInIfOnLoan()
{
Article article = new Article("JUnit Rocks");
article.checkOut();
{
LibraryItem article = new Article(author, "JUnit Rocks");
article.checkOut();

Assert.AreEqual("item has been checked in", article.checkIn());
}
Assert.AreEqual("item has been checked in", article.checkIn());
}

[Test]
public void shouldDeclineCheckInIfNotOnLoan()
{
Article article = new Article("JUnit Rocks");
{
LibraryItem article = new Article(author, "JUnit Rocks");

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