944,028 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3200
  • Java RSS
Mar 15th, 2005
0

how to override arrays and objects???

Expand Post »
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
Java Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
ultimate_fusion is offline Offline
44 posts
since Oct 2004
Mar 16th, 2005
0

Re: how to override arrays and objects???

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
Attached Files
File Type: java LibraryMain.java (3.7 KB, 29 views)
Last edited by LunLun; Mar 16th, 2005 at 9:56 am. Reason: Move the code to attachment
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LunLun is offline Offline
4 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Finding the Most Common Character in a String
Next Thread in Java Forum Timeline: Sorting Strings





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC