Gaga Rigs

User Input Song Stats

For ech listener the input is:

listener’s name
song title
length
song title
length
song title
length
and there can be any number of them.
For each listener, state their average song length

After looping thorugh all the input and at the end state the average song length.
Also state the longest and shorted song (assume no duplicates).

Time is in seconds (for now – think about how to do minutes and seconds).

Use the sentinel method to gather data.

Use the Buffered Reader.


import java.io.BufferedReader;

import java.io.InputStreamReader;

public class songUser

{

	public static void main(String[] args) throws Exception

	{
		final String SENTINAL = "STOP";
		final int SENTINAL_VALUE = 3;
		final double MIN_SEC = 60;

		String name;
		String songName, shortestName = null, longestName = null;
		String shortestNameAll = null, longestNameAll = null;
		String songMinutesInput, songSecondsInput;
		double songMinutes, songSeconds;
		double songSum2, shortest = -1, longest = -10;
		double shortestAll = 0, longestAll = 0;
		int counter = 0;

		double songMin, songMax;
		int songAvgmin;
		int songAvgsec = 0;
		double songCounter = 0;
		double songSum = 0, songTotalSum = 0;
		boolean once = true;
		int songTotalAvgMin, songTotalAvgSec;
		int counterAll = 0;

		BufferedReader ConsoleKeyboardIn = new BufferedReader(new InputStreamReader(System.in));

		System.out.print("Enter your name, or [stop] to end program: ");
		name = ConsoleKeyboardIn.readLine();

		while(!(name.equalsIgnoreCase(SENTINAL))) {
			shortest = 999;
			shortestAll = 999;
			for (int i = 0; i < SENTINAL_VALUE; i++){
				System.out.print("Enter the name of your song: ");
				songName = ConsoleKeyboardIn.readLine();

				System.out.print("Enter the minutes in the length of your song: ");
				songMinutesInput = ConsoleKeyboardIn.readLine();
				songMinutes = Double.parseDouble(songMinutesInput);

				System.out.print("Enter the seconds in the length of your song: ");
				songSecondsInput = ConsoleKeyboardIn.readLine();
				songSeconds = Double.parseDouble(songSecondsInput);

				songSum2 = ((songMinutes * MIN_SEC) + songSeconds);
				songSum += ((songMinutes * MIN_SEC) + songSeconds);

					if (songSum2 < shortest) {
						shortest = songSum2;
						shortestName = songName;
					}
					if (songSum2 < shortestAll)
					{
						shortestAll = songSum2;
						shortestNameAll = songName;

					}

					if (songSum2 > longest) {
						longest = songSum2;
						longestName = songName;
					}

					if (songSum2 > longestAll) {
						longestAll = songSum2;
						longestNameAll = songName;
					}

				counter++;
				counterAll++;
			}

			if(counter == SENTINAL_VALUE) {
				counter = 0;
			}

			System.out.print("Longest Song: " + longestName);
			System.out.println();
			System.out.print("Shortest Song: " + shortestName);
			System.out.println();

			songAvgmin = (int) ((songSum / 60) / 3.0);
			songAvgsec = (int) ((songSum / 3.0) % 60);
			if (songAvgsec < 10) {

				System.out.println("Song Average: " + songAvgmin + ":0"+ songAvgsec);

			} else {
				System.out.println("Song Average: " + songAvgmin + ":"+ songAvgsec);

			}

			songTotalSum += songSum;
			songSum = 0;

			System.out.print("Enter your name, or [stop] to end program: ");
			name = ConsoleKeyboardIn.readLine();

		}

		songTotalAvgMin = (int) ((songTotalSum / 60) / counterAll);
		songTotalAvgSec = (int) ((songTotalSum / counterAll) % 60);

		System.out.println("----------------");

		if (songTotalAvgSec > 10) {
			System.out.println("Average of all songs: " + songTotalAvgMin + ":" + songTotalAvgSec);
		} else {
			System.out.println("Song Average of all songs: " + songTotalAvgMin + ":0" + songTotalAvgSec);
		}

		System.out.println("Longest Song of all songs: " + longestNameAll);
		System.out.println("Shortest Song of all songs: " + shortestNameAll);

		System.exit(0);

	}

}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>