简体   繁体   中英

Loops in an assignment for beginner JAVA

I have an assignment for a beginner's JAVA class and I cannot seem to be able to resolve it. We need to create a program that calculates monthly commission based on transactions. The transaction amount is defined using a math.random().

The month starts in January. We use JOPtionPane.showconfirmdialog to ask if there is a customer. If yes, there is another confirmdialog asking if the client wishes to buy the item. If the client accepts to buy the item, we calculate commission.

If there are no customers or if we reach 15K in commission in a month, we skip to the following month and it repeats.

Finally, once we reach 100K in commission before the year ends, the program ends, telling the seller to go on vacation for the rest of the year. If only this was true in real life...

However, my problem is I am unable for some reason to program a loop that will exit the program (and display a message) if it happens that I do not reach 100K after December. I keep on getting

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12 at Commission.main(Commission.java:42)

public static void main(String[] args) {

    String[] months = { "January", "February", "March", "April",
            "May", // Initialize array for months of the year
            "June", "July", "August", "September", "October", "November",
            "December" };
    String[] diamonds = { "diamond", "ruby", "sapphire", "emerald",
            "topaz", "zircon" }; // Initialize array for different precious gems types
    double[] monthlyCommission; // Initialize double variable for total commission for the month
    monthlyCommission = new double[12]; // Initialize array with 12 values for monthly commission
    double yearTotal; // Initialize variable for total commission in the year
    double transaction; // Initialize value for a sale transaction
    double commission; // Initialize value for commission based on value of sale transaction
    int month = 0;
    int diamond;
    yearTotal = 0;

    JOptionPane.showMessageDialog(null, "Welcome to X's Jewelry Store!"); // Display welcome message dialog

    for (; monthlyCommission[month] < 15000;) {

        int storeCustomer = JOptionPane.showConfirmDialog(null,
                "Is there a customer in the store?", months[month]
                        + " month", JOptionPane.YES_NO_OPTION);
        while (storeCustomer == JOptionPane.NO_OPTION) { // Statement to process if there is no customer
            {
                month += 1;
                storeCustomer = JOptionPane.showConfirmDialog(null,
                        "Is there a customer in the store?", months[month]
                                + " month", JOptionPane.YES_NO_OPTION);
            }

        }
        if (storeCustomer == JOptionPane.YES_OPTION) { // Statement to process if there is a customer

            diamond = (int) (Math.random() * 6); // Choose randomly a value between 0-5
            transaction = Math.random() * 50000.0;
            transaction = Math.round(transaction * 100) / 100;

            int buyItem = JOptionPane.showConfirmDialog(null,
                    "Do you wish to buy this " + diamonds[diamond]
                            + " for "
                            + String.format("$%4.2f", transaction) + "?");
            if (buyItem == JOptionPane.NO_OPTION) // Statement to process if user does not want the item
                JOptionPane.showMessageDialog(null,
                        "No problem. See you next time.");

            if (buyItem == JOptionPane.YES_OPTION) { // Statement to process if user wishes to buy the item
                if (transaction <= 10000)
                    commission = transaction * 0.1;
                else if (transaction > 30000.0)
                    commission = 10000 * 0.10 + 20000 * 0.15
                            + (transaction - 30000) * 0.20;
                else
                    commission = 10000 * 0.10 + (transaction - 10000) * 0.15;

                commission = Math.round(commission * 100) / 100;
                monthlyCommission[month] += commission;
                yearTotal += commission;

                JOptionPane.showMessageDialog(null, String.format(
                        "Your commission for this transaction is $%4.2f",
                        commission));
                System.out.println(yearTotal); // Displays the commission total for the transaction
                if (yearTotal > 100000) // Exit loop if total commission for the year is greater than 100000
                    break;

            }

        }

        if (monthlyCommission[month] >= 15000) {
            JOptionPane.showMessageDialog(null, "You have earned $"
                    + String.format("%4.2f", monthlyCommission[month])
                    + ". You can rest the remainder of the month!"); // Display dialog once the monthly commission reaches 15000
            month += 1;
        }

    }
    JOptionPane.showMessageDialog(null,
            "Congratulations!! You have earned a total of $"
                    + String.format("%4.2f", yearTotal)
                    + ". Enjoy your vacation in Honolulu!"); // Display dialog once the yearly commission reaches 100000
}

}

I believe you issue lies with your beginning "for" statement. Your exit condition is when your monthly commission reaches of exceeds 15000, regardless of what month it is. Since you already increment the month if you commission is has reached 15000 ( line 72-77 ), your for loop should just iterate through the months 0-11. Thus line 20 should look more like:

    for( month = 0; month < 12; month++ ){

Also, the break at line 66 is kind of ugly, may I suggest moving that condition into the for loop as well? As such:

    for( month= 0; ( (month < 12) && (yearTotal<100000) ) ; month++ ){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM