-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExercise4.java
More file actions
228 lines (197 loc) · 6.83 KB
/
Exercise4.java
File metadata and controls
228 lines (197 loc) · 6.83 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
Exercise 4 - Online Library
You have to implement a library using Java Class "Library"
Methods: addBook, issueBook, returnBook, showAvailableBooks
Properties: Array to store the available books,
Array to store the issued books
Solved the bug of :-
(1) Adding books multiple times in library
(1) issuing the same book multiple times
(2) returning the same book multiple times
(3) issuing book in empty library
(4) returning book in empty library
*/
package com.company;
import java.util.ArrayList;
import java.util.Scanner;
class Library
{
public int n=0,a=0,j=0,q=0,flag=0,check=0;
public int issuebookno;
ArrayList<String> BooksName = new ArrayList<>();
ArrayList<String> ReturnBook= new ArrayList<>();
public void Bookno()
{
Scanner sc1 = new Scanner(System.in);
System.out.println("How Many Book You Want to add : ");
n = sc1.nextInt();
}
public void addBook()
{
Bookno();
Scanner sc2 = new Scanner(System.in);
System.out.println();
int i;
if(q==0)
{
for (i=0;i<n;i++)
{
System.out.print("Enter Book " + (i+1) + " Name : ");
BooksName.add(i,sc2.nextLine());
}
q++;
j=n;
a=n;
check++;
}
else
{
for (i=a;i<a+n;i++)
{
System.out.print("Enter Book " + (i+1) + " Name : ");
BooksName.add(i,sc2.nextLine());
j=i;
flag=1;
}
}
System.out.println();
System.out.println("Your Book is Added successfully.....");
System.out.println("------------------------------------------------------------");
}
public void showAvailableBooks()
{
System.out.println("------------------------------------------------------------");
System.out.println("Available Book is : ");
if(flag==0)
{
for (int i = 0; i < j; i++)
{
System.out.println((i + 1) + ". " + BooksName.get(i));
}
}
else
{
for (int i = 0; i <= j; i++)
{
System.out.println((i + 1) + ". " + BooksName.get(i));
}
}
System.out.println("------------------------------------------------------------");
}
public void issueBook()
{
if(check==0)
{
System.out.println("There are no Books in Library. Please ass some books in Library...");
}
else
{
//for Books issued
Scanner sc3 = new Scanner(System.in);
System.out.println("--------------------------------------------------------------");
System.out.println("Enter Your Book Number (0 Not Allowed ) to be issued : `");
issuebookno = sc3.nextInt();
System.out.println();
issuebookno -= 1;
//for already book is issued
if (BooksName.get(issuebookno) == null)
{
System.out.println("This Book is Already Issued By any other person. Please Issue any other book... ");
}
else
{
System.out.println(BooksName.get(issuebookno) + " Has Issued Successfully");
for (int i = 0; i <= j; i++)
{
if (i != issuebookno)
{
ReturnBook.add(i, null);
}
else
{
ReturnBook.add(i, BooksName.get(issuebookno));
}
}
BooksName.set(issuebookno, null);
}
}
System.out.println("--------------------------------------------------------------");
}
public void returnBook()
{
if(check==0)
{
System.out.println("There are no Books in Library. Please ass some books in Library...");
}
else
{
Scanner sc4 = new Scanner(System.in);
System.out.println("--------------------------------------------------------------");
System.out.println();
System.out.println("Enter Your Book Number to be return");
int returnbookno = sc4.nextInt();
returnbookno -= 1;
// for thebooks which are not returned
if (BooksName.get(returnbookno) == null)
{
BooksName.set(returnbookno, ReturnBook.get(returnbookno));
System.out.println(ReturnBook.get(returnbookno) + " Return Successfully .......");
}
else // for the books which are already returned
{
System.out.println("Book is already returned. Please return any other book which is not available in Library...");
}
}
System.out.println("--------------------------------------------------------------");
}
//for user input
public void takeChoice()
{
Scanner sc5 =new Scanner(System.in);
System.out.println(" 1. Add Book \t 2.Show Available Book \t 3.Issue Book \t 4.Return Book \t 5.Exit");
System.out.print("Enter Your Choice : ");
int take_UserInput = sc5.nextInt();
switch(take_UserInput)
{
case 1:
addBook();
takeChoice();
break;
case 2:
showAvailableBooks();
takeChoice();
break;
case 3:
issueBook();
takeChoice();
break;
case 4:
returnBook();
takeChoice();
break;
default:
{
if (take_UserInput == 5)
{
System.out.println("-------------------Thank's for using Library Management System------------------");
break;
}
else
{
System.out.println("Enter Valid Choice (1-5) ");
takeChoice();
}
}
}
}
}
public class Exercise4
{
public static void main(String[] args)
{
Library L1 = new Library();
System.out.println("----------------------Welcome To Library Management System----------------------");
System.out.println();
L1.takeChoice();
}
}