-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
192 lines (172 loc) · 6.16 KB
/
Main.java
File metadata and controls
192 lines (172 loc) · 6.16 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import java.util.Scanner;
import java.util.ArrayList;
class Library {
ArrayList<String> books = new ArrayList<String>();
int bookId;
String bookName;
String IssueDate;
String ReturnDate;
String name;
public void getVal() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Name: ");
name = sc.nextLine();
System.out.println("Enter Book ID: ");
while (!sc.hasNextInt()) {
System.out.println("Invalid input. Please enter a valid integer for Book ID: ");
sc.next(); // discard the invalid input
}
bookId = sc.nextInt();
sc.nextLine(); // consume the newline character
System.out.println("Enter Book Name: ");
bookName = sc.nextLine();
System.out.println("Enter Issue Date: ");
IssueDate = sc.nextLine();
System.out.println("Enter Return Date: ");
ReturnDate = sc.nextLine();
}
public void IssueBook() {
System.out.println("Issuing a book...");
getVal();
System.out.println("Book Issued");
}
// Book returning method
public void ReturnBook() {
if (bookId == 0 || bookName == null || name == null) {
System.out.println("No books data found to be returned");
return;
} else {
System.out.println("Returning a book...");
System.out.println("Book returned by " + name + " with Book ID: " + bookId + " on " + ReturnDate);
System.out.println("Book Returned");
}
}
// adding books using arraylist
public void addingBookusingArrayList() {
Scanner sc = new Scanner(System.in);
int amountofBooks = 0;
System.out.println("Enter amount of books you want to add: ");
amountofBooks = sc.nextInt();
for (int i = 0; i < amountofBooks; i++) {
System.out.println("Enter Book Name: ");
String bookName = sc.next();
books.add(bookName);
}
}
public void showBooks() {
if (books.isEmpty()) {
System.out.println("No books found");
return;
} else {
books.toArray();
for (String book : books) {
System.out.println(book);
}
}
}
// Main AddBook method
public void AddBook() {
System.out.println("Adding a book...");
// calling the addingBookusingArrayList method
addingBookusingArrayList();
System.out.println("Book Added");
}
public void RemoveBook() {
if(books.isEmpty()){
System.out.println("Books not found");
}
else{
Scanner scan = new Scanner(System.in);
String bookToRemove = scan.nextLine();
if(books.contains(bookToRemove)){
books.remove(bookToRemove);
System.out.println("Book removed of name: "+ bookToRemove);
}
else
System.out.println("Book not found");
}
}
public void info() {
if (bookId == 0 || bookName == null || IssueDate == null || ReturnDate == null || name == null) {
System.out.println("No Record Found");
return;
} else {
System.out.println("Name: " + name + ", BookID: " + bookId + ", Book Name: " + bookName + ", Issue Date: "
+ IssueDate);
}
}
public void CreateAccount() {
System.out.println("Creating a new account...");
getVal();
System.out.println("Account Created");
}
public void clearValues() {
bookId = 0;
bookName = null;
IssueDate = null;
ReturnDate = null;
name = null;
}
}
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Library lib = new Library();
boolean keepRunning = true;
while (keepRunning) {
System.out.println("*************************************");
System.out.println("Welcome to Library Management System!");
System.out.println("*************************************");
System.out.println(
" 1. Issue a book \n 2. Return a book \n 3. Add a book \n 4. Remove a book \n 5. Create a new account \n 6. Check information \n 7. Show Books \n 8. Exit");
System.out.print("Enter your choice: ");
int n = 0;
while (true) {
if (sc.hasNextInt()) {
n = sc.nextInt();
sc.nextLine(); // consume the newline character
if (n >= 1 && n <= 8) {
break;
} else {
System.out.println("Invalid input. Please enter a number between 1 and 7.");
}
} else {
System.out.println("Invalid input. Please enter a valid integer.");
sc.next(); // discard the invalid input
}
}
switch (n) {
case 1:
lib.IssueBook();
break;
case 2:
lib.ReturnBook();
lib.clearValues();
break;
case 3:
lib.AddBook();
break;
case 4:
lib.RemoveBook();
break;
case 5:
lib.CreateAccount();
break;
case 6:
lib.info();
break;
case 7:
lib.showBooks();
break;
case 8:
keepRunning = false;
System.out.println("Exiting Library Management System. Goodbye!");
break;
default:
System.out.println("Invalid Input");
break;
}
}
sc.close();
}
}