将数据添加到对象内包含的数组列表中

时间:2016-12-04 19:22:21

标签: java

我目前有三个主要课程。一个tv系列类,包含一个对象及其getter和setter:

public class TVSeries {
private String title;
private Genre genre;
private Rating rating;
private int numOfEps;
private ArrayList<String> actor = new ArrayList<String>();
private ArrayList<Integer> listOfReviews = new ArrayList<Integer>();

public TVSeries(String title, Genre genre, Rating rating, int numOfEps, ArrayList<String> actor, ArrayList<Integer> listOfReviews) {
    this.title = title;
    this.genre = genre;
    this.rating = rating;
    this.numOfEps = numOfEps;
    this.actor = actor;
    this.listOfReviews = listOfReviews;

一个系列库类,它是一个仅由arraylist tv系列对象组成的对象:

public class SeriesLibrary {
private ArrayList<TVSeries> tvSeries = new ArrayList<TVSeries>();

public SeriesLibrary(ArrayList<TVSeries> tvSeries) {
    this.tvSeries = tvSeries;
}

public ArrayList<TVSeries> getTvSeries() {
    return tvSeries;
}

public void setTvSeries(ArrayList<TVSeries> tvSeries) {
    this.tvSeries = tvSeries;
}

public TVSeries addTVSeries(Scanner sc) {
    System.out.println("Add a new TV Series");
    System.out.println("------------------------------");
    System.out.print("Please enter the TV series' Title: ");
    String title = sc.nextLine();
    System.out.print("Please enter the TV series' Genre: ");
    String strGenre = sc.nextLine();
    System.out.print("Please enter the TV series' Age Rating: ");
    String strRating = sc.nextLine();
    System.out.print("Please enter the number of Episodes in the TV Series: ");
    int numOfEps = sc.nextInt();
    sc.nextLine();
    System.out.println(title + " will be set up with no actors or ratings. These can be added later.");
    ArrayList<String> emptyActors = new ArrayList<String>();
    ArrayList<Integer> emptyReviews = new ArrayList<Integer>();
    Genre genre = Genre.valueOf(strGenre);
    Rating rating = Rating.valueOf(strRating);
    TVSeries newSeries = new TVSeries(title, genre, rating, numOfEps, emptyActors, emptyReviews);
    return newSeries;
} 

还有一个测试员班。我将代码添加到菜单中,但我真的不知道如何启动它。

我在系列库类中有一个方法,它允许用户添加信息来创建一个新的tv系列,并且该方法返回他们新创建的对象。

在测试器中,我将使用什么代码将这个新值添加到对象中的数组列表中,例如,称为seriesLibrary。我必须创建一个新方法吗?我改变现有的方法吗?

2 个答案:

答案 0 :(得分:0)

我不确定我是否正确理解了您的问题,但您可以执行以下操作:

ArrayList<TVSeries> tvSeriesLibrary = seriesLibrary.getTvSeries();
tvSeriesLibrary.add(newSeriesToAdd);
seriesLibrary.setTvSeries(tvSeriesLibrary);

假设seriesLibrary是您已初始化的SeriesLibrary对象,而newSeriesToAdd是从创建新TVSeries对象返回的对象。

答案 1 :(得分:0)

由于您的方法请求Scanner作为参数,您可以创建一个文件(txt文件),在其中键入您在方法中询问的所有信息,创建Scanner它会读取您的文件,然后将Scanner传递给您的方法。这是您特定问题的解决方案。

我建议你改变你的方法。由于需要从I / O通道(文件,流,标准输出,...)读取/扫描数据的方法的可测试性低,我总是习惯将我的程序的扫描/读取部分和代表某种类型的代码分开。逻辑运算。所以我的建议是创建一个单独的函数来读取/扫描您要求的数据,然后将所有这些数据传递给将处理该数据的方法(在您的情况下,使用正确的值创建TVSeries基于该数据)。在你的情况下,它可能是这样的:

public TVSeries addTVSeriesFromInput(Scanner sc) {
    System.out.println("Add a new TV Series");
    System.out.println("------------------------------");
    System.out.print("Please enter the TV series' Title: ");
    String title = sc.nextLine();
    System.out.print("Please enter the TV series' Genre: ");
    String strGenre = sc.nextLine();
    System.out.print("Please enter the TV series' Age Rating: ");
    String strRating = sc.nextLine();
    System.out.print("Please enter the number of Episodes in the TV Series: ");
    int numOfEps = sc.nextInt();
    sc.nextLine();
    System.out.println(title + " will be set up with no actors or ratings. These can be added later.");
    return addTVSeries(title, strGentre, strRating, numOfEps);
}

public TVSeries addTVSeries(String title, String strGenre, String strRating, int numOfEps){ 
    ArrayList<String> emptyActors = new ArrayList<String>();
    ArrayList<Integer> emptyReviews = new ArrayList<Integer>();
    Genre genre = Genre.valueOf(strGenre);
    Rating rating = Rating.valueOf(strRating);
    TVSeries newSeries = new TVSeries(title, genre, rating, numOfEps, emptyActors, emptyReviews);
    return newSeries;
}

PS:我发现您的方法被称为addTVSeries非常奇怪,但它只会创建一个新的TVSeries对象,而不会将其添加到列表tvSeries

相关问题