0% found this document useful (0 votes)
41 views2 pages

Total Sales Calculation in Java

The document describes a program that uses a two-dimensional array to track total sales for four salespeople and five products. The program takes in daily sales slips with the salesperson number, product number, and sales amount. It stores this data in the 2D array. The program then displays the data in a table with columns for each salesperson and rows for each product. It calculates totals for each product and each salesperson.

Uploaded by

Maike Sombra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views2 pages

Total Sales Calculation in Java

The document describes a program that uses a two-dimensional array to track total sales for four salespeople and five products. The program takes in daily sales slips with the salesperson number, product number, and sales amount. It stores this data in the 2D array. The program then displays the data in a table with columns for each salesperson and rows for each product. It calculates totals for each product and each salesperson.

Uploaded by

Maike Sombra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

(Total Sales) Use a two-dimensional array to solve the following problem: A company has

four salespeople (1 to 4) who sell five different products (1 to 5). Once a day, each
salesperson passes in a slip for each type of product sold. Each slip contains the
following:
a) The salesperson number
b) The product number
c) The total dollar value of that product sold that day

After entering all the information for last month, display the results in tabular format,
with each column representing a particular salesperson and each row representing a
particular product. Cross-total each row to get the total sales of each product for last
month. Cross-total each column to get the total sales by salesperson for last month.

// Program totals sales for salespeople and products.


import [Link];
class TotalSales
{
public static void main( String args[] )

m
er as
{
Scanner input = new Scanner( [Link] );

co
eH w
// sales array holds data on number of each product sold
// by each salesperson

o.
double sales[][] = new double[ 5 ][ 4 ];
rs e
ou urc
[Link]( "Enter salesperson number (-1 to end): " );
int person = [Link]();
o

while ( person != -1 )
{
aC s
vi y re

[Link]( "Enter product number: " );


int product = [Link]();
[Link]( "Enter sales amount: " );
double amount = [Link]();
ed d

// error-check the input


ar stu

if ( person >= 1 && person < 5 &&


product >= 1 && product < 6 && amount >= 0 )
sales[ product - 1 ][ person - 1 ] += amount;
is

else
[Link]( "Invalid input!" );
Th

[Link]( "Enter salesperson number (-1 to end): " );


person = [Link]();
} // end while
sh

This study source was downloaded by 100000809286976 from [Link] on 08-19-2021 22:26:44 GMT -05:00

[Link]
// total for each salesperson
double salesPersonTotal[] = new double[ 4 ];

// display the table


for ( int column = 0; column < 4; column++ )
salesPersonTotal[ column ] = 0;

[Link]( "%8s%14s%14s%14s%14s%10s\n",
"Product", "Salesperson 1", "Salesperson 2",
"Salesperson 3", "Salesperson 4", "Total" );
// for each column of each row, print the appropriate
// value representing a person's sales of a product
for ( int row = 0; row < 5; row++ )
{
double productTotal = 0.0;
[Link]( "%8d", ( row + 1 ) );

m
er as
for ( int column = 0; column < 4; column++ ) {
[Link]( "%14.2f", sales[ row ][ column ] );

co
productTotal += sales[ row ][ column ];

eH w
salesPersonTotal[ column ] += sales[ row ][ column ];

o.
}

rs e
ou urc
[Link]( "%10.2f\n", productTotal );
}
o

[Link]( "%8s", "Total" );


aC s

for ( int column = 0; column < 4; column++ )


vi y re

[Link]( "%14.2f", salesPersonTotal[ column ] );

[Link]();
}
ed d

}
ar stu
is
Th
sh

This study source was downloaded by 100000809286976 from [Link] on 08-19-2021 22:26:44 GMT -05:00

[Link]
Powered by TCPDF ([Link])

Common questions

Powered by AI

The method employed involves checking the input to ensure the salesperson number is between 1 and 4, the product number is between 1 and 5, and the sales amount is non-negative. If these conditions are met, the sales amount is added to the corresponding element in the array; otherwise, the program outputs "Invalid input!" .

The program uses 'System.out.printf' to format the sales data and display it in a tabular form. It aligns columns and rows with specific spacing to ensure that the data is easily readable. Each row corresponds to a product, while columns correspond to sales by each salesperson. Column headers are used to indicate which salesperson each column represents, and totals are calculated for both rows and columns .

Challenges include managing array bounds, ensuring data accuracy, and handling scalability for more products or salespeople. These could be mitigated by implementing robust input validation, dynamically allocating memory as needed, and possibly refactoring code to use a more flexible data structure like linked lists or hashmaps for larger datasets .

A two-dimensional array can efficiently manage and display sales data by using rows to represent each product and columns to represent each salesperson. This allows for easy computation of totals for each product and each salesperson. For instance, the sales data for four salespeople selling five different products can be stored in a 5x4 array, where each element in the array holds the sales amount for a specific product sold by a specific salesperson .

The program design uses procedural programming principles by breaking down tasks into sequences of instructions, using loops and conditionals to control the flow, and emphasizing data processing within discrete functions. This approach simplifies logical flow and minimizes complexity in handling and processing batch sales data efficiently, making it easier to understand and maintain .

A separate array named 'salesPersonTotal' is used to accumulate the total sales by each salesperson across all products. This approach allows for easy summation and storage of total sales information for each individual salesperson. This total is displayed at the end of each column in the table, representing the total sales achieved by each salesperson for the month .

To handle dynamic additions, the program would require a more flexible data structure like an ArrayList or a HashMap to store sales data instead of a fixed-size array. This would allow for runtime resizing as new salespeople or products are added. Additionally, input processes would need logic to handle new entries, possibly through a registration mechanism whereby new salespeople or products are assigned unique identifiers dynamically .

The total sales for each product are calculated by iterating over each row of the two-dimensional array and summing up the sales amount across all columns (salespeople). This total is then displayed as the final value in each row representing a product, providing a clear cross-total for each product sold last month .

Cross-totaling rows and columns provides a comprehensive view of sales dynamics. Row totals give insight into the performance of individual products, helping to identify best-selling items. Column totals offer a view of salesperson performance, aiding in performance assessment. Together, these totals help businesses make informed decisions regarding product strategies and salesperson management .

The loop structure allows for repeated data entry and processing until a termination condition is met (e.g., entering '-1' for the salesperson number). This ensures that all sales data can be continuously entered and aggregated over time. Nested loops are used to iterate through the array to calculate sales totals for both products and salespeople, facilitating comprehensive data processing and output generation .

You might also like