Project design
- Java course
- Beginner
- Lesson 112/116
- 6 min
Design the beginner final skeleton.
Project: Library catalog
In this five-lesson beginner final you will build a library catalog app that combines everything you have learned. In the first lesson you define a Book class and print a book.
In later lessons you will add layers for adding books, borrowing, error handling and a menu on top of this base. Each lesson adds one piece with a code step; the starting code includes the previous lesson's solution. This shows how a real app grows.
Book class design
A Book class carries a book's title (title) and availability (available). The constructor sets the title and makes availability true. toString shows the state in a readable form.
class Book {
String title;
boolean available;
Book(String title) {
this.title = title;
this.available = true;
}
}toString returns [müsait] title if available, otherwise [ödünç] title. This way when you print the list, each book's state is visible. This class is the foundation of the project.
Practice Define the Book class
Define the Book class: String title and boolean available fields, a Book(String title) constructor (set available to true) and a toString() method (prefix [müsait] if available, else [ödünç] ). In main create a Book and print it.
Starter code
public class Main {
public static void main(String[] args) {
// Create a Book and print it
}
}
class Book {
// Define title (String) and available (boolean) fields
// Constructor: set title, set available to true
// toString: "[müsait] title" if available, otherwise "[ödünç] title"
}Sample solution
class Book {
String title;
boolean available;
Book(String title) {
this.title = title;
this.available = true;
}
public String toString() {
return (available ? "[müsait] " : "[ödünç] ") + title;
}
}
// inside main:
Book b = new Book("Suç ve Ceza");
System.out.println(b);Expected output
[müsait] Suç ve Ceza
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What will we manage in this project?
- Books Correct answer
- Customers
- Orders
- Invoices
Explanation: the project is a library catalog app; you add and borrow books.
2 Define the available field
class Book {
String title;
____ available;
}
class Book {
String title;
boolean available;
}
Explanation: available is a true/false value, so it is boolean.
3 Match each piece to its purpose
| Term | Meaning |
|---|---|
title |
Book title |
available |
Availability |
Book(String) |
Constructor |
toString() |
String representation |