-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryShelves.java
More file actions
72 lines (52 loc) · 2.05 KB
/
Copy pathLibraryShelves.java
File metadata and controls
72 lines (52 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.LinkedList;
import java.util.Scanner;
public class LibraryShelves {
// Define a Hash table with linked lists for chaning
private LinkedList<String> [] shelves;
public LibraryShelves(int size){
// Conent the shevle variable to new linklist use parmertize fucntion size
shelves = new LinkedList[size];
// iniziating size using for loop
for(int i=0; i<size; i++)
{
// Size by shelves index count
shelves[i] = new LinkedList<>();
}
}
// Hash Fuction : calculate shelf number
private int hashFunction(String bookTitle){
// get the book title(letters) and divied shelves size ______---
return bookTitle.length() % shelves.length;
}
// Add a book to the hash Table the Variable is "Book title"
public void addBook(String bookTitle){
// Create the Shelf by BookTitle
int shelfNumber = hashFunction(bookTitle);
// add the book to divied shelves _______---
shelves[shelfNumber].addLast(bookTitle);
}
// Display the books on each shelf
public void displayShelves(){
for (int i=0; i<shelves.length; i++)
{
System.out.println("Shelf " + i + " : " +shelves[i]);
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in)
// Create a library with 5 shelves / OBj for class
LibraryShelves library = new LibraryShelves(6);
// Enter book name
System.out.println("Enter Book Name");
String libinpu = scan.nextLine();
// Connect the objet with
libinpu.has
library.addBook("The Avengers");
library.addBook("The Shannara Chronicles");
library.addBook("National Treasure");
library.addBook("Harry Potter");
library.addBook("One Piece");
// Display The Book on each shelf
library.displayShelves();
}
}