私はゲームのレビューがない場合、マップに追加する必要があるプログラムを作成しています。ゲームのレビューがある場合は、対応する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にまだまだ慣れていますが、何か助けに感謝します。ありがとう!
はじめに、HashMapはkey-> valueコレクションです(単一のキーが単一の値を指しています)、複数の値などのキーを必要とします。多分Multimap(Guavaを参照) > –
AlexC
あなたの質問に関連する(しかし完全な)コードだけを入れてください。 – nbrooks
@nbrooks関連コードのみを含めるように質問を編集しました – rls1982