Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import javax.swing.JFileChooser;
- import java.io.*;
- public class SuperBowlNonGUIClient {
- public static void main(String[] args) throws IOException {
- final int SIZE = 15;
- //array
- SuperBowlGame [] list = new SuperBowlGame[SIZE];
- String fileName;
- while(true) {
- //create a JFileChooser object
- JFileChooser openChooser = new JFileChooser("."); //current folder
- int status = openChooser.showOpenDialog(null);
- if(status == JFileChooser.APPROVE_OPTION) {
- fileName = openChooser.getSelectedFile().getAbsolutePath();
- break;
- }
- else { //cancel button is clicked
- System.out.println("No file is selected.");
- }
- }//end while
- //read the data file
- Scanner inFile = new Scanner(new FileReader(fileName));
- //remove the data file heading
- String number;
- String date;
- String site;
- String winning_team;
- String score;
- String losing_team;
- int attendance;
- int count = 0;
- while(inFile.hasNext()) {
- number = inFile.next();
- date = inFile.next();
- site = inFile.next();
- winning_team = inFile.next();
- score = inFile.next();
- losing_team = inFile.next();
- attendance = inFile.nextInt();
- //create an Inventory object and place it into the array
- list[count] = new SuperBowlGame(number, date, site, winning_team,
- score, losing_team, attendance);
- count++;
- }
- //use a menu to process the array
- boolean flag = true;
- int userCommand;
- Scanner console = new Scanner(System.in);
- while(flag) {
- showMenu();
- userCommand = console.nextInt();
- switch(userCommand) {
- case 1: //output the entire array
- for(int i=0; i<list.length; i++) {
- System.out.println(list[i]);
- }
- break;
- case 2: //outputs the average attendance
- double average;
- int sum = 0;
- for(int i=0; i<list.length; i++) {
- sum += list[i].getAttendance();
- }
- //calculates average
- average = sum / SIZE;
- System.out.println();
- System.out.printf("Average attendance: $.2f", average);
- break;
- case 3: //find the highest attended Super Bowl
- int maxIndex = 0;
- int highestAttendance = 0;
- for(int i=0; i<list.length; i++) {
- if(list[i].getAttendance() > highestAttendance) {
- highestAttendance = list[i].getAttendance();
- maxIndex = i;
- }
- }
- System.out.println(list[maxIndex]);
- // case 4:
- //
- // System.out.println("the objects that have less than 10 pieces in stock");
- //
- // for(int i=0; i<list.length;i++) {
- // if(list[i].getNumOfPieces() < 10) {
- // System.out.println(list[i]);
- // }
- // }
- // break;
- // case 5:
- // System.out.print("Enter the item name to search: ");
- // String searchItem = console.next();
- //
- // int foundIndex = -1;
- //
- // for(int i=0; i<list.length; i++) {
- //
- // if(searchItem.equalsIgnoreCase(list[i].getItemName())) {
- // foundIndex = i;
- // }
- // }
- // System.out.println(list[foundIndex]);
- //
- // if(foundIndex == -1) {
- // System.out.println("Not found");
- // }
- // else {
- // System.out.println(list[foundIndex]);
- // }
- //
- // break;
- case 6: //exit the program
- flag = false;
- break;
- default:
- System.out.println("Invalid command, try again.");
- }//end switch
- }//end while
- }//end main
- public static void showMenu() {
- System.out.println("\n\n"
- + "1 -- Output the Super Bowl information\n"
- + "2 -- Output the average attendance\n"
- + "3 -- Find the highest attendance\n"
- + "4 -- Find the lowest attendance\n"
- + "5 -- Search for a team and output it's "
- + "Super Bowl information\n"
- + "0 -- exit\n"
- + "Enter a command: ");
- }
- }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement