I'm trying to do #4 from:

Program Input:

Your program will display a welcome message to the user and a menu of options for the user to choose from.

Welcome to the Power Plant Analyzer program. Please choose from the following options:

1.  Upload data 
2.  View data 
3.  Download statistics 
4.  Print Month 
5.  Exit the program 

Design

You will need to start by implementing a class called Entry which stores the following information:

•   Month name
•   Day
•   Year
•   Power output

Add appropriate methods as you see fit.

  Program Options

Option 1: Upload Data

If the user chooses this option, the program will
a. Prompt the user for the file that contains the data. b. Read in the records into an array or an ArrayList

Input File Format:

Month Day Year Output

Sample Input File:

January 10 2018 236.9 
January 11 2018 267.6 
January 12 2018 278.1 

Write a method called UploadData to do this task. Choose the appropriate parameters and return type.

Once done reading in the file, the main menu will be displayed again.

Option 2: View Data

If the user chooses this option, the program will print to the screen the data read in.

Sample Output:

Date: January 10, 2018 Output: 236.9 
Date: January 11, 2018 Output: 267.6 
Date: January 12, 2018 Output: 278.1 

Write a method called PrintData to do this task. Choose the appropriate parameters and return type.

Once done printing, the main menu will be displayed again.

Option 3: Download Statistics

If the user chooses this option, the program will create a statistics file with the following data:

a.  Power output sorted from lowest to highest
b.  Day with highest output 
c.  Total by month 
d.  Average power output for all the data

The statistics file will have the same name as the input file but with _stats.txt appended to it. For example, if the input file was named data.txt, the stats file will be named data_stats.txt.

Note: It will remove the .txt from data.txt, before adding the _stats.txt. It will NOT create a file with the name data.txt_stats.txt. You can use the string substr method to remove the last 4 characters from the file name.

Write a method called CreateStatsFile to do this task. Choose the appropriate parameters and return type.

Once done creating the statistics file, the main menu will be displayed again.

Option 4: Print Month

If the user chooses this option, the program will ask for the month’s name and will search for it. It will display all the data for that month. If no data is available, an appropriate method should be displayed.

Write a method called PrintMonth to do this task. Choose the appropriate parameters and return type.

Once done searching, the main menu will be displayed again

I think I have my skeleton code set up properly but how do i'm now stuck.

public static String printMonth(ArrayList<Entry> MonthList) throws /*what?*/ {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("What is the month's name?");
    String month = keyboard.nextLine();

    for (int i = 0; i < MonthList.size(); i++)
        MonthList.get(i).print();

    Scanner fileIn = new Scanner(new File("data.txt"));
    fileIn.nextLine();
    if (fileIn.hasNext("January")) {

    }

    return month;
}

1 Answers

1
Community On Best Solutions

Check if month is available in the data by iterating the list.

public static String printMonth(ArrayList<Entry> uploadResult) throws /*what?*/ {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("What is the month's name?");
    String month = keyboard.nextLine(); 
    boolean monthPresent = false;
    for (Entry uploadResult : uploadResults) {
        if ( uploadResult.getMonth().equals(month)){
            monthPresent = true;
            System.out.println(uploadResult.getDay() + "--" + uploadResult.getPowerOutput());
        }

        if( !monthPresent ){
            System.out.println("Month info not available");
        }
    }
}