1
    import java.util.Scanner;

public class Library {
    public static void main(String [] args) {
    /*
     * Patron class: name + 3 books
     * Book class: author + title
     */
    Book TwelveYears = new Book ("12 Years a Slave", "Solomon Northup");
    Book Diary = new Book ("The Diary of John Smith", "John Smith");
    Book Fahrenheit451 = new Book ("Fahrenheit 451", "Ray Bradbury");
    Book Gatsby = new Book ("The Great Gatsby", "Fitzgerald");
    Book Antigone = new Book ("12 Years a Slave", "Sophocles");




    System.out.println("Welcome to library account management! Here is a list of books to check out: " +TwelveYears.getBookTitle()+", "+Diary.getBookTitle()+", "+Fahrenheit451.getBookTitle()+", "+Gatsby.getBookTitle()+", "+Antigone.getBookTitle()+".");
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter your name!");
    String enteredName = sc.nextLine();
    System.out.println("Enter your first book! Enter it exactly as seen above.");
    String book1 = sc.nextLine();
    System.out.println("Enter your second book!");
    String book2 = sc.nextLine();
    System.out.println("Enter your third book!");
    String book3 = sc.nextLine();
    Patron patron1 = new Patron(enteredName, book1, book2, book3);
    book1 = //line I need help on
    System.out.println("Here are the books you are checking out: " +book1+ ", " +book2+ ", " +book3+ ".");





    }

}

Here, I'm trying to take the book1 variable, and assosciate it with a member of a class properly. I think the best way to explain what I'm trying to do here is with an example: if book1 entered by the user is "12 Years a Slave" then book1's value would be "TwelveYears", one of the class definitions near the start of the code, so in the future, I could use an accessor/mutator method like book1.getAuthor.

I'm just learning java, so I lack the ways to properly explain this most likely, so I apologize, but any help would be greatly appreciated, and I would be happy to answer any questions if what I'm trying to do here, but I hope I was clear enough.

Thank you!

2
  • book1.getAuthor() won't work since book1 is an instance of a String. If that is what you want. Commented Nov 6, 2015 at 4:08
  • do u mean how to build your code in a a structured manner to retrieve the book based on what the user entered? like if user enter book name or author u return the relevant books? Commented Nov 6, 2015 at 6:03

2 Answers 2

2

If you have to decide which object to create based on a String, you can simply do a bunch of checks on the book titles and determine which object to create. Assuming TwelveYears and GreatGatsby both inherit from Book, then you can do:

Book unknownBook;
switch(bookTitleScanned)
{
    case("Twelve Years a Slave")
        unknownBook = new TwelveYears();
    case("Great Gatsby")
        unknownBook = new GreatGatsby();
    ...
}

Here your Book reference can point to TwelveYears and GreatGatsby because of polymorphism.

But here's probably a better way to do what you wanted:
Assuming all the objects for your purpose are fairly similar (in your example you have books like TwelveYears and GreatGatsby), it might be easier if you simply write one class, say Book, and let that class do all the handling, instead of creating separate classes.

Then you simply create instances of Book class, assign the book names as attributes to these instances, and collect them in some kind of Collections object, which will make your code much more flexible and reusable (hey you can loop over your Books if you put them in say an ArrayList).

Concretely, your Book class can have an attribute say bookName, and methods like

public void getBookName()
{
    return bookName;
}

or even:

public void handleParticularBook()
{
   if (bookName.equals("GreatGatsby"))
      System.out.println("My favorite book!");
   else if ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

I believe OP's question is "Using Input from Scanner to Assign Class", so I assume multiple classes are available.
Thank you for your well written answer! It helped a lot!
1

place all the books in ArrayList<Book>. Iterate ArrayList and use book1.equals(a1.get(i).getBookName) if matches the book1=a1.get(i).getBookTitle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.