Skip to content

Petros Mylonas(2 exercises) #64

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
34 changes: 3 additions & 31 deletions src/main/java/com/booleanuk/core/Article.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,7 @@
package com.booleanuk.core;

public class Article {
String title;

boolean onLoan = false;

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



public class Author {
private String name;
private String contactInformation;
private String website;

public Author(String name, String contactInformation, String website) {
this.name = name;
this.contactInformation = contactInformation;
this.website = website;
}

// Getter methods for author information
public String getName() {
return name;
}

public String getContactInformation() {
return contactInformation;
}

public String getWebsite() {
return website;
}
}

34 changes: 3 additions & 31 deletions src/main/java/com/booleanuk/core/Book.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,7 @@
package com.booleanuk.core;

public class Book {
String title;

boolean onLoan = false;

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";
public class Book extends LibraryItem {
public Book(String title,Author author) {
super(title,author);
}
}
84 changes: 15 additions & 69 deletions src/main/java/com/booleanuk/core/Library.java
Original file line number Diff line number Diff line change
@@ -1,78 +1,24 @@
package com.booleanuk.core;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Library {
List<Article> articles;
List<Book> books;
List<Newspaper> newspapers;
List<LibraryItem> items;

public void addToStock(Article item) {
this.articles.add(item);
public Library() {
this.items = new ArrayList<>();
}

public void addToStock(Book item) {
this.books.add(item);
public void addToStock(LibraryItem item) {
this.items.add(item);
}

public void addToStock(Newspaper item) {
this.newspapers.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))
.toList();
public String checkInItem(String title) {
List<LibraryItem> filtered = this.items.stream()
.filter(item -> item.title.equals(title))
.collect(Collectors.toList());

if (filtered.size() < 1) {
return "item is not part of the library's collection";
Expand All @@ -81,10 +27,10 @@ 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))
.toList();
public String checkOutItem(String title) {
List<LibraryItem> filtered = this.items.stream()
.filter(item -> item.title.equals(title))
.collect(Collectors.toList());

if (filtered.size() < 1) {
return "item is not part of the library's collection";
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/booleanuk/core/LibraryItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.booleanuk.core;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;



public class LibraryItem {
String title;
boolean onLoan = false;
private Author author;
public LibraryItem(String title,Author author) {
this.title = title;
this.author=author;
}
public Author getAuthor() {
return author;
}
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";
}
}
19 changes: 8 additions & 11 deletions src/main/java/com/booleanuk/core/Newspaper.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package com.booleanuk.core;

public class Newspaper {
String title;

boolean onLoan = false;

public class Newspaper extends LibraryItem {
public Newspaper(String title) {
this.title = title;
super(title,null);
}

public boolean isOnLoan() {
return onLoan;
@Override
public Author getAuthor() {
return null;
}

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

@Override
public String checkOut() {
return "newspapers are not available for loan";
}
}
}
49 changes: 31 additions & 18 deletions src/test/java/com/booleanuk/core/ArticleTest.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,48 @@
package com.booleanuk.core;

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

class ArticleTest {
@Test
public void shouldCheckOutIfAvailable() {
Article article = new Article("JUnit Rocks");
Assertions.assertEquals("item has been checked out", article.checkOut());
}
public class ArticleTest {

@Test
public void shouldDeclineIfNotAvailableToCheckout() {
Article article = new Article("JUnit Rocks");
article.checkOut();
public void shouldCheckOutArticleIfAvailable() {
Library library = new Library();
Author author = new Author("Petros Mylonas", "[email protected]", "www.petrosmylonas.com");
Article article = new Article("Java for Beginners",author);
library.addToStock(article);

Assertions.assertEquals("item is currently on loan", article.checkOut());
Assertions.assertEquals("item has been checked out", library.checkOutItem("Java for Beginners"));
}

@Test
public void shouldCheckInIfOnLoan() {
Article article = new Article("JUnit Rocks");
article.checkOut();
public void shouldDeclineArticleIfNotAvailableToCheckout() {
Library library = new Library();
Author author = new Author("Petros Mylonas", "[email protected]", "www.petrosmylonas.com");
Article article = new Article("Java for Beginners",author);
library.addToStock(article);
library.checkOutItem("Java for Beginners");

Assertions.assertEquals("item is currently on loan", library.checkOutItem("Java for Beginners"));
}

Assertions.assertEquals("item has been checked in", article.checkIn());
@Test
public void shouldCheckInArticleIfOnLoan() {
Library library = new Library();
Author author = new Author("Petros Mylonas", "[email protected]", "www.petrosmylonas.com");
Article article = new Article("Java for Beginners",author);
library.addToStock(article);
library.checkOutItem("Java for Beginners");

Assertions.assertEquals("item has been checked in", library.checkInItem("Java for Beginners"));
}

@Test
public void shouldDeclineCheckInIfNotOnLoan() {
Article article = new Article("JUnit Rocks");
public void shouldDeclineCheckInArticleIfNotOnLoan() {
Library library = new Library();
Author author = new Author("Petros Mylonas", "[email protected]", "www.petrosmylonas.com");
Article article = new Article("Java for Beginners",author);
library.addToStock(article);

Assertions.assertEquals("item is not currently on loan", article.checkIn());
Assertions.assertEquals("item is not currently on loan", library.checkInItem("Java for Beginners"));
}
}
16 changes: 16 additions & 0 deletions src/test/java/com/booleanuk/core/AuthorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.booleanuk.core;

import com.booleanuk.core.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class AuthorTest {

@Test
public void shouldGetAuthorInformation() {
Author author = new Author("Petros Mylonas", "[email protected]", "www.petrosmylonas.com");
Assertions.assertEquals("Petros Mylonas", author.getName());
Assertions.assertEquals("[email protected]", author.getContactInformation());
Assertions.assertEquals("www.petrosmylonas.com", author.getWebsite());
}
}
Loading