diff --git a/BookBorrow/Main.java b/BookBorrow/Main.java new file mode 100644 index 0000000..e187df4 --- /dev/null +++ b/BookBorrow/Main.java @@ -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 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; + } + + + + } + } +} diff --git a/BookBorrow/Readme.md b/BookBorrow/Readme.md new file mode 100644 index 0000000..d05f9ec --- /dev/null +++ b/BookBorrow/Readme.md @@ -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.
+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)
+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). +
+
+The functionality of the checkIn and checkOut methods in TextBooks class is as follows:
+checkIn : should set status attribute as Borrowed and should set the value of borrowedUser.
+checkOut : should set status attribute as Available +
+
+The functionality of the checkIn and checkOut methods in ReferenceBooks class is as follows:
+checkIn : display “Invalid”
+checkOut : display “Cannot be borrowed” +
+
+Sample Input and Output:
+1. Add Reference Book
+2. Add Text Book
+3. Check-Out
+4. Check-In
+5. List Books
+6. Exit
+ +
+ +### Enter your choice: 1 +Enter ID, Title and Author (Line by line)
+101
+Data Structures and Algorithms
+Cormen + +### Enter your choice: 2 +Enter ID, Title and Author (Line by line)
+102
+Programming Ruby
+Thomas + +### Enter your choice: 5 +ReferenceBook: 101: Data Structures and Algorithms: Cormen
+TextBook: 102: Programming Ruby: Thomas: Available + +### Enter your choice: 3 +Enter Book ID: 101
+Cannot be borrowed + +### Enter your choice: 3 +Enter Book ID: 102
+Enter Username: Ram + +### Enter your choice: 5 +ReferenceBook: 101: Data Structures and Algorithms: Cormen
+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
+TextBook: 102: Programming Ruby: Thomas: Available + +### Enter your choice: 6