Commit 3db3964e authored by Ben Anderson's avatar Ben Anderson

Initial Commit

parents
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="jUnitPractice" />
</profile>
</annotationProcessing>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.nisum.banderson</groupId>
<artifactId>jUnitPractice</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.7.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
import java.util.Objects;
public class Book {
private String title;
private String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public String getTitle() {
return title;
}
public Book setTitle(String title) {
this.title = title;
return this;
}
public String getAuthor() {
return author;
}
public Book setAuthor(String author) {
this.author = author;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Book)) return false;
Book book = (Book) o;
return Objects.equals(getTitle(), book.getTitle()) && Objects.equals(getAuthor(), book.getAuthor());
}
@Override
public int hashCode() {
return Objects.hash(getTitle(), getAuthor());
}
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Library {
private Map<Book, Integer> booksInStock;
public Library() {
booksInStock = new HashMap<>();
}
public List<Book> findBooksByTitle(String title) {
return booksInStock.keySet().stream()
.filter(book -> book.getTitle().equals(title))
.collect(Collectors.toList());
}
public List<Book> findBooksByAuthor(String author) {
return booksInStock.keySet().stream()
.filter(book -> book.getAuthor().equals(author))
.collect(Collectors.toList());
}
public boolean isBookAvailable(Book book) {
Integer quantity = booksInStock.get(book);
return quantity != null && quantity != 0;
}
public Book checkBookOut(Book book) {
if (!isBookAvailable(book)) {
return null;
}
booksInStock.put(book, booksInStock.get(book) - 1);
return book;
}
public void returnBook(Book book) throws Exception {
if (!booksInStock.containsKey(book)) {
throw new Exception("That book does not exist in our records!");
}
booksInStock.put(book, booksInStock.get(book) + 1);
}
public void buyBooks(Book... books) {
for (Book book : books) {
if (booksInStock.containsKey(book)) {
booksInStock.put(book, booksInStock.get(book) + 1);
} else {
booksInStock.put(book, 1);
}
}
}
}
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.Arrays;
import java.util.List;
class LibraryTest {
static Library library;
// @BeforeAll
// static void setUp() {
// library = new Library();
// }
@BeforeEach
void setBooks() {
library = new Library();
library.buyBooks(new Book(
"The Lion, The Witch, & The Wardrobe",
"C.S. Lewis"
), new Book(
"The Magician's Apprentice",
"C.S. Lewis"
), new Book(
"The Magician's Apprentice",
"C.S. Lewis"
), new Book(
"The Magician's Apprentice",
"C.S. Lewis"
), new Book(
"The Lord of The Rings",
"J.R.R. Tolkien"
), new Book(
"The Lord of The Rings",
"J.R.R. Tolkien"
)
);
}
@ParameterizedTest
@MethodSource
void findBooksByTitle(String title, Integer resultLength) {
List<Book> foundBooks = library.findBooksByTitle(title);
Assertions.assertEquals(resultLength, foundBooks.size());
}
private static List<Arguments> findBooksByTitle() {
return Arrays.asList(
Arguments.of("The Lord of The Rings", 2),
Arguments.of("The Lord of The Flies", 0)
);
}
@ParameterizedTest
@MethodSource
void findBooksByAuthor(String author, Integer resultLength) {
List<Book> foundBooks = library.findBooksByAuthor(author);
Assertions.assertEquals(resultLength, foundBooks.size());
}
private static List<Arguments> findBooksByAuthor() {
return Arrays.asList(
Arguments.of("C.S. Lewis", 4),
Arguments.of("J.K. Rowling", 0)
);
}
@ParameterizedTest
@MethodSource
void isBookAvailable(Book book, Boolean expected) {
Boolean actual = library.isBookAvailable(book);
Assertions.assertTrue(expected == actual);
}
private static List<Arguments> isBookAvailable() {
return Arrays.asList(
Arguments.of(new Book(
"The Magician's Apprentice",
"C.S. Lewis"
), true),
Arguments.of(new Book(
"Harry Potter and the Phoenix",
"J.K. Rowling"
), false)
);
}
@Test
void checkBookOut() {
Book book1 = new Book(
"Harry Potter and the Phoenix",
"J.K. Rowling"
);
Assertions.assertNull(library.checkBookOut(book1));
}
@Test
void returnBookWithError() {
Assertions.assertThrows(
Exception.class,
() -> library.returnBook(new Book("", ""))
);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment