Programming Challenge 2 Display sales report Console Welcome

Programming Challenge 2: Display sales report Console
Welcome to the Sales Report
Region Q1 Q2 Q3 Q4
1 $1,540.00 $2,010.00 $2,450.00 $1,845.00
2 $1,130.00 $1,168.00 $1,847.00 $1,491.00
3 $1,580.00 $2,305.00 $2,710.00 $1,284.00
4 $1,105.00 $4,102.00 $2,391.00 $1,576.00
Sales by region:
Region 1: $7,845.00
Region 2: $5,636.00
Region 3: $7,879.00
Region 4: $9,174.00
Sales by quarter:
Q1: $5,355.00
Q2: $9,585.00
Q3: $9,398.00
Q4: $6,196.00
Total annual sales, all regions: $30,534.00
Operation
• This application displays a four-section report of sales by quarter for a company with four sales regions (Region 1, Region 2, Region 3, and Region 4).
• The first section of the report lists the sales by quarter for each region.
• The second section summarizes the total annual sales by region.
• The third section summarizes the total annual sales by quarter for all regions.
• The fourth section prints the total annual sales for all sales regions.
Specifications
• The quarterly sales for each region should be hard-coded into the program using the numbers shown in the console output above. The sales numbers should be stored in a rectangular array.
• The first section of the report should use nested for loops to display the sales by quarter for each region. Use tabs to line up the columns for this section of the report.
• The second section of the report should use nested for loops to calculate the sales by region by adding up the quarterly sales for each region.
• The third section of the report should use nested for loops to calculate the sales by quarter by adding up the individual region sales for each quarter.
• The fourth section of the report should use an enhanced for loop to calculate the total annual sales for all regions.
• Use the NumberFormat class to format the sales numbers using the currency format.

Solution

/**
* The java class SalesReport that hard-coded the sales figures
* of 4 regions. The program that prints the sales by regions and
* quarters to console along with total sales.
* */
//SalesReport.java
import java.text.NumberFormat;
public class SalesReport {
  
   public static void main(String[] args) {
      
       //hard-coded sales in a sales array
       double sales[][]=
           {{1540,2010,2450,1845},
           {1130,1168,1847,1491},
           {1580,2305,2710,1284},
           {1105,4102,2391,1576}};
      
       //set totalSales=0
       double totalSales=0;
      
       System.out.println(\"Sales by region:\");
       //create an instance of NumberFormat with default us currecy,$
       NumberFormat nf=NumberFormat.getCurrencyInstance();
       //calculate sales by region
       for (int row = 0; row < sales.length; row++)
       {
           double regionSales=0;
           for (int col = 0; col < sales[row].length; col++) {
              
               regionSales+=sales[row][col];              
           }
           System.out.printf(\"Region %d :\",row+1);
           System.out.println(nf.format(regionSales));
           totalSales+=regionSales;
           regionSales=0;
       }
      
       System.out.println(\"\ \ Sales by quarter:\");
       //calculate sales by quarter
       for (int row = 0; row < sales.length; row++)
       {
           double quarterSales=0;
           for (int col = 0; col < sales[row].length; col++) {
              
               quarterSales+=sales[col][row];              
           }
           System.out.printf(\"Q %d :\",row+1);
           System.out.println(nf.format(quarterSales));
           quarterSales=0;
       }
       System.out.print(\"Total annual sales, all regions:\");
       //print totalSales
       System.out.println(nf.format(totalSales));
      
   }

}


----------------------------------------------------------------------------------------------------------------------------

Sample output:

Sales by region:
Region 1 :$7,845.00
Region 2 :$5,636.00
Region 3 :$7,879.00
Region 4 :$9,174.00


Sales by quarter:
Q 1 :$5,355.00
Q 2 :$9,585.00
Q 3 :$9,398.00
Q 4 :$6,196.00
Total annual sales, all regions:$30,534.00

Programming Challenge 2: Display sales report Console Welcome to the Sales Report Region Q1 Q2 Q3 Q4 1 $1,540.00 $2,010.00 $2,450.00 $1,845.00 2 $1,130.00 $1,16
Programming Challenge 2: Display sales report Console Welcome to the Sales Report Region Q1 Q2 Q3 Q4 1 $1,540.00 $2,010.00 $2,450.00 $1,845.00 2 $1,130.00 $1,16

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site