I have to 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 “*” 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 they append.
- Keeps running this until the user quits
the video game collection file is attached below
My First Class is just a outline of the VideoGame
/* * @author Lawton C Mizell * @verison 1.0 * Jan 28, 2015 * * 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 ( here is where the problems come in ) I`ll explain soon
import java.util.ArrayList; import java.util.Scanner; import java.io.*; /* * @author Lawton C Mizell * @version 1.0 * Jan 28, 2015 * * This class has an array list 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 a file name, whether or not to * append, and an external array list of * games is passed in via parameters. * Finally this class has a method that * returns an array list of video games * based on a search criterion (name and console). * Partial matches are accepted, the case can be * ignored, and the “*” is used to return all * games or consoles. * */ public class VideoGameManager { Scanner keyboard = new Scanner(System.in); //array list private ArrayList<VideoGame> games; //tab private static final String delim = "\t"; //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 (\t) 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; /* * @author Lawton C Mizell * @version 1.0 * Jan 29, 2015 * * * */ 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); } } }
So the issue is completing the following task:
- 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 they append.
I am able to load up the database and complete a search criteria based on certain words for the game name and based on certain words for the console. The issue comes into printing the current search results to the console and printing the current results to a new file, where the user has to specify the name. My current method just creates a new file with the same contents as that of the original JJCollection.txt