Skip to content

Instantly share code, notes, and snippets.

@jacobtender
Created April 10, 2019 00:47
Show Gist options
  • Save jacobtender/f7590aab857627fa1003706a98b109c3 to your computer and use it in GitHub Desktop.
Save jacobtender/f7590aab857627fa1003706a98b109c3 to your computer and use it in GitHub Desktop.
Solved Source for: Quarterly Sales Statistics
/*
Quarterly Sales Statistics
Write a program that lets the user enter four quarterly sales figures for six divisions of a company.
The figures should be stored in a two-dimensional array. Once the figures are entered, the
program should display the following data for each quarter:
• A list of the sales figures by division
• Each division’s increase or decrease from the previous quarter (this will not be displayed for
the first quarter)
• The total sales for the quarter
• The average sales for all divisions that quarter
• The division with the highest sales for that quarter
Input Validation: Do not accept negative numbers for sales figures.
*/
import java.util.Scanner;
public class QuarterlySalesStats
{
public static void dataEntry(int [][] companyFigures) {
Scanner keyboard = new Scanner(System.in);
for(int i = 0; i < companyFigures.length; i++){
for(int j = 0; j < companyFigures[0].length; j++) {
System.out.print("Enter division " + (i+1) + "'s sales figure for quarter " + (j+1) + ": ");
companyFigures[i][j] = keyboard.nextInt();
while(companyFigures[i][j] < 0){
System.out.println("Sales figure cannot be negative! Please try again.");
System.out.print("Enter division " + (i+1) + "'s sales figure for quarter " + (j+1) + ": ");
companyFigures[i][j] = keyboard.nextInt();
}
} // end inner for
} // end outer for
} // end dataEntry
public static void showFigures(int [][] companyFigures) {
System.out.println("\n=== Quarterly Sales Figures ===\n");
System.out.println("-----------\nLISTING\n-----------");
int change;
for(int i = 0; i < companyFigures.length; i++){
change = 0;
System.out.println("\nDivision: " + (i+1) + "\n-----------");
for(int j = 0; j < companyFigures[0].length; j++) {
if(j >= 1) {
change = companyFigures[i][j] - companyFigures[i][j-1];
if (change >= 1) {
System.out.println("Quarter " + (j+1) + ": " + companyFigures[i][j] + " (+" + change + ")");
} else {
System.out.println("Quarter " + (j+1) + ": " + companyFigures[i][j] + " (" + change + ")");
}
}else{
System.out.println("Quarter " + (j+1) + ": " + companyFigures[i][j]);
}
} // end inner for
} // end outer for
} // end showFigures
public static void calcQuarterSales(int[][] companyFigures, int NUMQUARTERS, int NUMDIVISIONS) {
int totalQuarterSales;
int[] totalQuarterSalesArray = new int[NUMQUARTERS];
int change;
System.out.println("\n-----------\nSTATS\n-----------\n");
for(int j = 0; j < companyFigures[0].length; j++){
totalQuarterSales = 0;
for(int i = 0; i < companyFigures.length; i++) {
totalQuarterSales += companyFigures[i][j];
} // end inner for
change = 0;
totalQuarterSalesArray[j] = totalQuarterSales;
System.out.println("Quarter " + (j+1));
// Totals
if(j >= 1) {
change = totalQuarterSalesArray[j] - totalQuarterSalesArray[j-1];
if( change >= 1){
System.out.println("\t\tTotal: " + totalQuarterSalesArray[j] + " (+" + change + ")");
} else {
System.out.println("\t\tTotal: " + totalQuarterSalesArray[j] + " (" + change + ")");
}
} else {
System.out.println("\t\tTotal: " + totalQuarterSalesArray[j]);
}
// Averages
Double avg = (totalQuarterSalesArray[j] / (NUMDIVISIONS * 1.0));
avg = Math.round(avg*100.0)/100.0;
System.out.printf("\t\tAverage: %.2f\n", avg);
// Highest Sales
int highestSale = 0;
int highestDivision = 0;
for(int i = 0; i < companyFigures.length; i++) {
if(companyFigures[i][j] > highestSale) {
highestSale = companyFigures[i][j];
highestDivision = i + 1;
}
}
System.out.println("\t\tHighest sales: division " + highestDivision + " (" + highestSale + ")");
} // end outer for
}
public static void main(String[] args) {
int NUMDIVISIONS = 6;
int NUMQUARTERS = 4;
int[][] companyFigures = new int[NUMDIVISIONS][NUMQUARTERS];
dataEntry(companyFigures);
showFigures(companyFigures);
calcQuarterSales(companyFigures, NUMQUARTERS, NUMDIVISIONS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment