Must be compatible with eclipse Objective: Write a program in which the user can
ID: 639001 • Letter: M
Question
Must be compatible with eclipse
Objective:
Write a program in which the user can:
Read a video game collection file. The file is assumed to have the name and the console and its tab delimited. It is advisable to put each of the games in an ArrayList since number of games is not known.
Search the contents of the file that has already been loaded in from part 1. The user enters the name of the game and 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
Explanation / Answer
Program code:
//VideoGame class that holds the
//name of the game and console
public class VideoGame
{
//instance variables
String gameName;
String gameConsole;
//constructor
public VideoGame(String gameN, String gameC)
{
gameName=gameN;
gameConsole=gameC;
}
//getter and setter methods
public String getGameName()
{
return gameName;
}
public void setGameName(String gameName)
{
this.gameName = gameName;
}
public String getGameConsole()
{
return gameConsole;
}
public void setGameConsole(String gameConsole)
{
this.gameConsole = gameConsole;
}
}
//import the required packages
import java.io.*;
import java.util.*;
//VideoGameCollectionManager class
public class VideoGameCollectionManager
{
//instance variables
ArrayList<VideoGame> videogame=new ArrayList<VideoGame>();
ArrayList<VideoGame> sList=new ArrayList<VideoGame>();
//constructor
public VideoGameCollectionManager(String filename)throws Exception
{
readData(filename);
}
//readData method that reads data from
//a file
public void readData(String fname)throws Exception
{
Scanner in=new Scanner(new File(fname));
String str[]=new String[2];
String line="";
while(in.hasNextLine())
{
line=in.nextLine();
str=line.split(" ");
System.out.println("str1: "+str[0]+" Str2: "+str[1]);
VideoGame vg=new VideoGame(str[0],str[1]);
videogame.add(vg);
}
}
//search method that searches for the proper string
//and adds them to new ArrayList
//and returns the list
public ArrayList<VideoGame> search()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the name of the game or '*' for all names: ");
String name=in.nextLine();
System.out.println("Enter the name of the console or '*' for all consoles: ");
String console=in.nextLine();
for(int i=0;i<videogame.size();i++)
{
if(videogame.get(i).getGameName().contains(name)||
videogame.get(i).getGameConsole().contains(console))
{
sList.add(videogame.get(i));
}
}
return sList;
}
//printToFile method that prints the
//data either by appending or
//by rewriting to the file
void printToFile(ArrayList<VideoGame> vg) throws IOException
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the file name to print out.");
String filename=in.next();
File f=new File(filename);
boolean flag=false;
if(!f.exists())
{
f.createNewFile();
}
System.out.println("Append to file? True or false.");
flag=in.nextBoolean();
if(!flag)
{
FileWriter fileWritter = new FileWriter(f.getName(),flag);
PrintWriter pw=new PrintWriter(fileWritter);
for(int i=0;i<vg.size();i++)
{
pw.write(vg.get(i).getGameName()+" "+vg.get(i).getGameConsole());
pw.write(" ");
}
pw.close();
fileWritter.close();
}
else
{
PrintWriter pw=new PrintWriter(f);
for(int i=0;i<vg.size();i++)
{
pw.write(vg.get(i).getGameName()+" "+vg.get(i).getGameConsole());
pw.write(" ");
}
pw.close();
}
}
}
//import the required packages
import java.io.File;
import java.util.*;
//implementation class
public class VideoGameCollectionFrontEnd
{
//class variable
static VideoGameCollectionManager vgcm;
//main method
public static void main(String args[])throws Exception
{
//declare the required instance variables
ArrayList<VideoGame> video=new ArrayList<VideoGame>();
Scanner in=new Scanner(System.in);
int choice;
String filename;
//prompt the user for the inputfile
System.out.println("Enter the file name: ");
filename=in.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
choice=in.nextInt();
//depending on the choice, the respective
//cases are invoked
switch(choice)
{
case 1:
vgcm=new VideoGameCollectionManager(filename);
break;
case 2:
video=vgcm.search();
break;
case 3:
System.out.println("The list of games in the current list are: ");
for(int i=0;i<video.size();i++)
System.out.println(video.get(i).getGameName()+" "+video.get(i).getGameConsole());
break;
case 4:
vgcm.printToFile(video);
break;
case 0:
System.exit(0);
break;
default:
System.out.println("Please enter the correct input.");
}
//continuous loop
}while(true);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.