Skip to content

Instantly share code, notes, and snippets.

@ellenia
Created November 28, 2017 00:10
Show Gist options
  • Save ellenia/0bb4fae503dd4a1ecf4a98974cb0a7cf to your computer and use it in GitHub Desktop.
Save ellenia/0bb4fae503dd4a1ecf4a98974cb0a7cf to your computer and use it in GitHub Desktop.
import java.util.Scanner;
import static java.lang.System.out;
public class QuarterlySalesSatistics2
{
public static void enterSalesFigures(double [][] companyInfo)
{
Scanner keyboard = new Scanner(System.in);
for(int divisionIndex = 0; divisionIndex < companyInfo.length; divisionIndex++)
{
for(int quarterIndex = 0; quarterIndex < companyInfo[0].length; quarterIndex++)
{
System.out.printf("Division %d, Quarter %d ", divisionIndex + 1, quarterIndex + 1);
companyInfo [divisionIndex][quarterIndex] = keyboard.nextDouble();
}
System.out.println();
}
}
public static void listSalesFigures(double [][] companyInfo)
{
double difference;
for(int divisionIndex = 0; divisionIndex < companyInfo.length; divisionIndex++)
{
difference = 0;
System.out.printf("|Division %d|\n", divisionIndex +1);
for(int quarterIndex = 0; quarterIndex < companyInfo[0].length; quarterIndex++)
{
if(quarterIndex >= 1)
{
difference = companyInfo[divisionIndex][quarterIndex] - companyInfo[divisionIndex][quarterIndex - 1];
}
System.out.printf("|Quarter %d|: $%,.2f\t, |Difference To Previous Quarter|:( $%,.2f ).\n", quarterIndex + 1,
companyInfo[divisionIndex][quarterIndex], difference);
}
System.out.println();
}
}
//@param method calculates total quarterly sales thru columns
public static double [] calculateTotalQuarterlySales(double [][] companyInfo, int numberOfQuarters)
{
double totalQuarterlySales; //total to accumulate sales for all quarters
double[] totalQtSalesArray = new double[numberOfQuarters]; //array that will hold total for each quarter
double difference = 0;
System.out.println();
for(int quarterIndex = 0; quarterIndex < companyInfo[0].length; quarterIndex++)
{
totalQuarterlySales = 0;
for(int divisionIndex = 0; divisionIndex < companyInfo.length; divisionIndex++)
{
totalQuarterlySales += companyInfo[divisionIndex][quarterIndex];
//totalQuarterlySales will increase by companyInfo
}
totalQtSalesArray [quarterIndex] = totalQuarterlySales;
//check condition and calculate each division’s increase or decrease from
//the previous quarter starting with
if(quarterIndex >= 1)
{
difference = totalQtSalesArray[quarterIndex] - totalQtSalesArray[quarterIndex - 1];
}
System.out.printf("Total Sales Per Quarter: |Quarter %d|: $%,.2f,\t |Difference To Previous Quarter|( $%.2f ).\n",
quarterIndex + 1, totalQtSalesArray[quarterIndex], difference);
}
//@return
return totalQtSalesArray;
//reference variable that stores totalQtSalesArray array
}
//@param method To calculate the average sales for all divisions that quarter
public static void calculeQtSalesAverage(double [] totalQtSales, int numberOfDivisions)
{
System.out.println();
for(int quarterlySalesIndex = 0; quarterlySalesIndex < totalQtSales.length; quarterlySalesIndex ++)
{
System.out.printf("Average Sales For: \t|Quarter %d|: $%,.2f.\n", quarterlySalesIndex + 1,
totalQtSales [quarterlySalesIndex] / numberOfDivisions);
}
}
public static void showHighestSales(double [][] companyInfo)
{
//hold highest sales value
double highestSales;
//hold highest sales value for a division
int highestSalesDivisionIndex;
for(int quarterIndex = 0; quarterIndex < companyInfo[0].length; quarterIndex++)
{
highestSales = 0;
highestSalesDivisionIndex = 0;
System.out.println();
for(int divisionIndex = 0; divisionIndex < companyInfo.length; divisionIndex++)
{
//check condition for any quarterly sales for any division to be
if(companyInfo[divisionIndex][quarterIndex] > highestSales)
{
highestSales = companyInfo[divisionIndex][quarterIndex];
highestSalesDivisionIndex = divisionIndex + 1;
}
}
//display division with the highest sales for that quarter
System.out.printf("In Quarter %d Division %d had the highest sales in the amount of\t: $%,.2f.\n",
quarterIndex + 1, highestSalesDivisionIndex + 1, highestSales);
}
}
public static void main(String[] args)
{
int numberOfDivisions = 6; //hold number of divisions
int numberOfQuarters = 4; //hold number of quarters
double [] totalQtSales;
double [][] companyInfo = new double [numberOfDivisions][numberOfQuarters];
enterSalesFigures(companyInfo);
listSalesFigures(companyInfo);
totalQtSales = calculateTotalQuarterlySales(companyInfo, numberOfQuarters);
calculeQtSalesAverage(totalQtSales, numberOfDivisions);
showHighestSales(companyInfo);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment