Hi,

I have an assignment that I need help finding out how to add the commission rate for salespeople. I'm to use arrays and methods.

The commission rates are as follows:
$1000 -- 2999 --> 2%
$3000 -- 4999 --> 3.5%
$5000 -- 9999 --> 4.25%
$10000 and above --> 6%

What would be the best way to add the commission rates for these values? (I'm reading from a file for the sales amounts and names of the people).
I've tried if-statements but you can't use a boolean operator for what I wanted to do. Any help is appreciated. I'll keep working on it to see if I can find another way.

This is what I have so far:

public static void main(String[] args) throws Exception {
        File file = new File("~hu/180/homework/hw10data");
        Scanner input = new Scanner(file);
        
        /** declare variables and arrays */
        String[]    names = new String[100];
        double[]    sales = new double[100];
        double[]    totalAmountEarned = new double[100];
        double      commissionRate = 0;
        double      averageSales = 0;
        double      bonus = 50;
        
        /** Call methods */
        getData(file, names, sales);
        getCommissionRate(sales, commissionRate);
        totalAmountEarned = getTotalSales(sales, commissionRate, totalAmountEarned);
        averageSales = getAverageSales(sales, averageSales);
    }
    // method to read the data from the file and return it
    public static int getData(File file, String[] names, double[] sales) throws Exception {
        file = new File("~hu/180/homework/hw10data");
        Scanner input = new Scanner(file);
        int k = 0;
        while (input.hasNext()) {
            names[k] = input.next();
            sales[k] = input.nextDouble();
            ++k;
        }
        return k;
    }
    // method to set the commission rates for the sales amounts
    public static double getCommissionRate(double[] sales, double commissionRate) {
            commissionRate = 0.02;
            commissionRate = 0.035;
            commissionRate = 0.0425;
            commissionRate = 0.06;
        
        return commissionRate;
    }
    // method to calculate the total amount earned
    public static double[] getTotalSales(double[] sales, double commissionRate, double[] totalAmountEarned) {
           int k = 0;
           for (k = 0; k < sales.length; ++k) {
               totalAmountEarned[k] = (sales[k] * commissionRate) + sales[k];
               ++k;
           }
           
           return totalAmountEarned;
    }
    // method to calculate the average sales amount
    public static double getAverageSales(double[] sales, double averageSales) {
        double sum = 0;
        int k = 0;
        for (k = 0; k < sales.length; ++k) {
            sum += sales[k];
            ++k;
        }
        averageSales = sum / k;
        
        return averageSales;
    }
}

Regards,

Soul

Recommended Answers

All 3 Replies

I'm not seeing any logic to determine which commisions rate to hand out. Seems that it should be in getCommissionRate(), but there's nothing there. That said, what was the disallowed boolean operation you were trying, and why was it not working?

And a few comments on the code:
- you open a Scanner to the file in main, pass it to getData, only to open a new Scanner. Take one of them out.
- I recommend using ArrayList<String> for names and ArrayList<double> for sales and totalAmountEarned. Unless you know for sure that the file has 100 entries, but it's still considered poor practise to hardcode array sizes. You can use the add() method to append to the list.
- As mentioned, getCommissionRate needs to be, well, written

I believe I've figured out my problem. I was trying to use a boolean on the entire array which gave an error. Instead I added a for-loop in order to read through each element in the array and give it a specific commission rate.

// method to set the commission rates for the sales amounts
    public static double getCommissionRate(double[] sales, double commissionRate) {
            int k = 0;
            for (k = 0; k < sales.length; ++k) {
                if (sales[k] >= 1000.0 && sales[k] <= 2999.0)
                    commissionRate = 0.02;
                if (sales[k] >= 3000.0 && sales[k] <= 4999.0)    
                    commissionRate = 0.035;
                if (sales[k] >= 5000.0 && sales[k] <= 9999.0)    
                    commissionRate = 0.0425;
                if (sales[k] >= 1000.0)    
                    commissionRate = 0.06;
                ++k;
            }
        
        return k;
    }

I've also taken out the extra scanner object. Didn't even notice I had done that before. hehe.

Thanks for the quick reply.

Regards,
Soul

You're changing the value of commissionRate, but there's 2 problems. First, you don't ever return this value, so any changes will be lost. Second is that you're setting the value of a single variable, where you [probably] want to be using an array of commission rates.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.