From 71d4372ea504195f530d24ca778f5aee676ab7ac Mon Sep 17 00:00:00 2001 From: Mathias Handeland <127216029+MathiasHandeland@users.noreply.github.com> Date: Sat, 16 Aug 2025 18:08:23 +0200 Subject: [PATCH] finished --- tdd-oop-inheritance.CSharp.Main/Article.cs | 37 ++-------- tdd-oop-inheritance.CSharp.Main/Author.cs | 16 +++++ tdd-oop-inheritance.CSharp.Main/Book.cs | 37 ++-------- tdd-oop-inheritance.CSharp.Main/Library.cs | 68 +++---------------- .../LibraryItem.cs | 48 +++++++++++++ tdd-oop-inheritance.CSharp.Main/Newspaper.cs | 19 ++---- .../ArticleTest.cs | 12 ++-- tdd-oop-inheritance.CSharp.Test/BookTest.cs | 12 ++-- .../LibraryTest.cs | 44 ++++++++++++ 9 files changed, 153 insertions(+), 140 deletions(-) create mode 100644 tdd-oop-inheritance.CSharp.Main/Author.cs create mode 100644 tdd-oop-inheritance.CSharp.Main/LibraryItem.cs create mode 100644 tdd-oop-inheritance.CSharp.Test/LibraryTest.cs diff --git a/tdd-oop-inheritance.CSharp.Main/Article.cs b/tdd-oop-inheritance.CSharp.Main/Article.cs index 2f92909..0302b75 100644 --- a/tdd-oop-inheritance.CSharp.Main/Article.cs +++ b/tdd-oop-inheritance.CSharp.Main/Article.cs @@ -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; } + } } diff --git a/tdd-oop-inheritance.CSharp.Main/Author.cs b/tdd-oop-inheritance.CSharp.Main/Author.cs new file mode 100644 index 0000000..54c82d6 --- /dev/null +++ b/tdd-oop-inheritance.CSharp.Main/Author.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/tdd-oop-inheritance.CSharp.Main/Book.cs b/tdd-oop-inheritance.CSharp.Main/Book.cs index ab271ef..ff87113 100644 --- a/tdd-oop-inheritance.CSharp.Main/Book.cs +++ b/tdd-oop-inheritance.CSharp.Main/Book.cs @@ -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; } + } } diff --git a/tdd-oop-inheritance.CSharp.Main/Library.cs b/tdd-oop-inheritance.CSharp.Main/Library.cs index 2c36d5b..261b404 100644 --- a/tdd-oop-inheritance.CSharp.Main/Library.cs +++ b/tdd-oop-inheritance.CSharp.Main/Library.cs @@ -6,48 +6,19 @@ namespace tdd_oop_inheritance.CSharp.Main { - public class Library { - List
articles = new List
(); - List books = new List(); - List newspapers = new List(); - - 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 items = new List(); + + 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
filtered = (List
)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
filtered = (List
)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 filtered = (List)this.books.Where(book => book.Equals(title)); + List filtered = (List)this.items.Where(item => item.title.Equals(title)).ToList(); if (filtered.Count() < 1) { return "item is not part of the library's collection"; @@ -56,8 +27,8 @@ public string checkInBook(string title) { return filtered[0].checkIn(); } - public string checkOutBook(string title) { - List filtered = (List)this.books.Where(book => book.Equals(title)); + public string checkOutItem(string title) { + List filtered = (List)this.items.Where(item => item.title.Equals(title)).ToList(); if (filtered.Count() < 1) { return "item is not part of the library's collection"; @@ -66,24 +37,5 @@ public string checkOutBook(string title) { return filtered[0].checkOut(); } - public string checkInNewspaper(string title) { - List filtered = (List)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 filtered = (List)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(); - } } } diff --git a/tdd-oop-inheritance.CSharp.Main/LibraryItem.cs b/tdd-oop-inheritance.CSharp.Main/LibraryItem.cs new file mode 100644 index 0000000..1a69060 --- /dev/null +++ b/tdd-oop-inheritance.CSharp.Main/LibraryItem.cs @@ -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"; + } + } +} diff --git a/tdd-oop-inheritance.CSharp.Main/Newspaper.cs b/tdd-oop-inheritance.CSharp.Main/Newspaper.cs index 1a35505..1e8c5a6 100644 --- a/tdd-oop-inheritance.CSharp.Main/Newspaper.cs +++ b/tdd-oop-inheritance.CSharp.Main/Newspaper.cs @@ -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"; } } diff --git a/tdd-oop-inheritance.CSharp.Test/ArticleTest.cs b/tdd-oop-inheritance.CSharp.Test/ArticleTest.cs index 31c16ff..fc9baa8 100644 --- a/tdd-oop-inheritance.CSharp.Test/ArticleTest.cs +++ b/tdd-oop-inheritance.CSharp.Test/ArticleTest.cs @@ -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()); @@ -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()); @@ -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()); } diff --git a/tdd-oop-inheritance.CSharp.Test/BookTest.cs b/tdd-oop-inheritance.CSharp.Test/BookTest.cs index 6e382ec..b84df72 100644 --- a/tdd-oop-inheritance.CSharp.Test/BookTest.cs +++ b/tdd-oop-inheritance.CSharp.Test/BookTest.cs @@ -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()); @@ -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()); @@ -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()); } diff --git a/tdd-oop-inheritance.CSharp.Test/LibraryTest.cs b/tdd-oop-inheritance.CSharp.Test/LibraryTest.cs new file mode 100644 index 0000000..0ffbbff --- /dev/null +++ b/tdd-oop-inheritance.CSharp.Test/LibraryTest.cs @@ -0,0 +1,44 @@ +using NUnit.Framework; +using tdd_oop_inheritance.CSharp.Main; + +namespace tdd_oop_inheritance.CSharp.Test +{ + public class LibraryTest + { + [Test] + public void shouldAddAndCheckOutBook() + { + Library library = new Library(); + var author = new Author("John Smith", "john@example.com", "www.johnsmith.com"); + Book book = new Book("Test Book", author); + library.addToStock(book); + + Assert.AreEqual("item has been checked out", library.checkOutItem("Test Book")); + Assert.AreEqual("item is currently on loan", library.checkOutItem("Test Book")); + } + + [Test] + public void shouldAddAndCheckInArticle() + { + Library library = new Library(); + var author = new Author("John Smith", "john@example.com", "www.johnsmith.com"); + Article article = new Article("Test Article", author); + library.addToStock(article); + library.checkOutItem("Test Article"); + + Assert.AreEqual("item has been checked in", library.checkInItem("Test Article")); + Assert.AreEqual("item has been checked out", library.checkOutItem("Test Article")); + } + + [Test] + public void shouldHandleNewspaperLoan() + { + Library library = new Library(); + Newspaper newspaper = new Newspaper("Test Newspaper"); + library.addToStock(newspaper); + + Assert.AreEqual("newspapers are not available for loan", library.checkOutItem("Test Newspaper")); + Assert.AreEqual("newspapers are not available for loan", library.checkInItem("Test Newspaper")); + } + } +} \ No newline at end of file