diff --git a/src/main/java/com/teamtreehouse/flashy/controllers/IndexController.java b/src/main/java/com/teamtreehouse/flashy/controllers/IndexController.java index f127754..68958eb 100644 --- a/src/main/java/com/teamtreehouse/flashy/controllers/IndexController.java +++ b/src/main/java/com/teamtreehouse/flashy/controllers/IndexController.java @@ -12,6 +12,7 @@ @Controller public class IndexController { + public static final int AMOUNT_IN_TOTAL = 3; private FlashCardService flashCardService; @Autowired @@ -22,7 +23,7 @@ public void setFlashCardService(FlashCardService flashCardService) { @RequestMapping("/") public String index(Model model) { StringBuilder ctaBuilder = new StringBuilder(); - List cards = flashCardService.getRandomFlashCards(5); + List cards = flashCardService.getRandomFlashCards(AMOUNT_IN_TOTAL); ctaBuilder.append("Refresh your memory about "); for (FlashCard card : cards) { ctaBuilder.append(card.getTerm()); @@ -30,12 +31,15 @@ public String index(Model model) { ctaBuilder.append(", "); } } - ctaBuilder.append(" and "); Long totalCount = flashCardService.getCurrentCount(); - ctaBuilder.append(totalCount); - ctaBuilder.append(" more"); - model.addAttribute("cta", ctaBuilder.toString()); - model.addAttribute("flashCardCount", totalCount); + if (totalCount > AMOUNT_IN_TOTAL){ + ctaBuilder.append(" and "); + ctaBuilder.append(totalCount - AMOUNT_IN_TOTAL); + ctaBuilder.append(" more."); + model.addAttribute("cta", ctaBuilder.toString()); + model.addAttribute("flashCardCount"); + } + return "index"; } diff --git a/src/main/java/com/teamtreehouse/flashy/services/FlashCardServiceImpl.java b/src/main/java/com/teamtreehouse/flashy/services/FlashCardServiceImpl.java index 5b660e9..651ef7c 100644 --- a/src/main/java/com/teamtreehouse/flashy/services/FlashCardServiceImpl.java +++ b/src/main/java/com/teamtreehouse/flashy/services/FlashCardServiceImpl.java @@ -1,8 +1,9 @@ package com.teamtreehouse.flashy.services; +import static java.util.stream.Collectors.toList; + import com.teamtreehouse.flashy.domain.FlashCard; import com.teamtreehouse.flashy.repositories.FlashCardRepository; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -12,8 +13,6 @@ import java.util.Map; import java.util.Random; -import static java.util.stream.Collectors.toList; - @Service public class FlashCardServiceImpl implements FlashCardService { private FlashCardRepository flashCardRepository; @@ -62,10 +61,9 @@ public FlashCard getNextFlashCardBasedOnViews(Map idToViewCounts) { continue; } Long lowestScore = idToViewCounts.get(leastViewedId); - if (entry.getValue() >= lowestScore) { - break; + if (entry.getValue() < lowestScore) { + leastViewedId = entry.getKey(); } - leastViewedId = entry.getKey(); } return flashCardRepository.findOne(leastViewedId); }