CommissionCalculator

From CompSciWiki
Revision as of 00:10, 9 April 2010 by TimC (Talk | contribs)

Jump to: navigation, search

Back to the Program-A-Day homepage

Problem

Today we'll look at combining everything you've done this week into one "large" program.


Imagine you are a worker in the Human Resources department, responsible for calculating the commission each salesperson earns on his or her sales. Each sales person earns 7% commission on their total sales, and the total sales for each person are stored in an array, with the array index corresponding to each salesperson's employee number.


public class Sales{
	public static void main(String [] args){
		double [] sales = new double [7];
		double [] commission = new double[sales.length];
		double percentage = 0.07;

		sales = fillSales(sales.length);
		printArray("Sales for employee #", sales);
	}

	public static double[] fillSales (int length){
		double [] sales =new double[length];
		String temp;


		for (int i=0; i<length; i++){
			temp = JOptionPane.showInputDialog(null, "Input the sales for employee " + (i+1) + ":");
			sales[i] = Double.parseDouble(temp);
		}

		return sales;
	}
		public static void printArray(String tagline, double [] sales){

		for (int i=0; i<sales.length;i++){
			System.out.println(tagline + (i+1)+ ": " + sales[i]);
		}
		System.out.println();
	}
}
 

By Students

When I started with Java, I had the hardest time working with arrays and loops without drawing out the array. In a program like this one, I would have had to draw out the sales array and the commissions array, and then step-by-step go through it to calculate the commission. Once I was able to wrap my head around arrays, it got a lot easier. It was one of those things that just came with practice.

Solution

Create a method called calculateCommission which takes the sales array, as well as a commission percentage double as input, and returns a new array of doubles which contains the list of commissions for each employee.

Code

Solution Code

Back to the Program-A-Day homepage