User Tag List

Closed Thread
Results 1 to 4 of 4
  1. #1
    Registered User
    Reputation

    Join Date
    Feb 2007
    Last Online
    02-22-2014 @ 09:34 PM
    Posts
    286
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Java] Help with using classes

    So I'm taking a Java class at school, and we are currently working on this:
    Code:
     Patrons of a library can borrow up to 3 books. A patron, therefore, has a name and up to 3 books. A book has an author and a title. Design and implement 2 classes, patron and book, to represent these objects and the following behavior.
               Client can instantiate a book with a title and author
               The client can examine but not modify a book's title or author
               The client can ask a patron whether it has borrowed a given book
               The client can tell a patron to return a given book. (Identified by title)
               The client can tell a patron to borrow a given book
      The patron class should use a separate instance variable for each book ( total 3) Each of these variables is initially null. When a book is borrowed, the patron looks for a variable that is not null. If no such variable is found, the method returns false. If a null variable is found it is reset to the new book and the method returns true. Similar considerations apply to other methods. Use the method aString.equals(aString) to compare two strings for equality. Be sure to include appropriate toString methods for your classes and test them with a tester program
    And this is what I currently have.



    Patron:
    Code:
    public class patron
    {
        private String name;
        private Book book1, book2, book3;
        
        public patron(String n)
        {
            name = n;
            book1 = null;
            book2 = null;
            book3 = null;
        }
        
        public String getName()
        {
            return name;
        }
        
        public String borrowBook (Book b)
        {
            if (book1 == null)
            {
                book1 = b;
                return "Book can be borrowed";
            }
            else if (book2 == null)
            {
                book2 =b;
                return "Book can be borrowed";
            }
            else if (book3 == null)
            {
                book3 =b;
                return "Book can be borrowed";
            }
            else
                return "Cannot borrow book";
        }
        
        public String returnBook(String title)
        {
            if (book1 != null)
                if (book1.getTitle().equals(title))
                {
                    book1 = null;
                    return "Book has been returned";
                }
            if (book2 != null)
                if (book1.getTitle().equals(title))
                {
                    book2 = null;
                    return "Book has been returned";
                }
            if (book3 != null)
                if (book1.getTitle().equals(title))
                {
                    book3 = null;
                    return "Book has been returned";
                }
                return "Book is not checked out";
        }
        
        public boolean hasBook(String title)
        {
            if (book1 != null)
                if (book1.getTitle().equals(title))
                    return true;
            if (book2 != null)
                if (book2.getTitle().equals(title))
                    return true;
            if (book3 != null)
                if (book3.getTitle().equals(title))
                    return true;
                return false;
        }
        
        public String toString()
        {
            String str = "Patron's name:  " + name;
            if (book1 != null)
                str = str + "\n" + book1;
            if (book2 != null)
                str = str + "\n" + book2;
            if (book3 != null)
                str = str + "\n" + book3;
            return str;
        }   
    }
    Book:
    Code:
    public class Book
    {
        public String author, title;
        
        public Book(String t, String a)
        {
            author = a;
            title = t;
        }
        
        public String getAuthor()
        {
            return author;
        }
        
        public String getTitle()
        {
            return title;
        }
        
        public String toString()
        {
            return "Title: " + title + 
                   " \nAuthor: " + author;
        }
    }
    And the tester program:
    Code:
    import java.util.Scanner;
    public class client
    {
     public static void main (String [] args)
     {
         Scanner reader = new Scanner(System.in);
         String name;
         String title1;
         String author1;
         String title2;
         String author2;
         String title3;
         String author3;
         int menu = 1;
         int menu1;
         
         System.out.print ("Enter the name of the patron: ");
         name = reader.nextLine();
         System.out.print ("Enter the title of the first book: ");
         title1  = reader.nextLine();
         System.out.print ("Enter the author of the first book: ");
         author1 = reader.nextLine();
         System.out.print ("Enter the title of the second book: ");
         title2  = reader.nextLine();
         System.out.print ("Enter the author of the second book: ");
         author2 = reader.nextLine();
         System.out.print ("Enter the title of the third book: ");
         title3  = reader.nextLine();
         System.out.print ("Enter the author of the third book: ");
         author3 = reader.nextLine();
         
         patron p = new patron (name);
         Book b1 = new Book (title1, author1);
         Book b2 = new Book (title2, author2);
         Book b3 = new Book (title3, author3);
     /* patron p = new patron ("Ken Lambert");
      Book b1 = new Book("Cider House Rules", "John Irving");
      Book b2 = new Book("The Perfect Storm", "Sebastian Junger");
      Book b3 = new Book("The Illiad", "Homer");
      Book b4 = new Book("Hamlet", "William Shakespeare"); */
      if ( menu == 1 || menu == 2 || menu == 3|| menu == 4)
      {
      System.out.println ("Which book would you like to borrow? (1, 2, or 3) ");
      System.out.println ("Enter 0 to return a book.");
      System.out.println ("Enter 4 to display the books available for check out.");
          menu = reader.nextInt();
          if (menu == 1)
          {
      System.out.println(p.borrowBook(b1));
          }
          if (menu == 2)
          {
      System.out.println(p.borrowBook(b2));
          }
          if (menu == 3)
          {
      System.out.println(p.borrowBook(b3));
          }
          if (menu == 4)
          {
        System.out.print ("Book 1: \n" + b1 + "\n" + "Book 2: \n" + b2 + "\n" + "Book 3: \n" + b3 + "\n");
          }
                if (menu >= 5 || menu <= -1)
          {
              System.out.print ("Invalid entry entered, please reenter the number of the book you would like to borrow: ");
              menu = reader.nextInt();
          }
      }
          if ( menu == 0)
          {
              while ( menu == 0)
              {
              System.out.println ("Which book would you like to return? (1, 2, or 3) ");
              menu1 = reader.nextInt();
                  if (menu1 != 1 || menu1 != 2 || menu1 != 3 || menu1 != 0)
                  {
                      System.out.print ("Invalid entry entered, please reenter the number of the book you would like to borrow: ");
                      System.out.print ("Enter 0 to exit");
                      menu1 = reader.nextInt();
                  }
                  if ( menu1 == 1)
                  {
                      System.out.println (p.returnBook (b1));    
                  }
                   if ( menu1 == 2)
                  {
                      System.out.println (p.returnBook (b2));    
                  }
                  if ( menu1 == 3)
                  {
                      System.out.println (p.returnBook (b3));    
                  }
                  if (menu1 == 0)
                  {
                      menu = 1;
                  }
              }
          }
    
    //  System.out.println(p.borrowBook(b4));
      
     // System.out.println(p);
          
          
      //    System.out.println (p.hasBook("Cider House Rules"));
      //    System.out.println (p.hasBook("The Perfect Storm"));
          
          System.out.println(p);
          
     }
    }
    The above does not work, so I'm asking for a push in the right direction on how to program this.

  2. #2
    Registered User
    Reputation Reputation Reputation

    Join Date
    Dec 2009
    Last Online
    09-03-2012 @ 06:13 AM
    Posts
    432
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: [Java] Help with using classes

    I suggest for you to redo the project, and use notes [and examples] you got from class.

  3. #3
    Registered User
    Reputation

    Join Date
    Feb 2007
    Last Online
    02-22-2014 @ 09:34 PM
    Posts
    286
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: [Java] Help with using classes

    Apparently I misinterpreted the teacher. This was all we needed to do ._.

    Code:
    import java.util.Scanner;
    public class LibraryTester
    {    
        public static void main (String[] args)
        {
            Scanner reader = new Scanner(System.in);
            String title1;
            String author1;
            String title2;
            String author2;
            String title3;
            String author3;
            String patron;
            
            System.out.print("Enter the name of your book: ");
            title1 = reader.nextLine();
            System.out.print("Enter the author's name(s): ");
            author1 = reader.nextLine();
            System.out.print("Enter the name of your second book: ");
            title2 = reader.nextLine();
            System.out.print("Enter the author's name(s): ");
            author2 = reader.nextLine();
            System.out.print("Enter the name of your third book: ");
            title3 = reader.nextLine();
            System.out.print("Enter the author's name(s): ");
            author3 = reader.nextLine();
            System.out.print("Enter the name of patron: ");
            patron = reader.nextLine();
            
    
            Patron p = new Patron(patron);
            Book b1 = new Book(title1, author1);
             Book b2 = new Book(title2, author2);
            Book b3 = new Book(title3, author3);
            
            
            System.out.println(p.borrowBook(b1));
            System.out.println(p.borrowBook(b2));
            System.out.println(p.borrowBook(b3));
            
            System.out.print(p);
            
            System.out.println(p.returnBook(title1));
            System.out.println(p.hasBook(title2));
            System.out.println(p.hasBook(title3));
            
            System.out.println(p);
        }
    }

  4. #4
    永遠の統治

    Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation
    Legify's Avatar
    Join Date
    Nov 2005
    Last Online
    @
    Location
    The Dark Abyss.
    Posts
    2,850
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: [Java] Help with using classes

    I'm guessing this is solved then?
    -Legify Xentos

    Visit the
    To view links or images in signatures your post count must be 5 or greater. You currently have 0 posts.
    !
    Check out the lastest graphics competitions
    To view links or images in signatures your post count must be 5 or greater. You currently have 0 posts.
    !

    Currently Inactive for the time being.

Closed Thread

Bookmarks

Posting Permissions

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