Welcome to the Java Programming Forums

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.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Sales not working

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Thanked 0 Times in 0 Posts

    Default Sales not working

    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
    		//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
    	}//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)
    		}// 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,"+" );
    				System.out.printf("%02d-%02d: ", count * 100, count * 100 + 99);
    			//print bar of asterisks
    			for(int stars = 0; stars < frequency[count]; stars++)
    			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

    //Jeremiah a. Walker
    //Tester application for GradeBook object using an array of grades.
    public class SalesBookTest
    	//main method begins program execution
    	public static void main(String[] args)
    		//array of student grade
    		int[][] SalesArray ={ {1000,900,880, 700}, 
    					{1000,900,800, 700},
    					{650,700,750, 550},
    					{750, 800, 850, 567},
    					{700,800,900, 2000},
    					{550,660,770, 880},
    					{910,1000,800, 567},
    					{700,701,800, 890},
    					{760,702,840, 678},
    					{870, 930, 703, 679},
    					{909,790,609, 564},
    					{877, 938, 638, 6764}
    		SalesBook mySalesBook = new SalesBook("Year 2010", SalesArray);
    	}//end main
    }//end class GradeBookTest

    Welcome to the Sales book for
    Year 2010!

    The Sales are:

    Qtr 1 Qtr 2 Qtr 3 Qtr 4 Total Average Pay
    SalesPerson 1 1000 900 880 700 3480.00 870.00 513.20
    SalesPerson 2 1000 900 800 700 3400.00 850.00 506.00
    SalesPerson 3 650 700 750 550 2650.00 662.50 438.50
    SalesPerson 4 750 800 850 567 2967.00 741.75 467.03
    SalesPerson 5 700 800 900 2000 4400.00 1100.00 596.00
    SalesPerson 6 550 660 770 880 2860.00 715.00 457.40
    SalesPerson 7 910 1000 800 567 3277.00 819.25 494.93
    SalesPerson 8 700 701 800 890 3091.00 772.75 478.19
    SalesPerson 9 760 702 840 678 2980.00 745.00 468.20
    SalesPerson 10 870 930 703 679 3182.00 795.50 486.38
    SalesPerson 11 909 790 609 564 2872.00 718.00 458.48
    SalesPerson 12 877 938 638 6764 9217.00 2304.25 1029.53
    Lowest Sales is 550
    Highest Sales was 6764

    Sales distribution:
    00-99: *******************************************
    100-199: ***
    200-299: *
    600-699: *
    The bars are not going where they should be. Why is this and how can I fix it? Thanks.

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Thanked 0 Times in 0 Posts

    Default Re: Sales not working

    THe bar chart should reflect the salary. I think I figured it out, but if you have some insight, I'd appreciate it. Thanks.

Similar Threads

  1. Type Erasure -- Sales pitch?
    By 2by4 in forum Collections and Generics
    Replies: 0
    Last Post: December 10th, 2011, 06:59 AM
  2. not sure why the 'else is not working
    By reddevilggg in forum Loops & Control Statements
    Replies: 3
    Last Post: September 30th, 2011, 03:54 PM
  3. Sales Analysis for various products
    By faridzul90 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 22nd, 2011, 08:36 AM
  4. Why isn't this working?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 21st, 2011, 04:08 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM