Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Can anyone help me with java code

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can anyone help me with java code

    Hi everyone

    I am writing a simple java code Library class and Book class. I have hard time to write the code to return a book after borrowing it. I need help. This is my code

    #########################Book class################################
    package Library;

    public class Book {
    String title;

    boolean borrowed = false;
    //borrowed = false;

    // Creates a new Book
    public Book(String bookTitle) {
    title = bookTitle;
    }
    // Marks the book as rented
    public void borrowed() {
    borrowed = true;
    }
    // Marks the book as not rented
    public void returned() {
    borrowed = false;
    }
    // Returns true if the book is rented, false otherwise
    public boolean isBorrowed() {
    if (borrowed)
    return true;
    else
    return false;
    }
    // Returns the title of the book
    public String getTitle() {
    return title;
    }
    }

    ##############################Library class###################################

    package Library;

    public class Library {
    String address;
    String title;

    public Book myBook;

    Library(String streetname) {
    address = streetname;
    }

    String[] tmp = new String[4];
    int bookcount = 0;

    //adding book
    public void addBook(Book book) {
    tmp[bookcount] = book.getTitle();
    bookcount++;
    }

    //Library hours
    public static void printOpeningHours() {
    System.out.println("Libraries are open daily from 9am to 5pm.");
    }

    //Library address
    public void printAddress() {
    System.out.println(this.address);
    }

    //Avaiable book
    public void printAvailableBooks() {
    for (int j = 0; j < bookcount; j++)
    {
    System.out.println(tmp[j]);
    }
    }

    //borrowed book
    public void borrowBook(String title) {
    for (int i = 0; i < bookcount; i++)
    {
    if (tmp[i].equals(title))
    tmp[i] = "";
    }
    }

    //returning book -----> I have trouble here
    /* public void returnBook(String title) {

    for (int i = 0; i < bookcount; i++)
    {
    if (tmp[i].equals(title))
    System.out.println("Your book is from other library. ");
    else

    }
    }*/

    ########################test bench#########################################

    public static void main(String[] args) {
    // Create two libraries
    Library firstLibrary = new Library("10 Main St.");
    Library secondLibrary = new Library("228 Liberty St.");

    // Add four books to the first library
    firstLibrary.addBook(new Book("The Da Vinci Code"));
    firstLibrary.addBook(new Book("Le Petit Prince"));
    firstLibrary.addBook(new Book("A Tale of Two Cities"));
    firstLibrary.addBook(new Book("The Lord of the Rings"));

    // Print opening hours and the addresses
    System.out.println("Library hours:");

    printOpeningHours();

    System.out.println();

    System.out.println("Library addresses:");

    firstLibrary.printAddress();

    secondLibrary.printAddress();

    System.out.println();

    System.out.println("Books available in the first library:");

    firstLibrary.printAvailableBooks();

    System.out.println();

    // Try to borrow The Lords of the Rings from both libraries
    System.out.println("Borrowing The Lord of the Rings:");

    firstLibrary.borrowBook("The Lord of the Rings");

    firstLibrary.borrowBook("The Lord of the Rings");

    secondLibrary.borrowBook("The Lord of the Rings");

    System.out.println();

    System.out.println("Borrowing Le Petit Prince:");

    // Print the titles of all available books from both libraries
    System.out.println("Books available in the first library:");

    firstLibrary.borrowBook("Le Petit Prince");

    firstLibrary.printAvailableBooks();

    System.out.println();

    System.out.println("Books available in the second library:");

    secondLibrary.printAvailableBooks();

    System.out.println();

    // Return The Lords of the Rings to the first library
    System.out.println("Returning The Lord of the Rings:");
    //firstLibrary.returnBook("The Lord of the Rings");
    System.out.println();
    // Print the titles of available from the first library
    System.out.println("Books available in the first library:");
    firstLibrary.printAvailableBooks();
    }
    }


  2. #2
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: Can anyone help me with java code

    Tell us what errors you are getting and then post the full error message here. Also, tell us whether the result wasn't what you were expecting and what you expect of it.
    Who holds the KEY to all knowledge?

Similar Threads

  1. Replies: 0
    Last Post: May 23rd, 2013, 04:35 PM
  2. Trouble Porting my Java File Reading Code to Android Code
    By Gravity Games in forum Android Development
    Replies: 0
    Last Post: December 6th, 2012, 04:38 PM
  3. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  4. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM
  5. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM