Assignment Brief:
Working with your project group, you are to create a software system that inspects a folder path that contains text files. The files store the results of a group of “Lotto QuickPick” game plays. The file names must be read into a suitable single dimension Array. Another developer has started the project already, so use the supplied SearchLotto.java as your starting point.
Download full brief here 

Download source code and associated attachements here

// Start of code---------------------
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/* Usage notes:
 * Redistribution, alteration and use of this code with or without
 * modification, are permitted provided that the following conditions are met:
 * In no event shall the copyright owner be liable for any direct, indirect,
 * incidental, special, exemplary, or consequential damages arising in any
 * way out of the use of this software, even if advised of the possibility
 * of such damage.
 */


public class lottoGP2 {

    public static void main(String[] args) throws FileNotFoundException {

        final String folderpath = "C:/Documents and Settings/Lesley/Desktop/";
        final String folderName = "LottoFiles";
        final String fileExtension = ".txt";

        boolean advancedMenuError = false;

        int orderBy = 0;
        int count = 0;
        int fileSelection = 0;
        // int selectedFile = 0;

        String searchWord = "";
        String anotherFile = "";
        String searchOption = "";
        String advancedMenuOption = "";
        String searchType = "";

        File folder = new File(folderpath + folderName);
        File[] listOfFiles = folder.listFiles();

        while (!folder.canRead()) {
            System.out
                    .println("Folder not found!!\nPlease check your file directory path and run again!");
            System.exit(0);
        }

        String[] filesArray = new String[listOfFiles.length];
        String[] searchFilesResult = new String[filesArray.length];

        Scanner console = new Scanner(System.in);

        folderToArray(listOfFiles, filesArray);

        // I have commented this out because it wont work on my system
        // Arrays.sort(filesArray);
        do {
            do {
                do {
                    System.out.print("\n\t***WELCOME TO FILE SEARCH***\n\n");

                    System.out
                            .print("What order would you like the files displayed?"
                                    + "\nPlease input either 1 or 2 (numeric digits only) \nto list all files in the following order:"
                                    + "\n\n1. Ascending aphabetical order\n2. Descending alphabetical order ");

                    while (!console.hasNextInt()) {
                        System.out
                                .print("\nInput error!\nPlease input either 1 or 2 (numeric digits only) \nto list all files in the following order:");
                        console.next();
                    }
                    orderBy = console.nextInt();

                    System.out.print("\n");

                    listFiles(listOfFiles, filesArray, orderBy);

                    System.out.print("\nThere are "
                            + countFiles(listOfFiles, filesArray, count)
                            + " files in the folder.\n");

                    do {
                        stars60();
                        System.out
                                .print("\nWould you like to continue to search options?");
                        advancedMenuError = false;
                        advancedMenuOption = (displayMenu(console,
                                advancedMenuOption));

                        if (advancedMenuOption.equals("M")|| advancedMenuOption.equals("m")) {
                            System.out
                                    .print("\nYou have chosen to start again!");
                            stars60();
                        } else if (advancedMenuOption.equals("Y")|| advancedMenuOption.equals("y")) {
                            System.out.print("\nYou have chosen to continue!");
                        } else {
                            advancedMenuError = true;
                        }
                    } while (advancedMenuError);

                } while (advancedMenuOption.equals("M")|| advancedMenuOption.equals("m"));

                stars60();

                searchFiles(console, filesArray, searchOption,
                        searchFilesResult, searchWord, fileExtension,
                        searchType);
                do {
                    advancedMenuError = false;
                    advancedMenuOption = (displayMenu(console,
                            advancedMenuOption));

                    if (advancedMenuOption.equals("M")|| advancedMenuOption.equals("m")) {
                        System.out.print("\nYou have chosen to start again!");
                        stars60();
                    } else if (advancedMenuOption.equals("Y")|| advancedMenuOption.equals("y")) {
                        System.out.print("\nYou have chosen to continue!");

                    } else {
                        advancedMenuError = true;
                    }
                } while (advancedMenuError);
            } while (advancedMenuOption.equals("M")
                    || advancedMenuOption.equals("m"));

            do {

                openSearchResults(console, searchFilesResult, folderName,
                        folderpath, count, fileSelection);

                do {
                    advancedMenuError = false;
                    advancedMenuOption = (displayMenu(console,
                            advancedMenuOption));

                    if (advancedMenuOption.equals("M")|| advancedMenuOption.equals("m")) {
                        System.out.print("\nYou have chosen to start again!");
                        stars60();
                    } else if (advancedMenuOption.equals("Y")|| advancedMenuOption.equals("y")) {
                        System.out
                                .print("\nYou have chosen to view another file!");
                        anotherFile = advancedMenuOption;
                    } else {
                        advancedMenuError = true;
                    }
                } while (advancedMenuError);
            } while (anotherFile.equals("Y") || anotherFile.equals("y"));

        } while (advancedMenuOption.equals("M")|| advancedMenuOption.equals("m"));

    }

