0

I have this system which I only want to try and borrow books from a library.
I have data already in arrays.
I need to check that nobody else has a book then add it to the data.
I have commented where i think the problem is.
i have tried syste.out and I jst get the original data that was in the array instead of the added data???
any ideas??

thanks for looking

the code is below

import javax.swing.*;
 class LibraryMain {
   //  Creates a staff of employees
   public static void main (String[] args)    {
   	 //  Sets up the list of books.
   	 Books[] BookList;
   	 BookList = new Books[5];
   	 BookList[0] = new Books("title", "author", "B1");
   	 BookList[1] = new Books("my", "by","B2");
   	 BookList[2] = new Books("lname", "by","B3");
              BookList[3] = new Books("lname", "by","B4");
              BookList[4] = new Books("lname", "by","B5");
   	
  System.out.println("Library System\n");
  //creates object to print out details
	Borrowers personnel = new Borrowers();
    personnel.records();
     System.exit(0); //exits
   }
    
}

class Borrowers{
  
  public Students[] BorrowersList;
   public Borrowers ()   {
//  Sets up the list of borrowers.
BorrowersList = new Students[4];
BorrowersList[0] = new Students ("adam", "S1","", "");
BorrowersList[1] = new Students ("tim", "S2","B1", "");
BorrowersList[2] = new Students ("Mr", "L3","B5", "");
BorrowersList[3] = new Students ("mrs", "L4","B3", "B5");
         
 for(int count = 0;count<1;count++){ //loop to continue to enter books
 //gets student ID
 String num = JOptionPane.showInputDialog(null,
		     	   "Enter the ID number",
			       "Library System",
			        JOptionPane.QUESTION_MESSAGE);
			      
        // gets bookid
         String num2 = JOptionPane.showInputDialog(null,
		    "Enter the Book ID",
			"Library System",
			 JOptionPane.QUESTION_MESSAGE);
			  //finds if any one has that book
			    for (int count2	=0; count <  4; count++)      {
         	if (BorrowersList[count2].bookid1.equals("num2")){
          			System.out.println("Book already on loan");
          				   break;
          						}
          		             else{
          				num = BorrowersList[count2].bookid1;
          				// problem here, wont override array??
          			    }
			       }
			    }
			 
	 }
			
 public void records()   {
 	//prints out borrowers details
    for (int count=0; count < BorrowersList.length; count++)      {
         System.out.println (BorrowersList[count]);
         System.out.println ("-----------------------------------");
       
      }
   }
}

 class Students{
   protected String name;
   protected String ID;
   protected String bookid1;
   protected String bookid2;

   //  Sets up students with the information.
   public Students (String sName, String sID, String sBookid1, String sBookid2)   {
      name = sName;
      ID = sID;
      bookid1 = sBookid1;
      bookid2 = sBookid2;
   
   }
   //  Returns information about the students as a string.
   public String toString()   {
   	String result = "Name           : " + name + "\n";
           result += "ID            : " + ID + "\n";
           result += "Books borrowed: " +  bookid1 + bookid2 ;
     

      return result;
   }
   
}

class Books{
   protected String title;
   protected String author;
   protected String ISBN;
	  //  Sets up books with the information.
  public Books (String sName, String sID, String sBookid)   {
      title = sName;
      author = sID;
      ISBN = sBookid;
   
   }
   
}

Code tags added. -Narue

2
Contributors
1
Reply
3
Views
12 Years
Discussion Span
Last Post by LunLun
0

First of all, I strongly&&friendly recommend formatting your code using a beautifier, like Jacobe:
http://www.tiobe.com/jacobe.htm (it's free for Windows and Linux).
It will make the program's flow much more obvious.

I found and fixed several issues (first 3 are listed below):

1)

    for (int count2 =0; count <  4; count++)      {
        if (BorrowersList[count2].bookid1.equals("num2"))

It should be count2 < 4; count2++)

2)

num = BorrowersList[count2].bookid1;

It should be:

BorrowersList[count2].bookid1 = num2;

because you update the list, not the num2's value.

3)

if (BorrowersList[count2].bookid1.equals("num2"))

It should be

