2 Replies - 382 Views - Last Post: 07 October 2012 - 03:51 PM Rate Topic: -----

#1 jblankinship  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-October 12

Library Program unresolved compilation problems

Posted 07 October 2012 - 10:38 AM

I am learning java. I have an assignment (see attachment). I am having problems with one of my methods (borrowBook) but I don't know why.

Patron.class

import java.awt.print.Book;

public class Patron {

	public String patron1;
	public Book book1, book2, book3;
	
	public Patron (){
		
		book1 = null;
		book2 = null;
		book3 = null;
	}
	public Patron(String yourName){
		patron1 = yourName;
		
		book1 = null;
		book2 = null;
		book3 = null;
	}
	public void setName(String yourName){
		patron1 = yourName;
	}
	public String getName(){
		return patron1;
	}
	public boolean borrowBook(Book book){
		if (book1 != null){
			book1 = book;
			return true;
		}
		if(book2 != null){
			book2 = book;
			return true;
		}
		if(book3 != null){
			book3 = book;
			return true;
		}
		return false;
	}
	public boolean returnBook(String title){
		if (book1.equals(title)){
			book1 = null;
			return true;
		}
		if (book2.equals(title)){
			book2 = null;
			return true;
		}
		if (book3.equals(title)){
			book3 = null;
			return true;
		}
		return false;
	}
	boolean hasBorrowed(String titlex){
		return false;
	}
	public String toString(){
		String str;
		str = "Name: " + "\n" + patron1 + "\n" +
				book1 + "\n" +
				book2 + "\n" +
				book3 + "\n";
		return str;
		}
}



Book class

public class Book {
	public String title;
	public String author;
	
	public Book(String title, String author) {
		
		this.title = title;
		this.author = author;

	}
	
	public String getTitle(String title){
		
		return title;
	}
	
	public String getAuthor(String author){
		
		return author;
	}
	
	public String toString(){
		String str;
		str = "Author:\t" + author + "\n" + "Title:\t" + title + "\n";
		return str;
	}

}



tester.java

import java.util.Scanner;

public class Tester {
  public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    //Create 2 Patron objects to use in the test
    Patron patron1 = new Patron("John Doe");
    Patron patron2 = new Patron();

    //Read in and save the info for the first book
    System.out.print("Please enter the title for John Doe's first book: ");
    String title1 = reader.nextLine();
    System.out.print("Please enter the author for John Doe's first book: ");
    String author1 = reader.nextLine();
    Book book1 = new Book(title1, author1);
    boolean result1 = patron1.borrowBook(book1);
    if (!result1) System.err.println("Book 1 for John Doe failed.");

    //Use the toString method to print the info for Patron John Doe after 1 book
    System.out.println("\n" + patron1.toString() + "\n");

    //Read in and save the info for the second book
    System.out.print("Please enter the title for John Doe's second book: ");
    String title2 = reader.nextLine();
    System.out.print("Please enter the author for John Doe's second book: ");
    String author2 = reader.nextLine();
    Book book2 = new Book(title2, author2);
    boolean result2 = patron1.borrowBook(book2);
    if (!result2) System.err.println("Book 2 for John Doe failed.");

    //Use the toString method to print the info for Patron John Doe after 2 books
    System.out.println("\n" + patron1.toString() + "\n");

    //Read in and save the info for the third book
    System.out.print("Please enter the title for John Doe's third book: ");
    String title3 = reader.nextLine();
    System.out.print("Please enter the author for John Doe's third book: ");
    String author3 = reader.nextLine();
    Book book3 = new Book(title3, author3);
    boolean result3 = patron1.borrowBook(book3);
    if (!result3) System.err.println("Book 3 for John Doe failed.");

    //Use the toString method to print the info for Patron John Doe
    System.out.println("\n" + patron1.toString() + "\n");

    //Read in and attempt to save the info for a fourth book
    System.out.print("Please enter the title for John Doe's fourth book: ");
    String title4 = reader.nextLine();
    System.out.print("Please enter the author for John Doe's fourth book: ");
    String author4 = reader.nextLine();
    Book book4 = new Book(title4, author4);

    //This attempt to borrow a fourth book should fail
    boolean result4 = patron1.borrowBook(book4);
    if (!result4) System.out.println("Book 4 for John Doe failed.\n" +
                                     "This message should print since 3" +
                                     " books max can be borrowed.\n");

    //Read for a book title that is already borrowed
    System.out.print("Please enter the title to return from John Doe's borrowed list: ");
    String titlex = reader.nextLine();

    //Check to see if this title has already been borrowed
    if (patron1.hasBorrowed(titlex)){
      //Try to return this title
      boolean resultx = patron1.returnBook(titlex);
      //Check to see if this title was successfully returned
      if (resultx){
        System.out.println(titlex + " has been returned.");
        //Now there should be room for the fourth book
        boolean result4x = patron1.borrowBook(book4);
        if (result4x)
          System.out.println("John Doe's fourth book successfully checked out.");
        else
          System.err.println("Book 4 for John Doe failed again.");
      }
      else
        System.err.println(titlex + " failed to be returned.");
    }
    else {
      System.err.println("Title not found in John Doe's checked out list.");
    }

    //Use the toString method to print the info for Patron John Doe
    System.out.println("\n" + patron1.toString() + "\n");

    //Read in your name and save it in patron2
    System.out.print("Please enter your name: ");
    String yourName = reader.nextLine();
    patron2.setName(yourName);

    //Use the toString method to print the info for Patron with your name
    System.out.println("\n" + patron2.toString() + "\n");
  }
}



The assignment was to create the book and patron class, the tester.java file was provided. So I am not sure were I have gone wrong but any suggestions would be appreciated.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Library Program unresolved compilation problems

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Library Program unresolved compilation problems

Posted 07 October 2012 - 10:55 AM

Could you go into more detail about what the error says, and where it's at?

I do notice in your borrowBook method that you are testing it the books are not null. By the looks of it you are setting books that can be borrowed to null, and books that are borrowed get a value. So if you are trying to borrow a book all the books are null and the method will always return false.

This post has been edited by Kinaces: 07 October 2012 - 10:57 AM

Was This Post Helpful? 0
  • +
  • -

#3 jblankinship  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-October 12

Re: Library Program unresolved compilation problems

Posted 07 October 2012 - 03:51 PM

View PostKinaces, on 07 October 2012 - 10:55 AM, said:

Could you go into more detail about what the error says, and where it's at?

I do notice in your borrowBook method that you are testing it the books are not null. By the looks of it you are setting books that can be borrowed to null, and books that are borrowed get a value. So if you are trying to borrow a book all the books are null and the method will always return false.


Sorry, it just won't compile. It gives a generic error message relating to the borrowBook class. I think it was not returning a value you. But I figured it out. Some how import java.awt.print.Book; was added to the top of my patron class. Once I removed this, it would compile.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1