how to override arrays and objects???

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2004
Posts: 44
Reputation: ultimate_fusion is an unknown quantity at this point 
Solved Threads: 0
ultimate_fusion ultimate_fusion is offline Offline
Light Poster

how to override arrays and objects???

 
0
  #1
Mar 15th, 2005
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
  1. import javax.swing.*;
  2. class LibraryMain {
  3. // Creates a staff of employees
  4. public static void main (String[] args) {
  5. // Sets up the list of books.
  6. Books[] BookList;
  7. BookList = new Books[5];
  8. BookList[0] = new Books("title", "author", "B1");
  9. BookList[1] = new Books("my", "by","B2");
  10. BookList[2] = new Books("lname", "by","B3");
  11. BookList[3] = new Books("lname", "by","B4");
  12. BookList[4] = new Books("lname", "by","B5");
  13.  
  14. System.out.println("Library System\n");
  15. //creates object to print out details
  16. Borrowers personnel = new Borrowers();
  17. personnel.records();
  18. System.exit(0); //exits
  19. }
  20.  
  21. }
  22.  
  23. class Borrowers{
  24.  
  25. public Students[] BorrowersList;
  26. public Borrowers () {
  27. // Sets up the list of borrowers.
  28. BorrowersList = new Students[4];
  29. BorrowersList[0] = new Students ("adam", "S1","", "");
  30. BorrowersList[1] = new Students ("tim", "S2","B1", "");
  31. BorrowersList[2] = new Students ("Mr", "L3","B5", "");
  32. BorrowersList[3] = new Students ("mrs", "L4","B3", "B5");
  33.  
  34. for(int count = 0;count<1;count++){ //loop to continue to enter books
  35. //gets student ID
  36. String num = JOptionPane.showInputDialog(null,
  37. "Enter the ID number",
  38. "Library System",
  39. JOptionPane.QUESTION_MESSAGE);
  40.  
  41. // gets bookid
  42. String num2 = JOptionPane.showInputDialog(null,
  43. "Enter the Book ID",
  44. "Library System",
  45. JOptionPane.QUESTION_MESSAGE);
  46. //finds if any one has that book
  47. for (int count2 =0; count < 4; count++) {
  48. if (BorrowersList[count2].bookid1.equals("num2")){
  49. System.out.println("Book already on loan");
  50. break;
  51. }
  52. else{
  53. num = BorrowersList[count2].bookid1;
  54. // problem here, wont override array??
  55. }
  56. }
  57. }
  58.  
  59. }
  60.  
  61. public void records() {
  62. //prints out borrowers details
  63. for (int count=0; count < BorrowersList.length; count++) {
  64. System.out.println (BorrowersList[count]);
  65. System.out.println ("-----------------------------------");
  66.  
  67. }
  68. }
  69. }
  70.  
  71. class Students{
  72. protected String name;
  73. protected String ID;
  74. protected String bookid1;
  75. protected String bookid2;
  76.  
  77. // Sets up students with the information.
  78. public Students (String sName, String sID, String sBookid1, String sBookid2) {
  79. name = sName;
  80. ID = sID;
  81. bookid1 = sBookid1;
  82. bookid2 = sBookid2;
  83.  
  84. }
  85. // Returns information about the students as a string.
  86. public String toString() {
  87. String result = "Name : " + name + "\n";
  88. result += "ID : " + ID + "\n";
  89. result += "Books borrowed: " + bookid1 + bookid2 ;
  90.  
  91.  
  92. return result;
  93. }
  94.  
  95. }
  96.  
  97. class Books{
  98. protected String title;
  99. protected String author;
  100. protected String ISBN;
  101. // Sets up books with the information.
  102. public Books (String sName, String sID, String sBookid) {
  103. title = sName;
  104. author = sID;
  105. ISBN = sBookid;
  106.  
  107. }
  108.  
  109. }
Code tags added. -Narue
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 4
Reputation: LunLun is an unknown quantity at this point 
Solved Threads: 0
LunLun's Avatar
LunLun LunLun is offline Offline
Newbie Poster

Re: how to override arrays and objects???

 
0
  #2
Mar 16th, 2005
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
Last edited by LunLun; Mar 16th, 2005 at 9:56 am. Reason: Move the code to attachment
Attached Files
File Type: java LibraryMain.java (3.7 KB, 9 views)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2361 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC