Problem:
Game
|
-
title : String
-
type : String
-
released : Boolean
-
price : int
-
copiesSold : int
|
// Constructor(s)
// setters getters
+ toString() : String
|
GameViewer
|
-
games : LinkedList<game>
|
+
GameViewer()
+
addGame(game : Game) : void
+
deleteGanne(title : String) : void
+
getReleasedGames() : LinkedList<Game>
+
getMostSellingGame() : Game
+
getLeastSellingGame() : Game
+
getHighestPriceGame() : Game
+
getLowestPriceGame() : Game
+
getAllGames() : LinkedList<Game>
+
printGames() : void
|
You
have to implement the previous two classes. The class Game has four private
variables, a constructor, setters and getters, and a toString method. The class
GameViewer has a LinkedList of games and a couple of methods that apply
operations on the LinkedList.
-
addGame(game : game): takes a game as parameter and adds it
to games
-
deleteGame(title : String): deletes a game from games with the
specified title
- getReleasedGames():
returns a LinkedList<Game> that contains all the games that have been released (released = true)
- getMostSellingGame
(): returns the game with the most copies sold
- getleastSellingGame():
returns the game with the least copies sold
- getHighestPriceGanne():
returns the game with the highest price
-
getLowestPriceGame(): returns the game with the lowest price
- getAllGames(): returns the games
linked list
-
printGames(): prints the games linked list
In
the tester class, you are required to create an object of type GameViewer and
add to its list a number of games.
You
should test all of the class' methods and print the output of each in a well
formed manner.
You
should only use ListIterator to loop over the linked list. Any other way is not
acceptable.
Solution:
/**-----------------------Game.java----------------------*/
public class Game {
private String title;
private String type;
private boolean released;
private int price;
private int copiesSold;
public Game(String title, String type, boolean released,
int price, int copiesSold) {
this.title = title;
this.type = type;
this.released = released;
this.price = price;
this.copiesSold = copiesSold;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isReleased() {
return released;
}
public void setReleased(boolean released) {
this.released = released;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getCopiesSold() {
return copiesSold;
}
public void setCopiesSold(int copiesSold) {
this.copiesSold = copiesSold;
}
@Override
public String toString() {
return "[title=" + title + ", type=" + type + ", released="
+ released + ", price=" + price +
", copiesSold=" + copiesSold + "]";
}
}
/**-----------------------GameViewer.java----------------------*/
import java.util.LinkedList;
import java.util.ListIterator;
public class GameViewer {
private LinkedList<Game> games;
public GameViewer() {
games = new LinkedList<Game>();
}
public void addGame(Game game) {
games.add(game);
}
public void deleteGame(String title) {
ListIterator<Game> itr = games.listIterator(0);
while (itr.hasNext()) {
Game game = itr.next();
if (game.getTitle().equalsIgnoreCase(title)) {
itr.remove();
return;
}
}
}
public LinkedList<Game> getReleasedGames() {
ListIterator<Game> itr = games.listIterator(0);
LinkedList<Game> released = new LinkedList<Game>();
while (itr.hasNext()) {
Game game = itr.next();
if (game.isReleased())
released.add(game);
}
return released;
}
public Game getMostSellingGame() {
ListIterator<Game> itr = games.listIterator(0);
Game mostSelling = games.getFirst();
while (itr.hasNext()) {
Game game = itr.next();
if (game.getCopiesSold() > mostSelling.getCopiesSold())
mostSelling = game;
}
return mostSelling;
}
public Game getLeastSellingGame() {
ListIterator<Game> itr = games.listIterator(0);
Game leastSelling = games.getFirst();
while (itr.hasNext()) {
Game game = itr.next();
if (game.getCopiesSold() < leastSelling.getCopiesSold())
leastSelling = game;
}
return leastSelling;
}
public Game getHighestPriceGame() {
ListIterator<Game> itr = games.listIterator(0);
Game highestPrice = games.getFirst();
while (itr.hasNext()) {
Game game = itr.next();
if (game.getPrice() > highestPrice.getPrice())
highestPrice = game;
}
return highestPrice;
}
public Game getLowestPriceGame() {
ListIterator<Game> itr = games.listIterator(0);
Game lowestPrice = games.getFirst();
while (itr.hasNext()) {
Game game = itr.next();
if (game.getPrice() < lowestPrice.getPrice())
lowestPrice = game;
}
return lowestPrice;
}
public LinkedList<Game> getAllGames() {
return games;
}
public void printGames() {
System.out.println(games);
}
}