Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. import java.util.*;
  2. import javax.swing.JFileChooser;
  3. import java.io.*;
  4.  
  5. public class SuperBowlNonGUIClient {
  6.  
  7. public static void main(String[] args) throws IOException {
  8.  
  9. final int SIZE = 15;
  10.  
  11. //array
  12. SuperBowlGame [] list = new SuperBowlGame[SIZE];
  13.  
  14. String fileName;
  15. while(true) {
  16. //create a JFileChooser object
  17. JFileChooser openChooser = new JFileChooser("."); //current folder
  18.  
  19. int status = openChooser.showOpenDialog(null);
  20.  
  21. if(status == JFileChooser.APPROVE_OPTION) {
  22. fileName = openChooser.getSelectedFile().getAbsolutePath();
  23. break;
  24. }
  25. else { //cancel button is clicked
  26. System.out.println("No file is selected.");
  27. }
  28. }//end while
  29.  
  30. //read the data file
  31. Scanner inFile = new Scanner(new FileReader(fileName));
  32.  
  33. //remove the data file heading
  34.  
  35. String number;
  36. String date;
  37. String site;
  38. String winning_team;
  39. String score;
  40. String losing_team;
  41. int attendance;
  42.  
  43. int count = 0;
  44.  
  45. while(inFile.hasNext()) {
  46. number = inFile.next();
  47. date = inFile.next();
  48. site = inFile.next();
  49. winning_team = inFile.next();
  50. score = inFile.next();
  51. losing_team = inFile.next();
  52. attendance = inFile.nextInt();
  53.  
  54.  
  55. //create an Inventory object and place it into the array
  56. list[count] = new SuperBowlGame(number, date, site, winning_team,
  57. score, losing_team, attendance);
  58.  
  59. count++;
  60. }
  61.  
  62. //use a menu to process the array
  63. boolean flag = true;
  64. int userCommand;
  65. Scanner console = new Scanner(System.in);
  66.  
  67. while(flag) {
  68. showMenu();
  69.  
  70. userCommand = console.nextInt();
  71. switch(userCommand) {
  72. case 1: //output the entire array
  73. for(int i=0; i<list.length; i++) {
  74. System.out.println(list[i]);
  75. }
  76. break;
  77.  
  78. case 2: //outputs the average attendance
  79. double average;
  80. int sum = 0;
  81.  
  82. for(int i=0; i<list.length; i++) {
  83. sum += list[i].getAttendance();
  84. }
  85. //calculates average
  86. average = sum / SIZE;
  87.  
  88. System.out.println();
  89. System.out.printf("Average attendance: $.2f", average);
  90.  
  91. break;
  92. case 3: //find the highest attended Super Bowl
  93. int maxIndex = 0;
  94. int highestAttendance = 0;
  95. for(int i=0; i<list.length; i++) {
  96. if(list[i].getAttendance() > highestAttendance) {
  97. highestAttendance = list[i].getAttendance();
  98. maxIndex = i;
  99. }
  100. }
  101. System.out.println(list[maxIndex]);
  102.  
  103.  
  104. // case 4:
  105. //
  106. // System.out.println("the objects that have less than 10 pieces in stock");
  107. //
  108. // for(int i=0; i<list.length;i++) {
  109. // if(list[i].getNumOfPieces() < 10) {
  110. // System.out.println(list[i]);
  111. // }
  112. // }
  113. // break;
  114. // case 5:
  115. // System.out.print("Enter the item name to search: ");
  116. // String searchItem = console.next();
  117. //
  118. // int foundIndex = -1;
  119. //
  120. // for(int i=0; i<list.length; i++) {
  121. //
  122. // if(searchItem.equalsIgnoreCase(list[i].getItemName())) {
  123. // foundIndex = i;
  124. // }
  125. // }
  126. // System.out.println(list[foundIndex]);
  127. //
  128. // if(foundIndex == -1) {
  129. // System.out.println("Not found");
  130. // }
  131. // else {
  132. // System.out.println(list[foundIndex]);
  133. // }
  134. //
  135. // break;
  136.  
  137. case 6: //exit the program
  138. flag = false;
  139. break;
  140. default:
  141. System.out.println("Invalid command, try again.");
  142.  
  143. }//end switch
  144.  
  145. }//end while
  146.  
  147. }//end main
  148.  
  149.  
  150. public static void showMenu() {
  151. System.out.println("\n\n"
  152. + "1 -- Output the Super Bowl information\n"
  153. + "2 -- Output the average attendance\n"
  154. + "3 -- Find the highest attendance\n"
  155. + "4 -- Find the lowest attendance\n"
  156. + "5 -- Search for a team and output it's "
  157. + "Super Bowl information\n"
  158. + "0 -- exit\n"
  159. + "Enter a command: ");
  160. }
  161. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement