The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Sales not working

Hi:
So, in my pursuit of learning programming, I am writing a program that takes Sales Information, gives back the total, average and pay(commission is 9% of total sales + 200).

I want to create a simple bar chart that will give us the distribution of salaries--IE, I want to see who made how much money. I have come accross a problem:

//Jeremiah A. Walker
/**
When we made the Sales book program the first time around, we ended up with a way for a user
to enter in a set of Sales and calculate the average. However, our program did not
maintain the individual Sales values in instance variables of the class. Thus, repeat calculations
require the user to reenter the same Sales. One way to solve this problem would be to store each of
our Sales entered in an individual instance of the class. For instance, we could create
instance variables Sales1, Sales2, Sales3 etc. to store our Sales, but this would be cumbersome and
time-consuming. This looks like a job for an array. In most courses, though, Salesmans take several
exams and do several assignment. Instructors are likely to want to analyze Sales across the entire
length of the course--both for a single Salesman and for the class as a whole. We will demonstrate
a multidimensional array here.
**/
public class SalesBook
{
private String courseName;//the name of the course this SalesBook represents
private int[][] Sales; // array of Salesmans Sales
//two-argument constructor initializes courseName and Sales array
public SalesBook(String name, int[][] SalesArray)
{
courseName = name;//initialize courseName
Sales = SalesArray;//store Sales
}//end two-argument SalesBook constructor
//method to set the course name
public void setCourseName(String name)
{
courseName = name; //store the course name
}//end method setCourseName
//method to retrieve the course name
public String getCourseName()
{
return courseName;
}//end method getCourseName
//display a welcome message to the SalesBook user
public void displayMessage()
{
//getCourseName gets the name of the course
System.out.printf("Welcome to the Sales book for\n%s!\n\n", getCourseName() );
}//end method displayMessage
//perform various operations on the data
public void processSales()
{
//output Sales array
outputSales();
//call methods getMinimum and getMaximum
System.out.printf("Lowest Sales is %d\nHighest Sales was %d\n\n", getMinimum(), getMaximum());
//call outputBarChart to print Sales distribution chart
outputBarChart();
}//end method processSales
public int getMinimum()
{
int lowSales = Sales[0][0];//assume Sales[0] is smallest
//loop through rows of Sales array
/**
in this method, we want to loop through the two-dimensional array Sales. To accomplish this,
the outer enhanced for loop will iterate through Sales, assigning successive rows to our
parameter, int[] SalesmanSales on successive iteration. The square brackets following the
parameter name indicate that SalesmanSales refers to a one-dimentional int array--namely, a
row in array Sales containing one Salesman's Sales. To fined the lowest Sales, the inner
for loop will compare the elements of the current one dimensional array SalesmanSales to
variable variable low Sales. For instance, on the first iteration, the outer for, row 0
of Sales is assigned to parameter SalesmanSales. The inner enhanced for loop will go through
SalesmanSales and compares each Sales value with lowSales. If a Sales is less than lowSales,
lowSales is set to that Sales. On the second loop, row 1 of Sales is assigned to SalesmanSales,
and the elements of this row are compared with variable lowSales. This repeat until
all rows of Sales have been traversed. When execution of the nested statement is complete,
lowSales contains the lowest Sales in the two-dimensional array. Method getMaximum works
similarly to method getMinimum.
**/
for(int[] SalesmanSales : Sales)
{
//loop through columns of current row
for(int Sales : SalesmanSales)
{
//if Sales less than lowSales, assign it to loSales
if(Sales < lowSales)
lowSales = Sales;
}//end inner for
}//end outer for
return lowSales;//gives us back lowest Sales
}//end getMinimum
public int getMaximum()
{
//works just like getMinimum
int highSales = Sales[0][0];//we assume that the first Sales in the array is the highest
//loop through Sales array
for(int[] SalesmanSales : Sales)
{
//loop through columns of current row
//if Sales is greater than highSales, assign Sales to highSales.
for(int Sales : SalesmanSales )
{
if(Sales > highSales)
highSales = Sales;
}//end inner for
}//end for
return highSales; //return highest Sales
}//end getMaximum
public double getAverage(int[] setOfSales)
{
int total = 0; //initialize total
//sum Sales for one Salesman
for(int Sales : setOfSales)
total += Sales;
//return average of Sales
return (double) total / setOfSales.length;
}//end getAverage
public double getTotal(int[] totalOfSales)
{
int total = 0;//initialize total
// int Salary = 200; //initiailize total at 200 and add commission
// double Commission = total * 0.09;
// double Pay = Salary + Commission;
//sum Sales for one Salesman
for(int Sales : totalOfSales)
total += Sales;
//return total of sales of our use
return (double) total;
// return Pay;
}//end getTotal
public double getPay(int[] getPay)
{
int total = 0;
for(int Sales : getPay)
total += Sales;
return (double) total * 0.09 + 200;
}//end pay
//output bar chart displaying Sales distribution
/**
To output the overall Sales distribution for a whole semester, the method here uses nested
enhanced for loops to create the one-dimensional array frequency based on all the Sales in
the two-dimensional array.
**/
public void outputBarChart()
{
System.out.println("Sales distribution: ");
//stores frequency of Sales in each range of 10 Sales
int[] frequency = new int[11];
//for each Sales, increment the appropriate frequency
for(int[] SalesmanSales : Sales )
{
for(int Sales : SalesmanSales)
++frequency[Sales/1000];
}// end for
//for each Sales frequency, print bar in chart
for(int count = 0; count < frequency.length; count++)
{
//output label
if(count == 10)
System.out.printf("%5d: ", 1000,"+" );
else
System.out.printf("%02d-%02d: ", count * 100, count * 100 + 99);
//print bar of asterisks
for(int stars = 0; stars < frequency[count]; stars++)
System.out.print("*");
System.out.println();//Start a new line of output
}//end for
}//end outputBarChart
//output the contents of the Sales array
/**
In method outputSales, we use nested for loops to output values of the array Sales and each Salesman's
semester average. The output shows the result, which resembles the tabular format of an actual
Sales book. We start by printing the columns. We use a counter-controled for loop here
so that we can identify each test with a number. We do the same for our Salesmans, and again to print
the Sales for each Salesman.
**/
public void outputSales()
{
System.out.println("The Sales are:\n");
System.out.print(" ");//allign column heads
//create a colomn heading for each of the tests
for(int Qtr = 0; Qtr < Sales[0].length; Qtr++)
System.out.printf("Qtr %d ", Qtr + 1);
System.out.print("Total ");//total of Sales
System.out.print("Average ");//Salesman average column heading
System.out.println("Pay ");
//create rows/columns of text representing array Sales
for(int sales = 0; sales < Sales.length; sales++)
{
System.out.printf("SalesPerson %2d", sales + 1);
for(int Qtr : Sales[sales])//output Salesman's Sales
System.out.printf("%8d", Qtr);
//call method getAverage to get the Salesman's average Sales;
//pass in row of Sales as the argument to getAverage
double average = getAverage(Sales[sales]);
double total = getTotal(Sales[sales]);
double pay = getPay(Sales[sales]);
System.out.printf("%9.2f", total);
System.out.printf("%9.2f", average);
System.out.printf("%9.2f\n", pay);
}//end for
}//end method outputSales
}//end class SalesBook

Re: Sales not working

Have you stepped through this with a debugger, or at least added some print lines, to help you figure out the flow of the program? Why are they going where they're going? Where do you want them to be instead? When does the flow of the program differ from the expected behavior?