Guest User

Untitled

a guest
Apr 25th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.66 KB | None | 0 0
  1. // Brandon Silver
  2. // Section 004
  3. // CS 1113
  4. // 2011-12-09
  5. //
  6. // Reads in football statistics from supplied input, prints
  7. // out the input, sorts the input, and then prints it back out.
  8. // Then, searches the sorted data for the supplied statistics.
  9.  
  10. import java.util.Scanner;
  11.  
  12. public class Football2 {
  13.     public static void main(String[] args)
  14.     {
  15.         // declare final values
  16.         final int ARR_SIZE = 100;
  17.         final String SENT = "NoSuchTeam";
  18.         final int TEAM_LENGTH = 4;
  19.        
  20.         // declare football team object array
  21.         FootballTeam[] footballArr = new FootballTeam[ARR_SIZE];
  22.  
  23.         // initialize footbal teams to fill footballArr
  24.         for (int i = 0; i < ARR_SIZE; i++)
  25.             footballArr[i] = new FootballTeam();
  26.  
  27.         // declare scanner
  28.         Scanner scan = new Scanner(System.in);
  29.        
  30.         // declare all other variables
  31.         String titleLine = "";
  32.         int numRecords = 0;
  33.         String sTeamKey = "";
  34.         int iYardsNumKey = 0;
  35.         int elementPos = 0;
  36.         FootballTeam footballTeamKey = new FootballTeam();
  37.  
  38.         // Read in first line (title) into a variable for printing
  39.         // (placed here b/c it doesn't really belong in readDatabase method)
  40.         if (scan.hasNext())
  41.             titleLine = scan.nextLine();
  42.         else
  43.         {
  44.             System.out.println("ERROR: No input detected. Quitting program.");
  45.             System.exit(0);
  46.         }
  47.         // Print titleLine
  48.         System.out.println(titleLine);
  49.  
  50.         // call readDatabase
  51.         numRecords = readDatabase(scan, ARR_SIZE, SENT, footballArr);
  52.        
  53.         // call printDatabase
  54.         printDatabase(numRecords, footballArr);
  55.  
  56.         System.out.println("Sort the database.");
  57.  
  58.         // call sortDatabase (broken, I have no idea how to do this)
  59.         sortDatabase(numRecords, footballArr);
  60.  
  61.         // print the newly sorted database (except it ain't sorted, see above)
  62.         printDatabase(numRecords, footballArr);
  63.        
  64.         // Defensively read in the team
  65.         if (scan.hasNext())
  66.             sTeamKey = scan.next();
  67.         // enter search sentinel loop (assuming it starts after first sentinel)
  68.         while (!sTeamKey.equals(SENT))
  69.         {
  70.             // Defensively read in the yards
  71.             if (scan.hasNextInt())
  72.                 iYardsNumKey = scan.nextInt();
  73.             else
  74.             {
  75.                 System.out.println("ERROR: Expecting an int. Quitting program.");
  76.                 System.exit(0);
  77.             }
  78.            
  79.             // output search query
  80.             System.out.println("\nIs there a team with an abbreviation = "
  81.                     + sTeamKey + "\n   and total yards = " + iYardsNumKey
  82.                     + " in the database, \n   and if so, what is its number of wins.");
  83.             // load in temporary values into key FootballTeam object
  84.             footballTeamKey.setName(sTeamKey);
  85.             footballTeamKey.setYards(iYardsNumKey);
  86.  
  87.             elementPos = seqSearch(numRecords, footballArr, footballTeamKey);
  88.            
  89.             // output search results
  90.             if (elementPos == -1)
  91.                 System.out.println("No, there is no such team in the database.");
  92.             else
  93.             {
  94.                 System.out.println("Yes. The number of wins is "
  95.                         + footballArr[elementPos].getWins() + ".");
  96.             }
  97.            
  98.             // read in the next team name to look for
  99.             if (scan.hasNext())
  100.                 sTeamKey = scan.next();
  101.             else
  102.             {
  103.                 System.out.println("ERROR: No input detected. Quitting Program.");
  104.                 System.exit(0);
  105.             }
  106.         } // end search sentinel loop
  107.  
  108.  
  109.  
  110.     } // end main method
  111.  
  112.     public static int readDatabase(Scanner scan, int ARR_SIZE, String nameSentinel,
  113.             FootballTeam[] football)
  114.     {
  115.         // declare variable to return
  116.         int numRecords = 0;
  117.         // declare temporary container variables
  118.         String sTeamName = "";
  119.         int iAttemptsNum = 0;
  120.         int iYards = 0;
  121.         int iWin = 0;
  122.         int iLose = 0;
  123.  
  124.         // Defensively read in team name
  125.             if (scan.hasNext())
  126.         {
  127.             sTeamName = scan.next();
  128.         }
  129.         else
  130.         {
  131.             System.out.println("ERROR: Invalid input. Quitting program.");
  132.             System.exit(0);
  133.         }
  134.         while (!sTeamName.equals(nameSentinel))
  135.         {
  136.             // attempts
  137.             if (scan.hasNextInt())
  138.             {
  139.                 iAttemptsNum = scan.nextInt();
  140.             }
  141.             else
  142.             {
  143.                 System.out.println("ERROR: Invalid input. Quitting program.");
  144.                 System.exit(0);
  145.             }
  146.  
  147.             // yards
  148.             if (scan.hasNext())
  149.             {
  150.                 iYards = scan.nextInt();
  151.             }
  152.             else
  153.             {
  154.                 System.out.println("ERROR: Invalid input. Quitting program.");
  155.                 System.exit(0);
  156.             }
  157.  
  158.             // wins
  159.             if (scan.hasNextInt())
  160.             {
  161.                 iWin = scan.nextInt();
  162.             }
  163.             else
  164.             {
  165.                 System.out.println("ERROR: Invalid input. Quitting program.");
  166.                 System.exit(0);
  167.             }
  168.  
  169.             // loses
  170.             if (scan.hasNextInt())
  171.             {
  172.                 iLose = scan.nextInt();
  173.             }
  174.             else
  175.             {
  176.                 System.out.println("ERROR: Invalid input. Quitting Program.");
  177.                 System.exit(0);
  178.             }
  179.            
  180.             // load the holder variables into the specific FootballTeam object
  181.             // in the FootballTeam object array.
  182.             // also verify that there is enough space in the array.
  183.             if (numRecords < ARR_SIZE - 1)
  184.             {
  185.                 football[numRecords].setName(sTeamName);
  186.                 football[numRecords].setAttempts(iAttemptsNum);
  187.                 football[numRecords].setYards(iYards);
  188.                 football[numRecords].setWins(iWin);
  189.                 football[numRecords].setLosses(iLose);
  190.  
  191.             }
  192.             else
  193.             {
  194.                 System.out.println("ERROR: There is not enough space in the"
  195.                         + " array. Quitting program.");
  196.                 System.exit(0);
  197.             }
  198.            
  199.             // increment the number of records
  200.             numRecords++;
  201.  
  202.             // Defensively read in team name
  203.             if (scan.hasNext())
  204.             {
  205.                 sTeamName = scan.next();
  206.             }
  207.             else
  208.             {
  209.                 System.out.println("ERROR: Invalid input. Quitting program.");
  210.                 System.exit(0);
  211.             }
  212.         } // end data input sentinel loop
  213.  
  214.         return numRecords;
  215.  
  216.     }
  217.  
  218.     public static void printDatabase(int numRecords, FootballTeam[] footballArr)
  219.     {
  220.         System.out.println("\nPrint the database:");
  221.         // begin output loop
  222.         for (int i = 0; i < numRecords; i++)
  223.         {
  224.             // add spaces to names as needed
  225. //          while (footballArr[i].getName().length() < 4)
  226. //          {
  227. //              footballArr[i].setName((footballArr[i].getName() + " "));
  228. //          }
  229.            
  230.             System.out.println(footballArr[i].toString());
  231.                    
  232.         }
  233.  
  234.     } // end printDatabase method
  235.  
  236.     public static void sortDatabase(int numRecords, FootballTeam[] footballArr)
  237.     {
  238.         int counter = 0;
  239.         String tempStr = "";
  240.         int tempInt = 0;
  241.         FootballTeam tempTeam = new FootballTeam();
  242.  
  243.         for (int i = 1; i < numRecords; i++)
  244.         {
  245.             counter = i - 1;
  246.             for (int j = i; j < numRecords; j++)
  247.             {
  248.                 if(footballArr[j].compareTo(footballArr[counter]) < 0)
  249.                     counter = j;
  250.                 else if (footballArr[j].compareTo(footballArr[counter]) == 0)
  251.                 {
  252.                         counter = i + 1;
  253.                 }
  254.             }
  255.  
  256.             tempTeam = footballArr[i-1];
  257.             footballArr[i-1] = footballArr[counter];
  258.             footballArr[counter] = tempTeam;
  259.         }
  260.  
  261.  
  262.     } // end sortDatabase method
  263.  
  264.     public static int seqSearch(int numRecords, FootballTeam[] footballArr,
  265.             FootballTeam footballTeamKey)
  266.     {
  267.         return 0;
  268.     } // end seqSearch method
  269.  
  270. } // end Football2 class
Add Comment
Please, Sign In to add comment