Karlnage Karlnage - 6 months ago 62
Java Question

Having some trouble finishing Sales Tracking program in Java

SO I am working on an assignment and I get it most of the way done. Unfortunately, implementing the final method has been unsuccessful. I'm not sure if I am just not initializing the variables correctly or what. If I test each of the other methods individually then it will display exactly what I need it to but getting the displaySalesInfo method to work seems to be beyond me. Any suggestions would be much appreciated.

public class SalesTracking {

public static void main(String[] args) {

// declare arrays
String[] monthArray = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
double[] monthlySales = new double[12];

int highestMonth = computeHighestMonth(monthlySales), lowestMonth = computeLowestMonth(monthlySales);
double totalSales = computeTotalSales(monthlySales), averageSales = computeAverageSales(monthlySales), highestSales = monthlySales[highestMonth], lowestSales = monthlySales[lowestMonth];




//call methods
getSales(monthArray, monthlySales);
//computeTotalSales(monthlySales);
//computeAverageSales(monthlySales);
//computeHighestMonth(monthlySales);
//computeLowestMonth(monthlySales);
displaySalesInfo(totalSales, averageSales, highestMonth, highestSales, lowestMonth, lowestSales);

} //end main

public static void getSales(String monthArray[], double monthlySales[]) {

//prepare for input
Scanner input = new Scanner(System.in);

//input sales
for(int i = 0; i < monthlySales.length; i++) {

//System.out.print("Enter sales for " + monthArray[i] + ": ");
monthlySales[i] = input.nextDouble();

} //end for

} //end getSales

public static double computeTotalSales(double monthlySales[]) {

double totalSales = 0;

for(int i = 0; i < monthlySales.length; i++) {

totalSales += monthlySales[i];

} //end for

//System.out.println("Yearly Total: " + totalSales);
return totalSales;

} //end computeTotalSales

public static double computeAverageSales(double monthlySales[]) {

double total = 0;

for(int i = 0; i < monthlySales.length; i++) {

total += monthlySales[i];

} //end for

double averageSales = total / monthlySales.length;

//System.out.println("Average Sales: " + averageSales);
return averageSales;

} //end computeAverageSales

public static int computeHighestMonth(double monthlySales[]) {

int highestMonth = 0;
double highestSales = monthlySales[0];

for(int i = 0; i < monthlySales.length; i++) {

double maxNumber = monthlySales[i];

if(maxNumber > monthlySales[highestMonth]) {

highestMonth = i;

} //end if

if(highestSales < monthlySales[i]) {

highestSales = monthlySales[i];

} //end if

} //end for

//System.out.println("The highest month is: " + highestMonth);
//System.out.println("The highest sales amount was: " + highestSales);
return highestMonth;

} //end computeHighestMonth

public static int computeLowestMonth(double monthlySales[]) {

int lowestMonth = 11;
double lowestSales = monthlySales[0];

for(int i = 0; i < monthlySales.length; i++) {

double minNumber = monthlySales[i];

if(minNumber < monthlySales[lowestMonth]) {

lowestMonth = i;

} //end if

if(lowestSales > monthlySales[i]) {

lowestSales = monthlySales[i];

} //end if

} //end for

//System.out.println("The lowest month is: " + lowestMonth);
//System.out.println("The lowest sales amount was: " + lowestSales);
return lowestMonth;

} //end computeLowestMonth

public static void displaySalesInfo(double totalSales, double averageSales, int highestMonth, double highestSales, int lowestMonth, double lowestSales) {

System.out.println("Yearly Total: " + totalSales);
System.out.println("Average Sales: " + averageSales);
System.out.println("The highest month is: " + highestMonth);
System.out.println("The highest sales amount was: " + highestSales);
System.out.println("The lowest month is: " + lowestMonth);
System.out.println("The lowest sales amount was: " + lowestSales);

} //end displaySaleInfo


} //end class

Answer

You Should have to call getSales(monthArray, monthlySales); before calculate other figures.

 getSales(monthArray, monthlySales); // call this before otheres
 int highestMonth = computeHighestMonth(monthlySales), lowestMonth = computeLowestMonth(monthlySales);
 double totalSales = computeTotalSales(monthlySales), averageSales = computeAverageSales(monthlySales), highestSales = monthlySales[highestMonth], lowestSales = monthlySales[lowestMonth];

Your programming technique is very bad. Less readability. I recommend the following code.

// declare arrays
String[] monthArray = { "January", "February", "March", "April", "May", "June", "July", "August", "September",
        "October", "November", "December" };
double[] monthlySales = new double[12];
getSales(monthArray, monthlySales);
int highestMonth = computeHighestMonth(monthlySales);
int lowestMonth = computeLowestMonth(monthlySales);
double totalSales = computeTotalSales(monthlySales);
double averageSales = computeAverageSales(monthlySales);
double highestSales = monthlySales[highestMonth];
double lowestSales = monthlySales[lowestMonth];
Comments