Update the SuperLottoPlus class (either your assignment, or my solution) to incl
ID: 3870485 • Letter: U
Question
Update the SuperLottoPlus class (either your assignment, or my solution) to include the following:
Create a new method called printTicket that takes a String of lottery numbers as a parameter (the return value of generateNumbers)
The method header should indicated that it throws an exception
Inside the method, write the passed String to a file called lottery.txt
When writing to the file, figure out how to make it append to the file rather than over-write the file
In the main method
When the user is prompted to enter the number of lottery tickets, catch the exception if anything other than an integer is entered
Also, manually throw and catch an exception if the number of lottery tickets is less than or equal to 0
Rather than just printing the lottery numbers to the screen, print it to the lottery.txt file (by calling printTicket)
Surround the call to the printTicket method with exception handling, and display a friendly message to the user if there are any issues with the file
Explanation / Answer
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
/**
* exception class for invali input
*
* @author
*
*/
class InvalidInputException extends Exception {
public InvalidInputException() {
// TODO Auto-generated constructor stub
super();
}
public InvalidInputException(String message) {
// TODO Auto-generated constructor stub
super(message);
}
}
/**
* SuperLottoPlus class will generate super lotto tickets
*
* @author James
*
*/
public class SuperLottoPlusSolution {
/**
*
* append data to the file
*
* @param lotteryNumbers
*/
public static void printTicket(String lotteryNumbers) {
BufferedWriter bw = null;
FileWriter fw = null;
try {
File file = new File("lottery.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// true = append file
fw = new FileWriter(file.getAbsoluteFile(), true);
bw = new BufferedWriter(fw);
bw.write(lotteryNumbers);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
* Method asks the user for the number of lotto tickets and then calls
* generateNumbers() numberOfTickets times.
*/
public static void main(String args[]) {
int numberOfTickets;
Scanner scnr = new Scanner(System.in);
System.out.println("How many Super Lotto tickets do you want?");
try {
numberOfTickets = scnr.nextInt();
if (numberOfTickets <= 0)
throw new InvalidInputException(
"Number of tickets cannot lessthan or equal to zero");
for (int i = 0; i < numberOfTickets; i++) {
String lotteryNumbers = generateNumbers();
printTicket(lotteryNumbers);
}
System.out
.println("LOTTEYR TICKETS WRITTEN SUCCESSFULLY TO lottery.txt");
} catch (InputMismatchException e) {
// TODO: handle exception
System.out.println("Invalid Input");
} catch (InvalidInputException e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* Returns true if duplicate or false otherwise
*
* @param array
* array of numbers
* @param key
* the number to search
* @return true if duplicate or false if not duplicate
*/
public static boolean isDuplicate(int[] array, int key) {
for (int i = 0; i < array.length; i++) {
if (array[i] == key)
return true;
}
return false;
}
/**
* Generate SuperLotto numbers and return a formatted String of results
*
* @return set of SuperLottoNumbers
*/
public static String generateNumbers() {
String output = " ";
Random rand = new Random();
int[] lotto = new int[6];
for (int i = 0; i < lotto.length - 1; i++) {
int temp = rand.nextInt(47) + 1;
while (isDuplicate(lotto, temp)) {
temp = rand.nextInt(7) + 1;
}
// NOTE: This line below was causing the error in class
// and it needed to be outside the loop
lotto[i] = temp;
output += lotto[i] + " ";
}
lotto[5] = rand.nextInt(27) + 1;
output += "(MEGA: " + lotto[5] + ")" + " ";
return output;
}
}
OUTPUT 1:
How many Super Lotto tickets do you want?
we
Invalid Input
OUTPUT 2:
How many Super Lotto tickets do you want?
-8
InvalidInputException: Number of tickets cannot lessthan or equal to zero
at SuperLottoPlusSolution.main(SuperLottoPlusSolution.java:92)
OUTPUT 3:
How many Super Lotto tickets do you want?
5
LOTTEYR TICKETS WRITTEN SUCCESSFULLY TO lottery.txt
lottery.txt
25 20 12 34 35 (MEGA: 14)
35 32 46 44 3 (MEGA: 13)
28 18 33 3 16 (MEGA: 9)
28 7 35 18 38 (MEGA: 26)
27 1 14 13 24 (MEGA: 21)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.