Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Someone please correct my code...

    Ok. This is my first java program and am having great difficulties. Its a book returns system, and the code that I have come up with at this point is as shown below. Its mainly for checking a book in and out of the library. I am using Netbeans.

    PHP Code:
    package bookreturnssubsystem;

    import java.io.*;
    import java.util.Date;

    public class 
    Book
    {
    // ***Attributes***    
    // The ISBN number, its title, its author,
    // the book's status and its return date (dueDate).
    private int mISBN;
    private 
    int studentId;
    private 
    String title;
    private 
    String author;
    private 
    boolean checkedOut false;
    private 
    long dueDate;
    private 
    long returnDate;

    // ***Methods***
    /**
    * Check-out a book
    * @param isbn the books's ISBN
    * @param title the book's title
    * @param author the book's author
    * @param status the books's status
    * @param duedate the book's due date
    **/
        
    public void checkoutBook(int isbnString titleString authorString statusint duedate) {
    //program to prompt user to enter book isbn number ans search database for //the book that matches the isbn.

            
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
            
            
    mISBN isbn;
            
    System.out.println("Enter the ISBN number");
            
    isbn stdin.read();
            
            
    this mISBN;
            
    this.studentId studentId;
            
            
    // due date calculated by adding current system date plus 3 days
            // /*milliseconds*/ 1000 * /*seconds*/ 60 * /*minutes*/ 60 * /*hours*/ 24 * /*days*/ 3;
            
    this.dueDate System.currentTimeMillis() + 1000 60 60 24 3;
            
    this.checkedOut true;
        }
     
        
     
    /**
    * Check-in a book
    * @param isbn the books's ISBN
    * @param title the book's title
    * @param author the book's author
    * @param status the books's status
    * @param duedate the book's due date
    **/
        
    public void checkinBook(int isbn) {
            
    this.mISBN mISBN;
            
    this.returnDate System.currentTimeMillis();
        }
        
        public 
    void calculateFine(long returnDatelong dueDate) {
            
    calculateFine = new calculateFine();
            
        }



    Below is a description of the check in and check out methods of the book class, which is what am trying to code.

    Check-out.
    The librarian initiates a book check-out by querying the database using the books ISBN number. When the database returns the result and the book details are shown, the librarian enters the student ID, and the program automatically updates the issue date and due date to the book record, and the ISBN and book title to the student record, after the librarian presses the check-out button.
    NOTE: The issue date is automatically updated as the current system date. The due date is automatically updated as 10 days from the current date. So the librarian will query the database using the ISBN and afterwards input the student ID and the program will do the rest. A maximum of 5 books can be checked-out at a time.
    Check-in
    The librarians can check-in a book(s) using its ISBN number. When the database returns the search results, the check-in process is completed when the librarian presses the check-in button. The return date for any particular book is automatically updated as the current system date. The program also computes the fine (if any), as difference between the due date and the return date at a rate of 50p a day. This is updated to the student record using the student ID.
    I have not yet got to the part where I need a database yet, just want to get the coding right before I go there.

    I hope this is not too much but id appreciate your help very much.

  • #2
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    What exactly are the problems you're having? Do you have compilation errors? Do you have logic errors? It's hard for us to guess what issues you're having if they aren't clearly stated.

    However, based on a quick look at your code I'm guessing you have at least a couple compilation errors.

    In your checkoutBook method you have the following lines of code:
    1) this = mISBN;
    2) this.studentId = studentId;

    Line 1) above should be a compilation error as you cannot assign an int to "this". Did you forget some code here? Such as this.something = mISBN? Or something similiar?

    Line 2) above is at least a logic error. Your list of parameters for checkoutBook doesn't have a variable called studentId that is passed to it. Which is what I would have expected based on the line of code. So at most this line of code will assign this.studentId to itself. I'm not sure whether this line will cause a compilation error, but I imagine not.

    What other issues are you having?

  • #3
    New Coder
    Join Date
    Oct 2008
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for helping out. Am new at this so I hope you understand.

    I have corrected the two problems you've pointed out. I have added the variable studentId in the checkout method, and line 1 now looks like
    PHP Code:
    this.mISBN isbn 
    But basically I want the checkout method to work by bringing up a dialogue that will allow the librarian to type in the ISBN number, then that to initiate a search query to the database, and display the result. Thats why Ive used the BufferedReader method to capture the isbn input (is this the right way??), but I dont know how to forward the result (which is the isbn) to a database. I have attached the class diagram I'm using so maybe u can have a look and tell me what you think.

    Other problems am having are:
    1. How do I add the date variables to the book records so that when a isbn search result displays a book, the checkout date and the duedate (if book is loaned out) are displayed with other attributes of the book class?? calculate the fine (if any) in the calculate fine method??

    2. How do I use the date variables from check-in and check-out methods to calculate a fine, say at the rate of 50p a day, for example??

    If you have any questions just ask and Ill respond as fast as I can.

    This is my new code, although its not so much different.

    PHP Code:
    package bookreturnssubsystem;

    import java.io.*;
    import java.util.Date;

    public class 
    Book
    {
    // ***Attributes***    
    // The ISBN number, its title, its author,
    // the book's status and its return date (dueDate).
    private int mISBN;
    private 
    int studentId;
    private 
    String title;
    private 
    String author;
    private 
    boolean checkedOut false;
    private 
    long dueDate;
    private 
    long returnDate;

    // ***Methods***
    /**
    * Check-out a book
    * @param isbn the books's ISBN
    * @param studentId the students ID
    * @param title the book's title
    * @param author the book's author
    * @param status the books's status
    * @param duedate the book's due date
    **/
        
    public void checkoutBook(int isbnint studentIdString titleString authorString statusint duedate) {
            
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
            
            
    mISBN isbn;
            
    System.out.println("Enter the ISBN number");
            
    isbn stdin.read();
            
            
    this.mISBN isbn;
            
    this.studentId studentId;
            
            
    // due date calculated by adding current system date plus 3 days
            // /*milliseconds*/ 1000 * /*seconds*/ 60 * /*minutes*/ 60 * /*hours*/ 24 * /*days*/ 3;
            
    this.dueDate System.currentTimeMillis() + 1000 60 60 24 3;
            
    this.checkedOut true;
        }  
        
     
    /**
    * Check-in a book
    * @param isbn the books's ISBN
    * @param title the book's title
    * @param author the book's author
    * @param status the books's status
    * @param duedate the book's due date
    **/
        
    public void checkinBook(int isbn) {
            
    this.mISBN mISBN;
            
    this.returnDate System.currentTimeMillis();
        }
        
        public 
    void calculateFine(long returnDatelong dueDate) {
            
    calculateFine = new calculateFine();
            
        }
        
    public static 
    void main(String[] args) {

    checkoutBook(); // not sure how to create a main method for this class
    }    
        
    /*     // Accessor Methods:

         public int getIsbn() {
             return mISBN;
         }
         public String getAuthors() {
             return author;
         }
         public String getPublisher() {
             return publisher;
         }
         public Date getDate() {
             return date;
         }
         public int getYear() {
             if (date == null)
                 return 0;
             Calendar calendar = Calendar.getInstance();
             calendar.setTime(date);
             return calendar.get(Calendar.YEAR);
         }
        
    } */ 
    Attached Thumbnails Attached Thumbnails Someone please correct my code...-class.jpg  

  • #4
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    I'm on no sleep so let's look at the obvious and easy things right now since me brain can't handle code design at the moment

    While looking at your code again I noticed the following which I guess can be addressed now rather than later:

    In your checkoutBook method you have the following lines of code:
    1) mISBN = isbn;
    2)System.out.println("Enter the ISBN number");
    3)isbn = stdin.read();
    4)this.mISBN = isbn;

    There seems to be some redundancy here. Line 1) assigns the parameter isbn which is passed to the method to (this.) mISBN. Fair enough, however in lines 3) and 4) you then read the isbn from stdin and reassign it to this.mISBN which overwrites the assignment in line 1).

    I haven't quite figured out the design and logic you're after here but to clean this up a bit I see a couple scenerios.

    1) The isbn is read from stdin and then passed to the checkoutBook method. If this is the case then you don't really need lines 1,2,3 from above.

    2) The isbn is read from stdin in the checkoutBook method. If this is the case then we don't need "int isbn" as a parameter of the method and we can remove line 1 from above.

    That's all the thinking my tired brain can handle at the moment but I also want to mention that it's nice to see a UML Class diagram as you provided. Have you started coding any of the other classes? If not it may be useful to get the basics into those classes as it might help you realize how you want/need them to interact and the roles that they will play in the overall design.

  • #5
    New Coder
    Join Date
    Oct 2008
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I started coding the book class since its the once that holds most of the functions I want this program to do (check-in and check-out books). I had started with the library database but realized it turn out to be abit too complicated.

    There are 3 parts to the system:
    1. Identification of the student;
    2. Returning the book(s);
    3. Calculating any Fines.

    So checking out a book is not part of the part of the program but I thought, how do you return a book to a system that doesn't know you took it?? (The system must know when the book was checked out so that it can set a due date of 3 days from the current system date, thus making it possible to calculate a fine if due date < return date) That's why I decided to include it so it could make things a bit simpler. So I thought!!

    To understand this better, the section below outlines the requirements analysis for the system, which I used to draw the class diagram.
    REQUIREMENTS SPECIFICATIONS.
    Requirement 1:
    Description: Identify a student.

    The program shall let the librarian query the library database through a unique Student ID. The search results shall produce the student name, together with the details of books loaned out to the student (if any).
    The library database should then have the following details:
    • Records of the all students in the university that have registered to use the library.
    • All the books in the library that are available for lending. Each book should have the following attributes:
    o ISBN number (unique).
    o Title of the book.
    o Author.
    o Book status (In library or checked out).
    o If book is checked out, it would display the check-out date and student ID of borrower.
    o Due date.
    • Records of books loaned out to students sorted using the unique student ID.
    • A record of all librarians that operate the book lending service. Each librarian record should contain the librarians name and a unique librarian ID.

    Requirement 2:
    Description: Add a student record.

    If the search results in requirement 1 above don’t produce a result, then the program will allow the librarian to add the student to the library database upon producing a valid university student ID card which contains the name of the student, the course the student is doing and a unique student ID. An optional value for the student’s phone number may also be entered.
    Therefore, each Student should have the following attributes:
    • Student ID.
    • Name.
    • Phone number.
    • Book(s) issued to the student (Identified by the title and ISBN). Other attributes related to the book(s) appear as records under the specific book(s) in question. E.g., author, issue date, due date, etc.
    • Fines total.

    Requirement 3:
    Description: Check-out a book.

    The librarian initiates a book check-out by querying the database using the books ISBN number. When the database returns the result and the book details are shown, the librarian enters the student ID, and the program automatically updates the issue date and due date to the book record, and the ISBN and book title to the student record, after the librarian presses the check-out button.
    NOTE: The issue date is automatically updated as the current system date. The due date is automatically updated as 10 days from the current date. So the librarian will query the database using the ISBN and afterwards input the student ID and the program will do the rest. A maximum of 5 books can be checked-out at a time.

    Requirement 4:
    Description: Check-in a book.

    The librarians can check-in a book(s) using its ISBN number. When the database returns the search results, the check-in process is completed when the librarian presses the check-in button. The return date for any particular book is automatically updated as the current system date. The program also computes the fine (if any), as difference between the due date and the return date at a rate of 50p a day. This is updated to the student record using the student ID.

    Ok, your scenario 2 is more like it, since I want the program to prompt the user for a isbn number, which I think can be used to look up a book from the list of books. But how do I implement it?? My problem is actual coding, ive done coutless trials but to no avail.

    Thanks alot for your responses by the way.

  • #6
    New Coder
    Join Date
    Oct 2008
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I started coding the book class since its the once that holds most of the functions I want this program to do (check-in and check-out books). I had started with the library database but realized it turn out to be abit too complicated.

    There are 3 parts to the system:
    1. Identification of the student;
    2. Returning the book(s);
    3. Calculating any Fines.

    So checking out a book is not part of the part of the program but I thought, how do you return a book to a system that doesn't know you took it?? (The system must know when the book was checked out so that it can set a due date of 3 days from the current system date, thus making it possible to calculate a fine if due date < return date) That's why I decided to include it so it could make things a bit simpler. So I thought!!

    To understand this better, the section below outlines the requirements analysis for the system, which I used to draw the class diagram.
    REQUIREMENTS SPECIFICATIONS.
    Requirement 1:
    Description: Identify a student.

    The program shall let the librarian query the library database through a unique Student ID. The search results shall produce the student name, together with the details of books loaned out to the student (if any).
    The library database should then have the following details:
    • Records of the all students in the university that have registered to use the library.
    • All the books in the library that are available for lending. Each book should have the following attributes:
    o ISBN number (unique).
    o Title of the book.
    o Author.
    o Book status (In library or checked out).
    o If book is checked out, it would display the check-out date and student ID of borrower.
    o Due date.
    • Records of books loaned out to students sorted using the unique student ID.
    • A record of all librarians that operate the book lending service. Each librarian record should contain the librarians name and a unique librarian ID.

    Requirement 2:
    Description: Add a student record.

    If the search results in requirement 1 above don’t produce a result, then the program will allow the librarian to add the student to the library database upon producing a valid university student ID card which contains the name of the student, the course the student is doing and a unique student ID. An optional value for the student’s phone number may also be entered.
    Therefore, each Student should have the following attributes:
    • Student ID.
    • Name.
    • Phone number.
    • Book(s) issued to the student (Identified by the title and ISBN). Other attributes related to the book(s) appear as records under the specific book(s) in question. E.g., author, issue date, due date, etc.
    • Fines total.

    Requirement 3:
    Description: Check-out a book.

    The librarian initiates a book check-out by querying the database using the books ISBN number. When the database returns the result and the book details are shown, the librarian enters the student ID, and the program automatically updates the issue date and due date to the book record, and the ISBN and book title to the student record, after the librarian presses the check-out button.
    NOTE: The issue date is automatically updated as the current system date. The due date is automatically updated as 10 days from the current date. So the librarian will query the database using the ISBN and afterwards input the student ID and the program will do the rest. A maximum of 5 books can be checked-out at a time.

    Requirement 4:
    Description: Check-in a book.

    The librarians can check-in a book(s) using its ISBN number. When the database returns the search results, the check-in process is completed when the librarian presses the check-in button. The return date for any particular book is automatically updated as the current system date. The program also computes the fine (if any), as difference between the due date and the return date at a rate of 50p a day. This is updated to the student record using the student ID.

    Ok, your scenario 2 is more like it, since I want the program to prompt the user for a isbn number, which I think can be used to look up a book from the list of books. But how do I implement it?? My problem is actual coding, ive done coutless trials but to no avail.

    Thanks alot for your responses by the way.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •