Skip to content

Enock Ladu #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
31 changes: 2 additions & 29 deletions src/main/java/com/booleanuk/core/Article.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,8 @@
package com.booleanuk.core;

public class Article {
String title;

boolean onLoan = false;
public class Article extends Stock{

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

public boolean 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";
super(title);
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/booleanuk/core/Author.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.booleanuk.core;

public class Author {

String name;

String contactInformation;

String website;

Article article;
Book book;
Newspaper newspaper;

public Author(Article article, String name, String contactInformation, String website) {
this.article = article;
this.name = name;
this.contactInformation = contactInformation;
this.website = website;
}
public Author(Book book, String name, String contactInformation, String website) {
this.book = book;
this.name = name;
this.contactInformation = contactInformation;
this.website = website;
}

public String getArticleAuthorInfo() {
return "Article: " + article.title +
"Author name: " + this.name +
", Contact information: " + this.contactInformation +
", Website: " + this.website;
}

public String getBookAuthorInfo() {
return "Book: " + book.title +
"Author name: " + this.name +
", Contact information: " + this.contactInformation +
", Website: " + this.website;
}
}
31 changes: 2 additions & 29 deletions src/main/java/com/booleanuk/core/Book.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,8 @@
package com.booleanuk.core;

public class Book {
String title;

boolean onLoan = false;
public class Book extends Stock {

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

public boolean 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";
super(title);
}
}
77 changes: 10 additions & 67 deletions src/main/java/com/booleanuk/core/Library.java
Original file line number Diff line number Diff line change
@@ -1,77 +1,20 @@
package com.booleanuk.core;

import java.util.ArrayList;
import java.util.List;

public class Library {
List<Article> articles;
List<Book> books;
List<Newspaper> newspapers;
List<Stock> stockItems = new ArrayList<>();

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(Stock item) {
this.stockItems.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 = this.articles.stream()
.filter(article -> article.title.equals(title))
.toList();

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

return filtered.get(0).checkIn();
}

public String checkOutArticle(String title) {
List<Article> filtered = this.articles.stream()
.filter(article -> article.title.equals(title))
.toList();

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

return filtered.get(0).checkOut();
}

public String checkInBook(String title) {
List<Book> filtered = this.books.stream()
.filter(book -> book.title.equals(title))
.toList();

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

return filtered.get(0).checkIn();
}

public String checkOutBook(String title) {
List<Book> filtered = this.books.stream()
.filter(book -> book.title.equals(title))
.toList();

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

return filtered.get(0).checkOut();
}

public String checkInNewspaper(String title) {
List<Newspaper> filtered = this.newspapers.stream()
.filter(newspaper -> newspaper.title.equals(title))
public String checkInStock(String title) {
List<Stock> filtered = this.stockItems.stream()
.filter(stock -> stock.title.equals(title))
.toList();

if (filtered.size() < 1) {
Expand All @@ -81,9 +24,9 @@ public String checkInNewspaper(String title) {
return filtered.get(0).checkIn();
}

public String checkOutNewspaper(String title) {
List<Newspaper> filtered = this.newspapers.stream()
.filter(newspaper -> newspaper.title.equals(title))
public String checkOutStock(String title) {
List<Stock> filtered = this.stockItems.stream()
.filter(stock -> stock.title.equals(title))
.toList();

if (filtered.size() < 1) {
Expand Down
15 changes: 4 additions & 11 deletions src/main/java/com/booleanuk/core/Newspaper.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
package com.booleanuk.core;

public class Newspaper {
String title;

boolean onLoan = false;

public class Newspaper extends Stock {
public Newspaper(String title) {
this.title = title;
super(title);
}

public boolean isOnLoan() {
return onLoan;
}

@Override
public String checkIn() {
return "newspapers are not available for loan";
}

@Override
public String checkOut() {
return "newspapers are not available for loan";
}
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/booleanuk/core/Stock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.booleanuk.core;

public class Stock {

String title;
boolean onLoan = false;

public Stock(String title) {
this.title = title;
}

public boolean 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";
}
}
39 changes: 39 additions & 0 deletions src/test/java/com/booleanuk/core/AuthorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.booleanuk.core;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class AuthorTest {

@Test
void getArticleAuthorInfo() {
Article article = new Article("An Adventure to Space");
Author author = new Author(article,"Neil Degrasse Tyson", "[email protected]", "www.neildegrassetyson.com");

String result = "Article: An Adventure to Space" +
"Author name: Neil Degrasse Tyson" +
", Contact information: [email protected]" +
", Website: www.neildegrassetyson.com";

String expectedResult = author.getArticleAuthorInfo();

Assertions.assertEquals(expectedResult, result);
}

@Test
void getBookAuthorInfo() {
Book book = new Book("Lord of the Rings");
Author author = new Author(book,"JRR Tolkien", "[email protected]", "www.lordoftherings.com");

String result = "Book: Lord of the Rings" +
"Author name: JRR Tolkien" +
", Contact information: [email protected]" +
", Website: www.lordoftherings.com";

String expectedResult = author.getBookAuthorInfo();

Assertions.assertEquals(expectedResult, result);
}
}
Loading