Instructions
Objective
Write a program to create a set of classes for storing and managing a game catalog in Java programming language.
Requirements and Specifications
Description
The purpose of this assignment is to practice OOP with exceptions, generics, and collections. Your task is to create a set of classes for storing and managing a game catalog. This will require an understanding of exceptions, generics, and the Java collections framework.
Overview
- Create the Java classes, interfaces, and enumerations described below. Each should be in its own .java file.
- Implement the methods and fields described.
- Test your code for correctness.
- Prepare the assignment for submission and submit it through Blackboard.
Rules
- This project is an individual effort, the Honor Code applies.
- You may import the following classes only: List, LinkedList, ArrayList, HashSet, EnumSet, Arrays, Collections.
- Comment your code in javadoc style, especially any parts that is not obvious what you are doing.
- Class fields should not be visible to other classes (unless otherwise noted). Create getter methods if necessary.
- You may add your own methods, but they must be declared private. No additional fields are allowed.
Source Code
import java.util.*;
/**
* This class represents the catalog of released games. It contains a list of games and provides methods
* for games with the UPC it has been released with, returning a subset of items in the catalog given some
* search terms, and sorting the catalog.
*/
public class GameCatalog {
/**
* A list of released games. A released game is an instance of the ReleasedGame class with
* the first type specified to be a string, and the second type specified to be Game (ReleasedGame<String, Game>).
*/
private final List<ReleasedGame<String, Game>> catalog;
/**
* A default constructor which initializes the catalog.
*/
public GameCatalog() {
catalog = new ArrayList<>();
}
/**
* Getter for catalog field
* @return inner game collection
*/
public List<ReleasedGame<String, Game>> getCatalog() {
return catalog;
}
/**
* A method for adding games to the catalog. The first argument is a string, the second the game.
* This method should create a new ReleasedGame<String, Game> instance with the given data.
* @param puc Product Unique Code of new game
* @param game Game instance to add
*/
public void add(String puc, Game game) {
if (catalog.stream().anyMatch(rg -> rg.getKey().equals(puc))) {
throw new GameCatalogException(puc, game);
}
if(game instanceof FirstPartyVideogame) {
FirstPartyVideogame firstPartyVideogame = (FirstPartyVideogame) game;
Collection<String> developers = firstPartyVideogame.getDevelopers();
if (developers.size() != 1 || !developers.iterator().next().equals(firstPartyVideogame.getPublisher())) {
throw new VideogameDeveloperException(Arrays.deepToString(developers.toArray()), firstPartyVideogame);
}
}
if(game instanceof SecondPartyVideogame) {
SecondPartyVideogame secondPartyVideogame = (SecondPartyVideogame) game;
int year = secondPartyVideogame.getYear();
Collection<String> developers = secondPartyVideogame.getDevelopers();
for (String developer : developers) {
if (catalog.stream().filter(rg -> (rg.getValue()) instanceof ThirdPartyVideoGame)
.map(rg -> (ThirdPartyVideoGame)rg.getValue())
.anyMatch(tpvg -> tpvg.getDevelopers().contains(developer) && year - tpvg.getYear() >= 2)) {
throw new VideogameDeveloperException(developer, secondPartyVideogame);
}
}
}
if(game instanceof ThirdPartyVideoGame) {
ThirdPartyVideoGame thirdPartyVideoGame = (ThirdPartyVideoGame) game;
Collection<String> developers = thirdPartyVideoGame.getDevelopers();
for (String developer : developers) {
if (catalog.stream().filter(rg -> (rg.getValue()) instanceof FirstPartyVideogame)
.map(rg -> (FirstPartyVideogame)rg.getValue())
.anyMatch(fpvg -> fpvg.getDevelopers().contains(developer))) {
throw new VideogameDeveloperException(developer, thirdPartyVideoGame);
}
}
}
catalog.add(new ReleasedGame<>(puc, game));
}
/**
* This method creates and returns a new list of released games.
* This new list should contain all released games from the catalog for which the given searchable's matches method returns true.
* @param searchable searhable instance to be used for search
* @return search result list
*/
public List<ReleasedGame<String, Game>> search(Searchable searchable) {
List<ReleasedGame<String, Game>> result = new ArrayList<>();
for(ReleasedGame<String, Game> game : catalog) {
if (searchable.matches(game)) {
result.add(game);
}
}
return result;
}
/**
* This method sorts (ascending) the field catalog according to the required rules.
*/
public void sort() {
Collections.sort(catalog);
}
}