Description: Write a program in which the user can: Read a videogame collection
ID: 668492 • Letter: D
Question
Description:
Write a program in which the user can:
Read a videogame collection file. The file is assumed to have the name & console and is tab delimited.
Search the contents of the file from part 1. The user enters the name of the game, then the name of the console.
Matches for the game and console entered are returned
Partial matches for the name or console are acceptable.
This is not case sensitive.
User may use the character '*' to indicate they want all games or consoles
Print out the current search results to the console.
Print out the current search results to a new file with the option to append to said file. The user must specify the name of the file and whether or not to append.
Keeps running until the user quits.
Suggested Methodology:
You can solve this in a number of way; here's a way you make take to approach this problem.
3 ClassesVideoGame
A simple class that represents one video game. It only has the name of the game, and the console of the game
VideoGameCollectionManager
This class has an ArrayList which is populated by reading from a tab delimited file. The first item in the file is always the name and the second is always the console. This class also handles printing to files where the following parameters are passed:
File name
Whether or not to append
An external list of games
Finally, this class has method that returns an ArrayList of video games based on a search criterion (name and console).
VideoGameCollectionFrontend
This is where the user inputs commands and sees the results. It also holds an array list of the results of the most recent search.
Example Dialog
Explanation / Answer
Program:
//VideoGame.java
//VideoGame class is used to hold the name of the game and console
public class VideoGame
{
// Declare the required variables
String gmName;
String gmConsole;
//constructor
public VideoGame(String gmN, String gmC)
{
gmName=gmN;
gmConsole=gmC;
}
// methods to get and set the values
public String getGmName()
{
return gmName;
}
public void setGmName(String gmName)
{
this.gmName = gmName;
}
public String getGmConsole()
{
return gmConsole;
}
public void setGameConsole(String gmConsole)
{
this.gmConsole = gmConsole;
}
}
VideoGameCollectionManager.java
// import the required packages
import java.io.*;
import java.util.*;
// Class to manage the Video Game Collections
public class VideoGameCollectionManager
{
//instance variables
ArrayList<VideoGame> videogm=new ArrayList<VideoGame>();
ArrayList<VideoGame> sL=new ArrayList<VideoGame>();
//constructor
public VideoGameCollectionManager(String filenm)throws Exception
{
readData(filenm);
}
// method to reads data from a file
public void readData(String fname)throws Exception
{
Scanner fin=new Scanner(new File(fname));
String strn[]=new String[2];
String lne="";
while(fin.hasNextLine())
{
lne=fin.nextLine();
strn=lne.split(" ");
VideoGame vGame=new VideoGame(strn[0],strn[1]);
videogm.add(vGame);
}
}
// method to search the proper string and adds it into new
ArrayList
public ArrayList<VideoGame> vsearch()
{
Scanner fin=new Scanner(System.fin);
System.out.println("Enter the name of the game or '*'
for all names: ");
String gname=fin.nextLine();
System.out.println("Enter the name of the console or
'*' for all consoles: ");
String cnsle=fin.nextLine();
for(int in=0;in<videogm.size();in++)
{
if(gname.equalsIgnoreCase("*") && cnsle.equalsIgnoreCase("*"))
{
sL.add(videogm.get(in));
}
else if(gname.equalsIgnoreCase("*") &&
videogm.get(in).getGmConsole().contains(cnsle))
{
sL.add(videogm.get(in));
}
else if(videogm.get(in).getGmName().contains(gname)&&
cnsle.equalsIgnoreCase("*"))
{
sL.add(videogm.get(in));
}
else if(videogm.get(in).getGmName().contains(gname)&&
videogm.get(in).getGmConsole().contains(cnsle))
{
sL.add(videogm.get(in));
}
}
for(int in=0;in<sL.size();in++)
{
System.out.println(sL.get(in).getGmName()+" "+
sL.get(in).getGmConsole());
}
return sL;
}
// method to prints the data
void printToFile(ArrayList<VideoGame> vGame) throws IOException
{
Scanner fin=new Scanner(System.fin);
System.out.println("Enter the file name to print out.");
String filenm=fin.next();
File fi=new File(filenm);
boolean flg=false;
if(!fi.exists())
{
fi.createNewFile();
}
System.out.println("Append to file? True or false.");
flg=fin.nextBoolean();
if(flg)
{
FileWriter fileWritter = new
FileWriter(fi.getName(),flg);
PrintWriter pwri=new PrintWriter(fileWritter);
for(int in=0;in<vGame.size();in++)
{
pwri.append(vGame.get(in).getGmName()+" "+
vGame.get(in).getGmConsole());
pwri.println();
}
pwri.close();
fileWritter.close();
}
else
{
PrintWriter pwri=new PrintWriter(fi);
for(int in=0;in<vGame.size();in++)
{
pwri.println(vGame.get(in).getGmName()+" "+
vGame.get(in).getGmConsole());
}
pwri.close();
}
}
}
VideoGameCollectionFrontEnd.java
//import the required packages
import java.io.File;
import java.util.*;
//implementation class
public class VideoGameCollectionFrontEnd
{
//class variable
static VideoGameCollectionManager vGamecm;
//main method
public static void main(String args[])throws Exception
{
//declare the required instance variables
ArrayList<VideoGame> video=new ArrayList<VideoGame>();
Scanner fin=new Scanner(System.fin);
int chic;
String filenm;
//prompt the user for the inputfile
System.out.println("Enter the file name: ");
filenm=fin.next();
//print the menu to the user
System.out.println("Welcome to the video game database!");
do
{
//menu
System.out.println("Enter 1 to load the video game
database");
System.out.println("Enter 2 to search the database");
System.out.println("Enter 3 to print current
results");
System.out.println("Enter 4 to print current results
to file");
System.out.println("Enter 0 to quit");
System.out.println("");
//enter the choice
chic=fin.nextInt();
//depending on the choice, the respective
//cases are invoked
switch(chic)
{
case 1:
vGamecm=new
VideoGameCollectionManager(filenm);
break;
case 2:
video=vGamecm.vsearch();
break;
case 3:
System.out.println("The list of games in the
current list are: ");
for(int in=0;in<video.size();in++)
System.out.println(video.get(in).get
GameName() +" "+video.get(in).
getGmConsole());
break;
case 4:
vGamecm.printToFile(video);
break;
case 0:
System.out.println("Goodbye.");
System.exit(0);
break;
default:
System.out.println("Please enter the correct
input.");
}
//continuous loop
}while(true);
}
}
Result:
Enter 1 to load the videogame database
Enter 2 to search the database
Enter 3 to print current results
Enter 4 to print current results to file
Enter 0 to quit
1
Enter the file name: Collection.txt
Enter 1 to load the videogame database
Enter 2 to search the database
Enter 3 to print current results
Enter 4 to print current results to file
Enter 0 to quit
2
Enter the name of the game or '*' for all names: super
Enter the name of the console or '*' for all consoles:
nintendo
Enter 1 to load the videogame database
Enter 2 to search the database
Enter 3 to print current results
Enter 4 to print current results to file
Enter 0 to quit
3
Super Alfred Chicken Super Nintendo [NA]
"Super Aquatic Games Starring the Aquabats, The" Super
Nintendo [NA]
Super Castlevania IV Super Nintendo [NA]
Super Dodge Ball Nintendo Entertainment System [US]
Super Empire Strikes Back Super Nintendo [NA]
Super Ghouls 'N Ghosts Super Nintendo [NA]
Super Glove Ball Nintendo Entertainment System [US]
Super Mario All-Stars Super Nintendo [NA]
Super Mario Bros. (5 Screw Cartridge) Nintendo Entertainment System
[US]
Super Mario Bros. / Duck Hunt (No Nintendo Seal of Quality) Nintendo
Entertainment System [US]
Super Mario Bros. 2 (No Nintendo Seal of Quality) Nintendo
Entertainment System [US]
Super Mario Bros. 3 (Bros. above Mario's Head) Nintendo
Entertainment
System [US]
Super Mario Kart Super Nintendo [NA]
Super Mario World Super Nintendo [NA]
Super Mario World 2: Yoshi's Island Super Nintendo [NA]
Super Metroid Super Nintendo [NA]
Super Noah's Ark 3D Super Nintendo [NA]
Super Pitfall (3 Screw Cartridge) Nintendo Entertainment
System [US]
Super Punch-Out!! Super Nintendo [NA]
Super Scope 6 Super Nintendo [NA]
Super Spike V'Ball/Nintendo World Cup Nintendo Entertainment
System [US]
Super Star Wars (JVC) Super Nintendo [NA]
Super Star Wars: Return of the Jedi (JVC) Super
Nintendo [EU]
Super Street Fighter II Super Nintendo [NA]
Super Team Games Nintendo Entertainment System [US]
Super Tennis Super Nintendo [NA]
Enter 1 to load the videogame database
Enter 2 to search the database
Enter 3 to print current results
Enter 4 to print current results to file
Enter 0 to quit
4
Enter the file name to print out: superGames.txt
Append to file (true/false): false
Enter 1 to load the videogame database
Enter 2 to search the database
Enter 3 to print current results
Enter 4 to print current results to file
Enter 0 to quit
0
Goodbye