A robust library system must handle books even when their full details are not yet known. This project demonstrates Constructor Overloading in Java. We provide two ways to instantiate a Book object: one that uses default placeholder values ("Без названия", 0) and another that accepts specific data upon creation. This ensures every book object in our system is consistently initialized, avoiding null pointers or uninitialized states.
- Constructor Overloading: Implemented a default (no-args) constructor and a parameterized constructor.
- Default State: The no-args constructor assigns "Без названия" and 0 pages.
- Data Integrity: Used the
thiskeyword to map parameters to instance fields. - Service Separation: Delegated data presentation to the
BookReporterclass. - Naming Conventions: Followed PascalCase for classes and camelCase for variables.
- Java 8+ (Constructor Overloading, Default Values, SRP)
- Book: The Model. Contains two constructors for flexible initialization.
- LibraryService: The Logic. Handles book creation.
- BookReporter: The View. Formats and prints book details.
- LibraryApp: The Orchestrator. Manages the book registration flow.
[LIBRARY]: New arrivals registered:
Book: Untitled, pages: 0
Book: Java for Teapots, pages: 350
Project Structure:
JavaBasics_Task_235/
├── src/
│ └── com/
│ └── yurii/
│ └── pavlenko/
│ ├── Book.java
│ ├── LibraryService.java
│ ├── BookReporter.java
│ └── LibraryApp.java
└── README.md
Code
package com.yurii.pavlenko;
public class LibraryApp {
public static void main(String[] args) {
LibraryService librarian = new LibraryService();
BookReporter reporter = new BookReporter();
Book placeholderBook = librarian.createUnknownBook();
Book javaBook = librarian.createDefinedBook("Java for Teapots", 350);
System.out.println("[LIBRARY]: New arrivals registered:");
reporter.printBookDetails(placeholderBook);
reporter.printBookDetails(javaBook);
}
}package com.yurii.pavlenko;
public class Book {
public String bookTitle;
public int numberOfPages;
public Book() {
this.bookTitle = "Untitled";
this.numberOfPages = 0;
}
public Book(String title, int pages) {
this.bookTitle = title;
this.numberOfPages = pages;
}
}This project is licensed under the MIT License.
Copyright (c) 2026 Yurii Pavlenko
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...
License: MIT