Smart Library Management System..What Wrong with my code
3. All Smarts’ university has a library with extensive collection of books. A student can rent up to 5 books at a time.
Write a program to manage:
a. Issuing books
b. Returning books
c. Searching for book using title
I have Written the following code..but everytime i run the libraryApp class my arraylist in libraryFunction size is 0.
I cant understand Why..can any1 plz help me with this.
Note: I have to the program using only arraylist.
package smartLibrary;
public class Student {
String name;
int age;
int rollNo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
public Student(String name, int age, int rollNo) {
super();
this.name = name;
this.age = age;
this.rollNo = rollNo;
}
}
package smartLibrary;
public class books {
int bookId;
String name;
String author;
String Title;
public books()
{
}
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public books(int bookId, String name, String author, String title) {
super();
this.bookId = bookId;
this.name = name;
this.author = author;
Title = title;
}
}
package smartLibrary;
public class library {
Student st;
books bk;
int noOfCopies;
public Student getSt() {
return st;
}
public void setSt(Student st) {
this.st = st;
}
public books getBk() {
return bk;
}
public void setBk(books bk) {
this.bk = bk;
}
public int getNoOfCopies() {
return noOfCopies;
}
public void setNoOfCopies(int noOfCopies) {
this.noOfCopies = noOfCopies;
}
public library(Student st, books bk, int noOfCopies) {
super();
this.st = st;
this.bk = bk;
this.noOfCopies = noOfCopies;
}
}
package smartLibrary;
import java.util.ArrayList;
public class LibraryFunc {
public static ArrayList<library> lib=new ArrayList<library>();
public static void issueBook(Student st1,books b,int copies)
{
int count=0; boolean flag=false;
library entry;
if(lib.size()==0)
{
System.out.println("The book is issued to "+st1.getName());
entry= new library(st1, b, copies);
lib.add(entry);
System.out.println(lib.size());
flag=true;
}
else
{
for(int i=0;i<lib.size();i++)
{
if(lib.get(i).getSt().getRollNo()==st1.getRollNo() )
{
count+=lib.get(i).getNoOfCopies();
}
}
System.out.println(count);
if(count<5)
{
System.out.println("The book "+b.getTitle()+ " is issued to "+st1.getName());
entry=new library(st1, b, copies);
lib.add(entry);
flag=true;
}
else
{
System.out.println("maximum 5 books can be issued");
}
}
if(flag==false)
{
System.out.println("Student not found");
}
}
public static void searchBook(String title,ArrayList<books> bookList)
{
boolean flag=false;
for(books b:bookList)
{
if(b.getTitle().equalsIgnoreCase(title))
{
System.out.println("Books found..");
flag=true;
break;
}
}
if(flag==false)
System.out.println("Book not found");
}
}
package smartLibrary;
import java.util.ArrayList;
public class libraryApp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<books> bookList1=new ArrayList<books>();
books b1=new books(1, "Java", "Siera and bates", "Head First");
books b2=new books(2, "Servlets", "Oreilly", "Start Servlets");
books b3=new books(3, "JSP", "Siera and bates", "Learn jsp");
books b4=new books(4, "CSS", "George W Bush", "Basics of CSS");
bookList1.add(b1);
bookList1.add(b2);
bookList1.add(b3);
bookList1.add(b4);
Student ct1=new Student("Tirtha",22, 2113 );
Student ct2=new Student("Satya",22, 2114 );
Student ct3=new Student("Sourav",22, 2115);
Student ct4=new Student("partho", 23,2116);
Student ct5=new Student("Arnab",23, 2117);
Student ct6=new Student("Shweta", 20,2118);
Student ct7=new Student("Manish", 22,2119);
switch(Integer.parseInt(args[0])) {
case 1:
LibraryFunc.issueBook(ct1,b1,2);
break;
case 2:
break;
case 3:
LibraryFunc.searchBook("Head Firsts",bookList1);
break;
default:
break;
}
}
}
Thanks in Advance.
Re: Smart Library Management System..What Wrong with my code
Hello funkyonly4u!
I added a print statement in the issueBook(Student st1,books b,int copies) method for printing the lib's size and hardcoded the switch in your main method to get case 1 and and it gave me size = 1. Do you get a zero there? It would help if you explained more the problem you are facing.
And please use the code tags to get highlighting in your code.
Re: Smart Library Management System..What Wrong with my code
No even i am getting size=1 but when i rerun the code giving args[0]=1 i still get the size=1. The previous details are not retained in the arraylist.
Why is that???
Re: Smart Library Management System..What Wrong with my code
Quote:
Originally Posted by
funkyonly4u
No even i am getting size=1 but when i rerun the code giving args[0]=1 i still get the size=1. The previous details are not retained in the arraylist.
Why is that???
The variables you use and their values exist only while your program executes. When it stops executing they don't exist anymore. When you run it again it starts again with new variables and values. If you want to fill your list with multiple objects you can use a loop (a while loop seems appropriate to your program) with a control variable which will indicate when the loop stops.