Skip to content

Commit 2cda796

Browse files
committed
completed 1 and 2
1 parent 3f328bf commit 2cda796

File tree

10 files changed

+144
-140
lines changed

10 files changed

+144
-140
lines changed
Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,11 @@
11
package com.booleanuk.core;
22

3-
public class Article {
4-
String title;
3+
public class Article extends Paper{
4+
private Author author;
55

6-
boolean onLoan = false;
76

8-
public Article(String title) {
9-
this.title = title;
10-
}
11-
12-
public boolean isOnLoan() {
13-
return onLoan;
14-
}
15-
16-
public String checkIn() {
17-
if (!this.isOnLoan()) {
18-
return "item is not currently on loan";
19-
}
20-
21-
this.onLoan = false;
22-
23-
return "item has been checked in";
24-
}
25-
26-
public String checkOut() {
27-
if (this.isOnLoan()) {
28-
return "item is currently on loan";
29-
}
30-
31-
this.onLoan = true;
32-
33-
return "item has been checked out";
7+
public Article(String title, Author author) {
8+
super(title);
9+
this.author = author;
3410
}
3511
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.booleanuk.core;
2+
3+
public class Author {
4+
5+
public Author(String name, String contactInformation, String website){
6+
}
7+
}
Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,10 @@
11
package com.booleanuk.core;
22

3-
public class Book {
4-
String title;
3+
public class Book extends Paper {
4+
private Author author;
55

6-
boolean onLoan = false;
7-
8-
public Book(String title) {
9-
this.title = title;
10-
}
11-
12-
public boolean isOnLoan() {
13-
return onLoan;
14-
}
15-
16-
public String checkIn() {
17-
if (!this.isOnLoan()) {
18-
return "item is not currently on loan";
19-
}
20-
21-
this.onLoan = false;
22-
23-
return "item has been checked in";
24-
}
25-
26-
public String checkOut() {
27-
if (this.isOnLoan()) {
28-
return "item is currently on loan";
29-
}
30-
31-
this.onLoan = true;
32-
33-
return "item has been checked out";
6+
public Book(String title, Author author) {
7+
super(title);
8+
this.author = author;
349
}
3510
}
Lines changed: 9 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
package com.booleanuk.core;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45

56
public class Library {
6-
List<Article> articles;
7-
List<Book> books;
8-
List<Newspaper> newspapers;
7+
List<Paper> collection = new ArrayList<>();
98

10-
public void addToStock(Article item) {
11-
this.articles.add(item);
12-
}
139

14-
public void addToStock(Book item) {
15-
this.books.add(item);
10+
public void addToStock(Paper item) {
11+
this.collection.add(item);
1612
}
1713

18-
public void addToStock(Newspaper item) {
19-
this.newspapers.add(item);
20-
}
14+
2115

2216
// The following methods may contain code that you are unfamiliar with. The strange syntax of article -> something
2317
// is called a lambda expression (https://www.w3schools.com/java/java_lambda.asp)
24-
public String checkInArticle(String title) {
25-
List<Article> filtered = this.articles.stream()
18+
public String checkInPaper(String title) {
19+
List<Paper> filtered = this.collection.stream()
2620
.filter(article -> article.title.equals(title))
2721
.toList();
2822

@@ -33,8 +27,8 @@ public String checkInArticle(String title) {
3327
return filtered.get(0).checkIn();
3428
}
3529

36-
public String checkOutArticle(String title) {
37-
List<Article> filtered = this.articles.stream()
30+
public String checkOutPaper(String title) {
31+
List<Paper> filtered = this.collection.stream()
3832
.filter(article -> article.title.equals(title))
3933
.toList();
4034

@@ -45,51 +39,4 @@ public String checkOutArticle(String title) {
4539
return filtered.get(0).checkOut();
4640
}
4741

48-
public String checkInBook(String title) {
49-
List<Book> filtered = this.books.stream()
50-
.filter(book -> book.title.equals(title))
51-
.toList();
52-
53-
if (filtered.size() < 1) {
54-
return "item is not part of the library's collection";
55-
}
56-
57-
return filtered.get(0).checkIn();
58-
}
59-
60-
public String checkOutBook(String title) {
61-
List<Book> filtered = this.books.stream()
62-
.filter(book -> book.title.equals(title))
63-
.toList();
64-
65-
if (filtered.size() < 1) {
66-
return "item is not part of the library's collection";
67-
}
68-
69-
return filtered.get(0).checkOut();
70-
}
71-
72-
public String checkInNewspaper(String title) {
73-
List<Newspaper> filtered = this.newspapers.stream()
74-
.filter(newspaper -> newspaper.title.equals(title))
75-
.toList();
76-
77-
if (filtered.size() < 1) {
78-
return "item is not part of the library's collection";
79-
}
80-
81-
return filtered.get(0).checkIn();
82-
}
83-
84-
public String checkOutNewspaper(String title) {
85-
List<Newspaper> filtered = this.newspapers.stream()
86-
.filter(newspaper -> newspaper.title.equals(title))
87-
.toList();
88-
89-
if (filtered.size() < 1) {
90-
return "item is not part of the library's collection";
91-
}
92-
93-
return filtered.get(0).checkOut();
94-
}
9542
}
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
package com.booleanuk.core;
22

3-
public class Newspaper {
4-
String title;
5-
6-
boolean onLoan = false;
3+
public class Newspaper extends Paper{
74

85
public Newspaper(String title) {
9-
this.title = title;
10-
}
11-
12-
public boolean isOnLoan() {
13-
return onLoan;
6+
super(title);
147
}
158

9+
@Override
1610
public String checkIn() {
1711
return "newspapers are not available for loan";
1812
}
1913

14+
@Override
2015
public String checkOut() {
16+
2117
return "newspapers are not available for loan";
2218
}
2319
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.booleanuk.core;
2+
3+
public class Paper {
4+
protected String title;
5+
protected boolean onLoan = false;
6+
7+
public Paper(String title) {
8+
this.title = title;
9+
}
10+
11+
public boolean isOnLoan() {
12+
return onLoan;
13+
}
14+
15+
public String checkIn() {
16+
if (!this.isOnLoan()) {
17+
return "item is not currently on loan";
18+
}
19+
20+
this.onLoan = false;
21+
22+
return "item has been checked in";
23+
}
24+
25+
public String checkOut() {
26+
if (this.isOnLoan()) {
27+
return "item is currently on loan";
28+
}
29+
30+
this.onLoan = true;
31+
32+
return "item has been checked out";
33+
}
34+
}

src/test/java/com/booleanuk/core/ArticleTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,36 @@
66
class ArticleTest {
77
@Test
88
public void shouldCheckOutIfAvailable() {
9-
Article article = new Article("JUnit Rocks");
9+
Author author = new Author("Bob","[email protected]","bob.com");
10+
Paper article = new Article("JUnit Rocks", author);
1011
Assertions.assertEquals("item has been checked out", article.checkOut());
1112
}
1213

1314
@Test
1415
public void shouldDeclineIfNotAvailableToCheckout() {
15-
Article article = new Article("JUnit Rocks");
16+
Author author = new Author("Bob","[email protected]","bob.com");
17+
18+
Paper article = new Article("JUnit Rocks",author);
1619
article.checkOut();
1720

1821
Assertions.assertEquals("item is currently on loan", article.checkOut());
1922
}
2023

2124
@Test
2225
public void shouldCheckInIfOnLoan() {
23-
Article article = new Article("JUnit Rocks");
26+
Author author = new Author("Bob","[email protected]","bob.com");
27+
28+
Paper article = new Article("JUnit Rocks", author);
2429
article.checkOut();
2530

2631
Assertions.assertEquals("item has been checked in", article.checkIn());
2732
}
2833

2934
@Test
3035
public void shouldDeclineCheckInIfNotOnLoan() {
31-
Article article = new Article("JUnit Rocks");
36+
Author author = new Author("Bob","[email protected]","bob.com");
37+
38+
Paper article = new Article("JUnit Rocks",author);
3239

3340
Assertions.assertEquals("item is not currently on loan", article.checkIn());
3441
}

src/test/java/com/booleanuk/core/BookTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,37 @@
66
public class BookTest {
77
@Test
88
public void shouldCheckOutIfAvailable() {
9-
Book book = new Book("JUnit Rocks");
9+
Author author = new Author("Bob","[email protected]","bob.com");
10+
11+
Paper book = new Book("JUnit Rocks",author);
1012
Assertions.assertEquals("item has been checked out", book.checkOut());
1113
}
1214

1315
@Test
1416
public void shouldDeclineIfNotAvailableToCheckout() {
15-
Book book = new Book("JUnit Rocks");
17+
Author author = new Author("Bob","[email protected]","bob.com");
18+
19+
Paper book = new Book("JUnit Rocks",author);
1620
book.checkOut();
1721

1822
Assertions.assertEquals("item is currently on loan", book.checkOut());
1923
}
2024

2125
@Test
2226
public void shouldCheckInIfOnLoan() {
23-
Book book = new Book("JUnit Rocks");
27+
Author author = new Author("Bob","[email protected]","bob.com");
28+
29+
Paper book = new Book("JUnit Rocks",author);
2430
book.checkOut();
2531

2632
Assertions.assertEquals("item has been checked in", book.checkIn());
2733
}
2834

2935
@Test
3036
public void shouldDeclineCheckInIfNotOnLoan() {
31-
Book book = new Book("JUnit Rocks");
37+
Author author = new Author("Bob","[email protected]","bob.com");
38+
39+
Paper book = new Book("JUnit Rocks",author);
3240

3341
Assertions.assertEquals("item is not currently on loan", book.checkIn());
3442
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.booleanuk.core;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import javax.swing.*;
7+
8+
public class LibraryTest {
9+
10+
@Test
11+
public void addToStock(){
12+
Library library = new Library();
13+
Author author = new Author("Bob","[email protected]","bob.com");
14+
Article article = new Article("article", author);
15+
library.addToStock(article);
16+
17+
Book book = new Book("book", author);
18+
library.addToStock(book);
19+
20+
Newspaper newspaper = new Newspaper("newspaper");
21+
library.addToStock(newspaper);
22+
23+
Assertions.assertTrue(library.collection.contains(article));
24+
Assertions.assertTrue(library.collection.contains(book));
25+
Assertions.assertTrue(library.collection.contains(newspaper));
26+
}
27+
28+
@Test
29+
public void checkOutPaper(){
30+
Library library = new Library();
31+
Author author = new Author("Bob","[email protected]","bob.com");
32+
Article article = new Article("article", author);
33+
library.addToStock(article);
34+
35+
Assertions.assertEquals("item has been checked out", library.checkOutPaper("article"));
36+
Assertions.assertEquals("item is currently on loan", library.checkOutPaper("article"));
37+
38+
}
39+
40+
@Test
41+
public void checkInPaper(){
42+
Library library = new Library();
43+
Author author = new Author("Bob","[email protected]","bob.com");
44+
45+
Article article = new Article("article", author);
46+
library.addToStock(article);
47+
48+
Assertions.assertEquals("item is not currently on loan", library.checkInPaper("article"));
49+
library.checkOutPaper("article");
50+
Assertions.assertEquals("item has been checked in", library.checkInPaper("article"));
51+
}
52+
53+
54+
}

0 commit comments

Comments
 (0)