1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
import java.util.Scanner;
public class getSales
{
private static String monthArray[] =
{
"January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"
};
//declare array
private static double monthlySales[] = new double[12];
private static int currentMonth = 0;
public static void main(String[] args)
{
// TODO Auto-generated method stub
//declare variables
double getSales, totalSales, averageSales, highestMonth, lowestMonth, highestSales, lowestSales;
for ( currentMonth = 0; currentMonth < monthlySales.length; currentMonth++) monthlySales[currentMonth] =
getSales();
int highestMonthIndex = computeHighestMonth(monthlySales);
int lowestMonthIndex = computeLowestMonth (monthlySales);
displaySaleInfo(computeTotalSales(monthlySales), computeAverageSales (monthlySales),
highestMonthIndex, monthlySales[highestMonthIndex], lowestMonthIndex, monthlySales[lowestMonthIndex]);
} //End of Main
private static int getSales()
{
Scanner input = new Scanner(System.in);
int size = monthArray.length;
for (int i = 0; i < size; i++){
System.out.print("Please Enter Sales for " + monthArray[i] + ":");
monthlySales[i] = input.nextInt();
//get sales method to store values into monthlysales
}
//gets monthly sales and returns total sales for the year
public static double computeTotalSales(double monthlySales[])
{
double sum = 0;
for (int i = 0; i < monthlySales.length; i++)
{
sum += monthlySales[i];
}
return sum;
}
//gets monthly sales and returns average sales
public static double computeAverageSales (double monthlySales[])
{
return computeTotalSales (monthlySales) / monthlySales.length;
}
//determine month with the highest sales
public static int computeHighestMonth (double monthlySales[])
{
int index = 0;
double val = monthlySales[0];
for (int i = 0; i < monthlySales.length; i++)
{
if (monthlySales[i] > val)
{
val = monthlySales[i];
index = i;
}
}
return index;
}
//determine month with the lowest sales
public static int computeLowestMonth (double monthlySales[])
{
int index = 0;
double val = monthlySales[0];
for (int i = 0; i < monthlySales.length; i++)
{
if (monthlySales[i] < val)
{
val = monthlySales[i];
index = i;
}
}
return index;
}
//displays all the sales data rounded to two decimal places
public static void displaySaleInfo (double totalSales, double averageSales,
int highestMonth, double highestSales, int lowestMonth, double lowestSales)
{
System.out.printf("TotalSales: %.2f" , totalSales);
System.out.printf("AverageSales: %.2f" , averageSales);
System.out.println("HighestMonth: " + monthArray[highestMonth]);
System.out.printf("HighestSales: %.2f" , highestSales);
System.out.println("LowestMonth: " + monthArray[lowestMonth]);
System.out.printf("LowestSales: %.2f" , lowestSales);
}
} //End of Class
|