פרויקט קטן: המדף כמערכת מחלקותMini project: the shelf as classes
מתכננים ובונים יחד את Shelf, Book ו-Author: אחריות לכל מחלקה, ממשק נקי ובדיקה ידנית.
Designing and building Shelf, Book and Author together: one job per class, a clean interface and manual testing.
ספר שמחזיק סופרA book that holds an author
תזכורת: מה ביקשוReminder: the task
כתבו את Author (שם, equals ו-hashCode לפי השם, toString שמחזיר את השם) ואת Book (כותר, Author, דירוג, getters, toString Dune (5)). הדפיסו Dune (5) by Herbert ואז true (הסופר שווה לסופר חדש באותו שם).
Write Author (a name, equals and hashCode by name, toString returning the name) and Book (a title, an Author, a rating, getters, toString Dune (5)). Print Dune (5) by Herbert then true (the author equals a new author with the same name).
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
Author herbert = new Author("Herbert");
Book dune = new Book("Dune", herbert, 5);
System.out.println(dune + " by " + dune.getAuthor());
System.out.println(dune.getAuthor().equals(new Author("Herbert")));
}
}
class Shelf {
private final List<Book> books = new ArrayList<>();
void add(Book b) {
books.add(b);
}
}
class Book {
private final String title;
private final Author author;
private final int rating;
Book(String title, Author author, int rating) {
this.title = title;
this.author = author;
this.rating = rating;
}
String getTitle() {
return title;
}
Author getAuthor() {
return author;
}
int getRating() {
return rating;
}
@Override
public String toString() {
return title + " (" + rating + ")";
}
}
class Author {
private final String name;
Author(String name) {
this.name = name;
}
String getName() {
return name;
}
@Override
public boolean equals(Object o) {
return o instanceof Author a && name.equals(a.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return name;
}
}Dune (5) by Herbert true
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
המדף מסרב לכפוליםThe shelf refuses duplicates
תזכורת: מה ביקשוReminder: the task
ב-Shelf (עם private final List<Book> books) כתבו boolean add(Book b) שמחזירה false ולא מוסיפה אם כבר יש ספר עם אותו כותר ואותו סופר, ו-int size(). הדפיסו true, false, 1.
In Shelf (with private final List<Book> books) write boolean add(Book b) that returns false and adds nothing if a book with the same title and author is already there, and int size(). Print true, false, 1.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
Shelf s = new Shelf();
Author a = new Author("Austen");
System.out.println(s.add(new Book("Emma", a, 4)));
System.out.println(s.add(new Book("Emma", new Author("Austen"), 4)));
System.out.println(s.size());
}
}
class Shelf {
private final List<Book> books = new ArrayList<>();
boolean add(Book b) {
for (Book x : books) {
if (x.getTitle().equals(b.getTitle()) && x.getAuthor().equals(b.getAuthor())) {
return false;
}
}
books.add(b);
return true;
}
int size() {
return books.size();
}
}
class Book {
private final String title;
private final Author author;
private final int rating;
Book(String title, Author author, int rating) {
this.title = title;
this.author = author;
this.rating = rating;
}
String getTitle() {
return title;
}
Author getAuthor() {
return author;
}
int getRating() {
return rating;
}
@Override
public String toString() {
return title + " (" + rating + ")";
}
}
class Author {
private final String name;
Author(String name) {
this.name = name;
}
String getName() {
return name;
}
@Override
public boolean equals(Object o) {
return o instanceof Author a && name.equals(a.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return name;
}
}true false 1
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
חיפוש לפי כותרFind by title
תזכורת: מה ביקשוReminder: the task
Book find(String title) מחזירה את הספר (בלי תלות באותיות גדולות וקטנות) או null. הדפיסו found Dune (5) עבור "dune", ואז not found עבור Hamlet.
Book find(String title) returns the book (ignoring upper and lower case) or null. Print found Dune (5) for "dune", then not found for Hamlet.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
Shelf s = new Shelf();
s.add(new Book("Dune", new Author("Herbert"), 5));
Book b = s.find("dune");
System.out.println(b == null ? "not found" : "found " + b);
System.out.println(s.find("Hamlet") == null ? "not found" : "found");
}
}
class Shelf {
private final List<Book> books = new ArrayList<>();
void add(Book b) {
books.add(b);
}
Book find(String title) {
for (Book b : books) {
if (b.getTitle().equalsIgnoreCase(title)) {
return b;
}
}
return null;
}
}
class Book {
private final String title;
private final Author author;
private final int rating;
Book(String title, Author author, int rating) {
this.title = title;
this.author = author;
this.rating = rating;
}
String getTitle() {
return title;
}
Author getAuthor() {
return author;
}
int getRating() {
return rating;
}
@Override
public String toString() {
return title + " (" + rating + ")";
}
}
class Author {
private final String name;
Author(String name) {
this.name = name;
}
String getName() {
return name;
}
@Override
public boolean equals(Object o) {
return o instanceof Author a && name.equals(a.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return name;
}
}found Dune (5) not found
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
כל הספרים של סופרAll books by an author
תזכורת: מה ביקשוReminder: the task
List<Book> byAuthor(Author a) מחזירה רשימה חדשה עם הספרים של הסופר. הוסיפו את Emma ו-Persuasion של Austen ואת Dune, והדפיסו [Emma (4), Persuasion (5)].
List<Book> byAuthor(Author a) returns a new list with the author's books. Add Austen's Emma and Persuasion and Dune, and print [Emma (4), Persuasion (5)].
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
Shelf s = new Shelf();
s.add(new Book("Emma", new Author("Austen"), 4));
s.add(new Book("Dune", new Author("Herbert"), 5));
s.add(new Book("Persuasion", new Author("Austen"), 5));
System.out.println(s.byAuthor(new Author("Austen")));
}
}
class Shelf {
private final List<Book> books = new ArrayList<>();
void add(Book b) {
books.add(b);
}
List<Book> byAuthor(Author a) {
List<Book> out = new ArrayList<>();
for (Book b : books) {
if (b.getAuthor().equals(a)) {
out.add(b);
}
}
return out;
}
}
class Book {
private final String title;
private final Author author;
private final int rating;
Book(String title, Author author, int rating) {
this.title = title;
this.author = author;
this.rating = rating;
}
String getTitle() {
return title;
}
Author getAuthor() {
return author;
}
int getRating() {
return rating;
}
@Override
public String toString() {
return title + " (" + rating + ")";
}
}
class Author {
private final String name;
Author(String name) {
this.name = name;
}
String getName() {
return name;
}
@Override
public boolean equals(Object o) {
return o instanceof Author a && name.equals(a.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return name;
}
}[Emma (4), Persuasion (5)]
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
דירוג ממוצעAverage rating
תזכורת: מה ביקשוReminder: the task
double averageRating() מחזירה את ממוצע הדירוגים, ו-0.0 למדף ריק (בלי חריגה). הדפיסו 0.0 לפני ההוספות ו-4.5 אחרי Emma (4) ו-Dune (5).
double averageRating() returns the average rating, and 0.0 for an empty shelf (no exception). Print 0.0 before adding and 4.5 after Emma (4) and Dune (5).
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
Shelf s = new Shelf();
System.out.println(s.averageRating());
s.add(new Book("Emma", new Author("Austen"), 4));
s.add(new Book("Dune", new Author("Herbert"), 5));
System.out.println(s.averageRating());
}
}
class Shelf {
private final List<Book> books = new ArrayList<>();
void add(Book b) {
books.add(b);
}
double averageRating() {
if (books.isEmpty()) {
return 0.0;
}
int sum = 0;
for (Book b : books) {
sum += b.getRating();
}
return (double) sum / books.size();
}
}
class Book {
private final String title;
private final Author author;
private final int rating;
Book(String title, Author author, int rating) {
this.title = title;
this.author = author;
this.rating = rating;
}
String getTitle() {
return title;
}
Author getAuthor() {
return author;
}
int getRating() {
return rating;
}
@Override
public String toString() {
return title + " (" + rating + ")";
}
}
class Author {
private final String name;
Author(String name) {
this.name = name;
}
String getName() {
return name;
}
@Override
public boolean equals(Object o) {
return o instanceof Author a && name.equals(a.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return name;
}
}0.0 4.5
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מדף, צעד 18: Shelf, Book ו-Author עובדים יחדShelf, step 18: Shelf, Book and Author working together
תזכורת: מה ביקשוReminder: the task
חברו הכול: Shelf.add (בלי כפולים) ו-Shelf.report() שמדפיסה את מספר הספרים, את הכותרים לפי סופר (לפי סדר ההופעה), ואת הספר עם הדירוג הגבוה. עם Emma, Dune, Persuasion ושוב Emma, הדוח הוא:
Shelf: 3 books / Austen: Emma, Persuasion / Herbert: Dune / Best: Dune (5).
Put it all together: Shelf.add (no duplicates) and Shelf.report() printing the number of books, the titles per author (in order of appearance), and the top-rated book. With Emma, Dune, Persuasion and Emma again, the report is:
Shelf: 3 books / Austen: Emma, Persuasion / Herbert: Dune / Best: Dune (5).
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Shelf s = new Shelf();
Author austen = new Author("Austen");
s.add(new Book("Emma", austen, 4));
s.add(new Book("Dune", new Author("Herbert"), 5));
s.add(new Book("Persuasion", austen, 3));
s.add(new Book("Emma", austen, 4));
s.report();
}
}
class Shelf {
private final List<Book> books = new ArrayList<>();
boolean add(Book b) {
for (Book x : books) {
if (x.getTitle().equals(b.getTitle()) && x.getAuthor().equals(b.getAuthor())) {
return false;
}
}
books.add(b);
return true;
}
int size() {
return books.size();
}
void report() {
System.out.println("Shelf: " + books.size() + " books");
Map<Author, List<String>> groups = new LinkedHashMap<>();
Book best = null;
for (Book b : books) {
groups.computeIfAbsent(b.getAuthor(), k -> new ArrayList<>()).add(b.getTitle());
if (best == null || b.getRating() > best.getRating()) {
best = b;
}
}
for (Map.Entry<Author, List<String>> e : groups.entrySet()) {
System.out.println(e.getKey() + ": " + String.join(", ", e.getValue()));
}
System.out.println("Best: " + best);
}
}
class Book {
private final String title;
private final Author author;
private final int rating;
Book(String title, Author author, int rating) {
this.title = title;
this.author = author;
this.rating = rating;
}
String getTitle() {
return title;
}
Author getAuthor() {
return author;
}
int getRating() {
return rating;
}
@Override
public String toString() {
return title + " (" + rating + ")";
}
}
class Author {
private final String name;
Author(String name) {
this.name = name;
}
String getName() {
return name;
}
@Override
public boolean equals(Object o) {
return o instanceof Author a && name.equals(a.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return name;
}
}Shelf: 3 books Austen: Emma, Persuasion Herbert: Dune Best: Dune (5)
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
השאלת ספריםLending books
תזכורת: מה ביקשוReminder: the task
הוסיפו ל-Shelf מעקב השאלות: Map<String, String> lentTo (כותר → שם). lend(title, person) מחזירה false אם הספר כבר מושאל, giveBack(title) מחזירה אותו, ו-status(title) מחזירה lent to Noa או on the shelf. הדפיסו true false, lent to Noa, on the shelf.
Add lending to Shelf: Map<String, String> lentTo (title → name). lend(title, person) returns false if the book is already lent, giveBack(title) returns it, and status(title) returns lent to Noa or on the shelf. Print true false, lent to Noa, on the shelf.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Shelf s = new Shelf();
s.add(new Book("Dune", new Author("Herbert"), 5));
System.out.println(s.lend("Dune", "Noa") + " " + s.lend("Dune", "Avi"));
System.out.println(s.status("Dune"));
s.giveBack("Dune");
System.out.println(s.status("Dune"));
}
}
class Shelf {
private final List<Book> books = new ArrayList<>();
private final Map<String, String> lentTo = new HashMap<>();
void add(Book b) {
books.add(b);
}
boolean lend(String title, String person) {
if (lentTo.containsKey(title)) {
return false;
}
lentTo.put(title, person);
return true;
}
void giveBack(String title) {
lentTo.remove(title);
}
String status(String title) {
return lentTo.containsKey(title) ? "lent to " + lentTo.get(title) : "on the shelf";
}
}
class Book {
private final String title;
private final Author author;
private final int rating;
Book(String title, Author author, int rating) {
this.title = title;
this.author = author;
this.rating = rating;
}
String getTitle() {
return title;
}
Author getAuthor() {
return author;
}
int getRating() {
return rating;
}
@Override
public String toString() {
return title + " (" + rating + ")";
}
}
class Author {
private final String name;
Author(String name) {
this.name = name;
}
String getName() {
return name;
}
@Override
public boolean equals(Object o) {
return o instanceof Author a && name.equals(a.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return name;
}
}true false lent to Noa on the shelf
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
בדיקות ידניותManual tests
תזכורת: מה ביקשוReminder: the task
כתבו ב-Main מתודת עזר static void check(boolean ok, String name) שמדפיסה PASS: ... או FAIL: ... וסופרת. בדקו 4 דברים על המדף: הוספה, דחיית כפול, גודל 1, וחיפוש בלי תלות באותיות. בסוף 4/4 passed.
In Main write a helper static void check(boolean ok, String name) printing PASS: ... or FAIL: ... and counting. Check 4 things about the shelf: adding, rejecting a duplicate, size 1, and case-insensitive find. Finally 4/4 passed.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
import java.util.ArrayList;
import java.util.List;
public class Main {
static int passed = 0;
static int total = 0;
static void check(boolean ok, String name) {
total++;
if (ok) {
passed++;
}
System.out.println((ok ? "PASS: " : "FAIL: ") + name);
}
public static void main(String[] args) {
Shelf s = new Shelf();
Author a = new Author("Austen");
check(s.add(new Book("Emma", a, 4)), "add a new book");
check(!s.add(new Book("Emma", a, 4)), "reject a duplicate");
check(s.size() == 1, "size is 1");
check(s.find("EMMA") != null, "find ignores case");
System.out.println(passed + "/" + total + " passed");
}
}
class Shelf {
private final List<Book> books = new ArrayList<>();
boolean add(Book b) {
for (Book x : books) {
if (x.getTitle().equals(b.getTitle()) && x.getAuthor().equals(b.getAuthor())) {
return false;
}
}
books.add(b);
return true;
}
int size() {
return books.size();
}
Book find(String title) {
for (Book b : books) {
if (b.getTitle().equalsIgnoreCase(title)) {
return b;
}
}
return null;
}
}
class Book {
private final String title;
private final Author author;
private final int rating;
Book(String title, Author author, int rating) {
this.title = title;
this.author = author;
this.rating = rating;
}
String getTitle() {
return title;
}
Author getAuthor() {
return author;
}
int getRating() {
return rating;
}
@Override
public String toString() {
return title + " (" + rating + ")";
}
}
class Author {
private final String name;
Author(String name) {
this.name = name;
}
String getName() {
return name;
}
@Override
public boolean equals(Object o) {
return o instanceof Author a && name.equals(a.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return name;
}
}PASS: add a new book PASS: reject a duplicate PASS: size is 1 PASS: find ignores case 4/4 passed
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.