-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrarySystem.java
More file actions
152 lines (123 loc) · 4.81 KB
/
LibrarySystem.java
File metadata and controls
152 lines (123 loc) · 4.81 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
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class LibrarySystem {
private static Map<Integer, Books> books = new HashMap<>();
private static int bookCounter = 1; //system's book ID number starts at 1
private static Scanner scanner = new Scanner(System.in);
//adds a book to the system. Asks for the book's title, genre, page count. Makes checked in true when send to maps
public static void addBook(){
System.out.println("Enter book's title.");
String title = scanner.nextLine();
System.out.println("Enter book's genre.");
String genre = scanner.nextLine();
System.out.println("Enter book's page count.");
int pageCount = scanner.nextInt();
books.put(bookCounter, new Books(title,genre,pageCount));
System.out.println("The book has been successfully been added to the system. It's # is: "+ bookCounter+"\n");
bookCounter++; //increments for the next book.
}
//checks in a book which changes the availability status
public static void checkInBook() {
System.out.println("Enter the book's #: ");
int bookIndex = scanner.nextInt();
scanner.nextLine();
Books userBook = books.get(bookIndex);
if (userBook.isCheckedIn()==false) {
userBook.checkIn();
System.out.println("You have checked in: " + userBook.getTitle());
} else if (userBook.isCheckedIn()) {
System.out.println("The book is already checked in.");
} else {
System.out.println("Book not found.");
}
}
//checks out a book which changes the availability status
// Check out a book
public static void checkOutBook() {
System.out.println("Enter the ID of the book to check out:");
int id = scanner.nextInt();
scanner.nextLine(); // Consume newline
Books book = books.get(id);
if (book != null && book.isCheckedIn()) {
book.checkOut();
System.out.println("You have checked out: " + book.getTitle());
} else if (book != null) {
System.out.println("The book is already checked out.");
} else {
System.out.println("Book not found.");
}
// books.put(bookCounter, book);
}
public static void viewBooks() {
books.forEach((key, value) -> System.out.println("\nBook " + key + ": " + "\nTitle - " + value.getTitle() + "\nGenre - " + value.getGenre() + "\nPage Count - " + value.getPageCount() + "\nAvailable - " + value.isCheckedIn()));
}
public static void banBook() {
System.out.println("Enter the book's #: ");
int bookIndex = scanner.nextInt();
scanner.nextLine();
Books userBook = books.get(bookIndex);
books.remove(bookIndex);
System.out.println("You have banned " + userBook.getTitle() + "...but now you must replace it >:(");
System.out.println("Enter book's title.");
String title = scanner.nextLine();
System.out.println("Enter book's genre.");
String genre = scanner.nextLine();
System.out.println("Enter book's page count.");
int pageCount = scanner.nextInt();
books.put(bookIndex, new Books(title,genre,pageCount));
System.out.println("You have successfully swapped " + userBook.getTitle() + " with " + title + " :D");
}
public static void displayMenu() {
int choice;
do {
System.out.println("Library Menu:");
System.out.println("1. Add Book");
System.out.println("2. View All Books");
System.out.println("3. Check Out Book");
System.out.println("4. Check In Book");
System.out.println("5. Ban A Book");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
addBook();
break;
case 2:
viewBooks();
break;
case 3:
checkOutBook();
break;
case 4:
checkInBook();
break;
case 5:
banBook();
break;
case 6:
System.out.println("Exiting the system. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 6);
}
public static void main(String[] args) {
//Create default books in the library
books.put(bookCounter, new Books("Hunger Games", "Young adult", 374));
bookCounter++;
books.put(bookCounter, new Books("Daemon", "Sci-fi", 444));
bookCounter++;
books.put(bookCounter, new Books("Where the Sidewalk Ends", "Children Story", 176));
bookCounter++;
books.put(bookCounter, new Books("The Giving Tree", "Children Story", 52));
bookCounter++;
displayMenu();
}
public Books get(int id) {
return books.get(id);
}
}