(Sales Summary) Use a two-dimensional array to solve the following problem. A co
ID: 3778748 • Letter: #
Question
(Sales Summary) 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 different 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
Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information
from all of the slips for last month is available. Write a program that will read all this information
for last month’s sales (one salesperson’s data at a time) and summarize the total sales by salesperson
by product. All totals should be stored in the two-dimensional array sales. After processing all the
information for last month, print the results in tabular format with each of the columns representing
a particular salesperson and each of the rows 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. Your tabular printout should include these cross totals to the
right of the totaled rows and to the bottom of the totaled columns.
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/*
Input format in the file personId, productId, cost:
1 2 5.2
2 5 6.2
3 1 7
4 4 2.5
*/
/*
Output Format :
1 2 3 4 Total cost
1 0 0 1 0 7.0
2 1 0 0 0 5.2
3 0 0 0 0 0.0
4 0 0 0 1 2.5
5 0 1 0 0 6.2
Sum 5.2 6.2 7.0 2.5 20.9
*/
public class Sales {
public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new FileReader("/home/kumar/Desktop/data.txt"));
String line = null;
int[][] sales = new int[5][4];
double[] productwiseCost = new double[5]; //Contains total cost for each product
double[] personwiseCost = new double[4];
while((line = br.readLine()) != null){
try{
String columns[] = line.split(" ");
int salesPersonId = Integer.parseInt(columns[0]);
int productId = Integer.parseInt(columns[1]);
double cost = Double.parseDouble(columns[2]);
salesPersonId--;
productId--;
sales[productId][salesPersonId]++;
productwiseCost[productId]+=cost;
personwiseCost[salesPersonId]+=cost;
} catch(Exception e){
System.out.println("Unable to parse a row. So skipping : line = "+ line);
}
}
// Product as row , Person as column
System.out.println(" 1 2 3 4 Total cost");
double totalCost = 0;
for(int i=0; i<5; i++){
System.out.print((i+1)+" ");
for(int j=0; j<4; j++){
System.out.print(sales[i][j]+" ");
if(j==3){
System.out.print(productwiseCost[i]);
}
}
System.out.println();
}
System.out.print("Sum ");
for(int index = 0 ; index < personwiseCost.length; index++){
System.out.print(personwiseCost[index]+" ");
totalCost+=personwiseCost[index];
}
System.out.print(totalCost);
System.out.println();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.