public class Sales
{
public Sales()
{
}
public Sales(double[][] sales)
{
this.sales=sales;
}
public void salesSlip(int salespersonNumber, int productNumber, double sum)
{
sales [salespersonNumber-1][productNumber-1]+=sum;
}
public void productTotal()
{
int cols=0;
double sum=0;
double[] productTotal= new double[sales[0].length];
for(cols=0; cols<sales[0].length; cols++)
{
for(int rows=0; rows<sales.length; rows++)
{
sum+= sales[rows][cols];
}
productTotal[cols]=sum;
}
}
public void salespersonTotal()
{
int rows=0;
double sum=0;
double[] salespersonTotal= new double[sales.length];
for(rows=0; rows<sales.length; rows++)
{
for(int cols=0; cols<sales[0].length; cols++)
{
sum+= sales[rows][cols];
}
salespersonTotal[rows]= sum;
}
}
public void calcTotal()
{
double total=0;
for(int i=0; i<5; i++)
total+= 0;
}
public String toString()
{
toString="\t\tProduct 1\tProduct 2\tProduct 3\tProduct 4\tProduct 5\tTOTAL\nSalesperson 1";
for(int i=0; i<sales[0].length; i++)
toString+="\t"+sales[0][i]+"0";
toString+="\t"+(salespersonTotal[0]);
toString+="\nSalesperson 2";
for(int i=0; i<sales[0].length; i++)
toString+="\t"+sales[1][i];
toString+="\t"+(salespersonTotal[1]);
toString+="\nSalesperson 3";
for(int i=0; i<sales[0].length; i++)
toString+="\t"+sales[2][i];
toString+="\t"+(salespersonTotal[2]);
toString+="\nTOTAL\t";
for(int i=0; i<5; i++)
toString+= "\t0.00";
toString+="\t"+total+"0";
return toString;
}
private double[][] sales;
private double[] productTotal;
private double[] salespersonTotal;
private String toString;
private double total;
}
Tester:
public class SalesTester
{
public static void main(String[] args)
{
double[][] sales={{0.00,0.00,0.00,0.00,0.00},{0.00,0.00,0.00,0.00,0.00},{0.00,0.00,0.00,0.00,0.00}} ;
Sales tester= new Sales(sales);
tester.salesSlip(1,1,20.00);
tester.salesSlip(1,2,30.00);
tester.salesSlip(1,3,0.00);
tester.salesSlip(1,4,10.00);
tester.salesSlip(1,5,0.00);
tester.salesSlip(2,1,0.00);
tester.salesSlip(2,2,0.00);
tester.salesSlip(2,3,10.00);
tester.salesSlip(2,4,0.00);
tester.salesSlip(2,5,30.00);
tester.salesSlip(3,1,50.00);
tester.salesSlip(3,2,60.00);
tester.salesSlip(3,3,50.00);
tester.salesSlip(3,4,30.00);
tester.salesSlip(3,5,35.00);
tester.productTotal();
tester.salespersonTotal();
tester.calcTotal();
System.out.println(tester.toString());
}
}