Putting it together
- Java course
- Beginner
- Lesson 116/116
- 6 min
Combine all parts into a working app.
Combine with a menu
In the final lesson you combine all pieces in a menu. You read a choice with Scanner; 1 adds a book, 2 borrows one. Error handling is built into the menu.
System.out.print("Seç: ");
int choice = input.nextInt();
if (choice == 1) {
System.out.print("Başlık: ");
input.nextLine();
cat.addBook(input.nextLine());
} else if (choice == 2) {
try { cat.borrowBook(); }
catch (IllegalStateException e) {
System.out.println("Hata: " + e.getMessage());
}
}After nextInt you clear the leftover line with nextLine; then you read the real title. This solves the nextInt/nextLine trap you learned earlier. The menu gathers all skills in one flow.
All pieces together
In this lesson the Book class, the Catalog class (addBook, borrowBook, throw) and the Scanner menu come together. The user adds a book and borrows one; all errors are caught.
The starting code includes the previous lessons' solutions; you only write the menu loop. This is the beginner-level final task: variables, conditionals, loops, collections, methods, exceptions and input/output all in one app.
Practice Run the menu
Write the menu in main: ask with System.out.print("Seç: "), read the choice with nextInt(). If choice == 1, print("Başlık: "), clear the line with nextLine(), read the title with nextLine() again and call cat.addBook(...). If choice == 2, try { cat.borrowBook(); } and catch IllegalStateException to print Hata: . Finally print cat.books. The user will type 1 then Suç ve Ceza.
Starter code
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Catalog cat = new Catalog();
Scanner input = new Scanner(System.in);
// Print Seç:, read choice with nextInt, if 1 ask title and add, if 2 call borrowBook
// Then print cat.books
}
}
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;
}
}
class Catalog {
ArrayList<Book> books = new ArrayList<>();
void addBook(String title) {
books.add(new Book(title));
}
void borrowBook() {
for (Book b : books) {
if (b.available) {
b.available = false;
return;
}
}
throw new IllegalStateException("Müsait kitap yok");
}
}Sample solution
System.out.print("Seç: ");
int choice = input.nextInt();
if (choice == 1) {
System.out.print("Başlık: ");
input.nextLine();
cat.addBook(input.nextLine());
} else if (choice == 2) {
try { cat.borrowBook(); }
catch (IllegalStateException e) {
System.out.println("Hata: " + e.getMessage());
}
}
System.out.println(cat.books);Expected output
Seç: Başlık: [[müsait] Suç ve Ceza]
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match each menu choice to its action
| Term | Meaning |
|---|---|
1 |
Add book |
2 |
Borrow |
Seç: |
Ask for input |
Hata: |
Error message |
2 Create the Scanner
Scanner input = new Scanner(System.____);
Scanner input = new Scanner(System.in);
Explanation: System.in is the console input and you bind Scanner to it.
3 What happens when 1 is selected in the menu?
- A book is borrowed
- An error is thrown
- The program closes
- A title is asked and added Correct answer
Explanation: when 1 is selected, a title is asked, read with nextLine and added with addBook.