Working by yourself, you are to create “Lotto QuickPick” software that will store 6 randomly generated numbers ranging from 1 to 45 for use in the Irish lotto and save the results to a file.
Download full brief here
Download source code here
//Start of code--------------------- package lottoquickpick; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintStream; 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 LottoQuickPick { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(System.in); String name, firstName, mobile, initials, mobileDigits; final String nameError = "Invalid name input!!\nPlease input A to Z characters only to continue: "; final String mobileError = "Invalid mobile number!!\nPlease input 0 to 9 digits only to continue: "; final String lineAmountError = "Invalid digit!!\nPlease input 0 to 9 digits only to continue: "; int lineAmount = 0; // Start of output to console------------------- welcomeHeader(); // Code below is for the name input------------------- System.out .print("\nPlease enter your full name to continue(A - Z only) ie. Joe Soap: "); name = input.nextLine(); while (name.length() <= 3 || !name.matches("[a-z A-Z]+")) { System.out.print(nameError); name = input.nextLine(); } //name.length(); // ------------------------------------------------- // Code below is for the mobile #------------------- System.out .print("Please enter your Mobile Number (0-9 digits only (ie. 0871234567): "); mobile = input.next(); while (mobile.length() != 10 || !mobile.matches("[0-9]+")) { System.out.print(mobileError); mobile = input.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); // 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(); // ------------------------------------------------- // code below is for line amount input-------------- System.out.print("\nHow many lotto lines would you like? "); while (!input.hasNextInt()) { System.out.print(lineAmountError); input.next(); } lineAmount = input.nextInt(); // ------------------------------------------------- // file name information for console String fileName = "\nC:/Documents and Settings/Lesley/Desktop/lotto_" + initials + "_" + mobileDigits + ".txt"; System.out.print("\nYour lotto numbers are:"); // Start of output file information - This file will save to the package // folder------------------- PrintStream out = new PrintStream(new File("C:/Documents and Settings/Lesley/Desktop/lotto_" + initials + "_" + mobileDigits + ".txt")); out.println("Name: " + nameCaps); out.println("Contact: " + mobile); out.println("\n"); out.print("\nYour lotto numbers are:"); // Code for lines------------------- int lottoLines = 0; @SuppressWarnings("unused") int sum = 0; for (int line = 0; line < lineAmount; line++) { // This nested section will allow for lotto #'s to line up if the // lotto line input is a double figure ie: 10 or 12 if (lineAmount > 9) { if (line < 9) { System.out.print("\nLine " + (line + 1) + ": "); out.println("\n"); out.print("Line " + (line + 1) + ": "); } else { System.out.print("\nLine " + (line + 1) + ": "); out.println("\n"); out.print("Line " + (line + 1) + ": "); } } else { System.out.print("\nLine " + (line + 1) + ": "); out.println("\n"); out.print("Line " + (line + 1) + ": "); } sum += lottoLines; { // read & store each lotto #'s------------------- String results = ""; int aNumber, numberOfNumbers = 6; int counter; for (counter = 1; counter <= numberOfNumbers; counter++) { aNumber = (int) ((Math.random() * 45) + 1); if (aNumber >= 10) { results += aNumber + " "; } else { results += " " + aNumber + " "; } } System.out.print(results); out.print(results); } } // ------------------------------------------------- // file name information console print-------------- System.out.print("\n\nYour Lotto Numbers have been saved to:" + fileName); out.println("\n"); out.println("\n"); out.println("Thank you for playing. Best of luck " + firstName + "."); // ------------------------------------------------- out.close(); input.close(); } // method for welcome header public static void welcomeHeader() { System.out.println("\nWELCOME TO LOTTO QUICKPICK"); } }//End of code--------------------- |
0 comments:
Post a Comment
Please feel free to leave comments or ask questions related to the tutorials.