    public static int countFiles(File[] listOfFiles, String[] filesArray,
            int count) {

        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                filesArray[i] = listOfFiles[i].getName();
                count++;
            }

        }
        return count;
    }

    public static String[] folderToArray(File[] listOfFiles, String[] filesArray) {

        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                filesArray[i] = listOfFiles[i].getName();
            }

        }
        return filesArray;
    }

    public static String displayMenu(Scanner console, String advancedMenuOption) {

        System.out.print("\nPlease choose one of the following options:"
                + "\nY to continue\nM for Top menu\nQ for Quit. ");

        String advancedMenuCheck = console.next();

        if (advancedMenuCheck.equals("Y") || advancedMenuCheck.equals("M")) {
            advancedMenuOption = advancedMenuCheck;
        } else if (advancedMenuCheck.equals("Q")|| advancedMenuCheck.equals("q")) {
            System.out
                    .print("\nYou have chosen to quit. Hope to see you again!");
            System.exit(0);
        } else {
            System.out.print("\nInput error!!");
            advancedMenuOption = advancedMenuCheck;
        }
        return advancedMenuOption;

    }

    public static String[] listFiles(File[] listOfFiles, String[] filesArray,
            int orderBy) {

        switch (orderBy) {
        case 1:
            System.out
                    .println("You have chosen to display in Ascending aphabetical order.\n");
            for (int i = 0; i < filesArray.length; i++) {

                filesArray[i] = listOfFiles[i].getName();
                System.out.println(filesArray[i]);
            }
            break;
        case 2:
            System.out
                    .println("You have chosen to display in Descending alphabetical order.\n");
            for (int i = filesArray.length - 1; i >= 0; i--) {

                filesArray[i] = listOfFiles[i].getName();
                System.out.println(filesArray[i]);

            }
            break;
        }
        return filesArray;
    }

    public static String[] searchFiles(Scanner console, String[] filesArray,
            String searchOption, String[] searchFilesResult, String searchWord,
            final String fileExtension, String searchType) {

        boolean invalidSearch = false;

        System.out
                .print("\n\t\t   Search options:\nPlease input either 1, 2 or 3 for preferred search option "
                        + "\nbelow (numeric digits only please):\n\n"
                        + "1. Initials only (Example: MR)\n\n"
                        + "2. Last 4 digits of contact number only (Example: 7762)\n\n"
                        + "3. Initials and last 4 digits of contact number combined, \n   but separated by a single space (Example: MR 7762)");
        do {
            searchOption = console.next();

            if (!searchOption.matches("[1-3]+")) {
                System.out
                        .print("\nInput error!! Please input a valid digit between 1 - 3 to continue");
            }
        } while (!searchOption.matches("[1-3]+"));

        int searchSelection = Integer.parseInt(searchOption);
        stars60();
        int i = 0;
        int j = 0;

        switch (searchSelection) {
        case 1:

            do {
                searchType = "initial only";
                invalidSearch = false;
                System.out
                        .print("\n\tYou have choosen to search by initials.\nPlease input initials to start search: (Example: MR)");
                searchWord = console.next();

                String searchInitial = "_" + searchWord.toUpperCase() + "_";

                int searchCount = 0;
                for (i = 0; i < filesArray.length; i++) {
                    if (!(filesArray[i]).contains(searchInitial)) {
                        searchCount++;
                    } else {
                        searchFilesResult[j] = filesArray[i];
                        j++;
                    }
                    if (searchCount == filesArray.length) {
                        System.out
                                .print("\nSearch Error!!\nYour search input of \""
                                        + searchWord + "\" does not exist.");
                        invalidSearch = true;
                    }
                }
            } while (invalidSearch);
            break;

        case 2:

            String contactDigitError = "\nSearch Error!!\nYour search input of \""
                    + searchWord
                    + "\" does not exist.\nYou have choosen to search by last 4 digits of contact number.\nPlease input the last 4 digits of contact number to start search: (Example: 7762)";

            searchType = "4 digit contact number only";

            System.out
                    .print("\nYou have choosen to search by last 4 digits of contact number.\nPlease input the last 4 digits of contact number to start search: (Example: 7762)");

            do {

                invalidSearch = false;

                searchWord = console.next();

                while (searchWord.length() != 4 && !searchWord.matches("[0-9]")) {
                    System.out.print(contactDigitError);
                    searchWord = console.next();
                }

                String searchContactDigits = "_" + searchWord;

                int searchCount = 0;
                for (i = 0; i < filesArray.length; i++) {
                    if (!(filesArray[i]).contains(searchContactDigits)) {
                        searchCount++;
                    } else {
                        searchFilesResult[j] = filesArray[i];
                        j++;
                    }
                    if (searchCount == filesArray.length) {
                        System.out.print(contactDigitError);
                        invalidSearch = true;
                    }
                }
            } while (invalidSearch);
            break;

        case 3:

            do {
                searchType = "initials and 4 digit contact number";
                invalidSearch = false;

                System.out
                        .print("\nYou have choosen to search by initials and last 4 digits of contact number.\nPlease input the Initials and last 4 digits of contact number combined, \nbut separated by a single space to start search: (Example: MR 7762)");

                // This next line puts an error from the last input*********
                // searchWord = console.nextLine();

                // This while loop will only allow for the underscore and not
                // the space as you requested.
                // We were haveing trouble with the next line which did not
                // allow for us to request a space as
                // as a console.hasNext
                while (!console.hasNext("[a-zA-z]+([0-9]+)*")) {
                    System.out
                            .print("\nInput error!\nPlease input either 1 or 2 (numeric digits only) \nto list all files in the following order:");
                    console.next();
                }
                // The code will skip a line but it does carry on
                searchWord = console.nextLine();

                /*
                 * Tried to get the swap to work but it was throwing out of
                 * bounds searchWord = searchWord.substring(0,2)
                 * +"_"+searchWord.substring(3);
                 *
                 * System.out.print(searchWord);
                 */

                String searchInitialContactDigits = "lotto_" + searchWord
                        + fileExtension;

                int searchCount = 0;

                for (i = 0; i < filesArray.length; i++) {
                    if (!(filesArray[i]).contains(searchInitialContactDigits)) {

                        searchCount++;
                    } else {
                        searchFilesResult[j] = filesArray[i];
                        j++;
                    }
                    if (searchCount == filesArray.length) {
                        System.out
                                .print("\nSearch Error!!\nYour search input of \""
                                        + searchWord + "\" does not exist.");
                        invalidSearch = true;
                    }
                }
            } while (invalidSearch);

            break;
        }
        System.out
                .print("\nYour "
                        + searchType
                        + " search for \""
                        + searchWord
                        + "\" was successful!\nWould you like to continue to view search results?");
        return searchFilesResult;
    }

    public static void stars60() {
        System.out.print("\n");
        for (int number = 1; number < 60; number++) {
            System.out.print("*");
        }
    }

    public static String openSearchResults(Scanner console,
            String[] searchFilesResult, final String folderName,
            final String folderpath, int count, int fileSelection)
            throws FileNotFoundException {
        stars60();
        System.out.print("\n\t\tSearch successful...");
        String line = "";
        do {
            System.out.print("\nPlease select a file to display on screen by "
                    + "inputting the \ncorresponding numeric digit below:");
            System.out.println();

            count = 0;

            for (int number = 1; number < searchFilesResult.length; number++) {
                if (searchFilesResult[number - 1] != null) {
                    System.out.print("\n[" + number + "] ");
                    System.out.print(searchFilesResult[number - 1]);
                    count++;
                }
            }
            fileSelection = console.nextInt();
            if (fileSelection > count) {
                System.out
                        .print("\nInput error!! Please input a valid digit between 1 - "
                                + count + " to continue:t");
            }
        } while (fileSelection > count);

        String fileName = folderpath + folderName + "/"
                + searchFilesResult[fileSelection - 1];
        System.out.print("\n\t\tStart of output to screen");
        stars60();

        System.out.print("\nYou have chosen to display "
                + searchFilesResult[fileSelection - 1] + " to the screen\n");

        Scanner input = new Scanner(new File(fileName));

        while (input.hasNextLine()) {

            line = input.nextLine();
            Scanner lineScan = new Scanner(line);
            System.out.print("\n" + line);
        }
        System.out.print("\n");
        stars60();
        System.out.println("\n\t\tEnd of output to screen");

        System.out.print("\nWould you like to view another file?");
        return line;
    }
}//End of code---------------------

0 comments:

Post a Comment

Please feel free to leave comments or ask questions related to the tutorials.