Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Smart Library Management System..What Wrong with my code

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb 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.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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???

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Smart Library Management System..What Wrong with my code

    Quote Originally Posted by funkyonly4u View Post
    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.

  5. The Following User Says Thank You to andreas90 For This Useful Post:

    funkyonly4u (June 30th, 2012)

Similar Threads

  1. what is wrong with my code? need help
    By balmung123 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 1st, 2012, 04:12 AM
  2. Library lending system
    By eshiku in forum Java Theory & Questions
    Replies: 1
    Last Post: February 5th, 2012, 03:50 PM
  3. What's wrong with my code?
    By mjballa in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 19th, 2011, 03:57 PM
  4. Banking System UML Class Diagram in to code.
    By djl1990 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 13th, 2011, 11:55 AM
  5. [SOLVED] New to java, need to know how to incorparate system time into a block of code.
    By lostbit in forum Java Theory & Questions
    Replies: 5
    Last Post: September 29th, 2011, 07:46 AM