Luninariel

Homework 3 - WIP

Sep 20th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.51 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.time.LocalDate;
  4. import java.util.Scanner;
  5.  
  6. /*
  7. <h1>Homework 3 </h1>
  8. <h2>CSC254 Fall 2018</h2>
  9. <p> This program counts the number of occurrences of each ball in the Missouri Lotto between dates specified by the user.</p>
  10. <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>
  11.  
  12. @author <Name Here>
  13. @since 2018/09/17
  14.  */
  15. public class Homework3 {
  16.     public static void main(String[] args) {
  17.         LocalDate startdate = getStartDate();
  18.         //System.out.println("You have Selected A Start Date Of: " + startdate);
  19.         LocalDate endingdate = getEndDate();
  20.         //System.out.println("You Have Selected An End Date Of: " + endingdate);
  21.         int[] counts = new int[45];
  22.         int totalDatalines = processFile(startdate, endingdate, counts);
  23.         tabulate(counts);
  24.     }
  25. /*
  26. This method calculates the frequency that each ball occurs within lotto.txt given the range defined by the dates the user puts in.
  27.  */
  28.     public static void printCounts(int[] counts) {
  29.         System.out.println("Ball Frequencies:");
  30.        for(int i = 1; i <= 44; i++ ){
  31.            System.out.printf("Ball %d was drawn %d times\n", i, counts[i]);
  32.        }
  33.  
  34.     }
  35. /*
  36. This method prints all of the occurrences of the number frequencies
  37.  */
  38.     public static void tabulate(int[] counts) {
  39.         printhighCount(counts);
  40.         printlowCount(counts);
  41.         printaverageCount(counts);
  42.         printCounts(counts);
  43.  
  44.     }
  45. /*
  46. This method produces which number was the luckiest number, how often it was picked, and which balls were picked that frequently.
  47.  */
  48.     private static void printhighCount(int[] counts) {
  49.         int highestSoFar = counts[1];
  50.         for(int i=0; i<45; i++)
  51.             if(highestSoFar<counts[i])
  52.                 highestSoFar=counts[i];
  53.             System.out.println("Highest count is: "+highestSoFar);
  54.  
  55.     }
  56. /*
  57. This method produces which number won least often, and how few times it was picked, and which balls were picked that infrequently
  58.  */
  59.     private static void printlowCount(int[] counts) {
  60.         int lowestSoFar = counts[1];
  61.         for(int i=1; i<45; i++)
  62.             if(lowestSoFar>counts[i])
  63.                 lowestSoFar=counts[i];
  64.             System.out.println("lowest count is "+lowestSoFar);
  65.     }
  66.  
  67.     private static void printaverageCount(int[] counts) {
  68.  
  69.     }
  70. /*
  71. This method counts the number of times any number from 1-44 occurred.
  72.  */
  73.     public static void countNumbers(String gameNumbers, int[] counts) {
  74.         String[] numberParts = gameNumbers.split("-");
  75.         for (int i = 0; i < numberParts.length; i++) {
  76.             int number = Integer.parseInt(numberParts[i]);
  77.             counts[number]++;
  78.         }
  79.  
  80.     }
  81. /*
  82. This method takes the dates from lotto.txt and validates whether they are between the dates entered by the user
  83.  */
  84.     public static boolean isBetweenDates(LocalDate firstDate, LocalDate secondDate, LocalDate gameDate) {
  85.         boolean isBetween = true;
  86.  
  87.         isBetween = (firstDate.compareTo(gameDate) <= 0 && secondDate.compareTo(gameDate) >= 0);
  88.  
  89.         return isBetween;
  90.     }
  91.  
  92.     /*
  93.     This method takes the data that was separated by processFile and turns it into a Local date so that it can later be compared.
  94.  
  95.      */
  96.     public static LocalDate makeLottoDate(String lottoYear, String lottoMonth, String lottoDay) {
  97.         LocalDate makeLottoDate = null;
  98.         int gameDay = Integer.parseInt(lottoDay.substring(0, 2));
  99.         String gameMonthStr = lottoMonth;
  100.         int gameMonth = 0;
  101.         int gameYear = Integer.parseInt(lottoYear);
  102.         if (gameMonthStr.equals("Jan"))
  103.             gameMonth = 1;
  104.         else if (gameMonthStr.equals("Feb"))
  105.             gameMonth = 2;
  106.         else if (gameMonthStr.equals("Mar"))
  107.             gameMonth = 3;
  108.         else if (gameMonthStr.equals("Apr"))
  109.             gameMonth = 4;
  110.         else if (gameMonthStr.equals("May"))
  111.             gameMonth = 5;
  112.         else if (gameMonthStr.equals("Jun"))
  113.             gameMonth = 6;
  114.         else if (gameMonthStr.equals("Jul"))
  115.             gameMonth = 7;
  116.         else if (gameMonthStr.equals("Aug"))
  117.             gameMonth = 8;
  118.         else if (gameMonthStr.equals("Sep"))
  119.             gameMonth = 9;
  120.         else if (gameMonthStr.equals("Oct"))
  121.             gameMonth = 10;
  122.         else if (gameMonthStr.equals("Nov"))
  123.             gameMonth = 11;
  124.         else if (gameMonthStr.equals("Dec"))
  125.             gameMonth = 12;
  126.         makeLottoDate = LocalDate.of(gameYear, gameMonth, gameDay);
  127.  
  128.         return makeLottoDate;
  129.     }
  130.  
  131. /*
  132. This method opens the file lotto.txt, targets the section that contains dates and winning numbers
  133. and then splits that section line by line based on spaces.
  134.  */
  135.  
  136.     public static int processFile(LocalDate startdate, LocalDate endingdate, int[] counts) {
  137.         int n = 0;
  138.         try {
  139.             Scanner input = new Scanner(new File("lotto.txt"));
  140.             while (input.hasNextLine()) {
  141.                 String line;
  142.                 String pattern = "((Sat)|(Wed)),.*Million";
  143.                 line = input.nextLine();
  144.                 if (line.matches(pattern)) {
  145.                     String[] parts = line.split("\\s+");
  146.                     n++;
  147.                     LocalDate gameDate = makeLottoDate(parts[3], parts[1], parts[2]);
  148.                     if (isBetweenDates(startdate, endingdate, gameDate)) {
  149.                         countNumbers(parts[4],counts);
  150.  
  151.                     }
  152.                 }
  153.             }
  154.         } catch (FileNotFoundException e) {
  155.             System.err.println("Error: Could not open file lotto.txt");
  156.         }
  157.         return n;
  158.     }
  159.  
  160.     /*
  161.     Used to ask the user the first date they would like to search.
  162.      */
  163.     public static LocalDate getStartDate() {
  164.         LocalDate date = null;
  165.         Scanner input = new Scanner(System.in);
  166.         while (date == null) {
  167.             try {
  168.                 System.out.print("Please Enter A Start Date In YYYY/MM/DD Format: ");
  169.                 String line = input.nextLine();
  170.  
  171.                 String[] parts = line.split("/");
  172.                 int theYear = Integer.parseInt(parts[0]);
  173.                 int theMonth = Integer.parseInt(parts[1]);
  174.                 int theDay = Integer.parseInt(parts[2]);
  175.                 date = LocalDate.of(theYear, theMonth, theDay);
  176.             } catch (Exception e) {
  177.                 System.out.println("Error in Date. Please Use The Correct Format Note: /'s Must Be Included");
  178.             }
  179.         }
  180.         return date;
  181.     }
  182.  
  183.     /*
  184.     Asks the user the second date they would like to search through
  185.      */
  186.     public static LocalDate getEndDate() {
  187.         LocalDate date2 = null;
  188.         Scanner input = new Scanner(System.in);
  189.         while (date2 == null) {
  190.             try {
  191.                 System.out.print("Please Enter A End Date To Search in YYY/MM/DD Format: ");
  192.                 String line2 = input.nextLine();
  193.  
  194.                 String[] parts2 = line2.split("/");
  195.                 int theYear2 = Integer.parseInt(parts2[0]);
  196.                 int theMonth2 = Integer.parseInt(parts2[1]);
  197.                 int theDay2 = Integer.parseInt(parts2[2]);
  198.                 date2 = LocalDate.of(theYear2, theMonth2, theDay2);
  199.             } catch (Exception e) {
  200.                 System.out.println("Error In Date. Please Use The Correct Format. Note: /'s are Required");
  201.             }
  202.         }
  203.         return date2;
  204.     }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment