Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description: Write a program in which the user can: Read a videogame collection

ID: 668660 • 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

My First Class is just a outline of the VideoGame

* A simple class that represents one video game.
*
*/
public class VideoGame {
//instance variables
private String gameName;
private String console;

//constructor
public VideoGame()
{
   gameName = "none";
   console = "none";
}
//parameterized constructor
public VideoGame(String aGameName, String aConsole)
{
   this.setName(aGameName);
   this.setConsole(aConsole);
}

//getters and setters
public String getName() {
   return gameName;
}

public void setName(String aName) {
   this.gameName = aName;
}

public String getConsole() {
   return console;
}

public void setConsole(String aConsole) {
   this.console = aConsole;
}


}

My second class is titled the VideoGameManager

import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;

public class VideoGameManager {
   Scanner keyboard = new Scanner(System.in);
   //array list
   private ArrayList<VideoGame> games;
   //tab
   private static final String delim = " ";
  
   //constructor
   public VideoGameManager(){
       games = new ArrayList<VideoGame>();
   }
  
  
   public void ReadGameFile(String fileName)
   {
       //This reconstructs a new instance of the VideGame array list.
       //This is done to clear the array list
       games = new ArrayList<VideoGame>();
       try
       {
           //Create a new file Scanner
           Scanner fileScanner = new Scanner(new File(fileName));
           //Reads each line in the file one-by-one
           while(fileScanner.hasNextLine())
           {
               //Stores the next line of code
               String nextLine = fileScanner.nextLine();
               //That line is then split using the delimiter ( )
               String[] splitStrings = nextLine.split(delim);
               //If the newly created array is not 2 items in length then
               //that line is not correctly formatted and should be ignored.
               if(splitStrings.length != 2)
                   continue;
               String gameName = splitStrings[0];//The first element is the game name
               String console = splitStrings[1];//Next is the console
               VideoGame newVideoGame = new VideoGame(gameName,console);
               games.add(newVideoGame);//Added to the array list
           }
           fileScanner.close();
       }
       catch(Exception e)
       {
           System.out.println(" File does not exist ");
       }
      
   }
   public void WriteToGameFile(String fileName, boolean append)
   {
       if(games == null)//if the file name is null then return
           return;
       try
       {
           //Creates the new instance of a print writer
           PrintWriter fileWriter = new PrintWriter(new FileOutputStream(fileName,append));
           for(VideoGame aVideoGame : games)
           {
               //Prints to the file
               fileWriter.println(aVideoGame.getName()+delim+
                                   aVideoGame.getConsole());
           }
           fileWriter.close();
       }
       catch(Exception e)
       {
           System.out.println("Error");
       }
   }
   //returns an array list of video games based on a search criterion (name and console)
   public void PrintSearchCriteria(String nameInput, String consoleInput)
   {
      
       for(VideoGame aVideoGame : games)
       {
           if(aVideoGame.getName().contains(nameInput) == true && aVideoGame.getConsole().contains(consoleInput) == true)
           {
               VideoGame newVideoGame = new VideoGame(aVideoGame.getName(),aVideoGame.getConsole());
               System.out.println(newVideoGame.getName()+delim+newVideoGame.getConsole());
                 
           }
           else if(aVideoGame.getName().contains(nameInput) == true && consoleInput.equalsIgnoreCase("*"))
           {
               VideoGame newVideoGame = new VideoGame(aVideoGame.getName(),aVideoGame.getConsole());
               System.out.println(newVideoGame.getName()+delim+newVideoGame.getConsole());
                 
           }
           else if(nameInput.equalsIgnoreCase("*") && aVideoGame.getConsole().contains(consoleInput) == true)
           {
               VideoGame newVideoGame = new VideoGame(aVideoGame.getName(),aVideoGame.getConsole());
               System.out.println(newVideoGame.getName()+delim+newVideoGame.getConsole());
                 
           }
          
       }
   }
  
   public void PrintCurrentResults(String fileName)
   {
       for(VideoGame aVideoGame : games)
       {
           System.out.println(aVideoGame.getName()+delim+
                                   aVideoGame.getConsole());
       }
   }
  
  
}

My last class is the front end of my program

import java.util.Scanner;

public class VideoGameFrontEnd {

   public static void main(String[] args) {
       //create and connect scanner object to keyboard
       Scanner keyboard = new Scanner(System.in);
       VideoGameManager vG = new VideoGameManager();
       System.out.println("Welcome to the video game database!");
       boolean quit = false;
       try{
       while(quit == false)
       {
          
      
          
       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");
       int input = keyboard.nextInt();
      
       switch(input){
       case 0: input = 0;
           System.out.println("Good Bye");
           quit = true;
           break;
          
       case 1: input = 1;
           System.out.println("Enter the file Name");
           String fileName = keyboard.next();
           vG.ReadGameFile(fileName);
           break;
       case 2: input = 2;
           System.out.println("Enter the name of the game or '*' for all names");
           String nameInput = keyboard.next();
           System.out.println("Enter the name of the console or '*' for all consoles");
           String consoleInput = keyboard.next();
           vG.PrintSearchCriteria(nameInput,consoleInput);
           break;
       case 3: input = 3;
           //print current results
           break;
       case 4: input = 4;
          
           System.out.println("Enter the file name to print out.");
           String fileNamePrintToFile = keyboard.next();
           System.out.println("Append to file ? True or False");
           String appendToFile = keyboard.next();
           if(appendToFile.equalsIgnoreCase("true"))
           {
               vG.WriteToGameFile(fileNamePrintToFile, true);
           }
           else if(appendToFile.equalsIgnoreCase("false"))
           {
               vG.WriteToGameFile(fileNamePrintToFile, false);
           }
           else
           {
               System.out.println("Incorrect Response, auto shutdown");
               System.exit(0);
           }
       default:
           break;
      
           }
      
       }
   }
   catch(Exception e)
   {
       System.out.println("error");
       System.exit(0);
   }
}

}