Skip to content

Nicolai Klokmose #54

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

public class Article {
String title;

public class Article extends Item {
protected String title;
protected Author author;
boolean onLoan = false;

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

public boolean isOnLoan() {
return onLoan;
public Article(String title, Author author) {
super(title);
this.setAuthor(author);
}

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

this.onLoan = false;

return "item has been checked in";
public Author getAuthor() {
return author;
}

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

this.onLoan = true;

return "item has been checked out";
public void setAuthor(Author author) {
this.author = author;
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/booleanuk/core/Author.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.booleanuk.core;

public class Author {
private String name;
private int phoneNumber;
private String http;

public Author(String name, int phoneNumber, String http) {
this.setName(name);
this.setPhoneNumber(phoneNumber);
this.setHttp(http);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(int phoneNumber) {
this.phoneNumber = phoneNumber;
}

public String getHttp() {
return http;
}

public void setHttp(String http) {
this.http = http;
}
}
33 changes: 11 additions & 22 deletions src/main/java/com/booleanuk/core/Book.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
package com.booleanuk.core;

public class Book {
String title;

public class Book extends Item {
protected String title;
protected Author author;
boolean onLoan = false;

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

public boolean isOnLoan() {
return onLoan;
public Book(String title, Author author) {
super(title);
this.setAuthor(author);
}

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

this.onLoan = false;

return "item has been checked in";
public Author getAuthor() {
return author;
}

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

this.onLoan = true;

return "item has been checked out";
public void setAuthor(Author author) {
this.author = author;
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/booleanuk/core/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.booleanuk.core;

public abstract class Item {
String title;

boolean onLoan = false;

public Item(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";
}
}
73 changes: 10 additions & 63 deletions src/main/java/com/booleanuk/core/Library.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
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<Item> 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(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 = this.articles.stream()
.filter(article -> article.title.equals(title))
List<Item> filtered = this.items.stream()
.filter(item -> item.title.equals(title))
.toList();

if (filtered.size() < 1) {
Expand All @@ -34,56 +29,8 @@ public String checkInArticle(String title) {
}

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();

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

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

public String checkOutNewspaper(String title) {
List<Newspaper> filtered = this.newspapers.stream()
.filter(newspaper -> newspaper.title.equals(title))
List<Item> filtered = this.items.stream()
.filter(item -> item.title.equals(title))
.toList();

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

public class Main {

public static void main(String[] args) {
Library library = new Library();
Author author = new Author("George Lucas", 123456, "www.lol.dk");

Item item_1 = new Article("sw1");
Item item_2 = new Book("sw2");
Item item_3 = new Newspaper("sw3");
Item item_4 = new Article("sw4");

Item item_5 = new Article("sw5", author);

library.addToStock(item_1);
library.addToStock(item_2);
library.addToStock(item_3);
library.addToStock(item_4);
library.addToStock(item_5);

System.out.println(library.checkInArticle("sw1"));
System.out.println(library.checkOutArticle("sw1"));

System.out.println(library.checkInArticle("sw2"));
System.out.println(library.checkOutArticle("sw2"));

System.out.println(library.checkInArticle("sw3"));
System.out.println(library.checkOutArticle("sw3"));

System.out.println(library.checkInArticle("sw4"));
System.out.println(library.checkOutArticle("sw4"));

System.out.println(library.checkInArticle("sw5"));
System.out.println(library.checkOutArticle("sw5"));

}
}
13 changes: 5 additions & 8 deletions src/main/java/com/booleanuk/core/Newspaper.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package com.booleanuk.core;

public class Newspaper {
String title;

public class Newspaper extends Item {
protected String title;
boolean onLoan = false;

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

public boolean isOnLoan() {
return onLoan;
super(title);
}

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

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

import org.junit.jupiter.api.Test;

public class ItemTest {

@Test
public void testItemClass() {

}
}