Repository navigation
Expand file tree
/
Copy pathBooks.java
More file actions
166 lines (144 loc) · 4.68 KB
/
Copy pathBooks.java
File metadata and controls
166 lines (144 loc) · 4.68 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import java.util.Objects;
import java.util.Scanner;
public class Books {
private String title;
private String author;
private boolean isAvailable;
public Books(String title, String author, boolean isAvailable) {
this.title = title;
this.author = author;
this.isAvailable = isAvailable;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public boolean getIsAvailable() {
return isAvailable;
}
public void setTitle(String title) {
this.title = title;
}
public void setAuthor(String author) {
this.author = author;
}
public void setAvailable(boolean available) {
isAvailable = available;
}
public String toString() {
return title + " " + author + " " + isAvailable;
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Books books = (Books) o;
return title.equals(books.title) && author.equals(books.author);
}
@Override
public int hashCode() {
return Objects.hash(title, author);
}
}
class Library {
private Books[] books;
private int indexAvailable = 0;
public Library() {
books = new Books[10];
}
public void addBooks(String title, String author, boolean isAvailable) {
if (indexAvailable < books.length) {
books[indexAvailable++] = new Books(title, author, isAvailable);
} else {
System.out.println("Library is full");
}
}
public void borrowBook(String title) {
for (Books book : books) {
if (book != null && book.getTitle().equals(title)) {
if (book.getIsAvailable()) {
book.setAvailable(false);
System.out.println("Borrowed!!!");
} else {
System.out.println("Not Available!!!");
}
return;
}
}
System.out.println("Book not found");
}
public void returnBook(String title) {
for (Books book : books) {
if (book != null && book.getTitle().equals(title)) {
if (!book.getIsAvailable()) {
book.setAvailable(true);
System.out.println("Book returned successfully.");
} else {
System.out.println("Book is already available.");
}
return;
}
}
System.out.println("Book not found.");
}
public void replaceBook(String title, String author) {
for (int i = 0; i < books.length; i++) {
if (books[i] != null && books[i].getTitle().equals(title)) {
books[i] = new Books(title, author, true);
return;
}
}
}
public void replaceBook(Books oldBook, Books newBook) {
for (int i = 0; i < books.length; i++) {
if (books[i] != null && books[i].equals(oldBook)) {
books[i] = newBook;
return;
}
}
}
public void viewAvailableBooks() {
for (Books book : books) {
if (book != null && book.getIsAvailable()) {
System.out.println(book);
}
}
}
}
class BookMain {
public static void main(String[] args) {
Library library = new Library();
Scanner scanner = new Scanner(System.in);
library.addBooks("Java", "Java", true);
library.addBooks("Python", "Python", true);
library.addBooks("C", "C", true);
while (true) {
System.out.println("\nMenu:");
System.out.println("1. View available books");
System.out.println("2. Borrow a book");
System.out.println("3. Return a book");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
library.viewAvailableBooks();
break;
case 2:
System.out.print("Enter the title of the book to borrow: ");
library.borrowBook(scanner.nextLine());
break;
case 3:
System.out.print("Enter the title of the book to return: ");
library.returnBook(scanner.nextLine());
break;
case 4:
System.out.println("BYEEE");
scanner.close();
System.exit(0);
}
}
}
}