I use a multidimensional array to store the total number of products sold (product range 1 to 5) from a particular seller (1 to 4 sellers).
T organized salesPersons in rows 1 through 4 and product identifiers in columns 1 through 5.
The only thing I can not do is to iterate over the rows exclusively to get the total quantity of each product, i.e. column 1: sum of rows 1 to 4 = total product 1, column 2: sum of rows 1 to 4 = product2 total, etc.
See the SalesTest application test code, followed by the Sales class:
package salestest;
import SalesLibary.Sales;
public class SalesTest {
public static void main(String[] args) {
int monthlySales [][]= {{13, 23, 45, 67, 56},
{43, 65, 76, 89, 90},
{43, 45, 76, 98, 90},
{34, 56, 76, 43, 87}};
Sales companySales = new Sales("Monneys Inc.", monthlySales);
companySales.displayMessage();
companySales.displaySales();
}
}
package SalesLibary;
public class Sales {
private int salesTotals[][];
private String companyName;
public Sales(String name, int monthlySales[][]) {
companyName = name;
salesTotals = monthlySales;
}
public void setCompanyName(String name) {
companyName = name;
}
public String getCompanyName() {
return companyName;
}
public void displaySales() {
System.out.printf("The monthly sales stats for company %s are: ", companyName);
System.out.println(" ");
System.out.print(" ");
for (int product = 0; product < salesTotals[0].length; product++) {
System.out.printf("Product %d ", product + 1);
}
System.out.println("Total ");
for (int salesPerson = 0; salesPerson < salesTotals.length; salesPerson++) {
System.out.printf("SalesPerson %2d", salesPerson + 1);
for (int total : salesTotals[salesPerson]) {
System.out.printf("%10d", total);
}
double total = getTotal(salesTotals[salesPerson]);
System.out.printf("%10.2f\n", total);
}
System.out.println("Product Total: ");
double productSum = getTotalProduct();
System.out.printf("%10.2f", productSum);
}
public double getTotal(int salesTotals[]) {
int total = 0;
for (int count : salesTotals) {
total += count;
}
return total;
}
public void displayMessage() {
System.out.printf("\nWlecome to %s monthly sales summaries!!!\n\n", getCompanyName());
}
public double getTotalProduct() {
int productTotal[];
int totalProduct = 0;
for (int salesPerson = 0; salesPerson < salesTotals.length; salesPerson++) {
productTotal = salesTotals[salesPerson];
for (int count : productTotal) {
totalProduct += count;
}
}
return totalProduct;
}
}