2016-12-04 90 views
-1

我目前有三個主要類。電視劇類,其中包含一個對象,它的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的電視連續劇對象的對象:

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; 
} 

以及測試類。我將代碼添加到菜單中,但我不知道如何啓動它。

我有一個系列庫類中的方法,它允許用戶添加信息來創建一個新的電視劇,該方法返回他們新創建的對象。

在測試儀內部,我會用什麼代碼將這個新值添加到對象中的數組列表中,例如稱爲seriesLibrary。我需要創建一個新的方法嗎?我是否改變我目前的方法?

+0

對於一些之所以前面的評論已經被刪除了,以及我的,我已經編輯了這篇文章,以包含我當前的代碼。 – AceVenturos

+0

你檢查了下面的答案嗎?如果您對其中一個答案感到滿意,請將其標記爲解決方案。否則發表評論並說出了什麼問題! –

回答

0

我不知道如果我正確地理解你的問題,但你可以做到以下幾點:

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

這是假設seriesLibrary是你已經初始化SeriesLibrary對象和newSeriesToAdd是將對象從創建一個新的TVSeries對象返回。

0

由於您的方法請求Scanner作爲參數,您可以創建一個文件(txt文件),在其中輸入您在方法中詢問的所有信息,創建一個讀取文件的文件,然後通過那Scanner給你的方法。這是針對您的特定問題的解決方案。

我會建議你改變你的方法。由於需要從I/O通道(文件,流,stdin,...)讀取/掃描數據的方法的可測試性差,我總是習慣於將我的程序的掃描/讀取部分和代表某種邏輯運算。所以我的建議是創建一個單獨的函數來讀取/掃描您要求的數據,然後將所有這些數據傳遞給將處理該數據的方法(在您的情況下,使用基於該數據的正確值創建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

相關問題