/* QuarterlySales.java
by Craig Persiko for CS 111A
(adapted from Tony Gaddis' Starting Out with C++)
This program demonstrates a two-dimensional array.
This program uses a BufferedReader instead of a Scanner for input.
This is an older way of doing things, but you may still see it.
*/
import java.io.*;
class QuarterlySales
{
public static void main(String[] args) throws Exception
{
double[][] sales = new double[3][4]; // 2D array, 3 rows and 4 columns.
double totalSales = 0; // To hold the total sales.
int r, c; // Loop counters.
// Set up keyboard input reader:
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.print("This program will calculate the total sales of");
System.out.println(" all the company's divisions.");
System.out.println("Enter the following sales information:");
// Nested loops to fill the array with quarterly
// sales figures for each division.
for (r = 0; r < 3; r++)
{
for (c = 0; c < 4; c++)
{
System.out.print("Division " + (r + 1));
System.out.print(", Quarter " + (c + 1) + ": $");
sales[r][c] = Double.parseDouble(keyboard.readLine());
}
System.out.println(); // blank line
}
// Nested loops to add all the elements:
// These loops use the "for each" loop structure
for (double[] row : sales) // for each row
for (double item : row) // for each item in the row
totalSales += item;
System.out.printf("The total sales for the company are: $%,.2f \n", totalSales);
}
}
/* Sample Output:
This program will calculate the total sales of all the company's divisions.
Enter the following sales information:
Division 1, Quarter 1: $2.50
Division 1, Quarter 2: $1.75
Division 1, Quarter 3: $4.00
Division 1, Quarter 4: $3.75
Division 2, Quarter 1: $100.00
Division 2, Quarter 2: $124.00
Division 2, Quarter 3: $111.11
Division 2, Quarter 4: $175.00
Division 3, Quarter 1: $56000.00
Division 3, Quarter 2: $49000.00
Division 3, Quarter 3: $52000.00
Division 3, Quarter 4: $100000.00
The total sales for the company are: $257,522.11
*/
syntax highlighted by Code2HTML, v. 0.9