if (BorrowersList[count2].bookid1.equals(num2))

because you compare with num2's value, not with the string "num2".
For instance, if num2 = "Book2", then "num2" is still "num2", not "Book2".
By the way, I believe it's easier to put Book1 / Student 1 instead of B1 / adam.
At least for me, it makes the code easier to read.

I tested the program in two cases:

Case 1:
    Input:
        S1
        Book5

    Output: 
        Sorry, the book is not available...
        Student1 has no books.

Case 2:
    Input:
        S1
        Book2

    Output:
        Student1 has Book2

Although the program works, it does not verify if a student has already one book, so, if Student2 borrows Book2, Book2 will replace Book1, instead of adding it as the second book. Of course, this could be fixed, if you want that. For now, I just want to make sure the program works.

Should you need more details, I'll be glad to provide them.

I attached the code in LibraryMain.java

Edited by pyTony: fixed formating

Attachments
import javax.swing.*;


class LibraryMain {
    // Creates a staff of employees
    public static void main(String[] args) {
        // Sets up the list of books.
        Books[] BookList;

        BookList = new Books[5];
        BookList[0] = new Books("Title1", "Author1", "Book1");
        BookList[1] = new Books("Title2", "Author2", "Book2");
        BookList[2] = new Books("Title3", "Author3", "Book3");
        BookList[3] = new Books("Title4", "Author4", "Book4");
        BookList[4] = new Books("Title5", "Author5", "Book5");
   	
        // creates object to print out details
        Borrowers personnel = new Borrowers();

        personnel.records();
        System.exit(0); // exits
    }
    
}


class Borrowers {
  
    public Students[] BorrowersList;
    public Borrowers() {
        // Sets up the list of borrowers.
        BorrowersList = new Students[4];
        BorrowersList[0] = new Students("Student1", "S1", "", "");
        BorrowersList[1] = new Students("Student2", "S2", "Book1", "");
        BorrowersList[2] = new Students("Student3", "L3", "Book5", "");
        BorrowersList[3] = new Students("Student4", "L4", "Book3", "Book5");
         
        for (int count = 0; count < 1; count++) { // loop to continue to enter books
            // gets student ID
            String num = JOptionPane.showInputDialog(null,
                    "Enter the Student ID", "Library System",
                    JOptionPane.QUESTION_MESSAGE);
			      
            // gets bookid
            String num2 = JOptionPane.showInputDialog(null, "Enter the Book ID",
                    "Library System", JOptionPane.QUESTION_MESSAGE);
                    
            System.out.println("\n");

            // finds if any one has that book
            boolean is_available = true;

            for (int count2 = 0; count2 < 4; count2++) {
                if (BorrowersList[count2].bookid1.equals(num2)) {
                    System.out.println("Sorry, the book is not available...");
                    is_available = false;
                    break;
                }
            }
            
            for (int count3 = 0; count3 < 4; count3++) {
                    if ((is_available == true)
                            && (BorrowersList[count3].ID.equals(num))) {
                        BorrowersList[count3].bookid1 = num2;
                        break;
                    }
                }
            }
			 
    }
			
    public void records() {
        // prints out borrowers details
        for (int count = 0; count < BorrowersList.length; count++) {
            System.out.println(BorrowersList[count]);
            System.out.println("-----------------------------------");
        }
    }
}


class Students {
    protected String name;
    protected String ID;
    protected String bookid1;
    protected String bookid2;

    // Sets up students with the information.
    public Students(String sName, String sID, String sBookid1, String sBookid2) {
        name = sName;
        ID = sID;
        bookid1 = sBookid1;
        bookid2 = sBookid2;
   
    }

    // Returns information about the students as a string.
    public String toString() {
        String result = "Name           : " + name + "\n";

        result += "ID             : " + ID + "\n";
        result += "Books borrowed : " + bookid1 + " " + bookid2;
     
        return result;
    }
   
}


class Books {
    protected String title;
    protected String author;
    protected String ISBN;
    // Sets up books with the information.
    public Books(String sName, String sID, String sBookid) {
        title = sName;
        author = sID;
        ISBN = sBookid;
    }
   
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.