The Lotto QuickPick software application that you wrote previously (https://www.dropbox.com/sh/8eb6sg5oad0lg89/3DRtDl6uuz) has been well received and the client is looking to enhance the functionality. They want to add options such as lotto game selection and a checkout screen showing the cost of the game based on how many lines were purchased.
Download full brief here
Download source code here
// Start of code--------------------- import java.io.File; import java.io.FileNotFoundException; import java.io.PrintStream; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Random; 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 DatabaseLottoQuickPick { public static void main(String[] args) throws FileNotFoundException { Scanner kb = new Scanner(System.in); // It is assumed that the dataBase text document will be read from the // lotto quick pick folder name above (ie: DatabaseLottoQuickPick) final String dataBaseFileName = "database_lotto.txt"; final String nameError = "Invalid name input!!\nPlease input A to Z characters only and at least one space to continue - ie. Joe Soap: "; final String mobileError = "Invalid mobile number!!\nPlease input 0 to 9 digits only to continue (10 digits please - ie. 0871234567): "; final int lottoNumbersPerLine = 6; boolean menuError = false; boolean lineError = false; String name, firstName, mobile, initials, mobileDigits, writeToFileName; String lottoLines = ""; String options = ""; String finalOption = ""; String menuSelection = ""; String lAmount = ""; int lineAmount = 0; int randomNumber = 0; int gameOption = 0; File dataBase = new File(dataBaseFileName); dataBase = new File(dataBaseFileName); Scanner input = exists(dataBase, dataBaseFileName); final String gameSelectionError = "Invalid input!!\nPlease input digits 1 to " + fileRowCount(dataBaseFileName) + " for game options or Q to quit lotto quickpick: "; NumberFormat numberFormat = new DecimalFormat("\u20AC##.00"); String[][] gameArray = new String[fileRowCount(dataBaseFileName)][3]; readFileToArray(input, dataBase, kb, gameArray); System.out.print("WELCOME TO LOTTO QUICKPICK\n"); // Code below is for the name input------------------- System.out .print("\nPlease enter your full name to continue(A - Z characters only and must contain at least one space - ie. Joe Soap: "); name = kb.nextLine(); while (name.length() <= 3 || !name.matches("[a-zA-z]+([ '-][a-zA-Z]+)*") || !name.contains(" ")) { System.out.print(nameError); name = kb.nextLine(); } // ------------------------------------------------- // Code below is for the mobile #------------------- System.out .print("Please enter your 10 digit Mobile Number (0-9 digits only - ie. 0871234567): "); mobile = kb.next(); while (mobile.length() != 10 || !mobile.matches("[0-9]+")) { System.out.print(mobileError); mobile = kb.next(); } // ------------------------------------------------- // All substring and indexOf code------------------- // last four digits of mobile # mobileDigits = (mobile.substring(6, 10)); // capital letters for full name String nameCaps = Character.toUpperCase(name.charAt(0)) + name.substring(1, name.indexOf(" ")) + " " + name.substring(name.indexOf(" ") + 1).substring(0, 1) .toUpperCase() + name.substring(name.indexOf(" ") + 2); // split first name for the end of output file message firstName = nameCaps.substring(0, name.indexOf(" ")); // initials code initials = nameCaps.substring(0, 1) + nameCaps.substring(name.lastIndexOf(" ") + 1).substring(0, 1) .toUpperCase(); // ------------------------------------------------- // file name information for console writeToFileName = "lotto_" + initials + "_" + mobileDigits + ".txt"; System.out.print("\nYour full name: " + nameCaps); System.out.print("\nYour Contact number: " + mobile); do { do { menu(gameArray, numberFormat, dataBaseFileName); System.out.println(); do { menuError = false; menuSelection = kb.next(); if (menuSelection.matches("Q") || menuSelection.matches("q")) { System.out .print("\nYou have chosen to quit!\nHope to see you again " + nameCaps + "."); System.exit(0); } else if (menuSelection.matches("[1-9]+")) { if (Integer.parseInt(menuSelection) < fileRowCount(dataBaseFileName) + 1) { gameOption = Integer.parseInt(menuSelection); } else if (Integer.parseInt(menuSelection) > fileRowCount(dataBaseFileName)) { menuError = true; System.out.print(gameSelectionError); } } else { menuError = true; System.out.print(gameSelectionError); } } while (menuError); System.out.println("\nWelcome " + firstName); do { System.out.print("\nYou have chosen to play [" + gameOption+ "] "+ (gameArray[gameOption - 1][0])+ ".\n" + gameArray[gameOption - 1][0]+ " costs "+ numberFormat.format(Double.parseDouble(gameArray[gameOption - 1][2])) + " to play and has a minimum purchase of " + gameArray[gameOption - 1][1] + " line(s)." + "\nHow many " + gameArray[gameOption - 1][0] + " would you like?"); lAmount = kb.next(); lineAmount = gameLines(kb, lAmount, gameArray, gameOption, lineAmount, dataBaseFileName, lineError); System.out.println("\nThe total cost to play " + gameArray[gameOption - 1][0] + " will be " + numberFormat.format(Double .parseDouble(gameArray[gameOption - 1][2]) * lineAmount)); System.out .println("\nWould you like to continue?\nPlease choose one of the following:" + "\nY for Yes\nN for No\nQ for Quit "); do { options = kb.next(); menuError = false; if (options.matches("[a-z A-Z]+")) { if (options.equals("Y") || options.equals("y") || options.equals("N") || options.equals("n") || options.equals("Q") || options.equals("q")) { System.out .print("\nYou have chosen to continue.\n\nName: " + nameCaps + "\nContact number: " + mobile + "\n"); // options=options; } else { System.out .print("\nInput Error!\nThis is a yes or no question.\n" + "If you would like to play another game please input Y for Yes, N for No or Q for Quit\nY for Yes\nN for No\nQ for Quit "); menuError = true; } } else if (options.matches("[0-9]+")) { System.out .print("\nInput Error!\nThis is a yes or no question.\n" + "If you would like to play another game please input Y for Yes, N for No or Q for Quit\nY for Yes\nN for No\nQ for Quit "); menuError = true; } } while (menuError); if (options.equals("Y") || options.equals("y")) { lottoLines = "\nYour " + (gameArray[gameOption - 1][0]) + " Numbers are:\n"; // Prints Line and # for each individual line for (int line1 = 1; line1 <= lineAmount; line1++) { if (lineAmount > 9) { if (line1 <= 9) { lottoLines += "Line " + line1 + ": "; } else { lottoLines += "Line " + line1 + ": "; } } else { lottoLines += "Line " + line1 + ": "; } lottoLines += numberGenerator(lottoLines, randomNumber, lottoNumbersPerLine); } System.out.print(lottoLines); // file name information console print-------------- System.out.print("\nYour " + (gameArray[gameOption - 1][0]) + " have been saved to:\n" + writeToFileName); printToFile(name, mobile, gameArray, gameOption, lineAmount, nameCaps, firstName, writeToFileName, initials, mobileDigits, lottoLines, numberFormat); System.out .print("\n\nThe total cost: " + numberFormat.format(Double .parseDouble(gameArray[gameOption - 1][2]) * lineAmount) + " Thank you for playing " + firstName + ".\n\n"); } } while (options.equals("N") || options.equals("n")); } while (options.equals("Q") || options.equals("q")); finalOption = finalLoopOption(kb, finalOption, firstName, menuError); } while (finalOption.equals("Y") || finalOption.equals("y")); kb.close(); input.close(); System.exit(0); } @SuppressWarnings("resource") public static void printToFile(String name, String mobile, String[][] gameArray, int gameOption, int lineAmount, String nameCaps, String firstName, String writeToFileName, String initials, String mobileDigits, String lottoLines, NumberFormat numberFormat) throws FileNotFoundException { PrintStream out = new PrintStream(new File(writeToFileName)); out.println("Name: " + nameCaps); out.println("Contact: " + mobile); out.println(lottoLines); out.println("\nThe total cost: " + numberFormat.format(Double .parseDouble(gameArray[gameOption - 1][2]) * lineAmount) + " Thank you for playing " + firstName + "."); } public static int gameLines(Scanner kb, String lAmount, String[][] gameArray, int gameOption, int lineAmount, final String dataBaseFileName, boolean lineError) throws NumberFormatException, FileNotFoundException { final String gameLineError = "\nInput Error!!\nPlease use numeric digits only - Take note of minimum line requirement below.\n" + gameArray[gameOption - 1][0] + " has a minimum purchase of " + gameArray[gameOption - 1][1] + " line(s)." + "\nHow many " + gameArray[gameOption - 1][0] + " would you like?"; for (int line = 1; line <= fileRowCount(dataBaseFileName); line++) { if (gameOption == line) do { lineError = false; if (lAmount.matches("[0-9]+") && Integer.parseInt(lAmount) >= Integer .parseInt(gameArray[gameOption - 1][1])) { lineAmount = Integer.parseInt(lAmount); } else { while (!lAmount.matches("[0-9]+") || Integer.parseInt(lAmount) < Integer .parseInt(gameArray[gameOption - 1][1]) || lAmount.matches("[a-z A-Z]+")) { System.out.print(gameLineError); lineError = true; lAmount = kb.next(); } } } while (lineError); } return lineAmount; } public static String numberGenerator(String lottoLines, int randomNumber, final int lottoNumbersPerLine) { // Generates 6 random digits for each line String lottoLines2 = ""; for (int counter = 1; counter <= lottoNumbersPerLine; counter++) { Random rand = new Random(); randomNumber = rand.nextInt(45) + 1; // Creates a space before a single digit so // it'll line up with double digit if (randomNumber < 10) { lottoLines2 += " "; } lottoLines2 += randomNumber + " "; } lottoLines2 += "\n"; return lottoLines2; } public static void menu(String[][] gameArray, NumberFormat numberFormat, final String dataBaseFileName) throws FileNotFoundException { final int menuNumber = fileRowCount(dataBaseFileName) + 1; System.out.print("\n\nLOTTO GAME RULES:\n"); for (int row = 0; row < menuNumber - 1; row++) { System.out .print(gameArray[row][0]+ " require at least "+ gameArray[row][1]+ " line(s) to be purchased @ " + numberFormat.format(Double.parseDouble(gameArray[row][2]))+ " per line.\n"); } System.out.print("\nPlease select a game to play!"); System.out.println(); for (int number = 1; number < menuNumber; number++) { System.out.print("\n[" + number + "] "); System.out.print(gameArray[number - 1][0]); } System.out.print("\n[Q] Quit"); } public static Scanner exists(File dataBase, final String dataBaseFileName) throws FileNotFoundException { while (!dataBase.canRead()) { System.out .println("File not found!!\nPlease check your file directory path and run again!"); System.exit(0); //dataBase = new File(dataBaseFileName); } // now we know that dataBase is a file that can be read return new Scanner(dataBase); } public static String finalLoopOption(Scanner kb, String finalOption, String firstName, boolean menuError) { String finalQuestion = "Please choose one of the following:\nY for Yes\nN for No"; System.out.print("Would you like to play another game?\n" + finalQuestion); menuError = false; do { menuError = false; finalOption = kb.next(); if (finalOption.equals("N") || finalOption.equals("n")) { System.out.print("\nThanks for playing " + firstName + " , Good Bye."); } else if (finalOption.equals("Y") || finalOption.equals("y")) { // finalOption = finalOption; } else { System.out .print("\nInput Error!\nThis is a yes or no question.\n" + "If you would like to play another game please input Y for yes or input N for quit\n" + finalQuestion); menuError = true; } } while (menuError); return finalOption; } @SuppressWarnings("resource") public static int fileRowCount(final String dataBaseFileName) throws FileNotFoundException { Scanner countLines = new Scanner(new File(dataBaseFileName)); int count = 0; while (countLines.hasNextLine()) { String line = countLines.nextLine(); @SuppressWarnings("unused") Scanner lineScan = new Scanner(line); count++; } return count; } private static void readFileToArray(Scanner input, File dataBase, Scanner kb, String[][] gameArray) { int row = 0; int column = 0; while (input.hasNextLine()) { String line = input.nextLine(); @SuppressWarnings("resource") Scanner lineScan = new Scanner(line); String game = lineScan.next() + " " + lineScan.next(); gameArray[row][column] = game;// Lotto name int lineLimit = lineScan.nextInt(); gameArray[row][column + 1] = Integer.toString(lineLimit); Double price = lineScan.nextDouble(); gameArray[row][column + 2] = price.toString(); row++; } } }//End of code--------------------- |
| Test run of program |
0 comments:
Post a Comment
Please feel free to leave comments or ask questions related to the tutorials.