Question: Help needed in the last method java ! Thanx StoreSales class needs to be completed ! SalesDemo.java import javax.swing.JOptionPane; import java.text.DecimalFormat; /** @@@@ @@@@@ This
Help needed in the last method java ! Thanx
StoreSales class needs to be completed !
SalesDemo.java
import javax.swing.JOptionPane; import java.text.DecimalFormat;
/** @@@@ @@@@@ This program gathers sales amounts for the week. It uses the StoreSales class to display the total, average, highest, and lowest sales amounts. */
public class SalesDemo { public static void main(String[] args) { final int ONE_WEEK = 7; // Number of elements
// Create an array to hold sales amounts for a week. double[] sales = new double[ONE_WEEK];
// Get the week's sales figures. getValues(sales);
// Create a SalesData object, initialized with the week's sales figures. StoreSales week = new StoreSales(sales);
// Create a DecimalFormat object. DecimalFormat dollar = new DecimalFormat("#,##0.00");
// Display the total, average, highest, and lowest sales amounts for the week. JOptionPane.showMessageDialog(null, "The total sales were $" + dollar.format(week.getTotal()) + " The average sales were $" + dollar.format(week.getAverage()) + " The highest sales were $" + dollar.format(week.getHighest()) + " The lowest sales were $" + dollar.format(week.getLowest())); //Get a value to search for String input = JOptionPane.showInputDialog("Enter a sales amount to search for"); double searchValue = Double.parseDouble(input); //Dispaly the number of times the search value appeared in the sales data JOptionPane.showMessageDialog(null, searchValue + " appears " + week.howMany(searchValue) + " times.");
System.exit(0); }
/** The getValues method asks the user to enter sales amounts for each element of an array. @param array The array to store the values in. */
private static void getValues(double[] array) { String input; // To hold user input.
// Get sales for each day of the week. for (int i = 0; i < array.length; i++) { input = JOptionPane.showInputDialog("Enter the sales for day " + (i + 1) + "."); array[i] = Double.parseDouble(input); } } }
StoreSales.java
/** @@@@ @@@@@ This class tracks the sales figures for a number of days in an array and provides methods for getting the total sales, average sales, highest amount sold and lowest amount sold. */
public class StoreSales { private double[] sales; // The sales data
/** The constructor copies the elements in an array to the sales array. @param s The array to copy. */
public StoreSales(double[] s) { // Create an array as large as s. sales = new double[s.length]; // Copy the elements from s to sales. for (int index = 0; index < s.length; index++) sales[index] = s[index]; }
/** getTotal method @return The total of the elements in the sales array. */
public double getTotal() { double total = 0.0; // Accumulator
// Accumulate the sum of the elements in the sales array. for (int index = 0; index < sales.length; index++) total += sales[index];
// Return the total. return total; }
/** getAverage method @return The average of the elements in the sales array. */
public double getAverage() { return getTotal() / sales.length; }
/** getHighest method @return The highest value stored in the sales array. */
public double getHighest() { double highest = sales[0];
for (int index = 1; index < sales.length; index++) { if (sales[index] > highest) highest = sales[index]; }
return highest; }
/** getLowest method @returns The lowest value stored in the sales array. */
public double getLowest() { double lowest = sales[0];
for (int index = 1; index < sales.length; index++) { if (sales[index] < lowest) lowest = sales[index]; }
return lowest; } /** howMany method @param searchValue, the value to search for in the array. @returns The number of times searchValue is in the sales array */ public int howMany(double searchValue) { //YOUR CODE GOES HERE return 0; //ALSO NEED TO ALTER WHAT THE METHOD RETURNS } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
