2016-11-06 22 views
0

私はゲームのレビューがない場合、マップに追加する必要があるプログラムを作成しています。ゲームのレビューがある場合は、対応するGameInfoにレビューを追加します。私のコードはうまくコンパイルされますが、私のコードが正しく実行されていることを示すために私のユニットテストは戻り値が返されません。私のコードは以下の通りです:Java:ユニットテストで正しい結果が返されない

class GameInfoCollection { 
    // TODO - you need to use a Map (from a String, the title, to a GameInfo) to keep track of all the GameInfo's 
    Map<String, Integer> titles = new HashMap<String, Integer>(); 
    // TODO - if there are no reviews for the game, create a new GameInfo (with this review) and add it to the map 
    // if there's one, add the given review to the corresponding GameInfo 
    public void addGameReview(String gameTitle, Review r) 
    { 
     if (titles.isEmpty()) { 
      GameInfo g = new GameInfo("Review"); 
     } else titles.put(gameTitle, 1); 

    } 

    public int getNumberOfReviewsForGame(String gameTitle) 
    { 
     // TODO - implement this 
     return titles.get(titles); 
    } 

ここに私のユニットテストがあります。これは、20ポイントを返す必要がありますが、それは任意のポイントを返していません。ここで

public void testGetNumberOfReviewsForGame() 
    {  
     GameInfoCollection gic=new GameInfoCollection(); 


     gic.addGameReview("g1",new Review("cool",5)); 
     gic.addGameReview("g1",new Review("cool",3)); 
     gic.addGameReview("g2",new Review("cool",2)); 
     gic.addGameReview("g3",new Review("cool",2)); 

     Assert.assertEquals(2,gic.getNumberOfReviewsForGame("g1")); 
     Assert.assertEquals(1,gic.getNumberOfReviewsForGame("g2")); 
     gic.addGameReview("g1",new Review("cool",3)); 
     Assert.assertEquals(3,gic.getNumberOfReviewsForGame("g1")); 
    } 

    @Grade(points=20) 
    @Test 

は、私はまだに持っていない最後のセクションがあるというのが私の全体program.Pleaseノートの私のコードです。

package assignment; 

import java.text.MessageFormat; 
import java.util.Scanner; 

import java.awt.Point; 
import java.awt.Dimension; 
import java.awt.Rectangle; 

import java.time.LocalDate; 

import java.util.*; // List, ArrayList, Map, HashMap 

class Review { 
    public String reviewText; 
    public int numberOfStars; 

    public Review(String reviewText, int numberOfStars) { 
     this.reviewText=reviewText; 
     this.numberOfStars=numberOfStars; 
    } 
} 

class GameInfo { 
    private String title; 
    // need an ArrayList to keep the reviews; 
    private Review[] reviews = new Review[10]; 
    int numReviews=0; 

    public GameInfo(String title) { 
     this.title=title; 
     // you may want to initialize any other variables you create here 
    } 

    public String getTitle() { 
     return title; 
    } 

    // TODO - adds the review to the 'array' of reviews. You need to keep all reviews in an array 
    public void addReview(Review r) { 
     reviews[numReviews] = r; 
     ++numReviews; 
    } 

    // TODO - returns the number of reviews which have been added to this GameInfo 
    public int getNumberOfReviews() { 
     return numReviews; 
    } 

    // TODO - returns the sum of the number of stars which have been added to this GameInfo 
    // you have to calculate this from your array 
    public int getSumOfStars() { 
     int sum=0; 
     for (int i=0; i<numReviews;++i) 
      sum +=reviews[i].numberOfStars; 
     return sum; 
    } 

    // TODO - returns the average number of stars for this GameInfo's reviews 
    // again, have to calculate this (or at least the sum of stars) from your array 
    public double getAverageStarRating() { 
     double firstNumber = getSumOfStars(); 
     double secondNumber = getNumberOfReviews(); 
     double avg = firstNumber/secondNumber; 

     return avg; 
    } 
} 

// TODO - you need to implement all these methods 
class GameInfoCollection { 
    // TODO - you need to use a Map (from a String, the title, to a GameInfo) to keep track of all the GameInfo's 
    Map<String, Integer> titles = new HashMap<String, Integer>(); 
    // TODO - if there are no reviews for the game, create a new GameInfo (with this review) and add it to the map 
    // if there's one, add the given review to the corresponding GameInfo 
    public void addGameReview(String gameTitle, Review r) 
    { 
     if (titles.isEmpty()) { 
      GameInfo g = new GameInfo("Review"); 
     } else titles.put(gameTitle, 1); 

    } 

    public int getNumberOfReviewsForGame(String gameTitle) 
    { 
     // TODO - implement this 
     return titles.get(titles); 
    } 

私はJavaにまだまだ慣れていますが、何か助けに感謝します。ありがとう!

+1

はじめに、HashMapはkey-> valueコレクションです(単一のキーが単一の値を指しています)、複数の値などのキーを必要とします。多分Multimap (Guavaを参照) > – AlexC

+0

あなたの質問に関連する(しかし完全な)コードだけを入れてください。 – nbrooks

+0

@nbrooks関連コードのみを含めるように質問を編集しました – rls1982

答えて

1

これは宿題のように見えますが、ユニットテストは教師が提供したコードの一部であると推測しています。ユニットテストが失敗しているということは、ユニットテストが間違っているのではなく、コードが正しく動作していないことを意味します。特に、完了していない部分がGameInfoCollectionです。

はそれでは、これらのTODOの各手段をコメント何にわたり行ってみよう:

// TODO - you need to use a Map (from a String, the title, to a GameInfo) to keep track of all the GameInfo's 

Aマップがキーと値を持っており、キーから値へのマッピングと言われています。宣言Map<String, Integer>では、最初の型(String)はキーの型であり、2番目の型は値の型です。 TODOは、値のタイプがGameInfoである必要があると言っています。

// TODO - if there are no reviews for the game, create a new GameInfo (with this review) and add it to the map 

このすべての部分はifブランチ内で実行する必要があります。 a)新しいGameInfoを作成し、b)レビューを入れ、c)マップに追加する必要があります。あなたは現在、a)の部分だけを行っていますが、それでもコンストラクタに適切な値を渡していない場合でも、GameInfoコンストラクタとそれが取る引数の名前を見てください。 「レビュー」はそれに合っていますか?

// if there's one, add the given review to the corresponding GameInfo 

これは、elseブランチでどうなるべきかについてです。

// TODO - implement this 

技術的には、この方法では、すでに実装がありますが、正しくマップの値型についての最初のTODOからの変化に合わせて、それを変更する必要があるとしています。

関連する問題