如何使用扫描仪将对象添加到数组列表?

How would I add a object to a arraylist using scanner?

我有一个数组列表,我的目标是不断向数组列表中添加一个对象,直到用户键入一个值。我有两个不同的 classes。我曾尝试使用扫描仪,但这只会更改变量的名称而不会创建新对象。我是否必须继承其他 class。我将如何继续增加 arraylist 的大小?任何帮助表示赞赏。

下面是我的第一个class:

public class Real_Entertainment 
{
    String BookName; 
    String Author; 
    double Rating; 
    int Purchases;

    public Real_Entertainment(String BookName, String Author, double Rating, int Purchases, int i)
    {
        this.BookName = BookName;
        this.Author = Author;
        this.Rating = Rating;
        this.Purchases = Purchases;
    }
    public void setBookName(String BookName)
    {
        this.BookName = BookName;
    }
    public void setAuthor (String Author )
    {
        this.Author = Author;
    }
    public void setRating (double Rating)
    {
        this.Rating = Rating;
    }
    public void setPurchased (int Purchases)
    {
        this.Purchases = Purchases;
    }

}

下面是我的主要class:

import java.util.Scanner;
import java.util.ArrayList;

public class Real_Main 
{
    public static void main(String[] args) 
    {
        ArrayList<Real_Entertainment> print = new ArrayList<Real_Entertainment>();
        Real_Entertainment Book1 = new Real_Entertainment("The Nickel Boys ", "Colson 
        Whitehead", 4.3, 500,000);
        Real_Entertainment Book2 = new Real_Entertainment("Olive, Again", "Elizabeth Strout", 6, 
        321,000);
        Real_Entertainment Book3 = new Real_Entertainment("Gingerbread", "Helen Oyeyemi", 2, 
        681,000);
        Real_Entertainment Book4 = new Real_Entertainment ("On Earth We're Briefly Gorgeous", 
        "Ocean Vuong", 2, 421,000);

        print.add(Book1);
        print.add(Book2);
        print.add(Book3);
        print.add(Book4);
    }

    public void addToList(ArrayList<Real_Entertainment> print)
    { 

         //The method where it adds to the arraylist    
    }

}

从概念上讲,您想要做的是捕获用户的输入(例如,书名),然后使用该数据实例化一个新的 Real_Entertainment 实例(如果您仅此而已)需要),然后将新实例添加到现有列表中。例如:

Scanner scanner = new Scanner(System.in);
System.out.print("What is your book's title? ");
String title = scanner.next();
Real_Entertainment realEntertainment = new Real_Entertainment(title, null, 0, 0, 0);
print.add(realEntertainment);