| | |
how to override arrays and objects???
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2004
Posts: 44
Reputation:
Solved Threads: 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
Code tags added. -Narue
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
Java Syntax (Toggle Plain Text)
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; } }
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
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
Last edited by LunLun; Mar 16th, 2005 at 9:56 am. Reason: Move the code to attachment
![]() |
Similar Threads
- instantiation; and arrays & structures really objects? (C#)
- Passing Arrays of Objects to Member Functions (C++)
- arrays of objects? (C)
- Passing arrays of objects to functions (C++)
Other Threads in the Java Forum
- Previous Thread: Finding the Most Common Character in a String
- Next Thread: Sorting Strings
Views: 2361 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte chat class classes click client code color compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool keyword linux list loop map method methods mobile netbeans newbie number object oracle pong print problem producer program programming project projectideas read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows





