Warmup¶
1. Bibliotheksverwaltungssystem¶
Erstellen Sie ein einfaches Bibliotheksverwaltungssystem in Java. Das System sollte folgende Funktionen unterstützen:
Hinzufügen von Büchern zur Bibliothek.
Ausleihen von Büchern an Benutzer.
Rückgabe von Büchern.
Anzeigen der verfügbaren Bücher.
Tipp
🚩 Erstellen Sie dazu zwei Klassen: Buch und Bibliothek.
Buch¶
Eigenschaften¶
title (String)Der Titel des Buchs.author (String)Der Autor des Buchs.available (boolean)Gibt an, ob das Buch ausgeliehen ist oder nicht.
Konstruktor¶
Ein Konstruktor, der Titel und Autor des Buchs als Parameter nimmt und das Buch initialisiert. Das Buch wird standardmäßig als nicht ausgeliehen markiert.
Lösung anzeigen
1// Represents one book with basic data and availability status.
2public class Book {
3
4 // Book title (used to find the book in the library).
5 private String title;
6 // Author name for display purposes.
7 private String author;
8 // true = can be borrowed, false = currently borrowed.
9 private boolean available;
10
11 // Creates a new book that is available by default.
12 public Book(String title, String author) {
13 this.title = title;
14 this.author = author;
15 this.available = true;
16 }
17
18 // Returns the current title.
19 public String getTitle() {
20 return title;
21 }
22
23 // Updates the title.
24 public void setTitle(String title) {
25 this.title = title;
26 }
27
28 // Returns the author name.
29 public String getAuthor() {
30 return author;
31 }
32
33 // Updates the author name.
34 public void setAuthor(String author) {
35 this.author = author;
36 }
37
38 // Tells whether the book can be borrowed right now.
39 public boolean isAvailable() {
40 return available;
41 }
42
43 // Sets availability after borrowing/returning.
44 public void setAvailable(boolean available) {
45 this.available = available;
46 }
47
48 // Returns a readable text version of this book.
49 @Override
50 public String toString() {
51 return "Book{" +
52 "title='" + title + '\'' +
53 ", author='" + author + '\'' +
54 ", available=" + available +
55 '}';
56 }
57}
Bibliothek¶
Eigenschaften¶
books (Arraylist)Eine Liste, die die Bücher in der Bibliothek enthält.
Methoden¶
addBookFügt ein Buch zur Bibliothek hinzu.borrowBookMarkiert ein Buch als ausgeliehen. Falls das nicht verfügbar ist, wirft eine NotAvailableException.returnBookMarkiert ein Buch als zurückgegeben und setzt es auf verfügbar.showAvailableBooksGibt die Liste er verfügbaren Bücher in der Bibliothek aus.
Lösung anzeigen
1import java.util.ArrayList;
2
3// Stores books and provides borrow/return operations.
4public class Library {
5
6 // Internal list of all books in this library.
7 private ArrayList<Book> books;
8
9 // Creates an empty library.
10 public Library() {
11 books = new ArrayList<>();
12 }
13
14 // Adds a new book to the library collection.
15 public void addBook(Book newBook) {
16 books.add(newBook);
17 }
18
19 // Borrows a book by title, or throws if not possible.
20 public void borrowBook(String title) throws NotAvailableException {
21 for (Book book : books) {
22 if (book.getTitle().equals(title)) {
23 if (!book.isAvailable()) {
24 throw new NotAvailableException(
25 "The book \"" + title + "\" is not available.");
26 }
27 book.setAvailable(false);
28 return;
29 }
30 }
31
32 throw new NotAvailableException(
33 "The book \"" + title + "\" is not available at this library.");
34 }
35
36 // Returns a borrowed book and marks it as available again.
37 public void returnBook(String title) {
38 for (Book book : books) {
39 if (book.getTitle().equals(title)) {
40 book.setAvailable(true);
41 return;
42 }
43 }
44 }
45
46 // Prints only books that are currently available.
47 public void showAvailableBooks() {
48 for (Book book : books) {
49 if (book.isAvailable()) {
50 System.out.println(book);
51 }
52 }
53 }
54}
Exceptions¶
NotAvailableExceptionEine benutzerdefinierte Ausnahme, die geworfen wird, wenn ein Benutzer versucht, ein nicht verfügbares Buch auszuleihen.
Wichtig
🚩 Implementieren Sie die obigen Klassen und Methoden. Verwenden Sie ArrayListen, Konstruktoren und Referenzdatentypen wie gefordert.
Bemerkung
Vergessen Sie nicht, Getter- und Setter-Methoden sowie eine toString()-Methode für die Buch-Klasse zu implementieren, um den Zugriff auf die Eigenschaften zu erleichtern und eine aussagekräftige Darstellung der Objekte zu erhalten.
Lösung anzeigen
1// Custom exception for cases where a book cannot be borrowed.
2public class NotAvailableException extends Exception {
3
4 // Creates the exception with a helpful error message.
5 public NotAvailableException(String message) {
6 super(message);
7 }
8}
2. Bibliotheksverwaltungssystem - erweitert¶
Erweitern Sie das oben beschriebene Beispiel um Videospiele. Neben Büchern können nun auch Videospiele ausgeliehen werden.
Videospiele haben folgende Eigenschaften:¶
titel (String)Der Titel des Videospiels.genre (String)Das Genre des Videospiels (Jump’n’Run, Racing, …)ausgeliehen (boolean)Gibt an, ob das Videospiel ausgeliehen ist oder nicht.
Konstruktor¶
Ein Konstruktor, der Titel und Genre des Spiels als Parameter nimmt und das Videospiel initialisiert. Das Videospiel wird standardmäßig als nicht ausgeliehen markiert.
Wichtig
🚩 Implementieren Sie die oben beschriebenen Klassen und Methoden. Nutzen Sie Vererbung und Polymorphismus, um die Implementierung zu vereinfachen.
Lösung anzeigen
1// Contract for items that can be rented from the library.
2public interface Rentable {
3 // Returns the display title of the item.
4 String getTitle();
5
6 // Returns whether the item can currently be borrowed.
7 Boolean isAvailable();
8
9 // Updates the availability after borrow/return actions.
10 void setAvailable(boolean available);
11}
1// Represents a video game that can be rented.
2public class VideoGame implements Rentable {
3
4 // Name shown to users.
5 private String title;
6 // Category of the game (for example, RPG or Action).
7 private String genre;
8 // true if available to borrow, false if currently borrowed.
9 private boolean available;
10
11 // Creates a new game that is available by default.
12 public VideoGame(String title, String genre) {
13 this.title = title;
14 this.genre = genre;
15 this.available = true;
16 }
17
18 // Returns the game title.
19 @Override
20 public String getTitle() {
21 return title;
22 }
23
24 // Returns the current availability status.
25 @Override
26 public Boolean isAvailable() {
27 return available;
28 }
29
30 // Marks the game as available or borrowed.
31 @Override
32 public void setAvailable(boolean available) {
33 this.available = available;
34 }
35
36 // Returns the genre of this game.
37 public String getGenre() {
38 return genre;
39 }
40
41 // Returns a readable text representation of the game.
42 @Override
43 public String toString() {
44 return "VideoGame{" +
45 "title='" + title + '\'' +
46 ", genre='" + genre + '\'' +
47 '}';
48 }
49}
1public class Library {
2
3 // Replace the internal list of books with rentables.
4 private ArrayList<Rentable> rentables;