daily pastebin goal
28%
SHARE
TWEET

Revision 7?

a guest Feb 18th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package observer;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class QuarterlySalesStats {
  6.    
  7.     public static void main(String[] args) {
  8.         @SuppressWarnings("resource")
  9.         Scanner chris = new Scanner(System.in);
  10.         int[][] salesFigures = new int[6][4];
  11.         /*
  12.         salesFigures[0][0] = 432;
  13.         salesFigures[0][1] = 443;
  14.         salesFigures[0][2] = 654;
  15.         salesFigures[0][3] = 865;
  16.        
  17.         salesFigures[1][0] = 234;
  18.         salesFigures[1][1] = 986;
  19.         salesFigures[1][2] = 458;
  20.         salesFigures[1][3] = 764;
  21.        
  22.         salesFigures[2][0] = 889;
  23.         salesFigures[2][1] = 234;
  24.         salesFigures[2][2] = 564;
  25.         salesFigures[2][3] = 974;
  26.        
  27.         salesFigures[3][0] = 643;
  28.         salesFigures[3][1] = 653;
  29.         salesFigures[3][2] = 434;
  30.         salesFigures[3][3] = 835;
  31.        
  32.         salesFigures[4][0] = 953;
  33.         salesFigures[4][1] = 423;
  34.         salesFigures[4][2] = 754;
  35.         salesFigures[4][3] = 893;
  36.        
  37.         salesFigures[5][0] = 678;
  38.         salesFigures[5][1] = 652;
  39.         salesFigures[5][2] = 946;
  40.         salesFigures[5][3] = 835;
  41.         */
  42.  
  43.  
  44.        
  45.         // ==== Input sales numbers ====
  46.         // salesFigures.length is length of first dimension, 6
  47.         for(int i = 0; i < salesFigures.length; i++) {
  48.             int temp = 0;
  49.             // salesFigures.length is length of second dimension, 6
  50.             for(int j = 0; j < salesFigures[0].length; j++) {
  51.                 // prevents negative input
  52.                 temp = -1;
  53.                 while(temp < 0) {
  54.                     System.out.print("Enter division " + (i + 1) + "'s sales figure for quarter " + (j + 1) + ": ");
  55.                     temp = chris.nextInt();
  56.                 }
  57.                 salesFigures[i][j] = temp;
  58.             }  
  59.         }
  60.        
  61.        
  62.         System.out.println();
  63.         System.out.println("=== Quarterly Sales Figures ===");
  64.         System.out.println();
  65.         System.out.println("-----------");
  66.         System.out.println("LISTING");
  67.         System.out.println("-----------");
  68.         System.out.println();
  69.        
  70.         for(int i = 0; i < salesFigures.length; i++) {
  71.             System.out.println("Division: " + (i + 1));
  72.             System.out.println("-----------");
  73.             for(int j = 0; j < salesFigures[0].length; j++) {
  74.                 if(j < 1) {
  75.                     System.out.println("Quarter 1: " + salesFigures[i][j]);
  76.                 } else {
  77.                     System.out.println("Quarter " + (j + 1) + ": " + salesFigures[i][j] + computeChange(salesFigures[i][j-1], salesFigures[i][j]));
  78.                 }
  79.             }
  80.             System.out.println();
  81.         }
  82.        
  83.         System.out.println("-----------");
  84.         System.out.println("STATS");
  85.         System.out.println("-----------");
  86.         System.out.println();
  87.         int prevTotal = 0;
  88.         for(int i = 0; i < 4; i++) {
  89.             System.out.println("Quarter " + (i + 1));
  90.             System.out.print("  Total: " + (computeQuarterlySales(i, salesFigures).replace("+", "").replace("-", "")));
  91.             if (i>0) {
  92.                 System.out.print(computeChange(prevTotal, Integer.parseInt(computeQuarterlySales(i, salesFigures))));
  93.             }
  94.             System.out.print("\n    Average: " + (averageQuarterlySales(i, salesFigures).replace("+", "").replace("-", "")));
  95.             int bestDivision = detBestDivision(i, salesFigures);
  96.             System.out.println("\n  Highest sales: division " + (bestDivision + 1 )+ " (" + salesFigures[bestDivision][i] + ")");
  97.             prevTotal = Integer.parseInt(computeQuarterlySales(i, salesFigures));
  98.         }
  99.  
  100.     }
  101.    
  102.     public static int detBestDivision(int quarter, int[][] sales) {
  103.         int topDiv = 0;
  104.         int topSales = 0;
  105.         for(int i = 0; i < sales.length; i++) {
  106.  
  107.             if(sales[i][quarter] > topSales) {
  108.  
  109.                 topDiv = i;
  110.                 topSales = sales[i][quarter];
  111.             }
  112.         }
  113.        
  114.        
  115.         return topDiv;
  116.     }
  117.     //modded
  118.     public static String averageQuarterlySales(int quarter, int[][] sales) {
  119.        
  120.         //We parse to int here because we changer return type fo computequarterlysales to String (you can change this)
  121.         double average = (double) Integer.parseInt(computeQuarterlySales(quarter, sales))/6;
  122.        
  123.        
  124.         String rounded =  String.format("%.2f", average);
  125.         return rounded;
  126.     }
  127.     //Added the sign to this, you can remove it too
  128.     public static String computeQuarterlySales(int quarter, int[][] sales) {
  129.         int total = 0;
  130.        
  131.         String result = "";
  132.             for(int i = 0; i < sales.length; i++) {
  133.                 total += sales[i][quarter];
  134.             }
  135.        
  136.        
  137.         result = Integer.toString(total);
  138.        
  139.         return result;
  140.     }
  141.     //added sign here as well.
  142.     public static String computeDivisionSales(int division, int[][] sales) {
  143.        
  144.         String result = "";
  145.         int total = 0;
  146.         for(int i = 0; i < 4; i++) {
  147.             total += sales[division][i];
  148.         }
  149.        
  150.        
  151.         result = Integer.toString(total);
  152.         return result;
  153.     }
  154.    
  155.     public static String computeChange(int before, int after) {
  156.         String change = "";
  157.        
  158.         int difference = after - before;
  159.         if(difference > 0) {
  160.             change = " (+" + difference + ")";
  161.         } else {
  162.             change = " (" + difference + ")";
  163.         }
  164.        
  165.        
  166.         return change;
  167.     }
  168. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top