Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.time.LocalDate;
- import java.util.Scanner;
- /*
- <h1>Homework 3 </h1>
- <h2>CSC254 Fall 2018</h2>
- <p> This program counts the number of occurrences of each ball in the Missouri Lotto between dates specified by the user.</p>
- <p> The data is stored in a file called <code>lotto.txt</code>. The data is the text copied from the Missouri Lotto Website.</p>
- @author <Name Here>
- @since 2018/09/17
- */
- public class Homework3 {
- public static void main(String[] args) {
- LocalDate startdate = getStartDate();
- //System.out.println("You have Selected A Start Date Of: " + startdate);
- LocalDate endingdate = getEndDate();
- //System.out.println("You Have Selected An End Date Of: " + endingdate);
- int[] counts = new int[45];
- int totalDatalines = processFile(startdate, endingdate, counts);
- tabulate(counts);
- }
- /*
- This method calculates the frequency that each ball occurs within lotto.txt given the range defined by the dates the user puts in.
- */
- public static void printCounts(int[] counts) {
- System.out.println("Ball Frequencies:");
- for(int i = 1; i <= 44; i++ ){
- System.out.printf("Ball %d was drawn %d times\n", i, counts[i]);
- }
- }
- /*
- This method prints all of the occurrences of the number frequencies
- */
- public static void tabulate(int[] counts) {
- printhighCount(counts);
- printlowCount(counts);
- printaverageCount(counts);
- printCounts(counts);
- }
- /*
- This method produces which number was the luckiest number, how often it was picked, and which balls were picked that frequently.
- */
- private static void printhighCount(int[] counts) {
- int highestSoFar = counts[1];
- for(int i=0; i<45; i++)
- if(highestSoFar<counts[i])
- highestSoFar=counts[i];
- System.out.println("Highest count is: "+highestSoFar);
- }
- /*
- This method produces which number won least often, and how few times it was picked, and which balls were picked that infrequently
- */
- private static void printlowCount(int[] counts) {
- int lowestSoFar = counts[1];
- for(int i=1; i<45; i++)
- if(lowestSoFar>counts[i])
- lowestSoFar=counts[i];
- System.out.println("lowest count is "+lowestSoFar);
- }
- private static void printaverageCount(int[] counts) {
- }
- /*
- This method counts the number of times any number from 1-44 occurred.
- */
- public static void countNumbers(String gameNumbers, int[] counts) {
- String[] numberParts = gameNumbers.split("-");
- for (int i = 0; i < numberParts.length; i++) {
- int number = Integer.parseInt(numberParts[i]);
- counts[number]++;
- }
- }
- /*
- This method takes the dates from lotto.txt and validates whether they are between the dates entered by the user
- */
- public static boolean isBetweenDates(LocalDate firstDate, LocalDate secondDate, LocalDate gameDate) {
- boolean isBetween = true;
- isBetween = (firstDate.compareTo(gameDate) <= 0 && secondDate.compareTo(gameDate) >= 0);
- return isBetween;
- }
- /*
- This method takes the data that was separated by processFile and turns it into a Local date so that it can later be compared.
- */
- public static LocalDate makeLottoDate(String lottoYear, String lottoMonth, String lottoDay) {
- LocalDate makeLottoDate = null;
- int gameDay = Integer.parseInt(lottoDay.substring(0, 2));
- String gameMonthStr = lottoMonth;
- int gameMonth = 0;
- int gameYear = Integer.parseInt(lottoYear);
- if (gameMonthStr.equals("Jan"))
- gameMonth = 1;
- else if (gameMonthStr.equals("Feb"))
- gameMonth = 2;
- else if (gameMonthStr.equals("Mar"))
- gameMonth = 3;
- else if (gameMonthStr.equals("Apr"))
- gameMonth = 4;
- else if (gameMonthStr.equals("May"))
- gameMonth = 5;
- else if (gameMonthStr.equals("Jun"))
- gameMonth = 6;
- else if (gameMonthStr.equals("Jul"))
- gameMonth = 7;
- else if (gameMonthStr.equals("Aug"))
- gameMonth = 8;
- else if (gameMonthStr.equals("Sep"))
- gameMonth = 9;
- else if (gameMonthStr.equals("Oct"))
- gameMonth = 10;
- else if (gameMonthStr.equals("Nov"))
- gameMonth = 11;
- else if (gameMonthStr.equals("Dec"))
- gameMonth = 12;
- makeLottoDate = LocalDate.of(gameYear, gameMonth, gameDay);
- return makeLottoDate;
- }
- /*
- This method opens the file lotto.txt, targets the section that contains dates and winning numbers
- and then splits that section line by line based on spaces.
- */
- public static int processFile(LocalDate startdate, LocalDate endingdate, int[] counts) {
- int n = 0;
- try {
- Scanner input = new Scanner(new File("lotto.txt"));
- while (input.hasNextLine()) {
- String line;
- String pattern = "((Sat)|(Wed)),.*Million";
- line = input.nextLine();
- if (line.matches(pattern)) {
- String[] parts = line.split("\\s+");
- n++;
- LocalDate gameDate = makeLottoDate(parts[3], parts[1], parts[2]);
- if (isBetweenDates(startdate, endingdate, gameDate)) {
- countNumbers(parts[4],counts);
- }
- }
- }
- } catch (FileNotFoundException e) {
- System.err.println("Error: Could not open file lotto.txt");
- }
- return n;
- }
- /*
- Used to ask the user the first date they would like to search.
- */
- public static LocalDate getStartDate() {
- LocalDate date = null;
- Scanner input = new Scanner(System.in);
- while (date == null) {
- try {
- System.out.print("Please Enter A Start Date In YYYY/MM/DD Format: ");
- String line = input.nextLine();
- String[] parts = line.split("/");
- int theYear = Integer.parseInt(parts[0]);
- int theMonth = Integer.parseInt(parts[1]);
- int theDay = Integer.parseInt(parts[2]);
- date = LocalDate.of(theYear, theMonth, theDay);
- } catch (Exception e) {
- System.out.println("Error in Date. Please Use The Correct Format Note: /'s Must Be Included");
- }
- }
- return date;
- }
- /*
- Asks the user the second date they would like to search through
- */
- public static LocalDate getEndDate() {
- LocalDate date2 = null;
- Scanner input = new Scanner(System.in);
- while (date2 == null) {
- try {
- System.out.print("Please Enter A End Date To Search in YYY/MM/DD Format: ");
- String line2 = input.nextLine();
- String[] parts2 = line2.split("/");
- int theYear2 = Integer.parseInt(parts2[0]);
- int theMonth2 = Integer.parseInt(parts2[1]);
- int theDay2 = Integer.parseInt(parts2[2]);
- date2 = LocalDate.of(theYear2, theMonth2, theDay2);
- } catch (Exception e) {
- System.out.println("Error In Date. Please Use The Correct Format. Note: /'s are Required");
- }
- }
- return date2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment