1. Hi, Guest,

    Currently we have three official hacks running. CSGO, Battlefield Hardline and Audition America. Be sure to check them out!

    CSGO - "50 Shades of Gaben" - CSGO Cheat

    Battlefield Hardline Cheat - FREE

    Audition Redbana Hack [ARH Modz]

    More to Come!
    Dismiss Notice
Dismiss Notice
CSGO VIP Cheats now available!! Click here to get a copy!
Dismiss Notice
Want to Shorten Your Long URL? Check out our sister website Tiniurl to solve your needs!

[Java] Help with using classes

Discussion in 'Homework Help' started by yingyang7793, Mar 18, 2011.

Thread Status:
Not open for further replies.
  1. yingyang7793

    yingyang7793

    Messages:
    283
    Likes Received:
    5
    Joined:
    Feb 24, 2007
    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.
      [FONT=Symbol]·         [/FONT]Client can instantiate a book with a title and author
      [FONT=Symbol]·         [/FONT]The client can examine but not modify a book's title or author
      [FONT=Symbol]·         [/FONT]The client can ask a patron whether it has borrowed a given book
      [FONT=Symbol]·         [/FONT]The client can tell a patron to return a given book. (Identified by title)
      [FONT=Symbol]·         [/FONT]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. أرمجدون

    أرمجدون

    Messages:
    431
    Likes Received:
    34
    Joined:
    Dec 24, 2009
    I suggest for you to redo the project, and use notes [and examples] you got from class.
     
  3. yingyang7793

    yingyang7793

    Messages:
    283
    Likes Received:
    5
    Joined:
    Feb 24, 2007
    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. Legify

    Legify Retired Staff Member

    Messages:
    2,663
    Likes Received:
    218
    Joined:
    Nov 25, 2005
    I'm guessing this is solved then?
     
Thread Status:
Not open for further replies.

Share This Page