Lab#SE03-1: Library/Book, Core-Model
Java SE Lab 03 part 1
📘 Linux Lab#SE03-1: Library/Book
Create a Maven/Gradle
Java SE Project
.- And from Lab SE#00-3 use the relationship between
User
andBook
.
- And from Lab SE#00-3 use the relationship between
Add the needed dependencies:
- Lombok
- Junit
- Java Faker
Create three
Java
classes to define the Model. Java SE Classes:- Person: User, Worker, Staf, Freelancer, Author
- Publication: Book, BooItem, Multimedia, MultimediaItem
With
Junit
dependency create unitary test to test objects and operations, and then integration tests.From here you should add new features once all the test are passed.
- Operations, you could think in a Manager Class o similar
- New fields/attributes and new classes
- New compositions and inherence: expand your model
- Use Factory design pattern to create new objects
1 Core classes and dependencies
You could then use these classes as follows:
moviemodel.java
import lombok.Data;
@Data
class Person {
private String name;
private int age;
}
class User extends Person {
private String username;
private String password;
}
class Staff extends Person {
private String department;
}
class Freelancer extends Person {
private String skills;
}
interface Publication {
void publish();
}
class Author extends Person {
private String authorName;
}
interface Publication {
void publish();
}
class Book implements Publication {
private String title;
private int pages;
private Author author;
@Override
public void publish() {
System.out.println("Publishing book: " + title);
}
}
class BookItem extends Book {
private int ISBN;
}
class Multimedia extends Publication {
private String title;
private String format;
}
class MultimediaItem extends Multimedia {
private int itemId;
}
1.1 Dependency: Lombok
Use Lombok
, link:
The @Data annotation is a
Lombok
annotation that generates getters and setters for the fields of a class, as well as equals, hashCode, and toString methods based on the fields.The @AllArgsConstructor annotation is a
Lombok
annotation that generates a constructor with all of the class’s fields as arguments.The @NoArgsConstructor annotation is a
Lombok
annotation that generates a no-argument constructor for a class.
@Data All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, @Setter on all non-final fields, and @RequiredArgsConstructor.
Adding lombok to your pom file: to include lombok as a provided dependency, add it to your
1.2 Dependency: Java Faker
Use Java Faker:
In pom.xml
, add the following xml dependency between
pom.xml
In your Java code:
testfaker.java
import com.github.javafaker.Faker;
import org.junit.jupiter.api.Test;
Faker faker = new Faker();
String name = faker.name().fullName(); // Miss Samanta Schmidt
String firstName = faker.name().firstName(); // Emory
String lastName = faker.name().lastName(); // Barton
String streetAddress = faker.address().streetAddress(); // 60018 Sawayn Brooks Suite 449
1.3 Dependency: JUnit 5
To configure support for JUnit Jupiter based tests, configure test scoped dependencies on the JUnit Jupiter API and the JUnit Jupiter TestEngine implementation similar to the following.
more on Java SE TDD