Skip to content

Adding Java code for Book Borrow System #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions BookBorrow/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import java.util.*;
import java.lang.*;
import java.io.*;


interface Borrower
{
void checkin();
void checkout();
}
class Book
{
int bookID;
String title;
String author;
String booktype;
String status="Available";
String borroweduser="";
Book(int bookID, String title, String author, String booktype)
{
this.bookID=bookID;
this.title=title;
this.author=author;
this.booktype=booktype;
}
}

public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
LinkedList<Book> lists=new LinkedList<>();
for(;;)
{
System.out.println("1. Add Reference Book\n2. Add Text Book\n3. Check-Out\n4. Check-In\n5. List Books\n6. Exit\n");
System.out.println("Enter your choice:");
int n=sc.nextInt();
String esc=sc.nextLine();
int id;
String title;
String author;
String username;


if(n==1)
{
System.out.println("Input ID, Title and Author");
id=sc.nextInt();
String buff=sc.nextLine();
title=sc.nextLine();
author=sc.nextLine();
Book b=new Book(id,title,author,"RefBook"); // RB: Reference Book
lists.add(b);
}

else if(n==2)
{
System.out.println("Input ID, Title and Author");
id=sc.nextInt();
String buff=sc.nextLine();
title=sc.nextLine();
author=sc.nextLine();
Book b=new Book(id,title,author,"TextBook"); // TB: Text Book
lists.add(b);

}
else if(n==3)
{
System.out.println("Input Book ID:");
id=sc.nextInt();
String buff=sc.nextLine();
for(Book b:lists)
{
if(b.bookID==id)
{
if(b.booktype.equals("RefBook"))
{
System.out.println("Cannot be borrowed");
break;
}
else
{
b.status="Borrowed";
username=sc.nextLine();
b.borroweduser=username;
}
}
}
}
else if(n==4)
{
System.out.println("Input Book ID:");
id=sc.nextInt();
for(Book b:lists)
{
if(b.bookID==id)
{
if(b.booktype.equals("RefBook"))
{
System.out.println("Invalid");
break;
}
else
{
b.status="Available";
}
}
}
}
else if(n==5)
{
for(Book b:lists)
{

if(b.booktype.equals("RefBook"))
{
System.out.println("ReferenceBook:"+b.bookID+":"+b.title+":"+b.author);
}
else
{
if(b.status.equals("Available"))
System.out.println("TextBook:"+b.bookID+":"+b.title+":"+b.author+":Available");
else
System.out.println("TextBook:"+b.bookID+":"+b.title+":"+b.author+":Borrowed by "+b.borroweduser);
}
}

}
else
{
break;
}



}
}
}
63 changes: 63 additions & 0 deletions BookBorrow/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Question
There is a class Book, which has the attributes bookID, title and author. There are two categories of books, TextBooks and ReferenceBooks.<br/>
The text books can be borrowed by a user while the reference books cannot be. There is an extra attribute status (default value is Available) <br/>
and borrowedUser in the class TextBooks. There is an interface Borrowable which has methods checkIn and checkOut. The class Book implements
the Borrowable interface (Think and decide whether class Book is an abstract class or not).
<br/>
<br/>
The functionality of the checkIn and checkOut methods in TextBooks class is as follows:<br/>
checkIn : should set status attribute as Borrowed and should set the value of borrowedUser.<br/>
checkOut : should set status attribute as Available
<br/>
<br/>
The functionality of the checkIn and checkOut methods in ReferenceBooks class is as follows:<br/>
checkIn : display “Invalid”<br/>
checkOut : display “Cannot be borrowed”
<br/>
<br/>
Sample Input and Output:<br/>
1. Add Reference Book<br/>
2. Add Text Book<br/>
3. Check-Out<br/>
4. Check-In<br/>
5. List Books<br/>
6. Exit<br/>

<br/>

### Enter your choice: 1
Enter ID, Title and Author (Line by line)<br/>
101<br/>
Data Structures and Algorithms<br/>
Cormen

### Enter your choice: 2
Enter ID, Title and Author (Line by line)<br/>
102<br/>
Programming Ruby<br/>
Thomas

### Enter your choice: 5
ReferenceBook: 101: Data Structures and Algorithms: Cormen<br/>
TextBook: 102: Programming Ruby: Thomas: Available

### Enter your choice: 3
Enter Book ID: 101<br/>
Cannot be borrowed

### Enter your choice: 3
Enter Book ID: 102<br/>
Enter Username: Ram

### Enter your choice: 5
ReferenceBook: 101: Data Structures and Algorithms: Cormen<br/>
TextBook: 102: Programming Ruby: Thomas: Borrowed by Ram

### Enter your choice: 4
Enter Book ID: 102

### Enter your choice: 5
ReferenceBook: 101: Data Structures and Algorithms: Cormen<br/>
TextBook: 102: Programming Ruby: Thomas: Available

### Enter your choice: 6