简体   繁体   English

使用扫描仪输入分配类

[英]Using Input from Scanner to Assign Class

    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. 在这里,我正在尝试使用book1变量,并将其与正确的类成员进行协调。 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. 我认为解释我在这里尝试做什么的最好方法是举个例子:如果用户输入的book1是“12年奴隶”,那么book1的值将是“TwelveYears”,其中一个类定义在开头附近代码,所以在将来,我可以使用像book1.getAuthor这样的访问器/ mutator方法。

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. 我只是在学习java,所以我没有办法正确解释这个问题,所以我很抱歉,但是我会非常感激任何帮助,如果我想在这里做什么,我很乐意回答任何问题,但我希望我足够清楚。

Thank you! 谢谢!

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. 如果必须根据String决定要创建哪个对象,则只需对书名进行一系列检查,并确定要创建的对象。 Assuming TwelveYears and GreatGatsby both inherit from Book , then you can do: 假设TwelveYearsGreatGatsby都继承自Book ,那么你可以这样做:

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 . 在这里,由于多态性,您的Book参考可以指向TwelveYearsGreatGatsby

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 book s 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. 假设所有用于你目的的对象都非常相似(在你的例子中你有像TwelveYearsGreatGatsby这样的book ),如果你简单地写一个类,比如Book ,让那个类做所有的处理,而不是创建它可能会更容易单独的课程。

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 Book s if you put them in say an ArrayList ). 然后,您只需创建Book类的实例,将书名作为属性分配给这些实例,并将它们收集到某种Collections对象中,这将使您的代码更加灵活和可重用(嘿,如果你可以循环你的Book把它们放在一个ArrayList

Concretely, your Book class can have an attribute say bookName , and methods like 具体来说,您的Book类可以有一个属性,例如bookName ,以及类似的方法

public void getBookName()
{
    return bookName;
}

or even: 甚至:

public void handleParticularBook()
{
   if (bookName.equals("GreatGatsby"))
      System.out.println("My favorite book!");
   else if ...
}

place all the books in ArrayList<Book> . 将所有书籍放在ArrayList<Book> Iterate ArrayList and use book1.equals(a1.get(i).getBookName) if matches the book1=a1.get(i).getBookTitle 如果匹配book1=a1.get(i).getBookTitle则迭代ArrayList并使用book1.equals(a1.get(i).getBookName)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM