vendredi 4 septembre 2015

How to fix error in if statement and when searching in an ArrayList in java

my Library calss need to be corrected , I wrote the error comments next to the wanted lines ... I used this: // ***** to show the error comments in eclipse. The main method of class Library must not modify , you just modify the subclasses under class Library ... I did like this :

import java.util.ArrayList;

public class Library {   

    ArrayList <String> list = new ArrayList<String>();
    String adress;

    pubilc void Library(String t){ // ***** it says - Syntax error on token "pubilc", public expected- This method has a constructor name

        this.adress=t;

    }

    public void addBook(String r){
        list.add(r);
    }
    public static void printOpeningHours(){
        System.out.println("Libraries are open daily from 9am to 5pm.");

    }
    public void printAddress(){
        System.out.println(adress);

    }
    public void borrowBook(String k){
        for (int i=0; i<list.size(); i++){
        if (list.get(i).equals(k)){
        System.out.println("You successfully borrowed The Lord of the Rings");
        list.remove(get(i)); // ***** it says The method get(int) is undefined for the type Library
        }else{
        System.out.println("Sorry, this book is already borrowed.");
        }

    }
    }
    public void printAvailableBooks(){
        System.out.println(list);

    }
    public void returnBook(String r){
        for (String b : list){
            if (b.get(b).equals(r)){ // ***** The method get(String) is undefined for the type String 
                return b; // ***** Void methods cannot return a value
                return;
            }
        }
        System.out.println("The Lord of the Ring");

    }


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

        // Add four books to the first library
        firstLibrary.addBook(new Book("The Da Vinci Code")); // ***** The method addBook(String) in the type Library is not applicable for the arguments (Book)
        firstLibrary.addBook(new Book("Le Petit Prince")); // ***** The method addBook(String) in the type Library is not applicable for the arguments (Book)
        firstLibrary.addBook(new Book("A Tale of Two Cities")); // ***** The method addBook(String) in the type Library is not applicable for the arguments (Book)
        firstLibrary.addBook(new Book("The Lord of the Rings")); // ***** The method addBook(String) in the type Library is not applicable for the arguments (Book)

        // 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();

        // 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();

        // Print the titles of all available books from both libraries
        System.out.println("Books available in the first library:");
        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();
    }
}

The output should be like this :

Library hours:
Libraries are open daily from 9am to 5pm.
Library addresses:
10 Main St.
228 Liberty St.
Borrowing The Lord of the Rings:
You successfully borrowed The Lord of the Rings
Sorry, this book is already borrowed.
Sorry, this book is not in our catalog.
Books available in the first library:
The Da Vinci Code
Le Petit Prince
A Tale of Two Cities
Books available in the second library:
No book in catalog
Returning The Lord of the Rings:
You successfully returned The Lord of the Rings
Books available in the first library:
The Da Vinci Code
Le Petit Prince
A Tale of Two Cities
The Lord of the Rings

Do you have any idea to correct the code to get the desired output ... thanks

Aucun commentaire:

Enregistrer un commentaire