Skip to content

Commit 364acfa

Browse files
Merge pull request #307 from singodiyashubham87/add-new-project
add library app in java
2 parents a50c33b + cf5e05d commit 364acfa

File tree

4 files changed

+338
-0
lines changed

4 files changed

+338
-0
lines changed

LibraryApp/libraryApp/Book.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package libraryApp;
2+
3+
public class Book {
4+
5+
private int isbn;
6+
private String title;
7+
private String author;
8+
private String genre;
9+
private int quantity;
10+
private int checkedOut;
11+
private int checkedIn;
12+
13+
//Constructor for book object
14+
public Book(int isbn, String title, String author, String genre, int quantity, int checkedOut) {
15+
this.isbn = isbn;
16+
this.title = title;
17+
this.author = author;
18+
this.genre = genre;
19+
this.quantity = quantity;
20+
this.checkedOut = checkedOut;
21+
this.checkedIn = quantity-checkedOut;
22+
}
23+
24+
public int getCheckedIn() {
25+
return checkedIn;
26+
}
27+
28+
public void setCheckedIn(int checkedIn) {
29+
this.checkedIn = checkedIn;
30+
}
31+
32+
public void setIsbn(int isbn) {
33+
this.isbn = isbn;
34+
}
35+
36+
public void setTitle(String title) {
37+
this.title = title;
38+
}
39+
40+
public void setAuthor(String author) {
41+
this.author = author;
42+
}
43+
44+
public void setGenre(String genre) {
45+
this.genre = genre;
46+
}
47+
48+
public void setQuantity(int quantity) {
49+
this.quantity = quantity;
50+
}
51+
52+
public void setCheckedOut(int checkedOut) {
53+
this.checkedOut = checkedOut;
54+
}
55+
56+
//Getter Methods
57+
public int getIsbn() {
58+
return isbn;
59+
}
60+
61+
public String getTitle() {
62+
return title;
63+
}
64+
65+
public String getAuthor() {
66+
return author;
67+
}
68+
69+
public String getGenre() {
70+
return genre;
71+
}
72+
73+
public int getQuantity() {
74+
return quantity;
75+
}
76+
77+
public int getCheckedOut() {
78+
return checkedOut;
79+
}
80+
81+
82+
83+
84+
}
85+
86+
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package libraryApp;
2+
import java.util.ArrayList;
3+
4+
public class BookRepository {
5+
6+
private ArrayList<Book> books = new ArrayList<>();
7+
private int booksFound = 0;
8+
9+
//Constructor to initialize books
10+
public BookRepository(){
11+
books.add(new Book(253910,"Pride and Prejudice C", "Jane Austen", "Love",10,7));
12+
books.add(new Book(391520,"Programming in ANSI C", "E. Balagurusamy", "Educational",15,10));
13+
books.add(new Book(715332,"Shrimad Bhagavad Gita", "Krishna Dvaipayana", "Motivational",20,18));
14+
books.add(new Book(935141,"Java: The Complete Reference", "Herbert Schildt", "Educational",12,9));
15+
books.add(new Book(459901,"It", "Stephan King", "Horror",7,5));
16+
books.add(new Book(855141,"Disneyland", "Mickey & Minnie", "Love",10,3));
17+
}
18+
19+
20+
//Searching books by Title Keyword
21+
public void searchByTitle(String title) {
22+
booksFound = 0;
23+
for(Book book : books) {
24+
String bookTitle = book.getTitle();
25+
if(bookTitle.toLowerCase().contains(title.toLowerCase())) {
26+
bookDetails(book);
27+
booksFound++;
28+
}
29+
}
30+
System.out.printf("\n%d Book%s Found.\n",booksFound,booksFound>1?"s":"");
31+
return;
32+
}
33+
34+
35+
//Searching books by ISBN Number
36+
public void searchByISBN(int isbn) {
37+
booksFound = 0;
38+
for(Book book : books) {
39+
if(book.getIsbn()==isbn) {
40+
bookDetails(book);
41+
booksFound++;
42+
break;
43+
}
44+
45+
}
46+
System.out.printf("\n%d Book%s Found.\n",booksFound,booksFound>1?"s":"");
47+
return;
48+
}
49+
50+
51+
//Searching books by Genre
52+
public boolean searchByGenre(String genre){
53+
booksFound = 0;
54+
for(Book book : books) {
55+
String bookGenre = book.getGenre();
56+
if(bookGenre.toLowerCase().equals(genre.toLowerCase())) {
57+
bookDetails(book);
58+
booksFound++;
59+
}
60+
}
61+
System.out.printf("\n%d Book%s Found.\n",booksFound,booksFound>1?"s":"");
62+
if(booksFound>0)
63+
return true;
64+
else
65+
return false;
66+
67+
}
68+
69+
70+
// Display Book Details
71+
public void bookDetails(Book book) {
72+
System.out.println("\n+> Book details: \n");
73+
System.out.println("\tTitle: "+book.getTitle()+"\n\tAuthor: "+ book.getAuthor()+"\n\tGenre: "+book.getGenre()+"\n\tISBN: "+book.getIsbn()+"\n\tQuantity: "+book.getQuantity()+"\n\tChecked Out: "+String.valueOf(book.getCheckedOut())+"\n\tAvailable: "+String.valueOf(book.getQuantity()-book.getCheckedOut()));
74+
}
75+
76+
77+
//Searching for ISBN number for checkIn and checkOut
78+
public int searchISBN(int isbn) {
79+
for(Book book:books)
80+
if(book.getIsbn()==isbn)
81+
return 1;
82+
return 0;
83+
}
84+
85+
86+
//withdrawing book
87+
public boolean getBook(int isbn) {
88+
for(Book book: books) {
89+
if(book.getIsbn()==isbn) {
90+
if((book.getQuantity()-book.getCheckedOut())>0) {
91+
book.setCheckedOut(book.getCheckedOut()+1);
92+
return true;
93+
}
94+
}
95+
}
96+
return false;
97+
}
98+
99+
100+
//submitting book
101+
public boolean submitBook(int isbn) {
102+
for(Book book: books) {
103+
if(book.getQuantity()>book.getCheckedIn()) {
104+
book.setCheckedOut(book.getCheckedOut()-1);
105+
return true;
106+
}
107+
}
108+
return false;
109+
}
110+
111+
112+
//Showing status of book
113+
public void bookStatus(int isbn) {
114+
for(Book book: books) {
115+
if(book.getIsbn()==isbn) {
116+
bookDetails(book);
117+
break;
118+
}
119+
}
120+
}
121+
122+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package libraryApp;
2+
3+
public class LibraryApp {
4+
5+
BookRepository repo = new BookRepository();
6+
7+
public void findByTitle(String title) {
8+
repo.searchByTitle(title);
9+
return;
10+
}
11+
public void findByISBN(int isbn) {
12+
repo.searchByISBN(isbn);
13+
return;
14+
}
15+
public boolean findByGenre(String genre) {
16+
if(repo.searchByGenre(genre))
17+
return true;
18+
else
19+
return false;
20+
}
21+
22+
23+
public int findISBN(int isbn) {
24+
return repo.searchISBN(isbn);
25+
}
26+
27+
public boolean withdrawBook(int isbn) {
28+
return repo.getBook(isbn);
29+
}
30+
31+
public boolean depositBook(int isbn) {
32+
return repo.submitBook(isbn);
33+
}
34+
35+
public void getStatus(int isbn) {
36+
repo.bookStatus(isbn);
37+
}
38+
}

LibraryApp/libraryApp/Main.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package libraryApp;
2+
import java.util.Scanner;
3+
4+
public class Main{
5+
static Scanner scan = new Scanner(System.in);
6+
static LibraryApp app = new LibraryApp();
7+
8+
9+
public static void main(String[] args) {
10+
11+
int userChoice=0;
12+
System.out.println("-----Welcome to the Library!-----\n");
13+
do{
14+
System.out.println("\n-----------------------------------");
15+
System.out.println("1. Search book by Title keyword.");
16+
System.out.println("2. Search book by ISBN number.");
17+
System.out.println("3. Search book by Genre.");
18+
System.out.println("4. Book Check In");
19+
System.out.println("5. Book Check Out");
20+
System.out.println("6. Exit from the library.");
21+
System.out.println("-----------------------------------");
22+
System.out.print("\nChoose any option: ");
23+
24+
userChoice = scan.nextInt();
25+
scan.nextLine();
26+
27+
switch(userChoice){
28+
case 1:
29+
System.out.print("Enter the Title of Book: ");
30+
app.findByTitle(scan.nextLine());
31+
break;
32+
case 2:
33+
System.out.println("Enter ISBN number: ");
34+
app.findByISBN(scan.nextInt());
35+
break;
36+
case 3:
37+
System.out.println("Enter Genre: ");
38+
app.findByGenre(scan.nextLine());
39+
break;
40+
case 4:
41+
checkIn();
42+
break;
43+
case 5:
44+
checkOut();
45+
break;
46+
case 6:
47+
System.out.println("\nThanks for visiting. \nSee you again.");
48+
break;
49+
default:
50+
System.out.println("\nInvalid Choice!");
51+
}
52+
}while(userChoice!=6);
53+
54+
}
55+
56+
57+
//Checking book In
58+
private static void checkIn() {
59+
System.out.println("Enter Book's ISBN number : ");
60+
int isbnNum = scan.nextInt();
61+
getStatus(isbnNum);
62+
int bookAvailable = app.findISBN(isbnNum);
63+
if(bookAvailable==1) {
64+
System.out.println(isbnNum);
65+
app.withdrawBook(isbnNum);
66+
System.out.println("Book CheckIn successful.");
67+
getStatus(isbnNum);
68+
}
69+
else
70+
System.out.printf("Book with %d ISBN number not Found in inventory.",isbnNum);
71+
}
72+
73+
74+
//Checking book Out
75+
private static void checkOut() {
76+
System.out.println("\nEnter Book's ISBN number : ");
77+
int isbnNum = scan.nextInt();
78+
int bookAvailable = app.findISBN(isbnNum);
79+
if(bookAvailable==1) {
80+
if(app.depositBook(isbnNum))
81+
System.out.println("Book CheckOut successful.");
82+
else
83+
System.out.println("No Space for more Books.");
84+
}
85+
else
86+
System.out.printf("Book with %d ISBN number not Found in inventory.",isbnNum);
87+
}
88+
89+
private static void getStatus(int isbn) {
90+
app.getStatus(isbn);
91+
}
92+
}

0 commit comments

Comments
 (0)